[fix](grouping)the grouping expr should check col name from base table first, then alias (#14077)
* [fix](grouping)the grouping expr should check col name from base table first, then alias * fix fe ut, the behavior would be same as mysql
This commit is contained in:
@ -302,7 +302,7 @@ public abstract class QueryStmt extends StatementBase implements Queriable {
|
||||
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 @@ public abstract class QueryStmt extends StatementBase implements Queriable {
|
||||
* 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());
|
||||
@ -430,9 +430,22 @@ public abstract class QueryStmt extends StatementBase implements Queriable {
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1090,7 +1090,7 @@ public class SelectStmt extends QueryStmt {
|
||||
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);
|
||||
@ -1960,7 +1960,7 @@ public class SelectStmt extends QueryStmt {
|
||||
}
|
||||
// substitute group by
|
||||
if (groupByClause != null) {
|
||||
substituteOrdinalsAliases(groupByClause.getGroupingExprs(), "GROUP BY", analyzer);
|
||||
substituteOrdinalsAliases(groupByClause.getGroupingExprs(), "GROUP BY", analyzer, false);
|
||||
}
|
||||
// substitute having
|
||||
if (havingClause != null) {
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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;
|
||||
"""
|
||||
}
|
||||
@ -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;"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user