Skip to content

Commit

Permalink
improved sql parser bigquery support
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Dec 12, 2024
1 parent 5aaf79c commit d7556f6
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ public void addArgument(SQLExpr arg) {
this.arguments.add(arg);
}

public void addArguments(List<SQLExpr> args) {
for (SQLExpr arg : args) {
addArgument(arg);
}
}

public SQLExpr getOwner() {
return this.owner;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public class BigQueryExprParser extends SQLExprParser {
"FIRST_VALUE",
"GROUPING",
"LAST_VALUE",
"LAG",
"LEAD",
"LOGICAL_AND",
"LOGICAL_OR",
"MAX",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1984,7 +1984,7 @@ protected SQLExpr methodRest(SQLExpr expr, boolean acceptLPAREN) {
if (lexer.token == Token.OVER) {
if (aggregateExpr == null) {
aggregateExpr = new SQLAggregateExpr(methodName);
aggregateExpr.getArguments().addAll(methodInvokeExpr.getArguments());
aggregateExpr.addArguments(methodInvokeExpr.getArguments());
}
over(aggregateExpr);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1820,6 +1820,7 @@ public SQLTableSource parseTableSourceRest(SQLTableSource tableSource) {
SQLTableSource unnestTableSource = parseUnnestTableSource();
if (unnestTableSource != null) {
if (lexer.identifierEquals(FnvHash.Constants.CROSS)
|| lexer.token == Token.CROSS
|| lexer.token == Token.LEFT
|| lexer.token == Token.RIGHT
|| lexer.token == Token.COMMA
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.alibaba.druid.bvt.sql.bigquery;

import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.expr.SQLAggregateExpr;
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import org.junit.Test;

import static org.junit.Assert.assertSame;

public class AggregateTest {
@Test
public void test_agg() {
String sql = "SELECT lag(date(d,'Asia/Jakarta')) over(partition by id order by n) FROM t1";
SQLSelectStatement stmt = (SQLSelectStatement) SQLUtils.parseSingleStatement(sql, DbType.bigquery);
SQLSelectQueryBlock queryBlock = stmt.getSelect().getQueryBlock();
SQLAggregateExpr expr = (SQLAggregateExpr) queryBlock.getSelectList().get(0).getExpr();
assertSame(expr, expr.getArgument(0).getParent());
}

@Test
public void test_agg_1() {
String sql = "SELECT xx(date(d,'Asia/Jakarta')) over(partition by id order by n) FROM t1";
SQLSelectStatement stmt = (SQLSelectStatement) SQLUtils.parseSingleStatement(sql, DbType.bigquery);
SQLSelectQueryBlock queryBlock = stmt.getSelect().getQueryBlock();
SQLAggregateExpr expr = (SQLAggregateExpr) queryBlock.getSelectList().get(0).getExpr();
assertSame(expr, expr.getArgument(0).getParent());
}
}

0 comments on commit d7556f6

Please sign in to comment.