Repository: drill
Updated Branches:
  refs/heads/master 3bccec911 -> fb1d3f384


DRILL-1862: Create window function required distribution and collation traits 
conditionally.


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

Branch: refs/heads/master
Commit: fb1d3f384e6c79a540e34e70f8268feb689208ee
Parents: 3bccec9
Author: Aman Sinha <[email protected]>
Authored: Tue Jun 9 12:48:48 2015 -0700
Committer: Aman Sinha <[email protected]>
Committed: Thu Jun 11 17:51:31 2015 -0700

----------------------------------------------------------------------
 .../exec/planner/physical/WindowPrule.java      | 28 ++++++++++++++----
 .../physical/impl/window/TestWindowFrame.java   | 31 ++++++++++----------
 2 files changed, 37 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/drill/blob/fb1d3f38/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 db9218a..f7728c8 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
@@ -22,7 +22,7 @@ import com.google.common.base.Predicate;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Lists;
-import com.sun.java.swing.plaf.windows.resources.windows;
+
 import org.apache.calcite.linq4j.Ord;
 import org.apache.calcite.rel.core.Window;
 import org.apache.calcite.util.BitSets;
@@ -58,14 +58,25 @@ public class WindowPrule extends RelOptRule {
     // TODO: Order window based on existing partition by
     //input.getTraitSet().subsumes()
 
+    boolean partitionby = false;
     for (final Ord<Window.Group> w : Ord.zip(window.groups)) {
       Window.Group windowBase = w.getValue();
-      DrillDistributionTrait distOnAllKeys =
-          new 
DrillDistributionTrait(DrillDistributionTrait.DistributionType.HASH_DISTRIBUTED,
-              ImmutableList.copyOf(getDistributionFields(windowBase)));
+      RelTraitSet traits = 
call.getPlanner().emptyTraitSet().plus(Prel.DRILL_PHYSICAL);
+      if (windowBase.keys.size() > 0) {
+        DrillDistributionTrait distOnAllKeys =
+            new 
DrillDistributionTrait(DrillDistributionTrait.DistributionType.HASH_DISTRIBUTED,
+                ImmutableList.copyOf(getDistributionFields(windowBase)));
+
+        partitionby = true;
+        traits = traits.plus(distOnAllKeys);
+      }
+
+      // Add collation trait if either partition-by or order-by is specified.
+      if (partitionby || windowBase.orderKeys.getFieldCollations().size() > 0) 
{
+        RelCollation collation = getCollation(windowBase);
+        traits = traits.plus(collation);
+      }
 
-      RelCollation collation = getCollation(windowBase);
-      RelTraitSet traits = 
call.getPlanner().emptyTraitSet().plus(Prel.DRILL_PHYSICAL).plus(collation).plus(distOnAllKeys);
       final RelNode convertedInput = convert(input, traits);
 
       List<RelDataTypeField> newRowFields = Lists.newArrayList();
@@ -115,6 +126,11 @@ public class WindowPrule extends RelOptRule {
     call.transformTo(input);
   }
 
+  /**
+   * Create a RelCollation that has partition-by as the leading keys followed 
by order-by keys
+   * @param window The window specification
+   * @return a RelCollation with {partition-by keys, order-by keys}
+   */
   private RelCollation getCollation(Window.Group window) {
     List<RelFieldCollation> fields = Lists.newArrayList();
     for (int group : BitSets.toIter(window.keys)) {

http://git-wip-us.apache.org/repos/asf/drill/blob/fb1d3f38/exec/java-exec/src/test/java/org/apache/drill/exec/physical/impl/window/TestWindowFrame.java
----------------------------------------------------------------------
diff --git 
a/exec/java-exec/src/test/java/org/apache/drill/exec/physical/impl/window/TestWindowFrame.java
 
b/exec/java-exec/src/test/java/org/apache/drill/exec/physical/impl/window/TestWindowFrame.java
index 15fefa5..4295002 100644
--- 
a/exec/java-exec/src/test/java/org/apache/drill/exec/physical/impl/window/TestWindowFrame.java
+++ 
b/exec/java-exec/src/test/java/org/apache/drill/exec/physical/impl/window/TestWindowFrame.java
@@ -17,6 +17,8 @@
  
******************************************************************************/
 package org.apache.drill.exec.physical.impl.window;
 
+import java.util.Properties;
+
 import org.apache.drill.BaseTestQuery;
 import org.apache.drill.DrillTestWrapper;
 import org.apache.drill.common.config.DrillConfig;
@@ -25,8 +27,6 @@ import org.apache.drill.exec.ExecConstants;
 import org.junit.BeforeClass;
 import org.junit.Test;
 
-import java.util.Properties;
-
 public class TestWindowFrame extends BaseTestQuery {
 
   private static final String TEST_RES_PATH = TestTools.getWorkingPath() + 
"/src/test/resources";
@@ -63,15 +63,10 @@ public class TestWindowFrame extends BaseTestQuery {
   }
 
   private void runTest(final String tableName, final boolean withOrderBy) 
throws Exception {
-    runSQL(String.format("alter session set `%s`= true", 
ExecConstants.ENABLE_WINDOW_FUNCTIONS));
 
-    try {
-      DrillTestWrapper testWrapper = withOrderBy ?
-        buildWindowWithOrderByQuery(tableName) : buildWindowQuery(tableName);
-      testWrapper.run();
-    } finally {
-      runSQL(String.format("alter session set `%s`= false", 
ExecConstants.ENABLE_WINDOW_FUNCTIONS));
-    }
+    DrillTestWrapper testWrapper = withOrderBy ?
+      buildWindowWithOrderByQuery(tableName) : buildWindowQuery(tableName);
+    testWrapper.run();
   }
 
   /**
@@ -170,13 +165,17 @@ public class TestWindowFrame extends BaseTestQuery {
 
   @Test // DRILL-3218
   public void testMaxVarChar() throws Exception {
-    runSQL(String.format("alter session set `%s`= true", 
ExecConstants.ENABLE_WINDOW_FUNCTIONS));
+    test("select max(cast(columns[2] as char(2))) over(partition by 
cast(columns[2] as char(2)) order by cast(columns[0] as int)) from 
dfs_test.`%s/window/allData.csv`", TEST_RES_PATH);
+  }
 
-    try {
-      test("select max(cast(columns[2] as char(2))) over(partition by 
cast(columns[2] as char(2)) order by cast(columns[0] as int)) from 
dfs_test.`%s/window/allData.csv`", TEST_RES_PATH);
-    } finally {
-      runSQL(String.format("alter session set `%s`= false", 
ExecConstants.ENABLE_WINDOW_FUNCTIONS));
-    }
+  @Test // DRILL-1862
+  public void testEmptyPartitionBy() throws Exception {
+    test("SELECT employee_id, position_id, salary, SUM(salary) OVER(ORDER BY 
position_id) FROM cp.`employee.json` LIMIT 10");
+  }
 
+  @Test // DRILL-3172
+  public void testEmptyOverClause() throws Exception {
+    test("SELECT employee_id, position_id, salary, SUM(salary) OVER() FROM 
cp.`employee.json` LIMIT 10");
   }
+
 }

Reply via email to