From 11b9679b3d8bf201ab6b3ef8e648ef6a87324eae Mon Sep 17 00:00:00 2001 From: Guoliang Sun Date: Tue, 21 Nov 2023 16:51:32 +0800 Subject: [PATCH] KE-11534 [Follow up]AL-8813 merge with new Calcite --- .../calcite/rel/rules/FilterJoinRule.java | 19 ++++++++----------- .../calcite/rel/rules/SubQueryRemoveRule.java | 16 +++++++--------- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/core/src/main/java/org/apache/calcite/rel/rules/FilterJoinRule.java b/core/src/main/java/org/apache/calcite/rel/rules/FilterJoinRule.java index 523e09f2fe6..559e3c7e37c 100644 --- a/core/src/main/java/org/apache/calcite/rel/rules/FilterJoinRule.java +++ b/core/src/main/java/org/apache/calcite/rel/rules/FilterJoinRule.java @@ -44,11 +44,6 @@ import static org.apache.calcite.plan.RelOptUtil.conjunctions; -/** - * Turn it off. - * Though try to turn it off in OLAPTableScan, sometimes it still triggerd. - */ - /** * Planner rule that pushes filters above and * within a join node into the join node and/or its children nodes. @@ -76,11 +71,6 @@ protected void perform(RelOptRuleCall call, @Nullable Filter filter, RelOptUtil.conjunctions(join.getCondition()); final List origJoinFilters = ImmutableList.copyOf(joinFilters); - // HACK POINT - if (join.getJoinType() != JoinRelType.INNER || joinFilters.size() != 0) { - return; - } - // If there is only the joinRel, // make sure it does not match a cartesian product joinRel // (with "true" condition), otherwise this rule will be applied @@ -98,7 +88,9 @@ protected void perform(RelOptRuleCall call, @Nullable Filter filter, // Simplify Outer Joins JoinRelType joinType = join.getJoinType(); + boolean pushInto = canPushIntoFromAbove(filter); if (config.isSmart() + && pushInto && !origAboveFilters.isEmpty() && join.getJoinType() != JoinRelType.INNER) { joinType = RelOptUtil.simplifyJoin(join, origAboveFilters, joinType); @@ -117,7 +109,7 @@ protected void perform(RelOptRuleCall call, @Nullable Filter filter, // filters. They can be pushed down if they are not on the NULL // generating side. boolean filterPushed = false; - if (RelOptUtil.classifyFilters( + if (pushInto && RelOptUtil.classifyFilters( join, aboveFilters, joinType.canPushIntoFromAbove(), @@ -257,6 +249,11 @@ private static List getConjunctions(Filter filter) { return conjunctions; } + // https://olapio.atlassian.net/browse/AL-8813 + protected boolean canPushIntoFromAbove(Filter filter) { + return true; + } + /** * Validates that target execution framework can satisfy join filters. * diff --git a/core/src/main/java/org/apache/calcite/rel/rules/SubQueryRemoveRule.java b/core/src/main/java/org/apache/calcite/rel/rules/SubQueryRemoveRule.java index 40b06ffe2f0..7ab4540556c 100644 --- a/core/src/main/java/org/apache/calcite/rel/rules/SubQueryRemoveRule.java +++ b/core/src/main/java/org/apache/calcite/rel/rules/SubQueryRemoveRule.java @@ -89,10 +89,10 @@ protected SubQueryRemoveRule(Config config) { protected RexNode apply(RexSubQuery e, Set variablesSet, RelOptUtil.Logic logic, - RelBuilder builder, int inputCount, int offset, boolean forceBuildInnerJoin) { + RelBuilder builder, int inputCount, int offset) { switch (e.getKind()) { case SCALAR_QUERY: - return rewriteScalarQuery(e, variablesSet, builder, inputCount, offset, forceBuildInnerJoin); + return rewriteScalarQuery(e, variablesSet, builder, inputCount, offset); case ARRAY_QUERY_CONSTRUCTOR: return rewriteCollection(e, SqlTypeName.ARRAY, variablesSet, builder, inputCount, offset); @@ -128,7 +128,7 @@ protected RexNode apply(RexSubQuery e, Set variablesSet, * @return Expression that may be used to replace the RexSubQuery */ private static RexNode rewriteScalarQuery(RexSubQuery e, Set variablesSet, - RelBuilder builder, int inputCount, int offset, boolean forceBuildInnerJoin) { + RelBuilder builder, int inputCount, int offset) { builder.push(e.rel); final RelMetadataQuery mq = e.rel.getCluster().getMetadataQuery(); final Boolean unique = mq.areColumnsUnique(builder.peek(), @@ -138,8 +138,7 @@ private static RexNode rewriteScalarQuery(RexSubQuery e, Set vari builder.aggregateCall(SqlStdOperatorTable.SINGLE_VALUE, builder.field(0))); } - builder.join(forceBuildInnerJoin ? JoinRelType.INNER : JoinRelType.LEFT, - builder.literal(true), variablesSet); + builder.join(JoinRelType.LEFT, builder.literal(true), variablesSet); return field(builder, inputCount, offset); } @@ -817,7 +816,7 @@ private static void matchProject(SubQueryRemoveRule rule, final Set variablesSet = RelOptUtil.getVariablesUsed(e.rel); final RexNode target = rule.apply(e, variablesSet, - logic, builder, 1, fieldCount, false); + logic, builder, 1, fieldCount); final RexShuttle shuttle = new ReplaceSubQueryShuttle(e, target); builder.project(shuttle.apply(project.getProjects()), project.getRowType().getFieldNames()); @@ -843,8 +842,7 @@ private static void matchFilter(SubQueryRemoveRule rule, final Set variablesSet = RelOptUtil.getVariablesUsed(e.rel); final RexNode target = rule.apply(e, variablesSet, logic, - builder, 1, builder.peek().getRowType().getFieldCount(), - c.getKind() == SqlKind.EQUALS && variablesSet.isEmpty()); + builder, 1, builder.peek().getRowType().getFieldCount()); final RexShuttle shuttle = new ReplaceSubQueryShuttle(e, target); c = c.accept(shuttle); } @@ -868,7 +866,7 @@ private static void matchJoin(SubQueryRemoveRule rule, RelOptRuleCall call) { final Set variablesSet = RelOptUtil.getVariablesUsed(e.rel); final RexNode target = rule.apply(e, variablesSet, - logic, builder, 2, fieldCount, false); + logic, builder, 2, fieldCount); final RexShuttle shuttle = new ReplaceSubQueryShuttle(e, target); builder.join(join.getJoinType(), shuttle.apply(join.getCondition())); builder.project(fields(builder, join.getRowType().getFieldCount()));