You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hive.apache.org by ha...@apache.org on 2015/05/01 06:38:41 UTC

[2/2] hive git commit: HIVE-10455 : CBO (Calcite Return Path): Different data types at Reducer before JoinOp (Pengcheng Xiong via Ashutosh Chauhan)

HIVE-10455 : CBO (Calcite Return Path): Different data types at Reducer before JoinOp (Pengcheng Xiong via Ashutosh Chauhan)

Signed-off-by: Ashutosh Chauhan <ha...@apache.org>


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

Branch: refs/heads/branch-1.2
Commit: edc4132053077ed5ce0dbbabe87e5049e4820b81
Parents: 8b7f692
Author: Pengcheng Xiong <px...@hortonworks.com>
Authored: Thu Apr 30 18:07:00 2015 -0700
Committer: Ashutosh Chauhan <ha...@apache.org>
Committed: Thu Apr 30 21:38:08 2015 -0700

----------------------------------------------------------------------
 .../calcite/reloperators/HiveSortExchange.java  | 37 +++++++++++++++-----
 .../rules/HiveInsertExchange4JoinRule.java      |  6 +++-
 .../calcite/translator/HiveOpConverter.java     | 28 ++++++++-------
 3 files changed, 49 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hive/blob/edc41320/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/reloperators/HiveSortExchange.java
----------------------------------------------------------------------
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/reloperators/HiveSortExchange.java b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/reloperators/HiveSortExchange.java
index 8cbc953..3961636 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/reloperators/HiveSortExchange.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/reloperators/HiveSortExchange.java
@@ -6,42 +6,61 @@ import org.apache.calcite.rel.RelCollation;
 import org.apache.calcite.rel.RelCollationTraitDef;
 import org.apache.calcite.rel.RelDistribution;
 import org.apache.calcite.rel.RelDistributionTraitDef;
-import org.apache.calcite.rel.RelInput;
 import org.apache.calcite.rel.RelNode;
 import org.apache.calcite.rel.core.SortExchange;
+import org.apache.calcite.rex.RexNode;
+import org.apache.hadoop.hive.ql.plan.ExprNodeDesc;
+
+import com.google.common.collect.ImmutableList;
 
 public class HiveSortExchange extends SortExchange {
+  private ImmutableList<RexNode> joinKeys;
+  private ExprNodeDesc[] joinExpressions;
 
   private HiveSortExchange(RelOptCluster cluster, RelTraitSet traitSet,
-      RelNode input, RelDistribution distribution, RelCollation collation) {
+      RelNode input, RelDistribution distribution, RelCollation collation, ImmutableList<RexNode> joinKeys) {
     super(cluster, traitSet, input, distribution, collation);
+    this.joinKeys = new ImmutableList.Builder<RexNode>().addAll(joinKeys).build();
   }
 
-  public HiveSortExchange(RelInput input) {
-    super(input);
-  }
-  
   /**
    * Creates a HiveSortExchange.
    *
    * @param input     Input relational expression
    * @param distribution Distribution specification
    * @param collation Collation specification
+   * @param joinKeys Join Keys specification
    */
   public static HiveSortExchange create(RelNode input,
-      RelDistribution distribution, RelCollation collation) {
+      RelDistribution distribution, RelCollation collation, ImmutableList<RexNode> joinKeys) {
     RelOptCluster cluster = input.getCluster();
     distribution = RelDistributionTraitDef.INSTANCE.canonize(distribution);
     collation = RelCollationTraitDef.INSTANCE.canonize(collation);
     RelTraitSet traitSet = RelTraitSet.createEmpty().plus(distribution).plus(collation);
-    return new HiveSortExchange(cluster, traitSet, input, distribution, collation);
+    return new HiveSortExchange(cluster, traitSet, input, distribution, collation, joinKeys);
   }
 
   @Override
   public SortExchange copy(RelTraitSet traitSet, RelNode newInput, RelDistribution newDistribution,
           RelCollation newCollation) {
     return new HiveSortExchange(getCluster(), traitSet, newInput,
-            newDistribution, newCollation);
+            newDistribution, newCollation, joinKeys);
+  }
+
+  public ImmutableList<RexNode> getJoinKeys() {
+    return joinKeys;
+  }
+
+  public void setJoinKeys(ImmutableList<RexNode> joinKeys) {
+    this.joinKeys = joinKeys;
+  }
+
+  public ExprNodeDesc[] getJoinExpressions() {
+    return joinExpressions;
+  }
+
+  public void setJoinExpressions(ExprNodeDesc[] joinExpressions) {
+    this.joinExpressions = joinExpressions;
   }
 
 }

http://git-wip-us.apache.org/repos/asf/hive/blob/edc41320/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveInsertExchange4JoinRule.java
----------------------------------------------------------------------
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveInsertExchange4JoinRule.java b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveInsertExchange4JoinRule.java
index 6cceacb..30db8fd 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveInsertExchange4JoinRule.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveInsertExchange4JoinRule.java
@@ -29,6 +29,7 @@ import org.apache.calcite.rel.RelNode;
 import org.apache.calcite.rel.core.Exchange;
 import org.apache.calcite.rel.core.Join;
 import org.apache.calcite.rel.rules.MultiJoin;
