mirror of
https://github.com/tencentmusic/supersonic.git
synced 2026-04-25 00:54:21 +08:00
Compare commits
3 Commits
21e213fb19
...
12f6cfa42d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
12f6cfa42d | ||
|
|
4c94f2b816 | ||
|
|
c81aa5859d |
@@ -170,6 +170,9 @@ public class QueryStructReq extends SemanticQueryReq {
|
||||
// 5. Set the limit clause
|
||||
plainSelect.setLimit(buildLimit(queryStructReq));
|
||||
|
||||
// 6. Set having clause
|
||||
plainSelect.setHaving(buildHavingClause(queryStructReq));
|
||||
|
||||
select.setSelect(plainSelect);
|
||||
|
||||
// 6. Set where clause
|
||||
@@ -239,7 +242,8 @@ public class QueryStructReq extends SemanticQueryReq {
|
||||
|
||||
private GroupByElement buildGroupByElement(QueryStructReq queryStructReq) {
|
||||
List<String> groups = queryStructReq.getGroups();
|
||||
if (!CollectionUtils.isEmpty(groups) && !queryStructReq.getAggregators().isEmpty()) {
|
||||
if ((!CollectionUtils.isEmpty(groups) && !queryStructReq.getAggregators().isEmpty())
|
||||
|| !queryStructReq.getMetricFilters().isEmpty()) {
|
||||
GroupByElement groupByElement = new GroupByElement();
|
||||
for (String group : groups) {
|
||||
groupByElement.addGroupByExpression(new Column(group));
|
||||
@@ -289,4 +293,23 @@ public class QueryStructReq extends SemanticQueryReq {
|
||||
}
|
||||
return Constants.TABLE_PREFIX + StringUtils.join(modelIds, "_");
|
||||
}
|
||||
|
||||
public Expression buildHavingClause(QueryStructReq queryStructReq) {
|
||||
if (queryStructReq.getMetricFilters().isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
List<Filter> filters = queryStructReq.getMetricFilters();
|
||||
SqlFilterUtils sqlFilterUtils = ContextUtils.getBean(SqlFilterUtils.class);
|
||||
String havingClause = sqlFilterUtils.getWhereClause(filters, false);
|
||||
if (StringUtils.isNotBlank(havingClause)) {
|
||||
try {
|
||||
return CCJSqlParserUtil.parseCondExpression(havingClause);
|
||||
} catch (JSQLParserException e) {
|
||||
log.error("Failed to parse having clause", e);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -34,6 +34,18 @@ public class SqlVariableParseUtilsTest {
|
||||
Assertions.assertEquals(expectedSql, actualSql);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testParseSql_if() {
|
||||
String sql = "select * from t_$interval$ where id = $id$ $if(name)$and name = $name$$endif$";
|
||||
List<SqlVariable> variables = Lists.newArrayList(mockNumSqlVariable(),
|
||||
mockExprSqlVariable(), mockStrSqlVariable());
|
||||
List<Param> params =
|
||||
Lists.newArrayList(mockIdParam(), mockNameParam(), mockIntervalParam());
|
||||
String actualSql = SqlVariableParseUtils.parse(sql, variables, params);
|
||||
String expectedSql = "select * from t_wk where id = 2 and name = 'alice'";
|
||||
Assertions.assertEquals(expectedSql, actualSql);
|
||||
}
|
||||
|
||||
private SqlVariable mockNumSqlVariable() {
|
||||
return mockSqlVariable("id", VariableValueType.NUMBER, 1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user