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 Nov 8, 2024
1 parent 31f7d73 commit ba6fd56
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,51 @@ protected void scanAlias() {
}
}
}

{
boolean hasSpecial = false;
int startIndex = pos + 1;
int endIndex = -1; // text.indexOf('\'', startIndex);
for (int i = startIndex; i < text.length(); ++i) {
final char ch = text.charAt(i);
if (ch == '\\') {
hasSpecial = true;
continue;
}
if (ch == '"') {
endIndex = i;
break;
}
}

if (endIndex == -1) {
throw new ParserException("unclosed str. " + info());
}

String stringVal;
if (token == Token.AS) {
stringVal = text.substring(pos, endIndex + 1);
} else {
if (startIndex == endIndex) {
stringVal = "";
} else {
stringVal = text.substring(startIndex, endIndex);
}
}

if (!hasSpecial) {
this.stringVal = stringVal;
int pos = endIndex + 1;
char ch = charAt(pos);
if (ch != '"') {
this.pos = pos;
this.ch = ch;
token = LITERAL_CHARS;
return;
}
}
}

mark = pos;
boolean hasSpecial = false;
Token preToken = this.token;
Expand All @@ -155,6 +200,33 @@ protected void scanAlias() {

ch = charAt(++pos);

if (ch == '\\') {
scanChar();
if (!hasSpecial) {
initBuff(bufPos);
arraycopy(mark + 1, buf, 0, bufPos);
hasSpecial = true;
}

switch (ch) {
case '\'':
putChar('\'');
break;
case '"':
putChar('"');
break;
case '\\':
putChar('\\');
break;
default:
putChar('\\');
putChar(ch);
break;
}

continue;
}

if (ch == '"') {
scanChar();
if (ch != '"') {
Expand All @@ -166,7 +238,7 @@ protected void scanAlias() {
arraycopy(mark + 1, buf, 0, bufPos);
hasSpecial = true;
}
putChar('\'');
putChar('"');
continue;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.alibaba.druid.sql.parser;

import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.ast.*;
import com.alibaba.druid.sql.ast.expr.*;
import com.alibaba.druid.sql.ast.statement.*;
Expand Down Expand Up @@ -1142,6 +1143,9 @@ protected void parseSelectList(SQLSelectQueryBlock queryBlock) {
break;
}
}
if (lexer.token == Token.RPAREN && dbType == DbType.bigquery) {
break;
}
}
}

Expand Down
15 changes: 15 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,18 @@
SELECT ARRAY(SELECT AS STRUCT
cast(coalesce(a,0) as NUMERIC) as seconds_amount,
cast(coalesce(b,0) as NUMERIC) as nanos_amount,
)
from t1
--------------------
SELECT ARRAY(
SELECT AS STRUCT CAST(coalesce(a, 0) AS NUMERIC) AS seconds_amount, CAST(coalesce(b, 0) AS NUMERIC) AS nanos_amount
)
FROM t1
------------------------------------------------------------------------------------------------------------------------
SELECT "[\"sameday\"]"
--------------------
SELECT '["sameday"]'
------------------------------------------------------------------------------------------------------------------------
SELECT DATEADD("2024-10-01", -1, 'mm');
--------------------
SELECT DATEADD('2024-10-01', -1, 'mm');
Expand Down

0 comments on commit ba6fd56

Please sign in to comment.