[FIX](agg)fix group by constant child expr bug (#13485)

This commit is contained in:
starocean999
2022-10-24 16:32:36 +08:00
committed by GitHub
parent 40e122e5ef
commit 7faad9f004
4 changed files with 33 additions and 12 deletions

View File

@ -93,20 +93,12 @@ public class GroupByClause implements ParseNode {
}
public void reset() {
groupingExprs = new ArrayList<>();
analyzed = false;
exprGenerated = false;
if (groupingType != GroupingType.GROUP_BY) {
groupingExprs = new ArrayList<>();
if (oriGroupingExprs != null) {
Expr.resetList(oriGroupingExprs);
groupingExprs.addAll(oriGroupingExprs);
}
} else {
if (groupingExprs != null) {
for (Expr e : groupingExprs) {
e.reset();
}
}
if (oriGroupingExprs != null) {
Expr.resetList(oriGroupingExprs);
groupingExprs.addAll(oriGroupingExprs);
}
if (groupingSetList != null) {
for (List<Expr> s : groupingSetList) {

View File

@ -1389,7 +1389,19 @@ public class SelectStmt extends QueryStmt {
}
List<Expr> oriGroupingExprs = groupByClause.getOriGroupingExprs();
if (oriGroupingExprs != null) {
// we must make sure the expr is analyzed before rewrite
try {
for (Expr expr : oriGroupingExprs) {
expr.analyze(analyzer);
}
} catch (AnalysisException ex) {
//ignore any exception
}
rewriter.rewriteList(oriGroupingExprs, analyzer);
// after rewrite, need reset the analyze status for later re-analyze
for (Expr expr : oriGroupingExprs) {
expr.reset();
}
}
}
if (orderByElements != null) {

View File

@ -2,3 +2,6 @@
-- !sql --
D
-- !sql --
D

View File

@ -57,6 +57,20 @@ suite("test_group_by_constant") {
end;
"""
qt_sql """
SELECT
case
when (inc_day = date_sub(curdate(), interval 1 day)) then 'A'
when (inc_day = date_sub(curdate(), interval 8 day)) then 'B'
when (inc_day = date_sub(curdate(), interval 365 day)) then 'C'
else 'D'
end val
from
table_group_by_constant
group by
val;
"""
sql """
DROP TABLE IF EXISTS `table_group_by_constant`;
"""