Skip to content

Commit

Permalink
feat: BigQuery SELECT AS STRUCT ... and SELECT AS VALUE ...
Browse files Browse the repository at this point in the history
Signed-off-by: Andreas Reichel <[email protected]>
  • Loading branch information
manticore-projects committed Jun 12, 2024
1 parent c9ecfc6 commit 5c360a2
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/main/java/net/sf/jsqlparser/statement/select/PlainSelect.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@
@SuppressWarnings({"PMD.CyclomaticComplexity"})
public class PlainSelect extends Select {

public enum BigQuerySelectQualifier {
AS_STRUCT, AS_VALUE
}

private Distinct distinct = null;
private BigQuerySelectQualifier bigQuerySelectQualifier = null;
private List<SelectItem<?>> selectItems;
private List<Table> intoTables;
private FromItem fromItem;
Expand Down Expand Up @@ -369,6 +374,15 @@ public void setDistinct(Distinct distinct) {
this.distinct = distinct;
}

public BigQuerySelectQualifier getBigQuerySelectQualifier() {
return bigQuerySelectQualifier;
}

public PlainSelect setBigQuerySelectQualifier(BigQuerySelectQualifier bigQuerySelectQualifier) {
this.bigQuerySelectQualifier = bigQuerySelectQualifier;
return this;
}

public Expression getHaving() {
return having;
}
Expand Down Expand Up @@ -478,6 +492,18 @@ public StringBuilder appendSelectBodyTo(StringBuilder builder) {
if (distinct != null) {
builder.append(distinct).append(" ");
}

if (bigQuerySelectQualifier != null) {
switch (bigQuerySelectQualifier) {
case AS_STRUCT:
builder.append("AS STRUCT ");
break;
case AS_VALUE:
builder.append("AS VALUE ");
break;
}
}

if (top != null) {
builder.append(top).append(" ");
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/net/sf/jsqlparser/util/deparser/SelectDeParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,17 @@ public void visit(PlainSelect plainSelect) {

deparseDistinctClause(plainSelect, plainSelect.getDistinct());

if (plainSelect.getBigQuerySelectQualifier() != null) {
switch (plainSelect.getBigQuerySelectQualifier()) {
case AS_STRUCT:
buffer.append("AS STRUCT ");
break;
case AS_VALUE:
buffer.append("AS VALUE ");
break;
}
}

Top top = plainSelect.getTop();
if (top != null) {
visit(top);
Expand Down
9 changes: 9 additions & 0 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -2395,6 +2395,15 @@ PlainSelect PlainSelect() #PlainSelect:
)
]

[
<K_AS>
(
<K_STRUCT> { plainSelect.setBigQuerySelectQualifier( PlainSelect.BigQuerySelectQualifier.AS_STRUCT ); }
|
<K_VALUE> { plainSelect.setBigQuerySelectQualifier( PlainSelect.BigQuerySelectQualifier.AS_VALUE ); }
)
]

[ LOOKAHEAD(2) top = Top() { plainSelect.setTop(top); } ]

selectItems=SelectItemsList()
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/net/sf/jsqlparser/statement/select/BigQueryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,16 @@ void testAggregateFunctionHaving() throws JSQLParserException {
"SELECT ANY_VALUE(fruit HAVING MAX sold) AS a_highest_selling_fruit FROM Store;\n";
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
}

@Test
void testAsStruct() throws JSQLParserException {
String sqlStr = "SELECT ARRAY(SELECT AS STRUCT 1 a, 2 b)";
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
}

@Test
void testAsValue() throws JSQLParserException {
String sqlStr = "SELECT AS VALUE STRUCT(1 AS a, 2 AS b) xyz";
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
}
}

0 comments on commit 5c360a2

Please sign in to comment.