Skip to content

Commit

Permalink
improved big query sql parser
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Oct 30, 2024
1 parent db150f9 commit 790ac68
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 0 deletions.
12 changes: 12 additions & 0 deletions core/src/main/java/com/alibaba/druid/sql/ast/expr/SQLCastExpr.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class SQLCastExpr extends SQLExprImpl implements SQLObjectWithDataType, S
protected boolean isTry;
protected SQLExpr expr;
protected SQLDataType dataType;
protected SQLExpr format;

public SQLCastExpr() {
}
Expand All @@ -46,6 +47,17 @@ public void setExpr(SQLExpr expr) {
this.expr = expr;
}

public SQLExpr getFormat() {
return format;
}

public void setFormat(SQLExpr format) {
if (format != null) {
format.setParent(this);
}
this.format = format;
}

public SQLDataType getDataType() {
return this.dataType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,14 @@ public SQLExpr exprRest(SQLExpr expr) {
}
return super.exprRest(expr);
}

@Override
protected SQLCastExpr parseCastFormat(SQLCastExpr cast) {
if (lexer.nextIfIdentifier("FORMAT")) {
cast.setFormat(
this.expr()
);
}
return cast;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,26 @@ public void scanComment() {

@Override
protected void scanString() {
if (pos + 2 < text.length()
&& text.charAt(pos + 1) == '\''
&& text.charAt(pos + 2) == '\''
) {
for (int i = pos + 3; i < text.length(); i++) {
char c = text.charAt(i);
if (c == '\''
&& i + 2 < text.length()
&& text.charAt(i + 1) == '\''
&& text.charAt(i + 2) == '\''
) {
stringVal = text.substring(pos + 3, i);
token = Token.LITERAL_TEXT_BLOCK;
pos = i + 2;
scanChar();
return;
}
}
}

{
boolean hasSpecial = false;
int startIndex = pos + 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,13 @@ public boolean visit(SQLCastExpr x) {
x.getExpr().accept(this);
print0(ucase ? " AS " : " as ");
x.getDataType().accept(this);

SQLExpr format = x.getFormat();
if (format != null) {
print0(ucase ? " FORMAT " : " format ");
format.accept(this);
}

print0(")");
tryPrintRparen(x);
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1270,6 +1270,10 @@ && dialectFeatureEnabled(SQLTimestampExpr)) {
sqlExpr = array;
break;
}
case LITERAL_TEXT_BLOCK:
sqlExpr = new SQLCharExpr(lexer.stringVal());
lexer.nextToken();
break;
default:
throw new ParserException("ERROR. " + lexer.info());
}
Expand Down Expand Up @@ -1307,11 +1311,16 @@ protected SQLExpr parseCast() {
}
arrayDataType.setUsedForCast(true);
}
cast = parseCastFormat(cast);
accept(Token.RPAREN);
return cast;
}
}

protected SQLCastExpr parseCastFormat(SQLCastExpr cast) {
return cast;
}

protected SQLExpr parseQueryExpr() {
return new SQLQueryExpr(
createSelectParser()
Expand Down
11 changes: 11 additions & 0 deletions core/src/test/resources/bvt/parser/bigquery/0.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
SELECT CAST(b'\x48\x65\x6c\x6c\x6f' AS STRING FORMAT 'ASCII') AS bytes_to_string;
--------------------
SELECT CAST(b'\x48\x65\x6c\x6c\x6f' AS STRING FORMAT 'ASCII') AS bytes_to_string;
------------------------------------------------------------------------------------------------------------------------
select """abc""", '''it's''', '''Title:"Boy"''', '''two
lines''', '''why\?'''
--------------------
SELECT 'abc', 'it''s', 'Title:"Boy"'
, 'two
lines', 'why\?'
------------------------------------------------------------------------------------------------------------------------
UPDATE
`p1` main
SET
Expand Down

0 comments on commit 790ac68

Please sign in to comment.