Skip to content

fix: allow arbitrary column aliases in LATERAL VIEW - #2452

Merged
manticore-projects merged 2 commits into
JSQLParser:masterfrom
fudianchn:fix/hive-lateral-view-n-column-aliases-2433
Aug 7, 2026
Merged

fix: allow arbitrary column aliases in LATERAL VIEW#2452
manticore-projects merged 2 commits into
JSQLParser:masterfrom
fudianchn:fix/hive-lateral-view-n-column-aliases-2433

Conversation

@fudianchn

Copy link
Copy Markdown
Contributor

What

Allow a Hive/Spark LATERAL VIEW to declare an arbitrary number of column aliases, not just two.

Why

LATERAL VIEW ... x AS c1, c2, c3, ..., cN is valid Hive/Spark SQL — json_tuple and similar UDTFs commonly yield many output columns. The grammar only absorbed the first two aliases (#2088); from the third onward the aliases were silently parsed as comma-separated tables in the FROM clause (implicit cross joins):

  • PlainSelect.joins incorrectly contained Join(Table("c3")) ... Join(Table("cN"))
  • LateralView.columnAlias.aliasColumns stopped at 2

Because toString() re-emits the leaked aliases as the join list, the broken AST round-trips to textually identical SQL, so the bug is invisible to round-trip checks.

Reported in #2433.

How

The column-alias production (LateralView) handled the second alias with an optional [ "," name ] block. This adds a ("," name)* loop immediately after it, so the 3rd…Nth aliases are consumed by the lateral view instead of leaking to the FROM clause. The existing single- and two-alias behaviour is unchanged (the loop simply runs zero times).

Testing

  • New HiveTest#testLateralViewManyColumnAliasesIssue2433 parses LATERAL VIEW json_tuple(...) x AS c1, c2, c3, c4, c5, c6, c7 and asserts at the AST level (a round-trip assertion would be vacuous here): PlainSelect.joins is null and LateralView.columnAlias.aliasColumns has size 7.
  • Verified the test fails on master (expected: <null> but was: <[c3, c4, c5, c6, c7]>) and passes with this change.
  • mvn clean test: 4648 tests, 0 failures, 0 errors (26 skipped) — the existing two-alias case ([BUG] JSQLParser Version 5.1-SNAPSHOT: Lateral View with two aliases recognizes second alias as table #2088) and all other tests unchanged.
  • mvn spotless:check: clean.

Fixes #2433

Hive/Spark LATERAL VIEW supports an arbitrary number of column aliases
(e.g. json_tuple yielding many output columns), but the grammar only
absorbed the first two aliases (JSQLParser#2088). Any further aliases were
silently parsed as comma-separated tables in the FROM clause (implicit
cross joins), and because toString() re-emitted them as the join list
the broken AST round-tripped identically and was hard to detect by
inspection.

Generalise the column-alias production to consume any number of
aliases after the first two.

Fixes JSQLParser#2433

Signed-off-by: 付典 <fudianchn@gmail.com>
Comment thread src/test/java/net/sf/jsqlparser/statement/select/HiveTest.java
Signed-off-by: 付典 <fudianchn@gmail.com>
@manticore-projects
manticore-projects merged commit f92f5e5 into JSQLParser:master Aug 7, 2026
7 checks passed
@manticore-projects

Copy link
Copy Markdown
Contributor

Thank you for your work and effort, good job!

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] JSQLParser Version : 5.3 : LATERAL VIEW with three or more column aliases silently mis-parses extras as cross-join tables

2 participants