Skip to content

Commit 97b49e0

Browse files
committed
generates literal from None
1 parent 59a719a commit 97b49e0

File tree

2 files changed

+50
-17
lines changed

2 files changed

+50
-17
lines changed

bigframes/bigquery/_operations/sql.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def sql_scalar(
7474
# template, then this will fail with an error earlier in the process,
7575
# aiding users in debugging.
7676
literals_sql = [
77-
sqlglot_ir._literal(column.values[0], column.dtype).sql(dialect="bigquery")
77+
sqlglot_ir._literal(None, column.dtype).sql(dialect="bigquery")
7878
for column in columns
7979
]
8080
select_sql = sql_template.format(*literals_sql)

tests/system/small/bigquery/test_sql.py

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
# limitations under the License.
1414

1515
import pandas as pd
16-
import pyarrow as pa
16+
import pytest
1717

1818
import bigframes.bigquery as bbq
1919
import bigframes.dtypes as dtypes
2020
import bigframes.pandas as bpd
2121

2222

23-
def test_sql_scalar_on_scalars_null_index(scalars_df_null_index):
23+
def test_sql_scalar_for_all_scalar_types(scalars_df_null_index):
2424
series = bbq.sql_scalar(
2525
"""
2626
CAST({0} AS INT64)
@@ -55,15 +55,39 @@ def test_sql_scalar_on_scalars_null_index(scalars_df_null_index):
5555
assert len(result) == len(scalars_df_null_index)
5656

5757

58-
def test_sql_scalar_w_bool_series(scalars_df_index):
58+
def test_sql_scalar_for_bool_series(scalars_df_index):
5959
series: bpd.Series = scalars_df_index["bool_col"]
6060
result = bbq.sql_scalar("CAST({0} AS INT64)", [series])
6161
expected = series.astype(dtypes.INT_DTYPE)
6262
expected.name = None
6363
pd.testing.assert_series_equal(result.to_pandas(), expected.to_pandas())
6464

6565

66-
def test_sql_scalar_w_array_series(repeated_df):
66+
@pytest.mark.parametrize(
67+
("column_name"),
68+
[
69+
pytest.param("bool_col"),
70+
pytest.param("bytes_col"),
71+
pytest.param("date_col"),
72+
pytest.param("datetime_col"),
73+
pytest.param("geography_col"),
74+
pytest.param("int64_col"),
75+
pytest.param("numeric_col"),
76+
pytest.param("float64_col"),
77+
pytest.param("string_col"),
78+
pytest.param("time_col"),
79+
pytest.param("timestamp_col"),
80+
],
81+
)
82+
def test_sql_scalar_outputs_all_scalar_types(scalars_df_index, column_name):
83+
series: bpd.Series = scalars_df_index[column_name]
84+
result = bbq.sql_scalar("{0}", [series])
85+
expected = series
86+
expected.name = None
87+
pd.testing.assert_series_equal(result.to_pandas(), expected.to_pandas())
88+
89+
90+
def test_sql_scalar_for_array_series(repeated_df):
6791
result = bbq.sql_scalar(
6892
"""
6993
ARRAY_LENGTH({0}) + ARRAY_LENGTH({1}) + ARRAY_LENGTH({2})
@@ -93,7 +117,14 @@ def test_sql_scalar_w_array_series(repeated_df):
93117
pd.testing.assert_series_equal(result.to_pandas(), expected.to_pandas())
94118

95119

96-
def test_sql_scalar_w_struct_series(nested_structs_df):
120+
def test_sql_scalar_outputs_array_series(repeated_df):
121+
result = bbq.sql_scalar("{0}", [repeated_df["int_list_col"]])
122+
expected = repeated_df["int_list_col"]
123+
expected.name = None
124+
pd.testing.assert_series_equal(result.to_pandas(), expected.to_pandas())
125+
126+
127+
def test_sql_scalar_for_struct_series(nested_structs_df):
97128
result = bbq.sql_scalar(
98129
"CHAR_LENGTH({0}.name) + {0}.age",
99130
[nested_structs_df["person"]],
@@ -104,7 +135,14 @@ def test_sql_scalar_w_struct_series(nested_structs_df):
104135
pd.testing.assert_series_equal(result.to_pandas(), expected.to_pandas())
105136

106137

107-
def test_sql_scalar_w_json_series(json_df):
138+
def test_sql_scalar_outputs_struct_series(nested_structs_df):
139+
result = bbq.sql_scalar("{0}", [nested_structs_df["person"]])
140+
expected = nested_structs_df["person"]
141+
expected.name = None
142+
pd.testing.assert_series_equal(result.to_pandas(), expected.to_pandas())
143+
144+
145+
def test_sql_scalar_for_json_series(json_df):
108146
result = bbq.sql_scalar(
109147
"""JSON_VALUE({0}, '$.int_value')""",
110148
[
@@ -116,13 +154,8 @@ def test_sql_scalar_w_json_series(json_df):
116154
pd.testing.assert_series_equal(result.to_pandas(), expected.to_pandas())
117155

118156

119-
def test_sql_scalar_w_array_output(json_df):
120-
result = bbq.sql_scalar(
121-
"""JSON_VALUE_ARRAY({0}, '$.order.items')""",
122-
[
123-
json_df["json_col"],
124-
],
125-
)
126-
assert len(result) == len(json_df)
127-
assert result.dtype == pd.ArrowDtype(pa.list_(pa.string()))
128-
assert result[15] == ["book", "pen"]
157+
def test_sql_scalar_outputs_json_series(json_df):
158+
result = bbq.sql_scalar("{0}", [json_df["json_col"]])
159+
expected = json_df["json_col"]
160+
expected.name = None
161+
pd.testing.assert_series_equal(result.to_pandas(), expected.to_pandas())

0 commit comments

Comments
 (0)