This is an automated email from the ASF dual-hosted git repository.
yiguolei pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/master by this push:
new 6b5e10c8be [fix](agg)having clause should use alias if there is no
group by clause (#14831)
6b5e10c8be is described below
commit 6b5e10c8beb4d6bf683aaff047c863b6e9e0fb0d
Author: starocean999 <[email protected]>
AuthorDate: Wed Dec 7 14:13:17 2022 +0800
[fix](agg)having clause should use alias if there is no group by clause
(#14831)
---
.../java/org/apache/doris/analysis/SelectStmt.java | 21 +++++++++++++++------
.../data/correctness_p0/test_group_having_alias.out | 5 +++++
.../correctness_p0/test_group_having_alias.groovy | 9 +++++++++
3 files changed, 29 insertions(+), 6 deletions(-)
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 3e9743beac..4205b4fa03 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,12 +985,21 @@ public class SelectStmt extends QueryStmt {
* (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'
*/
- try {
- // use col name from tableRefs first
- havingClauseAfterAnaylzed = havingClause.clone();
- havingClauseAfterAnaylzed.analyze(analyzer);
- } catch (AnalysisException ex) {
- // then consider alias name
+ if (groupByClause != null) {
+ // according to mysql
+ // if there is a group by clause, the having clause should use
column name not alias
+ // this is the same as group by clause
+ 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);
+ }
+ } else {
+ // according to mysql
+ // if there is no group by clause, the having clause should
use alias
havingClauseAfterAnaylzed = havingClause.substitute(aliasSMap,
analyzer, false);
}
havingClauseAfterAnaylzed =
rewriteQueryExprByMvColumnExpr(havingClauseAfterAnaylzed, analyzer);
diff --git a/regression-test/data/correctness_p0/test_group_having_alias.out
b/regression-test/data/correctness_p0/test_group_having_alias.out
index bb2ad80ef9..fa1e177399 100644
--- a/regression-test/data/correctness_p0/test_group_having_alias.out
+++ b/regression-test/data/correctness_p0/test_group_having_alias.out
@@ -5,3 +5,8 @@
-- !sql --
202245 3
+-- !sql --
+202245
+202245
+202245
+
diff --git
a/regression-test/suites/correctness_p0/test_group_having_alias.groovy
b/regression-test/suites/correctness_p0/test_group_having_alias.groovy
index 6416240291..1ff8dbbad8 100644
--- a/regression-test/suites/correctness_p0/test_group_having_alias.groovy
+++ b/regression-test/suites/correctness_p0/test_group_having_alias.groovy
@@ -62,6 +62,15 @@
ORDER BY date2;
"""
+ qt_sql """
+ SELECT
+ date_format(date, '%x%v') AS `date`
+ FROM `tb_holiday`
+ WHERE `date` between 20221111 AND 20221116
+ HAVING date = 202245
+ ORDER BY date;
+ """
+
sql """
DROP TABLE IF EXISTS `tb_holiday`;
"""
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]