13
13
# limitations under the License.
14
14
15
15
import pandas as pd
16
- import pyarrow as pa
16
+ import pytest
17
17
18
18
import bigframes .bigquery as bbq
19
19
import bigframes .dtypes as dtypes
20
20
import bigframes .pandas as bpd
21
21
22
22
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 ):
24
24
series = bbq .sql_scalar (
25
25
"""
26
26
CAST({0} AS INT64)
@@ -55,15 +55,39 @@ def test_sql_scalar_on_scalars_null_index(scalars_df_null_index):
55
55
assert len (result ) == len (scalars_df_null_index )
56
56
57
57
58
- def test_sql_scalar_w_bool_series (scalars_df_index ):
58
+ def test_sql_scalar_for_bool_series (scalars_df_index ):
59
59
series : bpd .Series = scalars_df_index ["bool_col" ]
60
60
result = bbq .sql_scalar ("CAST({0} AS INT64)" , [series ])
61
61
expected = series .astype (dtypes .INT_DTYPE )
62
62
expected .name = None
63
63
pd .testing .assert_series_equal (result .to_pandas (), expected .to_pandas ())
64
64
65
65
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 ):
67
91
result = bbq .sql_scalar (
68
92
"""
69
93
ARRAY_LENGTH({0}) + ARRAY_LENGTH({1}) + ARRAY_LENGTH({2})
@@ -93,7 +117,14 @@ def test_sql_scalar_w_array_series(repeated_df):
93
117
pd .testing .assert_series_equal (result .to_pandas (), expected .to_pandas ())
94
118
95
119
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 ):
97
128
result = bbq .sql_scalar (
98
129
"CHAR_LENGTH({0}.name) + {0}.age" ,
99
130
[nested_structs_df ["person" ]],
@@ -104,7 +135,14 @@ def test_sql_scalar_w_struct_series(nested_structs_df):
104
135
pd .testing .assert_series_equal (result .to_pandas (), expected .to_pandas ())
105
136
106
137
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 ):
108
146
result = bbq .sql_scalar (
109
147
"""JSON_VALUE({0}, '$.int_value')""" ,
110
148
[
@@ -116,13 +154,8 @@ def test_sql_scalar_w_json_series(json_df):
116
154
pd .testing .assert_series_equal (result .to_pandas (), expected .to_pandas ())
117
155
118
156
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