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-2953: Ensure sort operator be inserted to enforce desired order, when a cast function is involved.
Date Tue, 12 May 2015 14:20:01 GMT
Repository: drill
Updated Branches:
  refs/heads/master 8d5778362 -> 71fe8f02f


DRILL-2953: Ensure sort operator be inserted to enforce desired order, when a cast function
is involved.


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

Branch: refs/heads/master
Commit: 71fe8f02f3102eeb0c5f5d1504420f441f8f98d7
Parents: 8d57783
Author: Jinfeng Ni <jni@apache.org>
Authored: Mon May 11 15:39:24 2015 -0700
Committer: Jinfeng Ni <jni@apache.org>
Committed: Tue May 12 04:23:35 2015 -0700

----------------------------------------------------------------------
 .../exec/planner/physical/ProjectPrule.java     | 39 +++++++++++++++-----
 .../org/apache/drill/TestExampleQueries.java    | 34 +++++++++++++++++
 2 files changed, 63 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/drill/blob/71fe8f02/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/ProjectPrule.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/ProjectPrule.java
b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/ProjectPrule.java
index bf27a6f..4aca314 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/ProjectPrule.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/ProjectPrule.java
@@ -58,8 +58,12 @@ public class ProjectPrule extends Prule {
     RelTraitSet traits = input.getTraitSet().plus(Prel.DRILL_PHYSICAL);
     RelNode convertedInput = convert(input, traits);
 
-    Map<Integer, Integer> inToOut = getProjectMap(project);
-    boolean traitPull = new ProjectTraitPull(call, inToOut).go(project, convertedInput);
+    // Maintain two different map for distribution trait and collation trait.
+    // For now, the only difference comes from the way how cast function impacts propagating
trait.
+    final Map<Integer, Integer> distributionMap = getDistributionMap(project);
+    final Map<Integer, Integer> collationMap = getCollationMap(project);
+
+    boolean traitPull = new ProjectTraitPull(call, distributionMap, collationMap).go(project,
convertedInput);
 
     if(!traitPull){
       call.transformTo(new ProjectPrel(project.getCluster(), convertedInput.getTraitSet(),
convertedInput, project.getProjects(), project.getRowType()));
@@ -67,11 +71,13 @@ public class ProjectPrule extends Prule {
   }
 
   private class ProjectTraitPull extends SubsetTransformer<DrillProjectRel, RuntimeException>
{
-    final Map<Integer, Integer> inToOut;
+    final Map<Integer, Integer> distributionMap;
+    final Map<Integer, Integer> collationMap;
 
-    public ProjectTraitPull(RelOptRuleCall call, Map<Integer, Integer> inToOut) {
+    public ProjectTraitPull(RelOptRuleCall call, Map<Integer, Integer> distributionMap,
Map<Integer, Integer> collationMap) {
       super(call);
-      this.inToOut = inToOut;
+      this.distributionMap = distributionMap;
+      this.collationMap = collationMap;
     }
 
     @Override
@@ -80,8 +86,8 @@ public class ProjectPrule extends Prule {
       RelCollation childCollation = rel.getTraitSet().getTrait(RelCollationTraitDef.INSTANCE);
 
 
-      DrillDistributionTrait newDist = convertDist(childDist, inToOut);
-      RelCollation newCollation = convertRelCollation(childCollation, inToOut);
+      DrillDistributionTrait newDist = convertDist(childDist, distributionMap);
+      RelCollation newCollation = convertRelCollation(childCollation, collationMap);
       RelTraitSet newProjectTraits = newTraitSet(Prel.DRILL_PHYSICAL, newDist, newCollation);
       return new ProjectPrel(project.getCluster(), newProjectTraits, rel, project.getProjects(),
project.getRowType());
     }
@@ -126,17 +132,17 @@ public class ProjectPrule extends Prule {
     }
   }
 
-  private Map<Integer, Integer> getProjectMap(DrillProjectRel project) {
+  private Map<Integer, Integer> getDistributionMap(DrillProjectRel project) {
     Map<Integer, Integer> m = new HashMap<Integer, Integer>();
 
     for (Ord<RexNode> node : Ord.zip(project.getProjects())) {
+      // For distribution, either $0 or cast($0 as ...) would keep the distribution after
projection.
       if (node.e instanceof RexInputRef) {
         m.put( ((RexInputRef) node.e).getIndex(), node.i);
       } else if (node.e.isA(SqlKind.CAST)) {
         RexNode operand = ((RexCall) node.e).getOperands().get(0);
         if (operand instanceof RexInputRef) {
-          m.put(
-              ((RexInputRef) operand).getIndex(), node.i);
+          m.put(((RexInputRef) operand).getIndex(), node.i);
         }
       }
     }
@@ -144,4 +150,17 @@ public class ProjectPrule extends Prule {
 
   }
 
+  private Map<Integer, Integer> getCollationMap(DrillProjectRel project) {
+    Map<Integer, Integer> m = new HashMap<Integer, Integer>();
+
+    for (Ord<RexNode> node : Ord.zip(project.getProjects())) {
+      // For collation, only $0 will keep the sort-ness after projection.
+      if (node.e instanceof RexInputRef) {
+        m.put( ((RexInputRef) node.e).getIndex(), node.i);
+      }
+    }
+    return m;
+
+  }
+
 }

http://git-wip-us.apache.org/repos/asf/drill/blob/71fe8f02/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 dad43a3..75bbc13 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
@@ -961,4 +961,38 @@ public class TestExampleQueries extends BaseTestQuery{
         .run();
   }
 
+  @Test //DRILL-2953
+  public void testGbAndObDifferentExp() throws Exception {
+    String root = FileUtils.getResourceAsFile("/store/text/data/nations.csv").toURI().toString();
+    String query = String.format(
+        "select cast(columns[0] as int) as nation_key " +
+        " from dfs_test.`%s` " +
+        " group by columns[0] " +
+        " order by cast(columns[0] as int)", root);
+
+    testBuilder()
+        .sqlQuery(query)
+        .ordered()
+        .csvBaselineFile("testframework/testExampleQueries/testGroupByStarSchemaless.tsv")
+        .baselineTypes(TypeProtos.MinorType.INT)
+        .baselineColumns("nation_key")
+        .build()
+        .run();
+
+    String query2 = String.format(
+        "select cast(columns[0] as int) as nation_key " +
+            " from dfs_test.`%s` " +
+            " group by cast(columns[0] as int) " +
+            " order by cast(columns[0] as int)", root);
+
+    testBuilder()
+        .sqlQuery(query2)
+        .ordered()
+        .csvBaselineFile("testframework/testExampleQueries/testGroupByStarSchemaless.tsv")
+        .baselineTypes(TypeProtos.MinorType.INT)
+        .baselineColumns("nation_key")
+        .build()
+        .run();
+
+  }
 }


Mime
View raw message