(chat)(fix):not add group by if already has distinct (#685)

This commit is contained in:
Scott
2024-01-24 09:48:21 +08:00
committed by GitHub
parent 9d6f96e6d4
commit 48fb01f6bc
2 changed files with 19 additions and 0 deletions

View File

@@ -34,6 +34,12 @@ public class GroupByCorrector extends BaseSemanticCorrector {
SqlInfo sqlInfo = semanticParseInfo.getSqlInfo(); SqlInfo sqlInfo = semanticParseInfo.getSqlInfo();
String correctS2SQL = sqlInfo.getCorrectS2SQL(); String correctS2SQL = sqlInfo.getCorrectS2SQL();
SemanticSchema semanticSchema = queryContext.getSemanticSchema(); SemanticSchema semanticSchema = queryContext.getSemanticSchema();
// check if has distinct
boolean hasDistinct = SqlParserSelectHelper.hasDistinct(correctS2SQL);
if (hasDistinct) {
log.info("not add group by ,exist distinct in correctS2SQL:{}", correctS2SQL);
return;
}
//add alias field name //add alias field name
Set<String> dimensions = semanticSchema.getDimensions(modelIds).stream() Set<String> dimensions = semanticSchema.getDimensions(modelIds).stream()
.flatMap( .flatMap(

View File

@@ -26,6 +26,7 @@ import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Column; import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table; import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.Statement; import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.select.Distinct;
import net.sf.jsqlparser.statement.select.GroupByElement; import net.sf.jsqlparser.statement.select.GroupByElement;
import net.sf.jsqlparser.statement.select.OrderByElement; import net.sf.jsqlparser.statement.select.OrderByElement;
import net.sf.jsqlparser.statement.select.PlainSelect; import net.sf.jsqlparser.statement.select.PlainSelect;
@@ -384,6 +385,18 @@ public class SqlParserSelectHelper {
return false; return false;
} }
public static boolean hasDistinct(String sql) {
Select selectStatement = getSelect(sql);
SelectBody selectBody = selectStatement.getSelectBody();
if (!(selectBody instanceof PlainSelect)) {
return false;
}
PlainSelect plainSelect = (PlainSelect) selectBody;
Distinct distinct = plainSelect.getDistinct();
return Objects.nonNull(distinct);
}
public static boolean isLogicExpression(Expression whereExpression) { public static boolean isLogicExpression(Expression whereExpression) {
return whereExpression instanceof AndExpression || (whereExpression instanceof OrExpression return whereExpression instanceof AndExpression || (whereExpression instanceof OrExpression
|| (whereExpression instanceof XorExpression)); || (whereExpression instanceof XorExpression));