Skip to content

Commit

Permalink
feat: functions with extra keywords, like BigQuery Timeseries functions
Browse files Browse the repository at this point in the history
- fixes #2120

Signed-off-by: Andreas Reichel <andreas@manticore-projects.com>
Signed-off-by: manticore-projects <andreas@manticore-projects.com>
  • Loading branch information
manticore-projects committed Dec 7, 2024
1 parent 92bbd90 commit b0b0f4d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/main/java/net/sf/jsqlparser/expression/Function.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class Function extends ASTNodeAccessImpl implements Expression {
private Limit limit = null;
private KeepExpression keep = null;
private String onOverflowTruncate = null;
private String extraKeyword = null;

public Function() {}

Expand Down Expand Up @@ -255,6 +256,15 @@ public void setKeep(KeepExpression keep) {
this.keep = keep;
}

public String getExtraKeyword() {
return extraKeyword;
}

public Function setExtraKeyword(String extraKeyword) {
this.extraKeyword = extraKeyword;
return this;
}

@Override
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity"})
public String toString() {
Expand All @@ -272,6 +282,11 @@ public String toString() {
if (isAllColumns()) {
b.append("ALL ");
}

if (extraKeyword != null) {
b.append(extraKeyword).append(" ");
}

b.append(parameters);

if (havingClause != null) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -5903,6 +5903,7 @@ Function InternalFunction(boolean escaped):
String onOverflowTruncate = null;
Token overflowToken = null;
Limit limit;
Token extraKeywordToken;
}
{
[ LOOKAHEAD(2) prefixToken = <K_APPROXIMATE> ]
Expand All @@ -5922,7 +5923,8 @@ Function InternalFunction(boolean escaped):
|
LOOKAHEAD( AllTableColumns() ) expr=AllTableColumns() { expressionList = new ExpressionList(expr); }
|
LOOKAHEAD(3) expressionList=ExpressionList()
LOOKAHEAD(3) [ LOOKAHEAD(2) extraKeywordToken = <K_TABLE> { retval.setExtraKeyword(extraKeywordToken.image); } ]
expressionList=ExpressionList()
[ orderByList = OrderByElements() { retval.setOrderByElements(orderByList); } ]

// https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/LISTAGG.html
Expand Down
18 changes: 18 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 @@ -11,6 +11,7 @@

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.test.TestUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -92,4 +93,21 @@ void testAsValue() throws JSQLParserException {
String sqlStr = "SELECT AS VALUE STRUCT(1 AS a, 2 AS b) xyz";
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
}

@Test
void testTimeSeriesFunction() throws JSQLParserException {
String sqlStr = "with raw_data as (\n"
+ " select timestamp('2024-12-01') zetime\n"
+ " union all \n"
+ " select timestamp('2024-12-04')\n"
+ " )\n"
+ "select zetime from GAP_FILL(\n"
+ " TABLE raw_data,\n"
+ " ts_column => 'zetime',\n"
+ " bucket_width => INTERVAL 4 HOUR\n"
+ ")";
PlainSelect select = (PlainSelect) TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
TableFunction function = select.getFromItem(TableFunction.class);
Assertions.assertEquals("TABLE", function.getFunction().getExtraKeyword());
}
}

0 comments on commit b0b0f4d

Please sign in to comment.