From 659e6c4fcf8009088ca603fa8297e2bd7c7785e6 Mon Sep 17 00:00:00 2001 From: Shenyang Cai Date: Fri, 14 Aug 2026 22:24:11 +0000 Subject: [PATCH] fix: quote literal values for strings in SQLGlot compiler --- .../core/compile/sqlglot/sql/base.py | 2 ++ .../core/compile/sqlglot/sql/test_base.py | 21 +++++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/packages/bigframes/bigframes/core/compile/sqlglot/sql/base.py b/packages/bigframes/bigframes/core/compile/sqlglot/sql/base.py index f77dcbee4d93..9f5136cb8226 100644 --- a/packages/bigframes/bigframes/core/compile/sqlglot/sql/base.py +++ b/packages/bigframes/bigframes/core/compile/sqlglot/sql/base.py @@ -113,6 +113,8 @@ def literal(value: typing.Any, dtype: dtypes.Dtype | None = None) -> sge.Express return sge.func("ST_GEOGFROMTEXT", sge.convert(wkt)) elif dtype == dtypes.TIMEDELTA_DTYPE: return sge.convert(utils.timedelta_to_micros(value)) + elif dtype == dtypes.STRING_DTYPE: + return sge.convert(str(value)) else: if isinstance(value, np.generic): value = value.item() diff --git a/packages/bigframes/tests/unit/core/compile/sqlglot/sql/test_base.py b/packages/bigframes/tests/unit/core/compile/sqlglot/sql/test_base.py index 617f3636d403..41a44c4f6c34 100644 --- a/packages/bigframes/tests/unit/core/compile/sqlglot/sql/test_base.py +++ b/packages/bigframes/tests/unit/core/compile/sqlglot/sql/test_base.py @@ -127,11 +127,28 @@ def test_literal_for_geo(): "PARSE_JSON('{\\'a\\': 10}')", id="json", ), + pytest.param( + 2019, + sql.dtypes.STRING_DTYPE, + "'2019'", + id="string_from_int", + ), + pytest.param( + pa.scalar(2019), + sql.dtypes.STRING_DTYPE, + "'2019'", + id="string_from_pyarrow_scalar", + ), + pytest.param( + True, + sql.dtypes.STRING_DTYPE, + "'True'", + id="string_from_bool", + ), ), ) def test_literal_explicit_dtype(value, dtype, expected): - got = sql.to_sql(sql.literal(value, dtype=dtype)) - assert got == expected + assert sql.to_sql(sql.literal(value, dtype=dtype)) == expected @pytest.mark.parametrize(