This is an automated email from the ASF dual-hosted git repository. richardantal pushed a commit to branch 5.1 in repository https://gitbox.apache.org/repos/asf/phoenix.git
The following commit(s) were added to refs/heads/5.1 by this push: new e89f79f PHOENIX-6413 Having cannot resolve alias (#1168) e89f79f is described below commit e89f79f42220ff4b99c7f998dc182fde18a55538 Author: Baiqiang Zhao <zbq.d...@gmail.com> AuthorDate: Sat Mar 20 06:21:41 2021 +0800 PHOENIX-6413 Having cannot resolve alias (#1168) --- .../java/org/apache/phoenix/end2end/GroupByIT.java | 43 +++++++++++++++++++++- .../apache/phoenix/parse/ParseNodeRewriter.java | 15 +++++--- 2 files changed, 51 insertions(+), 7 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 4dcb1cd..11611d0 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,6 +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 02de80f..bfb5782 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++) {