Repository: phoenix
Updated Branches:
  refs/heads/4.x-HBase-1.1 fe2c33441 -> d7c436b0f


PHOENIX-2930 Cannot resolve columns aliased to its own name


Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo
Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/d7c436b0
Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/d7c436b0
Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/d7c436b0

Branch: refs/heads/4.x-HBase-1.1
Commit: d7c436b0ff46c67f07934c5186214c6f9b1d7f16
Parents: fe2c334
Author: maryannxue <maryann....@gmail.com>
Authored: Wed Sep 21 22:03:06 2016 -0700
Committer: maryannxue <maryann....@gmail.com>
Committed: Wed Sep 21 22:03:06 2016 -0700

----------------------------------------------------------------------
 .../apache/phoenix/end2end/GroupByCaseIT.java   | 36 ++++++++++++++++++++
 .../apache/phoenix/parse/ParseNodeRewriter.java | 16 +++++++--
 2 files changed, 49 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/d7c436b0/phoenix-core/src/it/java/org/apache/phoenix/end2end/GroupByCaseIT.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/it/java/org/apache/phoenix/end2end/GroupByCaseIT.java 
b/phoenix-core/src/it/java/org/apache/phoenix/end2end/GroupByCaseIT.java
index 4830a75..0acfe02 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/GroupByCaseIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/GroupByCaseIT.java
@@ -31,6 +31,7 @@ import java.sql.SQLException;
 import java.sql.Statement;
 import java.util.Properties;
 
+import org.apache.phoenix.schema.AmbiguousColumnException;
 import org.apache.phoenix.schema.types.PChar;
 import org.apache.phoenix.schema.types.PInteger;
 import org.apache.phoenix.util.ByteUtil;
@@ -445,4 +446,39 @@ public class GroupByCaseIT extends ParallelStatsDisabledIT 
{
                 "    SERVER FILTER BY FIRST KEY ONLY\n" + 
                 "    SERVER AGGREGATE INTO ORDERED DISTINCT ROWS BY [K1]", 
QueryUtil.getExplainPlan(rs));
     }
+    
+    @Test
+    public void testGroupByWithAliasWithSameColumnName() throws SQLException {
+        Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
+        Connection conn = DriverManager.getConnection(getUrl(), props);
+        String ddl = "create table test3 (pk integer primary key, col 
integer)";
+        conn.createStatement().execute(ddl);
+        ddl = "create table test4 (pk integer primary key, col integer)";
+        conn.createStatement().execute(ddl);
+        ddl = "create table test5 (notPk integer primary key, col integer)";
+        conn.createStatement().execute(ddl);
+        conn.createStatement().execute("UPSERT INTO test3 VALUES (1,2)");
+        conn.createStatement().execute("UPSERT INTO test4 VALUES (1,2)");
+        conn.createStatement().execute("UPSERT INTO test5 VALUES (1,2)");
+        conn.createStatement().executeQuery("select test3.pk as pk from test3 
group by pk");
+        conn.createStatement().executeQuery("select test3.pk as pk from test3 
group by test3.pk");
+        conn.createStatement().executeQuery("select test3.pk as pk from test3 
as t group by t.pk");
+        conn.createStatement().executeQuery("select test3.col as pk from 
test3");
+        conn.createStatement()
+                .executeQuery("select test3.pk as pk from test3 join test5 on 
(test3.pk=test5.notPk) group by pk");
+        try {
+            conn.createStatement().executeQuery("select test3.col as pk from 
test3 group by pk");
+            fail();
+        } catch (AmbiguousColumnException e) {}
+        try {
+            conn.createStatement().executeQuery("select col as pk from test3 
group by pk");
+            fail();
+        } catch (AmbiguousColumnException e) {}
+        try {
+            conn.createStatement()
+                    .executeQuery("select test3.pk as pk from test3 join test4 
on (test3.pk=test4.pk) group by pk");
+            fail();
+        } catch (AmbiguousColumnException e) {}
+        conn.close();
+    }
 }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/d7c436b0/phoenix-core/src/main/java/org/apache/phoenix/parse/ParseNodeRewriter.java
----------------------------------------------------------------------
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 12ef2e1..7a31110 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
@@ -25,6 +25,7 @@ import java.util.Map;
 import org.apache.phoenix.compile.ColumnResolver;
 import org.apache.phoenix.schema.AmbiguousColumnException;
 import org.apache.phoenix.schema.ColumnNotFoundException;
+import org.apache.phoenix.schema.ColumnRef;
 
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
@@ -400,14 +401,23 @@ public class ParseNodeRewriter extends 
TraverseAllParseNodeVisitor<ParseNode> {
             ParseNode aliasedNode = aliasMap.get(node.getName());
             // If we found something, then try to resolve it unless the two 
nodes are the same
             if (aliasedNode != null && !node.equals(aliasedNode)) {
+                ColumnRef ref;
                 try {
-                    // If we're able to resolve it, that means we have a 
conflict
-                    resolver.resolveColumn(node.getSchemaName(), 
node.getTableName(), node.getName());
-                    throw new AmbiguousColumnException(node.getName());
+                    ref = resolver.resolveColumn(node.getSchemaName(), 
node.getTableName(), node.getName());
                 } catch (ColumnNotFoundException e) {
                     // Not able to resolve alias as a column name as well, so 
we use the alias
                     return aliasedNode;
                 }
+                // We have resolved it to a column, so now check if the 
aliased node can be resolved as the same column
+                if (aliasedNode instanceof ColumnParseNode) {
+                    ColumnParseNode aliasedColumnNode = (ColumnParseNode) 
aliasedNode;
+                    ColumnRef aliasedRef = 
resolver.resolveColumn(aliasedColumnNode.getSchemaName(), 
aliasedColumnNode.getTableName(), aliasedColumnNode.getName());
+                    if (aliasedRef.equals(ref)) {
+                        return aliasedNode;
+                    }
+                }
+                // Otherwise it means we have a conflict
+                throw new AmbiguousColumnException(node.getName());
             }
         }
         return node;

Reply via email to