mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-12 04:27:39 +00:00
(improvement)(chat) group by corrector remove aggregate fields (#186)
This commit is contained in:
@@ -47,7 +47,15 @@ public class GroupByCorrector extends BaseSemanticCorrector {
|
|||||||
if (CollectionUtils.isEmpty(selectFields) || CollectionUtils.isEmpty(dimensions)) {
|
if (CollectionUtils.isEmpty(selectFields) || CollectionUtils.isEmpty(dimensions)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Set<String> groupByFields = selectFields.stream().filter(field -> dimensions.contains(field))
|
List<String> aggregateFields = SqlParserSelectHelper.getAggregateFields(sql);
|
||||||
|
Set<String> groupByFields = selectFields.stream()
|
||||||
|
.filter(field -> dimensions.contains(field))
|
||||||
|
.filter(field -> {
|
||||||
|
if (!CollectionUtils.isEmpty(aggregateFields) && aggregateFields.contains(field)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
})
|
||||||
.collect(Collectors.toSet());
|
.collect(Collectors.toSet());
|
||||||
semanticCorrectInfo.setSql(SqlParserUpdateHelper.addGroupBy(sql, groupByFields));
|
semanticCorrectInfo.setSql(SqlParserUpdateHelper.addGroupBy(sql, groupByFields));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import net.sf.jsqlparser.statement.select.OrderByElement;
|
|||||||
import net.sf.jsqlparser.statement.select.PlainSelect;
|
import net.sf.jsqlparser.statement.select.PlainSelect;
|
||||||
import net.sf.jsqlparser.statement.select.Select;
|
import net.sf.jsqlparser.statement.select.Select;
|
||||||
import net.sf.jsqlparser.statement.select.SelectBody;
|
import net.sf.jsqlparser.statement.select.SelectBody;
|
||||||
|
import net.sf.jsqlparser.statement.select.SelectExpressionItem;
|
||||||
import net.sf.jsqlparser.statement.select.SelectItem;
|
import net.sf.jsqlparser.statement.select.SelectItem;
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
@@ -207,6 +208,29 @@ public class SqlParserSelectHelper {
|
|||||||
return table.getName();
|
return table.getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static List<String> getAggregateFields(String sql) {
|
||||||
|
PlainSelect plainSelect = getPlainSelect(sql);
|
||||||
|
if (Objects.isNull(plainSelect)) {
|
||||||
|
return new ArrayList<>();
|
||||||
|
}
|
||||||
|
Set<String> result = new HashSet<>();
|
||||||
|
List<SelectItem> selectItems = plainSelect.getSelectItems();
|
||||||
|
for (SelectItem selectItem : selectItems) {
|
||||||
|
if (selectItem instanceof SelectExpressionItem) {
|
||||||
|
SelectExpressionItem expressionItem = (SelectExpressionItem) selectItem;
|
||||||
|
if (expressionItem.getExpression() instanceof Function) {
|
||||||
|
Function function = (Function) expressionItem.getExpression();
|
||||||
|
if (Objects.nonNull(function.getParameters())
|
||||||
|
&& !CollectionUtils.isEmpty(function.getParameters().getExpressions())) {
|
||||||
|
String columnName = function.getParameters().getExpressions().get(0).toString();
|
||||||
|
result.add(columnName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new ArrayList<>(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public static boolean hasAggregateFunction(String sql) {
|
public static boolean hasAggregateFunction(String sql) {
|
||||||
if (hasFunction(sql)) {
|
if (hasFunction(sql)) {
|
||||||
|
|||||||
@@ -261,4 +261,14 @@ class SqlParserSelectHelperTest {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getAggregateFields() {
|
||||||
|
|
||||||
|
String sql = "select 部门,sum (访问次数) from 超音数 where 数据日期 = '2023-08-08'"
|
||||||
|
+ " and 用户 = 'alice' and 发布日期 ='11' group by 部门 limit 1";
|
||||||
|
List<String> selectFields = SqlParserSelectHelper.getAggregateFields(sql);
|
||||||
|
Assert.assertEquals(selectFields.contains("访问次数"), true);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user