You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by mo...@apache.org on 2022/12/14 04:29:31 UTC

[doris] 10/13: [fix](agg)having clause should use alias if there is no group by clause (#14831)

This is an automated email from the ASF dual-hosted git repository.

morningman pushed a commit to branch branch-1.2-lts
in repository https://gitbox.apache.org/repos/asf/doris.git

commit a0342ee47c47925701e966924eda3585b033eea6
Author: starocean999 <40...@users.noreply.github.com>
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 3f754595f1..5e1cb19a73 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: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org