close
Skip to content

Commit 15e58e9

Browse files
hayssamsclaude
andauthored
feat: parse BigQuery JSON string literals as implicit casts (#2488)
isImplicitCastAhead() only fired for DATA_TYPE and DT_ZONE tokens, but JSON lexes as K_JSON, so BigQuery literals like JSON '{"a": 1}' failed to parse even though DataType() accepts K_JSON as a data type. Accept K_JSON when directly followed by a string literal. Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
1 parent fa731f9 commit 15e58e9

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

‎src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,8 @@ public class CCJSqlParser extends AbstractJSqlParser<CCJSqlParser> {
477477
// DT_ZONE (TIMESTAMP WITH TIME ZONE / WITHOUT TIME ZONE) is also a valid
478478
// implicit cast type prefix handled by DataType(), but distinct from DATA_TYPE.
479479
if (k1 == DT_ZONE) return true;
480+
// BigQuery JSON 'literal' - JSON lexes as K_JSON, but DataType() accepts it
481+
if (k1 == K_JSON) return getToken(2).kind == S_CHAR_LITERAL;
480482
if (k1 != DATA_TYPE) return false;
481483
int k2 = getToken(2).kind;
482484
if (k2 != OPENING_BRACKET) return true; // DATA_TYPE literal - simple cast

‎src/test/java/net/sf/jsqlparser/expression/CastExpressionTest.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ void testImplicitCast() throws JSQLParserException {
5252
Assertions.assertTrue(select.getSelectItem(0).getExpression() instanceof CastExpression);
5353
}
5454

55+
@Test
56+
void testImplicitCastJsonLiteral() throws JSQLParserException {
57+
String sqlStr = "SELECT JSON '{\"name\": \"Jakob\"}'";
58+
PlainSelect select = (PlainSelect) TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
59+
60+
Assertions.assertTrue(select.getSelectItem(0).getExpression() instanceof CastExpression);
61+
62+
// BigQuery wraps JSON literals in functions, e. g. BOOL(JSON 'true')
63+
sqlStr = "SELECT BOOL(JSON 'true') AS vacancy";
64+
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
65+
}
66+
5567
@Test
5668
void testImplicitCastTimestampIssue1364() throws JSQLParserException {
5769
String sqlStr = "SELECT TIMESTAMP WITH TIME ZONE '2004-10-19 10:23:54+02'";

0 commit comments

Comments
 (0)