diff --git a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveSortUnionReduceRule.java b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveSortUnionReduceRule.java new file mode 100644 index 0000000..73e1d81 --- /dev/null +++ b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveSortUnionReduceRule.java @@ -0,0 +1,106 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hive.ql.optimizer.calcite.rules; + +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.List; + +import org.apache.calcite.plan.RelOptRule; +import org.apache.calcite.plan.RelOptRuleCall; +import org.apache.calcite.plan.RelOptRuleOperand; +import org.apache.calcite.plan.hep.HepRelVertex; +import org.apache.calcite.rel.RelCollations; +import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rex.RexLiteral; +import org.apache.calcite.rex.RexNode; +import org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveSort; +import org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveUnion; + +/** + * Planner rule that pushes a + * {@link org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveSort} + * past a + * {@link org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveUnion}. + */ +public class HiveSortUnionReduceRule extends RelOptRule { + + // ~ Constructors ----------------------------------------------------------- + + public HiveSortUnionReduceRule() { + this(operand(HiveSort.class, operand(HiveUnion.class, any()))); + } + + private HiveSortUnionReduceRule(RelOptRuleOperand operand) { + super(operand); + } + + // ~ Methods ---------------------------------------------------------------- + + @Override + public void onMatch(RelOptRuleCall call) { + final HiveSort sort = call.rel(0); + final HiveUnion union = call.rel(1); + + transform(call, sort, union); + } + + public void transform(RelOptRuleCall call, HiveSort sort, HiveUnion union) { + // If sort does not consist only of a limit operation, we bail out + if (sort.getCollation() != RelCollations.EMPTY) { + return; + } + + List inputs = new ArrayList<>(); + int needed = RexLiteral.intValue(sort.fetch); + boolean ret = true; + for (RelNode input : union.getInputs()) { + if (((HepRelVertex) input).getCurrentRel() instanceof HiveSort) { + // change the fetch number + HiveSort inputAsSort = (HiveSort) ((HepRelVertex) input).getCurrentRel(); + if (RexLiteral.intValue(inputAsSort.fetch) != needed) { + ret = false; + RexNode fetch = inputAsSort.getCluster().getRexBuilder().makeExactLiteral( + BigDecimal.valueOf(Math.min(RexLiteral.intValue(inputAsSort.fetch), needed))); + HiveSort newInputAsSort = inputAsSort.copy(inputAsSort.getTraitSet(), + inputAsSort.getInput(), inputAsSort.getCollation(), inputAsSort.offset, fetch); + inputs.add(newInputAsSort); + } + else{ + inputs.add(inputAsSort); + } + } else { + ret = false; + // add a fetch + RelNode sortAdded = sort.copy(sort.getTraitSet(), input, sort.getCollation(), sort.offset, + sort.fetch); + inputs.add(sortAdded); + } + } + //there is nothing to change + if (ret) { + return; + } + //create new union and sort + HiveUnion unionCopy = new HiveUnion(union.getCluster(), union.getTraitSet(), inputs); + HiveSort result = sort.copy(sort.getTraitSet(), unionCopy, sort.getCollation(), sort.offset, + sort.fetch); + call.transformTo(result); + } + +} diff --git a/ql/src/java/org/apache/hadoop/hive/ql/parse/CalcitePlanner.java b/ql/src/java/org/apache/hadoop/hive/ql/parse/CalcitePlanner.java index 226b10d..2edcf7c 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/parse/CalcitePlanner.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/parse/CalcitePlanner.java @@ -150,6 +150,7 @@ import org.apache.hadoop.hive.ql.optimizer.calcite.rules.HiveRelFieldTrimmer; import org.apache.hadoop.hive.ql.optimizer.calcite.rules.HiveSortJoinReduceRule; import org.apache.hadoop.hive.ql.optimizer.calcite.rules.HiveSortJoinReduceRule.HiveSortProjectJoinReduceRule; +import org.apache.hadoop.hive.ql.optimizer.calcite.rules.HiveSortUnionReduceRule; import org.apache.hadoop.hive.ql.optimizer.calcite.rules.HiveWindowingFixRule; import org.apache.hadoop.hive.ql.optimizer.calcite.translator.ASTConverter; import org.apache.hadoop.hive.ql.optimizer.calcite.translator.HiveOpConverter; @@ -1034,6 +1035,7 @@ private RelNode applyPreJoinOrderingTransforms(RelNode basePlan, RelMetadataProv new FilterProjectTransposeRule(Filter.class, HiveFilter.DEFAULT_FILTER_FACTORY, HiveProject.class, HiveProject.DEFAULT_PROJECT_FACTORY)); + basePlan = hepPlan(basePlan, true, mdProvider, new HiveSortUnionReduceRule()); return basePlan; }