Skip to content

Commit 3b79caa

Browse files
fix: avoid invalid CAST(NULL AS NULL) in SQLGlot compiler (#17487)
This PR resolves a compilation crash caused by the SQLGlot compiler attempting to generate an invalid CAST(NULL AS NULL) statement in BigQuery, which triggers a syntax/validation error (e.g., Unexpected keyword NULL). Fixes #<524701452> 🦕 --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 4f5593a commit 3b79caa

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

  • packages/bigframes

packages/bigframes/bigframes/core/compile/sqlglot/sql/base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ def literal(value: typing.Any, dtype: dtypes.Dtype | None = None) -> sge.Express
6969
return sge.Null()
7070

7171
if value is None:
72+
if str(sqlglot_type).upper() == "NULL":
73+
return sge.Null()
7274
return cast(sge.Null(), sqlglot_type)
7375
if dtypes.is_struct_like(dtype):
7476
items = [

packages/bigframes/tests/unit/core/compile/sqlglot/sql/test_base.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,15 @@ def test_literal_explicit_dtype(value, dtype, expected):
159159
def test_literal_for_list(value: list, expected: str):
160160
got = sql.to_sql(sql.literal(value))
161161
assert got == expected
162+
163+
164+
def test_literal_null_type():
165+
import unittest.mock as mock
166+
167+
mock_dtype = mock.Mock()
168+
with mock.patch(
169+
"bigframes.core.compile.sqlglot.sql.base.sgt.from_bigframes_dtype",
170+
return_value="NULL",
171+
):
172+
got = sql.to_sql(sql.literal(None, dtype=mock_dtype))
173+
assert got == "NULL"

0 commit comments

Comments
 (0)