+import org.apache.calcite.rex.RexNode;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.hadoop.hive.ql.optimizer.calcite.HiveCalciteUtil;
@@ -91,6 +92,7 @@ public class HiveInsertExchange4JoinRule extends RelOptRule {
     List<RelNode> newInputs = new ArrayList<RelNode>();
     for (int i=0; i<call.rel(0).getInputs().size(); i++) {
       List<Integer> joinKeyPositions = new ArrayList<Integer>();
+      ImmutableList.Builder<RexNode> keyListBuilder = new ImmutableList.Builder<RexNode>();
       ImmutableList.Builder<RelFieldCollation> collationListBuilder =
               new ImmutableList.Builder<RelFieldCollation>();
       for (int j = 0; j < joinPredInfo.getEquiJoinPredicateElements().size(); j++) {
@@ -100,12 +102,14 @@ public class HiveInsertExchange4JoinRule extends RelOptRule {
           if (!joinKeyPositions.contains(pos)) {
             joinKeyPositions.add(pos);
             collationListBuilder.add(new RelFieldCollation(pos));
+            keyListBuilder.add(joinLeafPredInfo.getJoinKeyExprs(i).get(0));
           }
         }
       }
       HiveSortExchange exchange = HiveSortExchange.create(call.rel(0).getInput(i),
               new HiveRelDistribution(RelDistribution.Type.HASH_DISTRIBUTED, joinKeyPositions),
-              new HiveRelCollation(collationListBuilder.build()));
+              new HiveRelCollation(collationListBuilder.build()),
+              keyListBuilder.build());
       newInputs.add(exchange);
     }
 

http://git-wip-us.apache.org/repos/asf/hive/blob/edc41320/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/translator/HiveOpConverter.java
----------------------------------------------------------------------
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/translator/HiveOpConverter.java b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/translator/HiveOpConverter.java
index 94528e2..b8ae604 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/translator/HiveOpConverter.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/translator/HiveOpConverter.java
@@ -27,6 +27,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
+import org.apache.calcite.rel.RelCollation;
 import org.apache.calcite.rel.RelCollations;
 import org.apache.calcite.rel.RelDistribution;
 import org.apache.calcite.rel.RelDistribution.Type;
@@ -65,6 +66,7 @@ import org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveFilter;
 import org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveJoin;
 import org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveProject;
 import org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveSort;
+import org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveSortExchange;
 import org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveTableScan;
 import org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveUnion;
 import org.apache.hadoop.hive.ql.parse.JoinCond;
@@ -167,8 +169,8 @@ public class HiveOpConverter {
       return visit((HiveSort) rn);
     } else if (rn instanceof HiveUnion) {
       return visit((HiveUnion) rn);
-    } else if (rn instanceof SortExchange) {
-      return visit((SortExchange) rn);
+    } else if (rn instanceof HiveSortExchange) {
+      return visit((HiveSortExchange) rn);
     } else if (rn instanceof HiveAggregate) {
       return visit((HiveAggregate) rn);
     }
@@ -329,8 +331,11 @@ public class HiveOpConverter {
       joinPredInfo = JoinPredicateInfo.constructJoinPredicateInfo((MultiJoin)joinRel);
     }
 
-    // 3. Extract join keys from condition
-    ExprNodeDesc[][] joinKeys = extractJoinKeys(joinPredInfo, joinRel.getInputs(), inputs);
+    // 3. Extract join key expressions from HiveSortExchange
+    ExprNodeDesc[][] joinExpressions = new ExprNodeDesc[inputs.length][];
+    for (int i = 0; i < inputs.length; i++) {
+      joinExpressions[i] = ((HiveSortExchange) joinRel.getInput(i)).getJoinExpressions();
+    }
 
     // 4.a Generate tags
     for (int tag=0; tag<children.size(); tag++) {
@@ -338,7 +343,7 @@ public class HiveOpConverter {
       reduceSinkOp.getConf().setTag(tag);
     }
     // 4.b Generate Join operator
-    JoinOperator joinOp = genJoin(joinRel, joinPredInfo, children, joinKeys);
+    JoinOperator joinOp = genJoin(joinRel, joinPredInfo, children, joinExpressions);
 
     // 5. TODO: Extract condition for non-equi join elements (if any) and
     // add it
@@ -520,7 +525,7 @@ public class HiveOpConverter {
     return inputs[0].clone(unionOp);
   }
 
-  OpAttr visit(SortExchange exchangeRel) throws SemanticException {
+  OpAttr visit(HiveSortExchange exchangeRel) throws SemanticException {
     OpAttr inputOpAf = dispatch(exchangeRel.getInput());
 
     if (LOG.isDebugEnabled()) {
@@ -532,13 +537,12 @@ public class HiveOpConverter {
     if (distribution.getType() != Type.HASH_DISTRIBUTED) {
       throw new SemanticException("Only hash distribution supported for LogicalExchange");
     }
-    ExprNodeDesc[] expressions = new ExprNodeDesc[distribution.getKeys().size()];
-    for (int i = 0; i < distribution.getKeys().size(); i++) {
-      int key = distribution.getKeys().get(i);
-      ColumnInfo colInfo = inputOpAf.inputs.get(0).getSchema().getSignature().get(key);
-      ExprNodeDesc column = new ExprNodeColumnDesc(colInfo);
-      expressions[i] = column;
+    ExprNodeDesc[] expressions = new ExprNodeDesc[exchangeRel.getJoinKeys().size()];
+    for (int index = 0; index < exchangeRel.getJoinKeys().size(); index++) {
+      expressions[index] = convertToExprNode((RexNode) exchangeRel.getJoinKeys().get(index),
+          exchangeRel.getInput(), null, inputOpAf);
     }
+    exchangeRel.setJoinExpressions(expressions);
 
     ReduceSinkOperator rsOp = genReduceSink(inputOpAf.inputs.get(0), expressions,
         -1, -1, Operation.NOT_ACID, strictMode);