Skip to content

[CALCITE-7967] Run sub-query removal before RelDecorrelator in the default planning pipeline - #5462

Open
iwanttobepowerful wants to merge 1 commit into
apache:mainfrom
iwanttobepowerful:CALCITE-7967
Open

iwanttobepowerful wants to merge 1 commit into
apache:mainfrom
iwanttobepowerful:CALCITE-7967

Conversation

@iwanttobepowerful

Copy link
Copy Markdown
Contributor

@iwanttobepowerful
iwanttobepowerful force-pushed the CALCITE-7967 branch 2 times, most recently from e4e1559 to d74132c Compare November 27, 2026 06:55
@iwanttobepowerful
iwanttobepowerful marked this pull request as draft November 27, 2026 07:28
@iwanttobepowerful
iwanttobepowerful force-pushed the CALCITE-7967 branch 2 times, most recently from 6c70d9b to 5b3ea80 Compare November 27, 2026 13:58
@iwanttobepowerful
iwanttobepowerful marked this pull request as ready for review November 27, 2026 14:17
if (this.context.config().forceDecorrelate()
&& !this.context.config().topDownGeneralDecorrelationEnabled()) {
&& !this.context.config().topDownGeneralDecorrelationEnabled()
&& configHolder.get().isExpand()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this THE change?

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.

added a new isDecorrelateAfterSubQueryRemoval config


// TopDownGeneralDecorrelator cannot be used until the subquerys are completely removed.
// Likewise, if the sub-queries were not expanded during sql-to-rel, defer
// decorrelation to the program: RelDecorrelator must not run before the

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

what is "the program"?
(couldn't tell from a scan of the code around, maybe it's obvious if you read more of the class)

* <p>The decorrelator must not see a mixture of {@code RexSubQuery}
* expressions and {@code Correlate} nodes, otherwise it can leave dangling
* correlation variables. Default is true. */
@Value.Default default boolean isDecorrelateAfterSubQueryRemoval() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't understand why this config option is in SqlToRelConverter yet the converter never looks at the option.

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.

Thanks for pointing this out. You’re right: this option controls the planning pipeline, not SQL-to-rel conversion, so it does not belong in SqlToRelConverter.Config.

I’ve removed the option and the early decorrelation call in Prepare. The default pipeline now relies on Programs.standard(), which already runs sub-query removal before decorrelation. This makes the ordering unconditional rather than configurable. forceDecorrelate is unchanged and still controls whether decorrelation is attempted.

I also added regression tests covering both decorrelators with expansion enabled and disabled, and verifying that forceDecorrelate=false preserves EnumerableCorrelate. The full core test suite and Checkstyle checks pass.

* expressions (for example when {@link #isExpand()} is false); the planner
* program removes the sub-queries first and then decorrelates.
*
* <p>The decorrelator must not see a mixture of {@code RexSubQuery}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

instead of producing an incorrect plan the decorrelator should just throw if it cannot produce its result

@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants