(improvement)(chat) Special handling for count_distinct operator during SQL correcting and explaining (#320)

This commit is contained in:
yangde
2023-11-04 12:58:25 +08:00
committed by GitHub
parent b8989e204f
commit 2fe56e7462
4 changed files with 121 additions and 2 deletions

View File

@@ -41,5 +41,16 @@ public enum AggOperatorEnum {
return AggOperatorEnum.UNKNOWN;
}
/**
* Determine if aggType is count_Distinct type
* 1.outer SQL parses the count_distinct(field) operator as count(DISTINCT field).
* 2.tableSQL generates aggregation that ignores the count_distinct operator.
* @param aggType aggType
* @return is count_Distinct type or not
*/
public static boolean isCountDistinct(String aggType) {
return null != aggType && aggType.toUpperCase().equals(COUNT_DISTINCT.getOperator());
}
}

View File

@@ -5,6 +5,8 @@ import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import com.tencent.supersonic.common.pojo.enums.AggOperatorEnum;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.Function;
@@ -74,7 +76,12 @@ public class SqlParserSelectFunctionHelper {
return null;
}
Function sumFunction = new Function();
sumFunction.setName(aggregateName);
if (AggOperatorEnum.isCountDistinct(aggregateName)) {
sumFunction.setName("count");
sumFunction.setDistinct(true);
} else {
sumFunction.setName(aggregateName);
}
sumFunction.setParameters(new ExpressionList(expression));
return sumFunction;
}