This repository was archived by the owner on Apr 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathconftest.py
More file actions
274 lines (229 loc) · 9.24 KB
/
conftest.py
File metadata and controls
274 lines (229 loc) · 9.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import pathlib
import typing
from google.cloud import bigquery
import pandas as pd
import pyarrow as pa
import pytest
from bigframes import dtypes
import bigframes.core as core
import bigframes.pandas as bpd
import bigframes.testing.mocks as mocks
import bigframes.testing.utils
CURRENT_DIR = pathlib.Path(__file__).parent
DATA_DIR = CURRENT_DIR.parent.parent.parent.parent / "data"
def _create_compiler_session(table_name, table_schema):
"""Helper function to create a compiler session."""
from bigframes.testing import compiler_session
anonymous_dataset = bigquery.DatasetReference.from_string(
"bigframes-dev.sqlglot_test"
)
session = mocks.create_bigquery_session(
table_name=table_name,
table_schema=table_schema,
anonymous_dataset=anonymous_dataset,
)
session._executor = compiler_session.SQLCompilerExecutor()
return session
@pytest.fixture(scope="session")
def compiler_session(scalar_types_table_schema):
"""Compiler session for scalar types."""
return _create_compiler_session("scalar_types", scalar_types_table_schema)
@pytest.fixture(scope="session")
def compiler_session_w_repeated_types(repeated_types_table_schema):
"""Compiler session for repeated data types."""
return _create_compiler_session("repeated_types", repeated_types_table_schema)
@pytest.fixture(scope="session")
def compiler_session_w_nested_structs_types(nested_structs_types_table_schema):
"""Compiler session for nested STRUCT data types."""
return _create_compiler_session(
"nested_structs_types", nested_structs_types_table_schema
)
@pytest.fixture(scope="session")
def compiler_session_w_json_types(json_types_table_schema):
"""Compiler session for JSON data types."""
return _create_compiler_session("json_types", json_types_table_schema)
@pytest.fixture(scope="session")
def scalar_types_table_schema() -> typing.Sequence[bigquery.SchemaField]:
return [
bigquery.SchemaField("bool_col", "BOOLEAN"),
bigquery.SchemaField("bytes_col", "BYTES"),
bigquery.SchemaField("date_col", "DATE"),
bigquery.SchemaField("datetime_col", "DATETIME"),
bigquery.SchemaField("geography_col", "GEOGRAPHY"),
bigquery.SchemaField("int64_col", "INTEGER"),
bigquery.SchemaField("int64_too", "INTEGER"),
bigquery.SchemaField("numeric_col", "NUMERIC"),
bigquery.SchemaField("float64_col", "FLOAT"),
bigquery.SchemaField("rowindex", "INTEGER"),
bigquery.SchemaField("rowindex_2", "INTEGER", mode="REQUIRED"),
bigquery.SchemaField("string_col", "STRING"),
bigquery.SchemaField("time_col", "TIME"),
bigquery.SchemaField("timestamp_col", "TIMESTAMP"),
bigquery.SchemaField("duration_col", "INTEGER"),
]
@pytest.fixture(scope="session")
def scalar_types_df(compiler_session) -> bpd.DataFrame:
"""Returns a BigFrames DataFrame containing all scalar types and using the `rowindex`
column as the index."""
bf_df = compiler_session.read_gbq_table("bigframes-dev.sqlglot_test.scalar_types")
bf_df = bf_df.set_index("rowindex", drop=False)
return bf_df
@pytest.fixture(scope="session")
def scalar_types_pandas_df() -> pd.DataFrame:
"""Returns a pandas DataFrame containing all scalar types and using the `rowindex`
column as the index."""
# TODO: add tests for empty dataframes
df = pd.read_json(
DATA_DIR / "scalars.jsonl",
lines=True,
)
bigframes.testing.utils.convert_pandas_dtypes(df, bytes_col=True)
df = df.set_index("rowindex", drop=False)
return df
@pytest.fixture(scope="module")
def scalar_types_array_value(
scalar_types_pandas_df: pd.DataFrame, compiler_session: bigframes.Session
) -> core.ArrayValue:
managed_data_source = core.local_data.ManagedArrowTable.from_pandas(
scalar_types_pandas_df
)
return core.ArrayValue.from_managed(managed_data_source, compiler_session)
@pytest.fixture(scope="session")
def nested_structs_types_table_schema() -> typing.Sequence[bigquery.SchemaField]:
return [
bigquery.SchemaField("id", "INTEGER"),
bigquery.SchemaField(
"people",
"RECORD",
fields=[
bigquery.SchemaField("name", "STRING"),
bigquery.SchemaField("age", "INTEGER"),
bigquery.SchemaField(
"address",
"RECORD",
fields=[
bigquery.SchemaField("city", "STRING"),
bigquery.SchemaField("country", "STRING"),
],
),
],
),
]
@pytest.fixture(scope="session")
def nested_structs_types_df(compiler_session_w_nested_structs_types) -> bpd.DataFrame:
"""Returns a BigFrames DataFrame containing all scalar types and using the `rowindex`
column as the index."""
bf_df = compiler_session_w_nested_structs_types.read_gbq_table(
"bigframes-dev.sqlglot_test.nested_structs_types"
)
bf_df = bf_df.set_index("id", drop=False)
return bf_df
@pytest.fixture(scope="session")
def nested_structs_pandas_df() -> pd.DataFrame:
"""Returns a pandas DataFrame containing STRUCT types and using the `id`
column as the index."""
df = pd.read_json(
DATA_DIR / "nested_structs.jsonl",
lines=True,
)
df = df.set_index("id")
address_struct_schema = pa.struct(
[pa.field("city", pa.string()), pa.field("country", pa.string())]
)
person_struct_schema = pa.struct(
[
pa.field("name", pa.string()),
pa.field("age", pa.int64()),
pa.field("address", address_struct_schema),
]
)
df["person"] = df["person"].astype(pd.ArrowDtype(person_struct_schema))
return df
@pytest.fixture(scope="session")
def repeated_types_table_schema() -> typing.Sequence[bigquery.SchemaField]:
return [
bigquery.SchemaField("rowindex", "INTEGER"),
bigquery.SchemaField("int_list_col", "INTEGER", "REPEATED"),
bigquery.SchemaField("bool_list_col", "BOOLEAN", "REPEATED"),
bigquery.SchemaField("float_list_col", "FLOAT", "REPEATED"),
bigquery.SchemaField("date_list_col", "DATE", "REPEATED"),
bigquery.SchemaField("date_time_list_col", "DATETIME", "REPEATED"),
bigquery.SchemaField("numeric_list_col", "NUMERIC", "REPEATED"),
bigquery.SchemaField("string_list_col", "STRING", "REPEATED"),
]
@pytest.fixture(scope="session")
def repeated_types_df(compiler_session_w_repeated_types) -> bpd.DataFrame:
"""Returns a BigFrames DataFrame containing all scalar types and using the `rowindex`
column as the index."""
bf_df = compiler_session_w_repeated_types.read_gbq_table(
"bigframes-dev.sqlglot_test.repeated_types"
)
bf_df = bf_df.set_index("rowindex", drop=False)
return bf_df
@pytest.fixture(scope="session")
def repeated_types_pandas_df() -> pd.DataFrame:
"""Returns a pandas DataFrame containing LIST types and using the `rowindex`
column as the index."""
df = pd.read_json(
DATA_DIR / "repeated.jsonl",
lines=True,
)
# TODO: add dtype conversion here if needed.
df = df.set_index("rowindex")
return df
@pytest.fixture(scope="session")
def json_types_table_schema() -> typing.Sequence[bigquery.SchemaField]:
return [
bigquery.SchemaField("rowindex", "INTEGER"),
bigquery.SchemaField("json_col", "JSON"),
]
@pytest.fixture(scope="session")
def json_types_df(compiler_session_w_json_types) -> bpd.DataFrame:
"""Returns a BigFrames DataFrame containing JSON types and using the `rowindex`
column as the index."""
bf_df = compiler_session_w_json_types.read_gbq_table(
"bigframes-dev.sqlglot_test.json_types"
)
# TODO(b/427305807): Why `drop=False` will produce two "rowindex" columns?
bf_df = bf_df.set_index("rowindex", drop=True)
return bf_df
@pytest.fixture(scope="session")
def json_pandas_df() -> pd.DataFrame:
"""Returns a pandas DataFrame containing JSON types and using the `rowindex`
column as the index."""
json_data = [
"null",
"true",
"100",
"0.98",
'"a string"',
"[]",
"[1, 2, 3]",
'[{"a": 1}, {"a": 2}, {"a": null}, {}]',
'"100"',
'{"date": "2024-07-16"}',
'{"int_value": 2, "null_filed": null}',
'{"list_data": [10, 20, 30]}',
]
df = pd.DataFrame(
{
"rowindex": pd.Series(range(len(json_data)), dtype=dtypes.INT_DTYPE),
"json_col": pd.Series(json_data, dtype=dtypes.JSON_DTYPE),
},
)
# TODO(b/427305807): Why `drop=False` will produce two "rowindex" columns?
df = df.set_index("rowindex", drop=True)
return df