Skip to content

Commit f92f5e5

Browse files
authored
fix: allow arbitrary column aliases in LATERAL VIEW (#2452)
* 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> * test(hive): 按评审要求补充解析反解析断言 Signed-off-by: 付典 <fudianchn@gmail.com> --------- Signed-off-by: 付典 <fudianchn@gmail.com>
1 parent aa542c0 commit f92f5e5

2 files changed

Lines changed: 35 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: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@
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.schema.Table;
20+
import org.junit.jupiter.api.Test;
21+
1922
public class HiveTest {
2023

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

0 commit comments

Comments
 (0)