-
Notifications
You must be signed in to change notification settings - Fork 13.9k
[FLINK-39583][table-planner] Normalize Calcite correl variables for more effective sub-plan digest reuse #27959
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ferenc-csaky
wants to merge
1
commit into
apache:master
Choose a base branch
from
ferenc-csaky:normalize-calcite-cor-vars
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
198 changes: 198 additions & 0 deletions
198
...in/java/org/apache/flink/table/planner/plan/optimize/CorrelVariableNormalizerShuttle.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,198 @@ | ||
| /* | ||
| * 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.flink.table.planner.plan.optimize; | ||
|
|
||
| import org.apache.calcite.rel.RelNode; | ||
| import org.apache.calcite.rel.RelShuttleImpl; | ||
| import org.apache.calcite.rel.core.CorrelationId; | ||
| import org.apache.calcite.rel.logical.LogicalCorrelate; | ||
| import org.apache.calcite.rel.logical.LogicalFilter; | ||
| import org.apache.calcite.rel.logical.LogicalJoin; | ||
| import org.apache.calcite.rel.logical.LogicalProject; | ||
| import org.apache.calcite.rel.logical.LogicalTableFunctionScan; | ||
| import org.apache.calcite.rex.RexBuilder; | ||
| import org.apache.calcite.rex.RexCorrelVariable; | ||
| import org.apache.calcite.rex.RexNode; | ||
| import org.apache.calcite.rex.RexShuttle; | ||
| import org.apache.calcite.rex.RexSubQuery; | ||
|
|
||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
|
|
||
| /** | ||
| * Normalizes correlation variable ids in a RelNode tree to make equivalent subplans digest-match. | ||
| */ | ||
| public final class CorrelVariableNormalizerShuttle extends RelShuttleImpl { | ||
|
|
||
| private final Map<Integer, Integer> idMap = new LinkedHashMap<>(); | ||
|
|
||
| private final RexBuilder rexBuilder; | ||
| private final RexShuttle rexCorrelNormalizer; | ||
|
|
||
| public CorrelVariableNormalizerShuttle(RexBuilder rexBuilder) { | ||
| this.rexBuilder = rexBuilder; | ||
| rexCorrelNormalizer = new RexCorrelNormalizer(); | ||
| } | ||
|
|
||
| @Override | ||
| public RelNode visit(LogicalCorrelate correlate) { | ||
| var adjustedId = adjustCorrelationId(correlate.getCorrelationId()); | ||
| if (adjustedId.isPresent()) { | ||
| var left = correlate.getLeft().accept(this); | ||
| var right = correlate.getRight().accept(this); | ||
| return correlate.copy( | ||
| correlate.getTraitSet(), | ||
| left, | ||
| right, | ||
| adjustedId.get(), | ||
| correlate.getRequiredColumns(), | ||
| correlate.getJoinType()); | ||
| } | ||
|
|
||
| return super.visit(correlate); | ||
| } | ||
|
|
||
| @Override | ||
| public RelNode visit(RelNode relNode) { | ||
| if (relNode instanceof LogicalTableFunctionScan && relNode.getInputs().isEmpty()) { | ||
| // visitChild applies the RexShuttle while walking RelNode inputs. A zero-input table | ||
| // function scan is a leaf, but unlike a regular TableScan it can still contain RexNodes | ||
| // (e.g., UNNEST over a correl variable), so rewrite it explicitly. | ||
| return relNode.accept(rexCorrelNormalizer); | ||
| } | ||
|
|
||
| return super.visit(relNode); | ||
| } | ||
|
|
||
| @Override | ||
| protected RelNode visitChild(RelNode parent, int i, RelNode child) { | ||
| if (i == 0) { | ||
| parent = parent.accept(rexCorrelNormalizer); | ||
| parent = remapVariablesSet(parent); | ||
| } | ||
|
|
||
| return super.visitChild(parent, i, child); | ||
| } | ||
|
|
||
| /** | ||
| * Filter, Project, and Join carry a {@link CorrelationId} set alongside their RexNodes. {@code | ||
| * RelNode.accept(RexShuttle)} only rewrites the RexNodes and preserves the old {@code | ||
| * variablesSet} via {@code copy()}, so ids we just adjusted in the condition/projects are still | ||
| * advertised under their old names. To overcome that, we need to rebuild that variable with the | ||
| * adjusted ids set as well. | ||
| */ | ||
| private RelNode remapVariablesSet(RelNode relNode) { | ||
| var oldSet = relNode.getVariablesSet(); | ||
| if (oldSet.isEmpty()) { | ||
| return relNode; | ||
| } | ||
|
|
||
| var builder = com.google.common.collect.ImmutableSet.<CorrelationId>builder(); | ||
| boolean changed = false; | ||
| for (var id : oldSet) { | ||
| var adjusted = adjustCorrelationId(id); | ||
| if (adjusted.isPresent()) { | ||
| builder.add(adjusted.get()); | ||
| changed = true; | ||
| } else { | ||
| builder.add(id); | ||
| } | ||
| } | ||
|
|
||
| if (!changed) { | ||
| return relNode; | ||
| } | ||
|
|
||
| var newSet = builder.build(); | ||
| if (relNode instanceof LogicalFilter) { | ||
| var filter = (LogicalFilter) relNode; | ||
| return new LogicalFilter( | ||
| filter.getCluster(), | ||
| filter.getTraitSet(), | ||
| filter.getHints(), | ||
| filter.getInput(), | ||
| filter.getCondition(), | ||
| newSet); | ||
| } | ||
|
|
||
| if (relNode instanceof LogicalProject) { | ||
| var project = (LogicalProject) relNode; | ||
| return new LogicalProject( | ||
| project.getCluster(), | ||
| project.getTraitSet(), | ||
| project.getHints(), | ||
| project.getInput(), | ||
| project.getProjects(), | ||
| project.getRowType(), | ||
| newSet); | ||
| } | ||
|
|
||
| if (relNode instanceof LogicalJoin) { | ||
| var join = (LogicalJoin) relNode; | ||
| return new LogicalJoin( | ||
| join.getCluster(), | ||
| join.getTraitSet(), | ||
| join.getHints(), | ||
| join.getLeft(), | ||
| join.getRight(), | ||
| join.getCondition(), | ||
| newSet, | ||
| join.getJoinType(), | ||
| join.isSemiJoinDone(), | ||
| com.google.common.collect.ImmutableList.copyOf(join.getSystemFieldList())); | ||
| } | ||
|
|
||
| return relNode; | ||
| } | ||
|
|
||
| private Optional<CorrelationId> adjustCorrelationId(CorrelationId correlationId) { | ||
| if (correlationId.getName().startsWith(CorrelationId.CORREL_PREFIX)) { | ||
| int oldId = correlationId.getId(); | ||
| int newId = idMap.computeIfAbsent(oldId, k -> idMap.size() + 1); | ||
| if (newId != oldId) { | ||
| return Optional.of(new CorrelationId(newId)); | ||
| } | ||
| } | ||
|
|
||
| return Optional.empty(); | ||
| } | ||
|
|
||
| private final class RexCorrelNormalizer extends RexShuttle { | ||
|
|
||
| @Override | ||
| public RexNode visitCorrelVariable(RexCorrelVariable variable) { | ||
| var adjustedId = adjustCorrelationId(variable.id); | ||
| if (adjustedId.isPresent()) { | ||
| return rexBuilder.makeCorrel(variable.getType(), adjustedId.get()); | ||
| } else { | ||
| return super.visitCorrelVariable(variable); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public RexNode visitSubQuery(RexSubQuery subQuery) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this should also be covered by the tests. Right now they don't test sub-queries for correl variable normalization. |
||
| // Let the base shuttle rewrite the RexSubQuery's operands first, so any | ||
| // RexCorrelVariables they carry (e.g., the LHS of IN/SOME) are also adjusted. | ||
| RexSubQuery withOperands = (RexSubQuery) super.visitSubQuery(subQuery); | ||
| RelNode rewritten = withOperands.rel.accept(CorrelVariableNormalizerShuttle.this); | ||
| return rewritten == withOperands.rel ? withOperands : withOperands.clone(rewritten); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.