Skip to content

Commit 4814ccd

Browse files
feat: LATERAL VIEW supports multiple alias columns
- fixes #2088 Signed-off-by: Andreas Reichel <[email protected]>
1 parent be8ff93 commit 4814ccd

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

src/main/java/net/sf/jsqlparser/expression/Alias.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void setAliasColumns(List<AliasColumn> aliasColumns) {
6565

6666
@Override
6767
public String toString() {
68-
String alias = (useAs ? " AS " : " ") + name;
68+
String alias = (useAs ? " AS " : " ") + (name != null ? name : "");
6969

7070
if (aliasColumns != null && !aliasColumns.isEmpty()) {
7171
StringBuilder ac = new StringBuilder();
@@ -75,10 +75,10 @@ public String toString() {
7575
}
7676
ac.append(col.name);
7777
if (col.colDataType != null) {
78-
ac.append(" ").append(col.colDataType.toString());
78+
ac.append(" ").append(col.colDataType);
7979
}
8080
}
81-
alias += "(" + ac + ")";
81+
alias += name != null ? "(" + ac + ")" : ac;
8282
}
8383

8484
return alias;
@@ -99,6 +99,16 @@ public Alias withAliasColumns(List<AliasColumn> aliasColumns) {
9999
return this;
100100
}
101101

102+
103+
public Alias addAliasColumns(String... columnNames) {
104+
List<AliasColumn> collection =
105+
Optional.ofNullable(getAliasColumns()).orElseGet(ArrayList::new);
106+
for (String columnName : columnNames) {
107+
collection.add(new AliasColumn(columnName));
108+
}
109+
return this.withAliasColumns(collection);
110+
}
111+
102112
public Alias addAliasColumns(AliasColumn... aliasColumns) {
103113
List<AliasColumn> collection =
104114
Optional.ofNullable(getAliasColumns()).orElseGet(ArrayList::new);

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2314,9 +2314,15 @@ LateralView LateralView() #LateralView:
23142314
tableAlias = new Alias(tableName, false);
23152315
}
23162316
]
2317-
<K_AS> columnName = RelObjectNameWithoutStart()
2317+
<K_AS> columnName = RelObjectNameWithoutStart() { columnAlias = new Alias(columnName, true); }
2318+
2319+
// Spark SQL supports multiple Alias Columns: https://spark.apache.org/docs/latest/sql-ref-syntax-qry-select-lateral-view.html
2320+
// we simulate this by setting the alias name to null and then just adding the columns
2321+
[
2322+
LOOKAHEAD(2) "," { columnAlias.setName(null); columnAlias.addAliasColumns( columnName); }
2323+
columnName = RelObjectNameWithoutStart() { columnAlias.addAliasColumns( columnName); }
2324+
]
23182325
{
2319-
columnAlias = new Alias(columnName, true);
23202326
return new LateralView(
23212327
useOuter
23222328
, generatorFunction

src/test/java/net/sf/jsqlparser/expression/AliasTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,12 @@ void testUDTF() throws JSQLParserException {
2020
String sqlStr = "select udtf_1(words) as (a1, a2) from tab";
2121
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
2222
}
23+
24+
@Test
25+
void testLateralViewMultipleColumns() throws JSQLParserException {
26+
String sqlStr = "SELECT k, v \n" +
27+
"FROM table \n" +
28+
"LATERAL VIEW EXPLODE(a) exploded_data AS k, v;";
29+
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
30+
}
2331
}

0 commit comments

Comments
 (0)