diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/QueryStmt.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/QueryStmt.java index 03ee4048f98b9d..a4e99e44cddaba 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/QueryStmt.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/QueryStmt.java @@ -302,7 +302,7 @@ protected void createSortInfo(Analyzer analyzer) throws AnalysisException { isAscOrder.add(Boolean.valueOf(orderByElement.getIsAsc())); nullsFirstParams.add(orderByElement.getNullsFirstParam()); } - substituteOrdinalsAliases(orderingExprs, "ORDER BY", analyzer); + substituteOrdinalsAliases(orderingExprs, "ORDER BY", analyzer, true); // save the order by element after analyzed orderByElementsAfterAnalyzed = Lists.newArrayList(); @@ -415,7 +415,7 @@ protected Expr getFirstAmbiguousAlias(Expr expr) { * Modifies exprs list in-place. */ protected void substituteOrdinalsAliases(List exprs, String errorPrefix, - Analyzer analyzer) throws AnalysisException { + Analyzer analyzer, boolean aliasFirst) throws AnalysisException { Expr ambiguousAlias = getFirstAmbiguousAlias(exprs); if (ambiguousAlias != null) { ErrorReport.reportAnalysisException(ErrorCode.ERR_NON_UNIQ_ERROR, ambiguousAlias.toColumnLabel()); @@ -430,9 +430,22 @@ protected void substituteOrdinalsAliases(List exprs, String errorPrefix, // alias substitution is not performed in the same way. Expr substituteExpr = trySubstituteOrdinal(expr, errorPrefix, analyzer); if (substituteExpr == null) { - substituteExpr = expr.trySubstitute(aliasSMap, analyzer, false); + if (aliasFirst) { + substituteExpr = expr.trySubstitute(aliasSMap, analyzer, false); + i.set(substituteExpr); + } else { + try { + // use col name from tableRefs first + expr.analyze(analyzer); + } catch (AnalysisException ex) { + // then consider alias name + substituteExpr = expr.trySubstitute(aliasSMap, analyzer, false); + i.set(substituteExpr); + } + } + } else { + i.set(substituteExpr); } - i.set(substituteExpr); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java index 55aebf92800132..7ccc3cca310191 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java @@ -1090,7 +1090,7 @@ private void analyzeAggregation(Analyzer analyzer) throws AnalysisException { groupingInfo.buildRepeat(groupingExprs, groupByClause.getGroupingSetList()); } - substituteOrdinalsAliases(groupingExprs, "GROUP BY", analyzer); + substituteOrdinalsAliases(groupingExprs, "GROUP BY", analyzer, false); if (!groupByClause.isGroupByExtension() && !groupingExprs.isEmpty()) { ArrayList tempExprs = new ArrayList<>(groupingExprs); @@ -1960,7 +1960,7 @@ public void substituteSelectList(Analyzer analyzer, List newColLabels) } // substitute group by if (groupByClause != null) { - substituteOrdinalsAliases(groupByClause.getGroupingExprs(), "GROUP BY", analyzer); + substituteOrdinalsAliases(groupByClause.getGroupingExprs(), "GROUP BY", analyzer, false); } // substitute having if (havingClause != null) { diff --git a/regression-test/data/correctness_p0/test_grouping_with_alias.out b/regression-test/data/correctness_p0/test_grouping_with_alias.out new file mode 100644 index 00000000000000..d2cd147634f798 --- /dev/null +++ b/regression-test/data/correctness_p0/test_grouping_with_alias.out @@ -0,0 +1,6 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !select -- +a 1 +all 1 +all 2 + diff --git a/regression-test/suites/correctness_p0/test_grouping_with_alias.groovy b/regression-test/suites/correctness_p0/test_grouping_with_alias.groovy new file mode 100644 index 00000000000000..4b6a29fe433392 --- /dev/null +++ b/regression-test/suites/correctness_p0/test_grouping_with_alias.groovy @@ -0,0 +1,37 @@ +// Licensed to the Apache Software Foundation (ASF) under one + // or more contributor license agreements. See the NOTICE file + // distributed with this work for additional information + // regarding copyright ownership. The ASF licenses this file + // to you under the Apache License, Version 2.0 (the + // "License"); you may not use this file except in compliance + // with the License. You may obtain a copy of the License at + // + // http://www.apache.org/licenses/LICENSE-2.0 + // + // Unless required by applicable law or agreed to in writing, + // software distributed under the License is distributed on an + // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + // KIND, either express or implied. See the License for the + // specific language governing permissions and limitations + // under the License. + + suite("test_grouping_with_alias") { + qt_select """ + select + coalesce(col1, 'all') as col1, + count(*) as cnt + from + ( + select + null as col1 + union all + select + 'a' as col1 + ) t + group by + grouping sets ((col1),()) + order by + col1, + cnt; + """ + } \ No newline at end of file diff --git a/regression-test/suites/query/keyword/test_keyword.groovy b/regression-test/suites/query/keyword/test_keyword.groovy index 857e3d57a4d0df..ba47584d54519a 100644 --- a/regression-test/suites/query/keyword/test_keyword.groovy +++ b/regression-test/suites/query/keyword/test_keyword.groovy @@ -106,7 +106,7 @@ suite("test_keyword", "query,p0") { qt_alias19 "select k1 as a, k2 as b, k3 as c from baseall t group by a, b, c having a > 5 order by a, b, c;" qt_alias20 "select * from (select 1 as a) b right join (select 2 as a) c using(a);" qt_alias21 "select * from (select 1 as a) b full join (select 2 as a) c using(a);" - sql "select k1 as k7, k2 as k8, k3 as k9 from baseall t group by k7, k8, k9 having k7 > 5 \ + try_sql "select k1 as k7, k2 as k8, k3 as k9 from baseall t group by k7, k8, k9 having k7 > 5 \ order by k7;" - sql "select k1 as k7, k2 as k8, k3 as k9 from baseall t where k8 > 0 group by k7, k8, k9 having k7 > 5 order by k7;" + try_sql "select k1 as k7, k2 as k8, k3 as k9 from baseall t where k8 > 0 group by k7, k8, k9 having k7 > 5 order by k7;" }