Skip to content

Commit

Permalink
[fix](agg)having clause should use column name first then alias (apac…
Browse files Browse the repository at this point in the history
…he#14408)

* [fix](agg)having clause should use column name first then alias

* fix fe ut
  • Loading branch information
starocean999 authored Nov 24, 2022
1 parent f6de03e commit ae4f4b9
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -432,20 +432,18 @@ protected void substituteOrdinalsAliases(List<Expr> 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -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`;
"""
}

0 comments on commit ae4f4b9

Please sign in to comment.