Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions fe/fe-core/src/main/java/org/apache/doris/analysis/QueryStmt.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -415,7 +415,7 @@ protected Expr getFirstAmbiguousAlias(Expr expr) {
* Modifies exprs list in-place.
*/
protected void substituteOrdinalsAliases(List<Expr> 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());
Expand All @@ -430,9 +430,22 @@ protected void substituteOrdinalsAliases(List<Expr> 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Expr> tempExprs = new ArrayList<>(groupingExprs);
Expand Down Expand Up @@ -1960,7 +1960,7 @@ public void substituteSelectList(Analyzer analyzer, List<String> 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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -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;
"""
}
4 changes: 2 additions & 2 deletions regression-test/suites/query/keyword/test_keyword.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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;"
}