diff --git ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveSortLimitPullUpConstantsRule.java ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveSortLimitPullUpConstantsRule.java index d14b0ba..3be9b0a 100644 --- ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveSortLimitPullUpConstantsRule.java +++ ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveSortLimitPullUpConstantsRule.java @@ -108,6 +108,7 @@ public void onMatch(RelOptRuleCall call) { } // Create expressions for Project operators before and after the Sort + boolean atLeastOneConstant = false; List fields = sort.getInput().getRowType().getFieldList(); List> newChildExprs = new ArrayList<>(); List topChildExprs = new ArrayList<>(); @@ -116,6 +117,7 @@ public void onMatch(RelOptRuleCall call) { RexNode expr = rexBuilder.makeInputRef(sort.getInput(), i); RelDataTypeField field = fields.get(i); if (constants.containsKey(expr)) { + atLeastOneConstant = true; topChildExprs.add(constants.get(expr)); topChildExprsFields.add(field.getName()); } else { @@ -125,6 +127,11 @@ public void onMatch(RelOptRuleCall call) { } } + // No constants were found + if (!atLeastOneConstant) { + return; + } + // Update field collations final Mappings.TargetMapping mapping = RelOptUtil.permutation(Pair.left(newChildExprs), sort.getInput().getRowType()).inverse(); diff --git ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveUnionPullUpConstantsRule.java ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveUnionPullUpConstantsRule.java index 3155cb1..2552f87 100644 --- ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveUnionPullUpConstantsRule.java +++ ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveUnionPullUpConstantsRule.java @@ -96,6 +96,7 @@ public void onMatch(RelOptRuleCall call) { } // Create expressions for Project operators before and after the Union + boolean atLeastOneConstant = false; List fields = union.getRowType().getFieldList(); List> newChildExprs = new ArrayList<>(); List topChildExprs = new ArrayList<>(); @@ -104,6 +105,7 @@ public void onMatch(RelOptRuleCall call) { RexNode expr = rexBuilder.makeInputRef(union, i); RelDataTypeField field = fields.get(i); if (constants.containsKey(expr)) { + atLeastOneConstant = true; topChildExprs.add(constants.get(expr)); topChildExprsFields.add(field.getName()); } else { @@ -113,6 +115,11 @@ public void onMatch(RelOptRuleCall call) { } } + // No constants were found + if (!atLeastOneConstant) { + return; + } + // Update top Project positions final Mappings.TargetMapping mapping = RelOptUtil.permutation(Pair.left(newChildExprs), union.getInput(0).getRowType()).inverse();