Skip to content

Commit 9614f97

Browse files
feat: embed str values directly in pyformat
Updates `bigframes/core/pyformat.py` to directly embed string values in the formatted output. This allows for dynamic substitution of SQL identifiers, such as table or column names. Updated unit tests to reflect this new behavior by using valid SQL identifiers in the test cases.
1 parent 12e04d5 commit 9614f97

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

bigframes/core/pyformat.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ def _field_to_template_value(
104104
if isinstance(value, bigframes.dataframe.DataFrame):
105105
return _table_to_sql(value._to_placeholder_table(dry_run=dry_run))
106106

107+
if isinstance(value, str):
108+
return value
109+
107110
return bigframes.core.sql.simple_literal(value)
108111

109112

tests/unit/core/test_pyformat.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ def test_pyformat_with_pandas_dataframe_not_dry_run_no_session_raises_valueerror
444444

445445
def test_pyformat_with_query_string_replaces_variables(session):
446446
pyformat_args = {
447-
"my_string": "some string value",
447+
"my_string": "`my_table`",
448448
"max_value": 2.25,
449449
"year": 2025,
450450
"null_value": None,
@@ -456,19 +456,17 @@ def test_pyformat_with_query_string_replaces_variables(session):
456456
SELECT {year} - year AS age,
457457
@myparam AS myparam,
458458
'{{my_string}}' AS escaped_string,
459-
{my_string} AS my_string,
460-
{null_value} AS null_value,
461-
FROM my_dataset.my_table
459+
*
460+
FROM {my_string}
462461
WHERE height < {max_value}
463462
""".strip()
464463

465464
expected_sql = """
466465
SELECT 2025 - year AS age,
467466
@myparam AS myparam,
468467
'{my_string}' AS escaped_string,
469-
'some string value' AS my_string,
470-
NULL AS null_value,
471-
FROM my_dataset.my_table
468+
*
469+
FROM `my_table`
472470
WHERE height < 2.25
473471
""".strip()
474472

tests/unit/session/test_read_gbq_colab.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def test_read_gbq_colab_includes_formatted_values_in_dry_run(monkeypatch, dry_ru
6060

6161
pyformat_args = {
6262
"some_integer": 123,
63-
"some_string": "This could be dangerous, but we escape it",
63+
"some_string": "some_column",
6464
"bf_df": bf_df,
6565
"pd_df": pd_df,
6666
# 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
8484
expected = textwrap.dedent(
8585
f"""
8686
SELECT 123 as some_integer,
87-
'This could be dangerous, but we escape it' as some_string,
87+
some_column as some_string,
8888
'{{escaped}}' as escaped
8989
FROM `proj`.`dset`.`temp_{"table" if dry_run else "view"}` AS bf_df
9090
FULL OUTER JOIN `proj`.`dset`.`temp_{"table" if dry_run else "view"}` AS pd_df

0 commit comments

Comments
 (0)