drill-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From amansi...@apache.org
Subject drill git commit: DRILL-3657: When constants are referred in Window Prel, ensure the indices are shifted properly
Date Fri, 21 Aug 2015 05:26:52 GMT
Repository: drill
Updated Branches:
  refs/heads/master 32ee76d2e -> 31dd19cb6


DRILL-3657: When constants are referred in Window Prel, ensure the indices are shifted properly


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

Branch: refs/heads/master
Commit: 31dd19cb666c14794032bbd3dcec489ecd2633dd
Parents: 32ee76d
Author: Hsuan-Yi Chu <hsuanyi@usc.edu>
Authored: Sun Aug 16 19:04:03 2015 -0700
Committer: Aman Sinha <asinha@maprtech.com>
Committed: Thu Aug 20 22:12:41 2015 -0700

----------------------------------------------------------------------
 .../exec/planner/physical/WindowPrule.java      | 36 +++++++++++++++++++-
 .../apache/drill/exec/TestWindowFunctions.java  | 33 ++++++++++++++++++
 2 files changed, 68 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/drill/blob/31dd19cb/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/WindowPrule.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/WindowPrule.java
b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/WindowPrule.java
index f6d2b67..1980335 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/WindowPrule.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/WindowPrule.java
@@ -40,9 +40,10 @@ import org.apache.calcite.plan.RelTraitSet;
 import org.apache.calcite.rel.type.RelDataType;
 import org.apache.calcite.rel.type.RelDataTypeField;
 import org.apache.calcite.rel.type.RelRecordType;
+import org.apache.calcite.rex.RexInputRef;
+import org.apache.calcite.rex.RexNode;
 import org.apache.calcite.sql.SqlAggFunction;
 
-import java.util.ArrayList;
 import java.util.List;
 
 public class WindowPrule extends Prule {
@@ -62,6 +63,11 @@ public class WindowPrule extends Prule {
 
     boolean partitionby = false;
     boolean addMerge = false;
+
+    // The start index of the constant fields of DrillWindowRel
+    final int startConstantsIndex = window.getInput().getRowType().getFieldCount();
+
+    int constantShiftIndex = 0;
     for (final Ord<Window.Group> w : Ord.zip(window.groups)) {
       Window.Group windowBase = w.getValue();
       RelTraitSet traits = call.getPlanner().emptyTraitSet().plus(Prel.DRILL_PHYSICAL);
@@ -129,6 +135,32 @@ public class WindowPrule extends Prule {
       List<Window.RexWinAggCall> newWinAggCalls = Lists.newArrayList();
       for(Ord<Window.RexWinAggCall> aggOrd : Ord.zip(windowBase.aggCalls)) {
         Window.RexWinAggCall aggCall = aggOrd.getValue();
+
+        // If the argument points at the constant and
+        // additional fields have been generated by the Window below,
+        // the index of constants will be shifted
+        final List<RexNode> newOperandsOfWindowFunction = Lists.newArrayList();
+        for(RexNode operand : aggCall.getOperands()) {
+          if(operand instanceof RexInputRef) {
+            final RexInputRef rexInputRef = (RexInputRef) operand;
+            final int refIndex = rexInputRef.getIndex();
+
+            // Check if this RexInputRef points at the constants
+            if(rexInputRef.getIndex() >= startConstantsIndex) {
+              operand = new RexInputRef(refIndex + constantShiftIndex,
+                  window.constants.get(refIndex - startConstantsIndex).getType());
+            }
+          }
+
+          newOperandsOfWindowFunction.add(operand);
+        }
+
+        aggCall = new Window.RexWinAggCall(
+            (SqlAggFunction) aggCall.getOperator(),
+            aggCall.getType(),
+            newOperandsOfWindowFunction,
+            aggCall.ordinal);
+
         newWinAggCalls.add(new Window.RexWinAggCall(
             (SqlAggFunction)aggCall.getOperator(), aggCall.getType(), aggCall.getOperands(),
aggOrd.i)
         );
@@ -150,6 +182,8 @@ public class WindowPrule extends Prule {
           window.getConstants(),
           rowType,
           windowBase);
+
+      constantShiftIndex += windowBase.aggCalls.size();
     }
 
     call.transformTo(input);

http://git-wip-us.apache.org/repos/asf/drill/blob/31dd19cb/exec/java-exec/src/test/java/org/apache/drill/exec/TestWindowFunctions.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/test/java/org/apache/drill/exec/TestWindowFunctions.java b/exec/java-exec/src/test/java/org/apache/drill/exec/TestWindowFunctions.java
index ae5562f..29b702c 100644
--- a/exec/java-exec/src/test/java/org/apache/drill/exec/TestWindowFunctions.java
+++ b/exec/java-exec/src/test/java/org/apache/drill/exec/TestWindowFunctions.java
@@ -417,4 +417,37 @@ public class TestWindowFunctions extends BaseTestQuery {
         .build()
         .run();
   }
+
+  @Test // see DRILL-3657
+  public void testConstantsInMultiplePartitions() throws Exception {
+    String root = FileUtils.getResourceAsFile("/store/text/data/t.json").toURI().toString();
+    String query = String.format(
+        "select sum(1) over(partition by b1 order by a1) as sum1, sum(1) over(partition by
a1) as sum2, rank() over(order by b1) as rank1, rank() over(order by 1) as rank2 \n" +
+        "from dfs_test.`%s` \n" +
+        "order by 1, 2, 3, 4", root);
+
+    // Validate the plan
+    final String[] expectedPlan = {"Window.*SUM\\(\\$3\\).*\n" +
+        ".*SelectionVectorRemover.*\n" +
+        ".*Sort.*\n" +
+        ".*Window.*SUM\\(\\$2\\).*"
+    };
+    PlanTestBase.testPlanMatchingPatterns(query, expectedPlan, new String[]{});
+    testBuilder()
+        .sqlQuery(query)
+        .unOrdered()
+        .baselineColumns("sum1", "sum2", "rank1", "rank2")
+        .baselineValues(2l, 5l, 1l, 1l)
+        .baselineValues(2l, 5l, 1l, 1l)
+        .baselineValues(2l, 5l, 6l, 1l)
+        .baselineValues(2l, 5l, 6l, 1l)
+        .baselineValues(3l, 5l, 3l, 1l)
+        .baselineValues(3l, 5l, 3l, 1l)
+        .baselineValues(3l, 5l, 3l, 1l)
+        .baselineValues(3l, 5l, 8l, 1l)
+        .baselineValues(3l, 5l, 8l, 1l)
+        .baselineValues(3l, 5l, 8l, 1l)
+        .build()
+        .run();
+  }
 }
\ No newline at end of file


Mime
View raw message