From ae4f4b9bf15b270fd0083b3e5d89fd0f80a6f4b9 Mon Sep 17 00:00:00 2001 From: starocean999 <40539150+starocean999@users.noreply.github.com> Date: Thu, 24 Nov 2022 10:31:58 +0800 Subject: [PATCH] [fix](agg)having clause should use column name first then alias (#14408) * [fix](agg)having clause should use column name first then alias * fix fe ut --- .../org/apache/doris/analysis/QueryStmt.java | 8 +-- .../org/apache/doris/analysis/SelectStmt.java | 9 ++- .../test_group_having_alias.out | 7 ++ .../test_group_having_alias.groovy | 68 +++++++++++++++++++ 4 files changed, 86 insertions(+), 6 deletions(-) create mode 100644 regression-test/data/correctness_p0/test_group_having_alias.out create mode 100644 regression-test/suites/correctness_p0/test_group_having_alias.groovy 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 a4e99e44cddaba..ea0d3f8881a20c 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 @@ -432,20 +432,18 @@ protected void substituteOrdinalsAliases(List exprs, String errorPrefix, if (substituteExpr == null) { if (aliasFirst) { substituteExpr = expr.trySubstitute(aliasSMap, analyzer, false); - i.set(substituteExpr); } else { try { // use col name from tableRefs first - expr.analyze(analyzer); + substituteExpr = expr.clone(); + substituteExpr.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 edd89f75105b9f..3f754595f1b62e 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 @@ -985,7 +985,14 @@ private void analyzeAggregation(Analyzer analyzer) throws AnalysisException { * (select min(k1) from table b where a.key=b.k2); * TODO: the a.key should be replaced by a.k1 instead of unknown column 'key' in 'a' */ - havingClauseAfterAnaylzed = havingClause.substitute(aliasSMap, analyzer, false); + try { + // use col name from tableRefs first + havingClauseAfterAnaylzed = havingClause.clone(); + havingClauseAfterAnaylzed.analyze(analyzer); + } catch (AnalysisException ex) { + // then consider alias name + havingClauseAfterAnaylzed = havingClause.substitute(aliasSMap, analyzer, false); + } havingClauseAfterAnaylzed = rewriteQueryExprByMvColumnExpr(havingClauseAfterAnaylzed, analyzer); havingClauseAfterAnaylzed.checkReturnsBool("HAVING clause", true); if (groupingInfo != null) { diff --git a/regression-test/data/correctness_p0/test_group_having_alias.out b/regression-test/data/correctness_p0/test_group_having_alias.out new file mode 100644 index 00000000000000..bb2ad80ef973e6 --- /dev/null +++ b/regression-test/data/correctness_p0/test_group_having_alias.out @@ -0,0 +1,7 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !sql -- +202245 1 + +-- !sql -- +202245 3 + diff --git a/regression-test/suites/correctness_p0/test_group_having_alias.groovy b/regression-test/suites/correctness_p0/test_group_having_alias.groovy new file mode 100644 index 00000000000000..64162402915f38 --- /dev/null +++ b/regression-test/suites/correctness_p0/test_group_having_alias.groovy @@ -0,0 +1,68 @@ +// 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_group_having_alias") { + sql """ + DROP TABLE IF EXISTS `tb_holiday`; + """ + + sql """ + CREATE TABLE `tb_holiday` ( + `date` bigint(20) NOT NULL , + `holiday` tinyint(4) NOT NULL , + `holiday_cn` varchar(9) NOT NULL + ) ENGINE=OLAP + UNIQUE KEY(`date`) + DISTRIBUTED BY HASH(`date`) BUCKETS 4 + PROPERTIES ( + "replication_allocation" = "tag.location.default: 1", + "in_memory" = "false", + "storage_format" = "V2", + "disable_auto_compaction" = "false" + ); + """ + + sql """ + insert into tb_holiday values (20221111, 1, 1 ),(20221112, 1, 1 ),(20221113, 1, 1 ),(20221116, 2, 2 ),(20221117, 2, 2 ),(20221118, 2, 2 ); + """ + + qt_sql """ + SELECT + date_format(date, '%x%v') AS `date`, + count(date) AS `diff_days` + FROM `tb_holiday` + WHERE `date` between 20221111 AND 20221116 + GROUP BY date + HAVING date = 20221111 + ORDER BY date; + """ + + qt_sql """ + SELECT + date_format(date, '%x%v') AS `date2`, + count(date) AS `diff_days` + FROM `tb_holiday` + WHERE `date` between 20221111 AND 20221116 + GROUP BY date2 + HAVING date2 = 202245 + ORDER BY date2; + """ + + sql """ + DROP TABLE IF EXISTS `tb_holiday`; + """ + } \ No newline at end of file