Skip to content

fix: Support parentheses in BigQuery CAST FORMAT syntax#7395

Merged
geooo109 merged 2 commits intotobymao:mainfrom
ShubhamKapoor992:bq-crash-format
Mar 26, 2026
Merged

fix: Support parentheses in BigQuery CAST FORMAT syntax#7395
geooo109 merged 2 commits intotobymao:mainfrom
ShubhamKapoor992:bq-crash-format

Conversation

@ShubhamKapoor992
Copy link
Copy Markdown
Contributor

@ShubhamKapoor992 ShubhamKapoor992 commented Mar 25, 2026

Fixes #7390

Summary
Fixes parsing of BigQuery's CAST with FORMAT clause when the format string is wrapped in parentheses. BigQuery allows both FORMAT 'YYYY' and FORMAT ('YYYY') syntax, but SQLGlot only supported the former.

Problem
The parser failed with ParseError when encountering parentheses around the format string:

This worked:
CAST(date AS STRING FORMAT 'YYYY')

This failed (but is valid BigQuery syntax):
CAST(date AS STRING FORMAT ('YYYY'))

Error message:

ParseError: Required keyword: 'expressions' missing for <class 'sqlglot.expressions.core.Aliases'>

Solution

Modified _parse_cast() in sqlglot/parser.py to handle optional parentheses around the format string. The fix:

  • Checks for opening parenthesis after FORMAT keyword
  • Parses the format string
  • Matches closing parenthesis if opening was found
  • Maintains backward compatibility with non-parenthesized format strings

Changes

  • sqlglot/parser.py: Added logic to handle optional parentheses in CAST FORMAT (lines 7585-7591)
  • tests/dialects/test_bigquery.py: Added test_cast_format_with_parentheses() with 5 test cases covering various scenarios

Testing

  • ✅ All 1066 existing tests pass
  • ✅ New test covers: simple cases, subqueries, AT TIME ZONE, and TIMESTAMP formats
  • ✅ Backward compatible - existing syntax without parentheses continues to work

Impact

  • Dialects affected: BigQuery, Teradata (both support CAST FORMAT syntax)
  • Breaking changes: None
  • Backward compatibility: Full - parentheses are optional

@geooo109 geooo109 merged commit 2b19bdc into tobymao:main Mar 26, 2026
8 checks passed
@ShubhamKapoor992 ShubhamKapoor992 deleted the bq-crash-format branch March 26, 2026 11:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sqlglot fails to parse BigQuery CAST FORMAT when format string is wrapped in parentheses

2 participants