Skip to content

fix: INNER of a following INNER JOIN wrongly consumed as table source alias (#6711) - #6713

Open
CuriousLinYu wants to merge 2 commits into
alibaba:masterfrom
CuriousLinYu:fix-issue-6711
Open

fix: INNER of a following INNER JOIN wrongly consumed as table source alias (#6711)#6713
CuriousLinYu wants to merge 2 commits into
alibaba:masterfrom
CuriousLinYu:fix-issue-6711

Conversation

@CuriousLinYu

Copy link
Copy Markdown

What does this PR do?

Fixes #6711.

When a FROM clause contains a join followed by another INNER JOIN:

SELECT c.id
FROM context_table c
INNER JOIN loan_table l ON c.id = l.id
INNER JOIN adjust_table a ON l.id = a.id

the INNER token of the second join is silently consumed as the alias of the previous table source / join node, producing a corrupted AST:

  • the first join node gets alias = "INNER"
  • the following join degrades to a plain JOIN
  • serialization emits invalid SQL with a bogus AS INNER:
SELECT c.id
FROM context_table c
INNER JOIN loan_table l ON c.id = l.id AS INNER
JOIN adjust_table a ON l.id = a.id

Since the corrupted output is still accepted by the same parser, re-validation with the same parser cannot detect the semantic damage.

Root cause

SQLSelectParser.parseTableSourceRest() guards LEFT / RIGHT / FULL against being consumed as a table-source alias when they actually start the next join (lookahead for JOIN / OUTER / ANTI / ARRAY / SEMI), but INNER was missing from that lookahead group, so it fell through to the alias path.

Fix

Add case INNER with the same lookahead as LEFT / RIGHT / FULL, so INNER is only treated as an alias when it is not followed by JOIN / OUTER / ANTI / ARRAY / SEMI. Fully symmetric with the existing handling; real INNER aliases (when not followed by a join keyword) keep working.

SQLSelectParser is the shared base parser, so this affects all dialects that reuse it (mysql, odps, postgresql, ...) — not only odps as reported in the issue.

Tests

New test class Issue6711 covering:

  • INNER JOIN followed by INNER JOIN (odps / mysql / postgresql) — asserts both join nodes are INNER_JOIN with null aliases
  • LEFT / RIGHT / FULL JOIN followed by INNER JOIN
  • three consecutive INNER JOINs
  • parse → print → parse → print round-trip stability, and output must not contain AS INNER
  • single INNER JOIN regression guard

Verified locally: mvn -pl core test passes with the full core module suite (1759 test classes, 0 failures, 0 errors).

Before the fix, 5 of the 6 new tests fail on master (commit fa8dc99); after the fix all 6 pass.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


lintyu seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

1 similar comment
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


lintyu seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

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.

[BUG] ODPS 解析连续 INNER JOIN 时将 INNER 错误识别为别名

2 participants