drill-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j..@apache.org
Subject drill git commit: DRILL-3210 : Expand star column properly when it is used together with window function in projection list of SQL query.
Date Thu, 25 Jun 2015 12:53:21 GMT
Repository: drill
Updated Branches:
  refs/heads/master 3f0d9221d -> 58c3c4c69


DRILL-3210 : Expand star column properly when it is used together with window function in
projection list of SQL query.


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

Branch: refs/heads/master
Commit: 58c3c4c69748bdf2423c1b4f543407faef50a767
Parents: 3f0d922
Author: Jinfeng Ni <jni@apache.org>
Authored: Wed Jun 24 13:16:27 2015 -0700
Committer: Jinfeng Ni <jni@apache.org>
Committed: Wed Jun 24 23:28:56 2015 -0700

----------------------------------------------------------------------
 .../drill/exec/planner/physical/WindowPrel.java | 30 +++++++++++++++-
 .../org/apache/drill/TestExampleQueries.java    | 37 ++++++++++++++++++++
 2 files changed, 66 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/drill/blob/58c3c4c6/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/WindowPrel.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/WindowPrel.java
b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/WindowPrel.java
index 122dee8..170438e 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/WindowPrel.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/WindowPrel.java
@@ -20,6 +20,8 @@ package org.apache.drill.exec.planner.physical;
 
 import com.google.common.collect.Lists;
 
+import org.apache.calcite.rel.type.RelDataTypeField;
+import org.apache.calcite.rel.type.RelRecordType;
 import org.apache.drill.common.expression.ExpressionPosition;
 import org.apache.drill.common.expression.FieldReference;
 import org.apache.drill.common.expression.FunctionCall;
@@ -42,9 +44,11 @@ import org.apache.calcite.rex.RexLiteral;
 import org.apache.calcite.util.BitSets;
 
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 
 import static com.google.common.base.Preconditions.checkState;
 
@@ -60,7 +64,8 @@ public class WindowPrel extends DrillWindowRelBase implements Prel {
 
   @Override
   public RelNode copy(RelTraitSet traitSet, List<RelNode> inputs) {
-    return new WindowPrel(getCluster(), traitSet, sole(inputs), constants, getRowType(),
groups.get(0));
+    final RelDataType copiedRowType = deriveCopiedRowTypeFromInput(sole(inputs));
+    return new WindowPrel(getCluster(), traitSet, sole(inputs), constants, copiedRowType,
groups.get(0));
   }
 
   @Override
@@ -145,4 +150,27 @@ public class WindowPrel extends DrillWindowRelBase implements Prel {
   public Iterator<Prel> iterator() {
     return PrelUtil.iter(getInput());
   }
+
+  /**
+   * Derive rowType for the copied WindowPrel based on input.
+   * When copy() is called, the input might be different from the current one's input.
+   * We have to use the new input's field in the copied WindowPrel.
+   */
+  private RelDataType deriveCopiedRowTypeFromInput(final RelNode input) {
+    final RelDataType inputRowType = input.getRowType();
+    final RelDataType windowRowType = this.getRowType();
+
+    final List<RelDataTypeField> fieldList = new ArrayList<>(inputRowType.getFieldList());
+    final int inputFieldCount = inputRowType.getFieldCount();
+    final int windowFieldCount = windowRowType.getFieldCount();
+
+    for (int i = inputFieldCount; i < windowFieldCount; i++) {
+      fieldList.add(windowRowType.getFieldList().get(i));
+    }
+
+    final RelDataType rowType = this.getCluster().getRexBuilder().getTypeFactory().createStructType(fieldList);
+
+    return rowType;
+  }
+
 }

http://git-wip-us.apache.org/repos/asf/drill/blob/58c3c4c6/exec/java-exec/src/test/java/org/apache/drill/TestExampleQueries.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/test/java/org/apache/drill/TestExampleQueries.java b/exec/java-exec/src/test/java/org/apache/drill/TestExampleQueries.java
index 6d03d81..e8af325 100644
--- a/exec/java-exec/src/test/java/org/apache/drill/TestExampleQueries.java
+++ b/exec/java-exec/src/test/java/org/apache/drill/TestExampleQueries.java
@@ -1054,4 +1054,41 @@ public class TestExampleQueries extends BaseTestQuery {
 
   }
 
+  @Test // DRILL-3210
+  public void testWindowFunAndStarCol() throws Exception {
+    // SingleTableQuery : star + window function
+    final String query =
+        " select * , sum(n_nationkey) over (partition by n_regionkey) as sumwin " +
+        " from cp.`tpch/nation.parquet`";
+    final String baseQuery =
+        " select n_nationkey, n_name, n_regionkey, n_comment, " +
+        "   sum(n_nationkey) over (partition by n_regionkey) as sumwin " +
+        " from cp.`tpch/nation.parquet`";
+    testBuilder()
+        .sqlQuery(query)
+        .unOrdered()
+        .sqlBaselineQuery(baseQuery)
+        .build()
+        .run();;
+
+    // JoinQuery: star + window function
+    final String joinQuery =
+        " select *, sum(n.n_nationkey) over (partition by r.r_regionkey order by r.r_name)
as sumwin" +
+        " from cp.`tpch/nation.parquet` n, cp.`tpch/region.parquet` r " +
+        " where n.n_regionkey = r.r_regionkey";
+    final String joinBaseQuery =
+        " select n.n_nationkey, n.n_name, n.n_regionkey, n.n_comment, r.r_regionkey, r.r_name,
r.r_comment, " +
+        "   sum(n.n_nationkey) over (partition by r.r_regionkey order by r.r_name) as sumwin
" +
+        " from cp.`tpch/nation.parquet` n, cp.`tpch/region.parquet` r " +
+        " where n.n_regionkey = r.r_regionkey";
+
+    testBuilder()
+        .sqlQuery(joinQuery)
+        .unOrdered()
+        .sqlBaselineQuery(joinBaseQuery)
+        .build()
+        .run();
+
+  }
+
 }


Mime
View raw message