Skip to content

Commit 2863e50

Browse files
committed
fix: allow arbitrary column aliases in LATERAL VIEW
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 (#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 #2433 Signed-off-by: 付典 <fudianchn@gmail.com>
1 parent f0ba123 commit 2863e50

2 files changed

Lines changed: 36 additions & 3 deletions

File tree

src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5053,6 +5053,9 @@ LateralView LateralView() #LateralView:
50535053
[
50545054
LOOKAHEAD(2) "," { columnAlias.setName(null); columnAlias.addAliasColumns( columnName); }
50555055
columnName = RelObjectName() { columnAlias.addAliasColumns( columnName); }
5056+
(
5057+
LOOKAHEAD(2) "," columnName = RelObjectName() { columnAlias.addAliasColumns( columnName); }
5058+
)*
50565059
]
50575060
{
50585061
return new LateralView(

src/test/java/net/sf/jsqlparser/statement/select/HiveTest.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@
99
*/
1010
package net.sf.jsqlparser.statement.select;
1111

12-
import net.sf.jsqlparser.schema.Table;
13-
import org.junit.jupiter.api.Test;
14-
1512
import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed;
1613
import static org.junit.jupiter.api.Assertions.assertEquals;
14+
import static org.junit.jupiter.api.Assertions.assertNotNull;
15+
import static org.junit.jupiter.api.Assertions.assertNull;
1716
import static org.junit.jupiter.api.Assertions.assertTrue;
1817

18+
import net.sf.jsqlparser.expression.Alias;
19+
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
20+
import net.sf.jsqlparser.schema.Table;
21+
import org.junit.jupiter.api.Test;
22+
1923
public class HiveTest {
2024

2125
@Test
@@ -54,4 +58,30 @@ public void testGroupSimplified() throws Exception {
5458
+ "GROUP BY GROUPING SETS (())";
5559
assertSqlCanBeParsedAndDeparsed(sql, true);
5660
}
61+
62+
@Test
63+
public void testLateralViewManyColumnAliasesIssue2433() throws Exception {
64+
// Hive/Spark LATERAL VIEW allows an arbitrary number of column aliases
65+
// (e.g. json_tuple yielding many columns). Only the first two were absorbed;
66+
// any further aliases leaked into the FROM clause as implicit cross-join
67+
// tables, so the failure was silent and even round-tripped identically.
68+
String sql = "SELECT a FROM t"
69+
+ " LATERAL VIEW json_tuple(j, 'a', 'b', 'c', 'd', 'e', 'f', 'g')"
70+
+ " x AS c1, c2, c3, c4, c5, c6, c7";
71+
72+
Select select = (Select) CCJSqlParserUtil.parse(sql);
73+
PlainSelect plainSelect = (PlainSelect) select.getSelectBody();
74+
75+
// The extra aliases must not leak as cross-join tables.
76+
assertNull(plainSelect.getJoins());
77+
78+
java.util.List<LateralView> lateralViews = plainSelect.getLateralViews();
79+
assertNotNull(lateralViews);
80+
assertEquals(1, lateralViews.size());
81+
82+
Alias columnAlias = lateralViews.get(0).getColumnAlias();
83+
assertNotNull(columnAlias);
84+
assertNotNull(columnAlias.getAliasColumns());
85+
assertEquals(7, columnAlias.getAliasColumns().size());
86+
}
5787
}

0 commit comments

Comments
 (0)