Skip to content

[CALCITE-7440] Preserve correlated scope mapping in RelToSqlConverter#4828

Open
bvolpato wants to merge 2 commits intoapache:mainfrom
bvolpato:bvolpato/calcite-7440-rel2sql-correl-scope
Open

[CALCITE-7440] Preserve correlated scope mapping in RelToSqlConverter#4828
bvolpato wants to merge 2 commits intoapache:mainfrom
bvolpato:bvolpato/calcite-7440-rel2sql-correl-scope

Conversation

@bvolpato
Copy link

@bvolpato bvolpato commented Mar 9, 2026

Summary

RelToSqlConverter failed during PostgreSQL round-trip validation when semi-join rewrites introduced a correlated filter with an unresolved correlation id.

Reproduction query

WITH product_keys AS (
  SELECT p."product_id",
         (SELECT MAX(p3."product_id")
          FROM "foodmart"."product" p3
          WHERE p3."product_id" = p."product_id") AS "mx"
  FROM "foodmart"."product" p
)
SELECT DISTINCT pk."product_id"
FROM product_keys pk
LEFT JOIN "foodmart"."product" p2 USING ("product_id")
WHERE pk."product_id" IN (
  SELECT p4."product_id"
  FROM "foodmart"."product" p4
)

Error before this PR

java.lang.NullPointerException: variable $cor1 is not found
  at org.apache.calcite.rel.rel2sql.SqlImplementor.getAliasContext(SqlImplementor.java:1590)

Fix

Register missing RexCorrelVariable ids while visiting filter conditions, without overriding existing correlation mappings.

Test

  • Added RelToSqlConverterTest.testPostgresqlRoundTripCorrelatedProjectWithSemiJoinRules
  • Ran: ./gradlew :core:test --tests org.apache.calcite.rel.rel2sql.RelToSqlConverterTest
  • Result: 563 passed, 0 failed

Disclaimer

The changes were done with the assistance of Codex (gpt-5.3-codex) in response to an actual bug when using Calcite in production. The changes were manually verified prior to sending this to review.

@bvolpato bvolpato force-pushed the bvolpato/calcite-7440-rel2sql-correl-scope branch 2 times, most recently from 556d0d8 to 7d4937e Compare March 9, 2026 20:57
@bvolpato bvolpato force-pushed the bvolpato/calcite-7440-rel2sql-correl-scope branch from 6309f8d to 5447f29 Compare March 10, 2026 04:40
@bvolpato bvolpato marked this pull request as ready for review March 10, 2026 04:49
@bvolpato bvolpato marked this pull request as draft March 10, 2026 12:39
@bvolpato bvolpato force-pushed the bvolpato/calcite-7440-rel2sql-correl-scope branch from 660aa39 to 4449f5a Compare March 10, 2026 12:44
@bvolpato bvolpato force-pushed the bvolpato/calcite-7440-rel2sql-correl-scope branch from 4449f5a to f11eed6 Compare March 10, 2026 13:01
@sonarqubecloud
Copy link

@bvolpato bvolpato marked this pull request as ready for review March 11, 2026 02:15
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.

1 participant