Skip to content

Fix issue #2353: Incorrect canonicalization of NATURAL JOIN with INNE…#2355

Open
revitalkr wants to merge 1 commit into
apache:mainfrom
SatoriCyber:fix/natural-join-snowflake
Open

Fix issue #2353: Incorrect canonicalization of NATURAL JOIN with INNE…#2355
revitalkr wants to merge 1 commit into
apache:mainfrom
SatoriCyber:fix/natural-join-snowflake

Conversation

@revitalkr
Copy link
Copy Markdown

…R JOIN in Snowflake dialect produces non-equivalent query
Closes #2353

The current canonicalization rewrites queries such as:

SELECT *
FROM t1
NATURAL JOIN t5
INNER JOIN t0 ON (t0.v1 + t5.v0) > 0

into:

t1 NATURAL JOIN (t5 INNER JOIN t0 ...)

This transformation is not semantics-preserving.

NATURAL JOIN implicitly generates join predicates based on all shared column names.
Moving t0 into the right-hand side of the NATURAL JOIN changes the set of visible columns, which may introduce additional implicit join conditions and lead to different results.

Minimal example (see issue for full repro):

SELECT *
FROM t1
NATURAL JOIN t5
INNER JOIN t0 ON (t0.v1 + t5.v0) > 0

Expected behavior:
The canonicalization should preserve semantics, e.g.:

(t1 NATURAL JOIN t5) INNER JOIN t0 ...

Fix:
Avoid right-associative parsing when NATURAL JOIN is present, ensuring that NATURAL JOIN is evaluated left-to-right.

Tests:

  • Fixed the join_precedence test, which previously assumed incorrect Snowflake behavior.
  • Verified the actual behavior against Snowflake.
  • Updated the test to reflect the behavior observed in Snowflake.

…h INNER JOIN in Snowflake dialect produces non-equivalent query
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.

Incorrect canonicalization of NATURAL JOIN with INNER JOIN in Snowflake dialect produces non-equivalent query

1 participant