Repository: phoenix
Updated Branches:
  refs/heads/calcite 542683540 -> 95479c16e


PHOENIX-2203 Support UNNEST WITH ORDINALITY in Calcite-Phoenix


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

Branch: refs/heads/calcite
Commit: 95479c16efdf7fdb40685c22c13f9bbf9f43a159
Parents: 5426835
Author: maryannxue <maryann....@gmail.com>
Authored: Fri Apr 8 11:56:59 2016 -0400
Committer: maryannxue <maryann....@gmail.com>
Committed: Fri Apr 8 11:56:59 2016 -0400

----------------------------------------------------------------------
 .../org/apache/phoenix/calcite/CalciteIT.java   | 14 ++++++++++++
 .../phoenix/calcite/rel/PhoenixUncollect.java   | 24 +++++++++++++-------
 .../calcite/rules/PhoenixConverterRules.java    |  3 ++-
 3 files changed, 32 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/95479c16/phoenix-core/src/it/java/org/apache/phoenix/calcite/CalciteIT.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/calcite/CalciteIT.java 
b/phoenix-core/src/it/java/org/apache/phoenix/calcite/CalciteIT.java
index d583a9e..623a60f 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/calcite/CalciteIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/calcite/CalciteIT.java
@@ -899,6 +899,20 @@ public class CalciteIT extends BaseCalciteIT {
                         {88}, 
                         {80}})
                 .close();
+        start(false, 1000f).sql("SELECT t.o, t.s FROM UNNEST((SELECT scores 
FROM " + SCORES_TABLE_NAME + ")) WITH ORDINALITY AS t(s, o)")
+                .explainIs("PhoenixToEnumerableConverter\n" +
+                           "  PhoenixClientProject(O=[$1], S=[$0])\n" +
+                           "    PhoenixUncollect(withOrdinality=[true])\n" +
+                           "      PhoenixServerProject(EXPR$0=[$2])\n" +
+                           "        PhoenixTableScan(table=[[phoenix, 
SCORES]])\n")
+                .resultIs(0, new Object[][] {
+                        {1, 85},
+                        {2, 80},
+                        {3, 82},
+                        {1, 87},
+                        {2, 88},
+                        {3, 80}})
+                .close();
         start(false, 1000f).sql("SELECT s.student_id, t.score FROM " + 
SCORES_TABLE_NAME + " s, UNNEST((SELECT scores FROM " + SCORES_TABLE_NAME + " 
s2 where s.student_id = s2.student_id)) AS t(score)")
                 .explainIs("PhoenixToEnumerableConverter\n" +
                            "  PhoenixClientProject(STUDENT_ID=[$0], 
SCORE=[$3])\n" +

http://git-wip-us.apache.org/repos/asf/phoenix/blob/95479c16/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixUncollect.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixUncollect.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixUncollect.java
index 3076890..f591fde 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixUncollect.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixUncollect.java
@@ -1,7 +1,7 @@
 package org.apache.phoenix.calcite.rel;
 
 import java.sql.SQLException;
-import java.util.Arrays;
+import java.util.List;
 
 import org.apache.calcite.plan.RelOptCluster;
 import org.apache.calcite.plan.RelOptCost;
@@ -17,24 +17,27 @@ import org.apache.phoenix.expression.Expression;
 import org.apache.phoenix.expression.LiteralExpression;
 import org.apache.phoenix.schema.PTable;
 import org.apache.phoenix.schema.types.PDataType;
+import org.apache.phoenix.schema.types.PInteger;
+
+import com.google.common.collect.Lists;
 
 public class PhoenixUncollect extends Uncollect implements PhoenixRel {
     
-    public static PhoenixUncollect create(RelNode input) {
+    public static PhoenixUncollect create(RelNode input, boolean 
withOrdinality) {
         RelOptCluster cluster = input.getCluster();
         RelTraitSet traits = cluster.traitSetOf(PhoenixConvention.CLIENT);
-        return new PhoenixUncollect(cluster, traits, input);
+        return new PhoenixUncollect(cluster, traits, input, withOrdinality);
     }
 
     private PhoenixUncollect(RelOptCluster cluster, RelTraitSet traitSet,
-            RelNode child) {
-        super(cluster, traitSet, child);
+            RelNode child, boolean withOrdinality) {
+        super(cluster, traitSet, child, withOrdinality);
     }
 
     @Override
     public PhoenixUncollect copy(RelTraitSet traitSet,
         RelNode newInput) {
-        return create(newInput);
+        return create(newInput, withOrdinality);
     }
 
     @Override
@@ -52,13 +55,18 @@ public class PhoenixUncollect extends Uncollect implements 
PhoenixRel {
         @SuppressWarnings("rawtypes")
         PDataType baseType = 
PDataType.fromTypeId(arrayExpression.getDataType().getSqlType() - 
PDataType.ARRAY_TYPE_BASE);
         try {
-            implementor.project(Arrays.<Expression> 
asList(LiteralExpression.newConstant(null, baseType)));
+            List<Expression> fields = Lists.newArrayList();
+            fields.add(LiteralExpression.newConstant(null, baseType));
+            if (withOrdinality) {
+                fields.add(LiteralExpression.newConstant(null, 
PInteger.INSTANCE));
+            }
+            implementor.project(fields);
         } catch (SQLException e) {
             throw new RuntimeException(e);
         }
         PTable projectedTable = 
implementor.getTableMapping().createProjectedTable(implementor.getCurrentContext().retainPKColumns);
         implementor.setTableMapping(new TableMapping(projectedTable));
-        return new UnnestArrayPlan(plan, arrayExpression, false);
+        return new UnnestArrayPlan(plan, arrayExpression, withOrdinality);
     }
 
 }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/95479c16/phoenix-core/src/main/java/org/apache/phoenix/calcite/rules/PhoenixConverterRules.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rules/PhoenixConverterRules.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rules/PhoenixConverterRules.java
index 81dbe21..396974d 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rules/PhoenixConverterRules.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rules/PhoenixConverterRules.java
@@ -680,7 +680,8 @@ public class PhoenixConverterRules {
             return PhoenixUncollect.create(
                 convert(
                         uncollect.getInput(), 
-                        
uncollect.getInput().getTraitSet().replace(PhoenixConvention.GENERIC)));
+                        
uncollect.getInput().getTraitSet().replace(PhoenixConvention.GENERIC)),
+                uncollect.withOrdinality);
         }
     }
 

Reply via email to