Skip to content

Commit

Permalink
KE-11534 [Follow up]AL-8813 merge with new Calcite
Browse files Browse the repository at this point in the history
  • Loading branch information
gleonSun authored and pfzhan committed Dec 9, 2023
1 parent 5984c4b commit 11b9679
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -76,11 +71,6 @@ protected void perform(RelOptRuleCall call, @Nullable Filter filter,
RelOptUtil.conjunctions(join.getCondition());
final List<RexNode> 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
Expand All @@ -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);
Expand All @@ -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(),
Expand Down Expand Up @@ -257,6 +249,11 @@ private static List<RexNode> 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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ protected SubQueryRemoveRule(Config config) {

protected RexNode apply(RexSubQuery e, Set<CorrelationId> 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);
Expand Down Expand Up @@ -128,7 +128,7 @@ protected RexNode apply(RexSubQuery e, Set<CorrelationId> variablesSet,
* @return Expression that may be used to replace the RexSubQuery
*/
private static RexNode rewriteScalarQuery(RexSubQuery e, Set<CorrelationId> 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(),
Expand All @@ -138,8 +138,7 @@ private static RexNode rewriteScalarQuery(RexSubQuery e, Set<CorrelationId> 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);
}

Expand Down Expand Up @@ -817,7 +816,7 @@ private static void matchProject(SubQueryRemoveRule rule,
final Set<CorrelationId> 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());
Expand All @@ -843,8 +842,7 @@ private static void matchFilter(SubQueryRemoveRule rule,
final Set<CorrelationId> 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);
}
Expand All @@ -868,7 +866,7 @@ private static void matchJoin(SubQueryRemoveRule rule, RelOptRuleCall call) {
final Set<CorrelationId> 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()));
Expand Down

0 comments on commit 11b9679

Please sign in to comment.