You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@phoenix.apache.org by ri...@apache.org on 2021/07/27 12:36:36 UTC

[phoenix] branch 4.16 updated: PHOENIX-6413 Having cannot resolve alias (#1168)

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

richardantal pushed a commit to branch 4.16
in repository https://gitbox.apache.org/repos/asf/phoenix.git


The following commit(s) were added to refs/heads/4.16 by this push:
     new 14abe87  PHOENIX-6413 Having cannot resolve alias (#1168)
14abe87 is described below

commit 14abe87b85e6b6cff463402294609ae313560373
Author: Baiqiang Zhao <zb...@gmail.com>
AuthorDate: Sat Mar 20 06:21:41 2021 +0800

    PHOENIX-6413 Having cannot resolve alias (#1168)
    
    Change-Id: I37c2e9448ef53e66115f523dc4a71877287d52be
---
 .../java/org/apache/phoenix/end2end/GroupByIT.java | 43 ++++++++++++++++++++++
 .../apache/phoenix/parse/ParseNodeRewriter.java    | 15 +++++---
 2 files changed, 52 insertions(+), 6 deletions(-)

diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/GroupByIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/GroupByIT.java
index 83d34eb..93ad97e 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/GroupByIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/GroupByIT.java
@@ -333,4 +333,47 @@ public class GroupByIT extends BaseQueryIT {
             conn.close();
         }
     }
+
+    @Test
+    public void testGroupByHavingWithAlias() throws Exception {
+        Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
+        Connection conn = DriverManager.getConnection(getUrl(), props);
+        conn.setAutoCommit(false);
+
+        try {
+            conn = DriverManager.getConnection(getUrl(), props);
+            conn.setAutoCommit(false);
+            String tableName = generateUniqueName();
+            String ddl = "CREATE TABLE " + tableName + " (a_string varchar not null, col1 integer"
+              + " CONSTRAINT pk PRIMARY KEY (a_string))";
+            createTestTable(getUrl(), ddl);
+
+            String dml = "UPSERT INTO " + tableName + " VALUES(?, ?)";
+            PreparedStatement stmt = conn.prepareStatement(dml);
+            stmt.setString(1, "a");
+            stmt.setInt(2, 40);
+            stmt.execute();
+            stmt.setString(1, "b");
+            stmt.setInt(2, 20);
+            stmt.execute();
+            stmt.setString(1, "c");
+            stmt.setInt(2, 30);
+            stmt.execute();
+            stmt.execute();
+            conn.commit();
+
+            String query = "SELECT a_string, sum(col1) as sumCol1 FROM " + tableName
+              + " GROUP BY a_string HAVING sumCol1>20 ORDER BY sumCol1";
+            ResultSet rs = conn.createStatement().executeQuery(query);
+            assertTrue(rs.next());
+            assertEquals("c", rs.getString(1));
+            assertEquals(30, rs.getInt(2));
+            assertTrue(rs.next());
+            assertEquals("a", rs.getString(1));
+            assertEquals(40, rs.getInt(2));
+            assertFalse(rs.next());
+        } finally {
+            conn.close();
+        }
+    }
 }
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/parse/ParseNodeRewriter.java b/phoenix-core/src/main/java/org/apache/phoenix/parse/ParseNodeRewriter.java
index 01533ef..8b30b97 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/parse/ParseNodeRewriter.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/parse/ParseNodeRewriter.java
@@ -102,12 +102,7 @@ public class ParseNodeRewriter extends TraverseAllParseNodeVisitor<ParseNode> {
                 normOffset = offset.accept(rewriter);
             }
         }
-        ParseNode having = statement.getHaving();
-        ParseNode normHaving= having;
-        if (having != null) {
-            rewriter.reset();
-            normHaving = having.accept(rewriter);
-        }
+
         List<AliasedNode> selectNodes = statement.getSelect();
         List<AliasedNode> normSelectNodes = selectNodes;
         for (int i = 0; i < selectNodes.size(); i++) {
@@ -157,6 +152,14 @@ public class ParseNodeRewriter extends TraverseAllParseNodeVisitor<ParseNode> {
             }
             normGroupByNodes.add(normGroupByNode);
         }
+
+        ParseNode having = statement.getHaving();
+        ParseNode normHaving= having;
+        if (having != null) {
+            rewriter.reset();
+            normHaving = having.accept(rewriter);
+        }
+
         List<OrderByNode> orderByNodes = statement.getOrderBy();
         List<OrderByNode> normOrderByNodes = orderByNodes;
         for (int i = 0; i < orderByNodes.size(); i++) {