diff --git a/bigframes/core/pyformat.py b/bigframes/core/pyformat.py index eab86dc629..8f49556ff4 100644 --- a/bigframes/core/pyformat.py +++ b/bigframes/core/pyformat.py @@ -104,6 +104,9 @@ def _field_to_template_value( if isinstance(value, bigframes.dataframe.DataFrame): return _table_to_sql(value._to_placeholder_table(dry_run=dry_run)) + if isinstance(value, str): + return value + return bigframes.core.sql.simple_literal(value) diff --git a/tests/system/small/session/test_read_gbq_colab.py b/tests/system/small/session/test_read_gbq_colab.py index 6d3cf6fe88..65f47fe4e3 100644 --- a/tests/system/small/session/test_read_gbq_colab.py +++ b/tests/system/small/session/test_read_gbq_colab.py @@ -143,7 +143,7 @@ def test_read_gbq_colab_repr_avoids_requery(maybe_ordered_session): def test_read_gbq_colab_includes_formatted_scalars(session): pyformat_args = { "some_integer": 123, - "some_string": "This could be dangerous, but we escape it", + "some_string": "This could be dangerous.", # This is not a supported type, but ignored if not referenced. "some_object": object(), } @@ -153,7 +153,7 @@ def test_read_gbq_colab_includes_formatted_scalars(session): df = session._read_gbq_colab( """ SELECT {some_integer} as some_integer, - {some_string} as some_string, + '{some_string}' as some_string, '{{escaped}}' as escaped """, pyformat_args=pyformat_args, @@ -165,7 +165,7 @@ def test_read_gbq_colab_includes_formatted_scalars(session): { "some_integer": pandas.Series([123], dtype=pandas.Int64Dtype()), "some_string": pandas.Series( - ["This could be dangerous, but we escape it"], + ["This could be dangerous."], dtype="string[pyarrow]", ), "escaped": pandas.Series(["{escaped}"], dtype="string[pyarrow]"), diff --git a/tests/unit/core/test_pyformat.py b/tests/unit/core/test_pyformat.py index 447ce37766..db7cedba8f 100644 --- a/tests/unit/core/test_pyformat.py +++ b/tests/unit/core/test_pyformat.py @@ -444,7 +444,7 @@ def test_pyformat_with_pandas_dataframe_not_dry_run_no_session_raises_valueerror def test_pyformat_with_query_string_replaces_variables(session): pyformat_args = { - "my_string": "some string value", + "my_string": "`my_table`", "max_value": 2.25, "year": 2025, "null_value": None, @@ -456,9 +456,8 @@ def test_pyformat_with_query_string_replaces_variables(session): SELECT {year} - year AS age, @myparam AS myparam, '{{my_string}}' AS escaped_string, - {my_string} AS my_string, - {null_value} AS null_value, - FROM my_dataset.my_table + * + FROM {my_string} WHERE height < {max_value} """.strip() @@ -466,9 +465,8 @@ def test_pyformat_with_query_string_replaces_variables(session): SELECT 2025 - year AS age, @myparam AS myparam, '{my_string}' AS escaped_string, - 'some string value' AS my_string, - NULL AS null_value, - FROM my_dataset.my_table + * + FROM `my_table` WHERE height < 2.25 """.strip() diff --git a/tests/unit/pandas/io/test_api.py b/tests/unit/pandas/io/test_api.py index 14419236c9..dbdf427d91 100644 --- a/tests/unit/pandas/io/test_api.py +++ b/tests/unit/pandas/io/test_api.py @@ -108,7 +108,7 @@ def test_read_gbq_colab_calls_set_location( mock_with_default_session.return_value = mock_df query_or_table = "SELECT {param1} AS param1" - sample_pyformat_args = {"param1": "value1"} + sample_pyformat_args = {"param1": "'value1'"} result = bf_io_api._read_gbq_colab( query_or_table, pyformat_args=sample_pyformat_args, dry_run=False ) diff --git a/tests/unit/session/test_read_gbq_colab.py b/tests/unit/session/test_read_gbq_colab.py index 52b091c045..b1dc1ec702 100644 --- a/tests/unit/session/test_read_gbq_colab.py +++ b/tests/unit/session/test_read_gbq_colab.py @@ -60,7 +60,7 @@ def test_read_gbq_colab_includes_formatted_values_in_dry_run(monkeypatch, dry_ru pyformat_args = { "some_integer": 123, - "some_string": "This could be dangerous, but we escape it", + "some_string": "some_column", "bf_df": bf_df, "pd_df": pd_df, # This is not a supported type, but ignored if not referenced. @@ -84,7 +84,7 @@ def test_read_gbq_colab_includes_formatted_values_in_dry_run(monkeypatch, dry_ru expected = textwrap.dedent( f""" SELECT 123 as some_integer, - 'This could be dangerous, but we escape it' as some_string, + some_column as some_string, '{{escaped}}' as escaped FROM `proj`.`dset`.`temp_{"table" if dry_run else "view"}` AS bf_df FULL OUTER JOIN `proj`.`dset`.`temp_{"table" if dry_run else "view"}` AS pd_df