(improvment)(chat) if exist count() in dsl,set query to NATIVE and only order by field and group by field can add to select (#206)

This commit is contained in:
lexluo09
2023-10-13 14:25:24 +08:00
committed by GitHub
parent 119e5b8c58
commit f605cf0ef9
11 changed files with 99 additions and 49 deletions

View File

@@ -1,19 +0,0 @@
package com.tencent.supersonic.common.util.jsqlparser;
import net.sf.jsqlparser.expression.ExpressionVisitorAdapter;
import net.sf.jsqlparser.expression.Function;
public class AggregateFunctionVisitor extends ExpressionVisitorAdapter {
private boolean hasAggregateFunction = false;
public boolean hasAggregateFunction() {
return hasAggregateFunction;
}
@Override
public void visit(Function function) {
super.visit(function);
hasAggregateFunction = true;
}
}

View File

@@ -0,0 +1,21 @@
package com.tencent.supersonic.common.util.jsqlparser;
import java.util.HashSet;
import java.util.Set;
import net.sf.jsqlparser.expression.ExpressionVisitorAdapter;
import net.sf.jsqlparser.expression.Function;
public class FunctionVisitor extends ExpressionVisitorAdapter {
private Set<String> functionNames = new HashSet<>();
public Set<String> getFunctionNames() {
return functionNames;
}
@Override
public void visit(Function function) {
super.visit(function);
functionNames.add(function.getName());
}
}

View File

@@ -1,8 +1,10 @@
package com.tencent.supersonic.common.util.jsqlparser;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.Function;
@@ -13,6 +15,7 @@ import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.SelectBody;
import net.sf.jsqlparser.statement.select.SelectItem;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
/**
* Sql Parser Select function Helper
@@ -21,30 +24,34 @@ import org.apache.commons.lang3.StringUtils;
public class SqlParserSelectFunctionHelper {
public static boolean hasAggregateFunction(String sql) {
if (hasFunction(sql)) {
if (!CollectionUtils.isEmpty(getFunctions(sql))) {
return true;
}
return SqlParserSelectHelper.hasGroupBy(sql);
}
public static boolean hasFunction(String sql) {
public static boolean hasFunction(String sql, String functionName) {
Set<String> functions = getFunctions(sql);
if (!CollectionUtils.isEmpty(functions)) {
return functions.stream().anyMatch(function -> function.equalsIgnoreCase(functionName));
}
return false;
}
public static Set<String> getFunctions(String sql) {
Select selectStatement = SqlParserSelectHelper.getSelect(sql);
SelectBody selectBody = selectStatement.getSelectBody();
if (!(selectBody instanceof PlainSelect)) {
return false;
return new HashSet<>();
}
PlainSelect plainSelect = (PlainSelect) selectBody;
List<SelectItem> selectItems = plainSelect.getSelectItems();
AggregateFunctionVisitor visitor = new AggregateFunctionVisitor();
FunctionVisitor visitor = new FunctionVisitor();
for (SelectItem selectItem : selectItems) {
selectItem.accept(visitor);
}
boolean selectFunction = visitor.hasAggregateFunction();
if (selectFunction) {
return true;
}
return false;
return visitor.getFunctionNames();
}
public static Function getFunction(Expression expression, Map<String, String> fieldNameToAggregate) {

View File

@@ -43,4 +43,34 @@ class SqlParserSelectFunctionHelperTest {
Assert.assertEquals(hasAggregateFunction, true);
}
@Test
void hasFunction() throws JSQLParserException {
String sql = "select 部门,sum (访问次数) from 超音数 where 数据日期 = '2023-08-08' "
+ "and 用户 =alice and 发布日期 ='11' group by 部门 limit 1";
boolean hasFunction = SqlParserSelectFunctionHelper.hasFunction(sql, "sum");
Assert.assertEquals(hasFunction, true);
sql = "select 部门,count (访问次数) from 超音数 where 数据日期 = '2023-08-08' "
+ "and 用户 =alice and 发布日期 ='11' group by 部门 limit 1";
hasFunction = SqlParserSelectFunctionHelper.hasFunction(sql, "count");
Assert.assertEquals(hasFunction, true);
sql = "select 部门,count (*) from 超音数 where 数据日期 = '2023-08-08' "
+ "and 用户 =alice and 发布日期 ='11' group by 部门 limit 1";
hasFunction = SqlParserSelectFunctionHelper.hasFunction(sql, "count");
Assert.assertEquals(hasFunction, true);
sql = "SELECT user_name, pv FROM t_34 WHERE sys_imp_date <= '2023-09-03' "
+ "AND sys_imp_date >= '2023-08-04' GROUP BY user_name ORDER BY sum(pv) DESC LIMIT 10";
hasFunction = SqlParserSelectFunctionHelper.hasFunction(sql, "sum");
Assert.assertEquals(hasFunction, false);
sql = "select 部门,min (访问次数) from 超音数 where 数据日期 = '2023-08-08' "
+ "and 用户 =alice and 发布日期 ='11' group by 部门 limit 1";
hasFunction = SqlParserSelectFunctionHelper.hasFunction(sql, "min");
Assert.assertEquals(hasFunction, true);
}
}