Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.apache.calcite.rel.RelHomogeneousShuttle;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.core.Correlate;
import org.apache.calcite.rel.core.CorrelationId;
import org.apache.calcite.rel.core.Filter;
import org.apache.calcite.rel.core.Project;
Expand Down Expand Up @@ -208,6 +209,11 @@ private static Set<RexNode> findCorrelationDependentCalls(CorrelationId corrId,
SimpleCorrelationCollector finder = new SimpleCorrelationCollector(corrId);
plan.accept(new RelHomogeneousShuttle() {
@Override public RelNode visit(RelNode other) {
// Don't descend into a nested correlate that reuses this id: the refs below belong to it.
if (other instanceof Correlate
&& ((Correlate) other).getCorrelationId().equals(corrId)) {
return other;
}
if (other instanceof Project || other instanceof Filter) {
other.accept(finder);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2548,4 +2548,43 @@ private RelNode decorrelateSql(String sql) {
+ " LogicalTableScan(table=[[bookstore, authors]])\n";
assertThat(after, hasTree(planAfter));
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-7753">[CALCITE-7753]
* CorrelateProjectExtractor corrupts plans with nested Correlates that reuse
* the same correlation id</a>. */
@Test void testNestedCorrelatesSharingCorrelationId() {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in jira: it is not reproducible with pure SQL in Calcite

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the bug requires two nested Correlates that share the same correlation id, and pure SQL (in Calcite) never produces that shape: Calcite's SqlToRelConverter gives every correlated subquery scope its own id.

final RelBuilder builder = RelBuilder.create(config().build())
.transform(c -> c.withSimplify(false));
final Holder<@Nullable RexCorrelVariable> v = Holder.empty();
builder.scan("EMP").variable(v::set);
final RelNode emp = builder.build();

final RelNode inner = builder
.scan("EMP")
.scan("EMP")
.filter(builder.equals(builder.field(v.get(), "DEPTNO"), builder.field("DEPTNO")))
.correlate(JoinRelType.INNER, v.get().id, builder.field(2, 0, "DEPTNO"))
.filter(builder.isNull(builder.field(v.get(), "COMM")))
.build();

final RelNode before = builder
.push(emp)
.push(inner)
.correlate(JoinRelType.LEFT, v.get().id, builder.field(2, 0, "COMM"))
.build();
final String planBefore = ""
+ "LogicalCorrelate(correlation=[$cor0], joinType=[left], requiredColumns=[{6}])\n"
+ " LogicalTableScan(table=[[scott, EMP]])\n"
+ " LogicalFilter(condition=[IS NULL($cor0.COMM)])\n"
+ " LogicalCorrelate(correlation=[$cor0], joinType=[inner], requiredColumns=[{7}])\n"
+ " LogicalTableScan(table=[[scott, EMP]])\n"
+ " LogicalFilter(condition=[=($cor0.DEPTNO, $7)])\n"
+ " LogicalTableScan(table=[[scott, EMP]])\n";
assertThat(before, hasTree(planBefore));

RelDecorrelator.decorrelateQuery(before, builder,
RuleSets.ofList(Collections.emptyList()),
RuleSets.ofList(Collections.emptyList()));
}
}
Loading