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 69
Expand file tree
/
Copy pathtest_read_gbq_table.py
More file actions
188 lines (167 loc) · 5.8 KB
/
test_read_gbq_table.py
File metadata and controls
188 lines (167 loc) · 5.8 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
# Copyright 2024 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.
"""Unit tests for read_gbq_table helper functions."""
import unittest.mock as mock
import warnings
import google.cloud.bigquery
import pytest
import bigframes.enums
import bigframes.exceptions
import bigframes.session._io.bigquery.read_gbq_table as bf_read_gbq_table
from bigframes.testing import mocks
@pytest.mark.parametrize(
("index_cols", "primary_keys", "expected"),
(
(["col1", "col2"], ["col1", "col2", "col3"], ("col1", "col2", "col3")),
(
["col1", "col2", "col3"],
["col1", "col2", "col3"],
("col1", "col2", "col3"),
),
(
["col2", "col3", "col1"],
[
"col3",
"col2",
],
("col2", "col3"),
),
(["col1", "col2"], [], ()),
([], ["col1", "col2", "col3"], ("col1", "col2", "col3")),
([], [], ()),
),
)
def test_infer_unique_columns(index_cols, primary_keys, expected):
"""If a primary key is set on the table, we use that as the index column
by default, no error should be raised in this case.
See internal issue 335727141.
"""
table = google.cloud.bigquery.Table.from_api_repr(
{
"tableReference": {
"projectId": "my-project",
"datasetId": "my_dataset",
"tableId": "my_table",
},
"clustering": {
"fields": ["col1", "col2"],
},
},
)
table.schema = (
google.cloud.bigquery.SchemaField("col1", "INT64"),
google.cloud.bigquery.SchemaField("col2", "INT64"),
google.cloud.bigquery.SchemaField("col3", "INT64"),
google.cloud.bigquery.SchemaField("col4", "INT64"),
)
# TODO(b/305264153): use setter for table_constraints in client library
# when available.
table._properties["tableConstraints"] = {
"primaryKey": {
"columns": primary_keys,
},
}
result = bf_read_gbq_table.infer_unique_columns(table, index_cols)
assert result == expected
@pytest.mark.parametrize(
("index_cols", "values_distinct", "expected"),
(
(
["col1", "col2", "col3"],
True,
("col1", "col2", "col3"),
),
(
["col2", "col3", "col1"],
True,
("col2", "col3", "col1"),
),
(["col1", "col2"], False, ()),
([], False, ()),
),
)
def test_check_if_index_columns_are_unique(index_cols, values_distinct, expected):
table = google.cloud.bigquery.Table.from_api_repr(
{
"tableReference": {
"projectId": "my-project",
"datasetId": "my_dataset",
"tableId": "my_table",
},
"clustering": {
"fields": ["col1", "col2"],
},
},
)
table.schema = (
google.cloud.bigquery.SchemaField("col1", "INT64"),
google.cloud.bigquery.SchemaField("col2", "INT64"),
google.cloud.bigquery.SchemaField("col3", "INT64"),
google.cloud.bigquery.SchemaField("col4", "INT64"),
)
bqclient = mock.create_autospec(google.cloud.bigquery.Client, instance=True)
bqclient.project = "test-project"
session = mocks.create_bigquery_session(
bqclient=bqclient, table_schema=table.schema
)
# Mock bqclient _after_ creating session to override its mocks.
bqclient.get_table.return_value = table
bqclient._query_and_wait_bigframes.side_effect = None
bqclient._query_and_wait_bigframes.return_value = (
{"total_count": 3, "distinct_count": 3 if values_distinct else 2},
)
table._properties["location"] = session._location
result = bf_read_gbq_table.check_if_index_columns_are_unique(
bqclient=bqclient,
table=table,
index_cols=index_cols,
publisher=session._publisher,
)
assert result == expected
def test_get_index_cols_warns_if_clustered_but_sequential_index():
table = google.cloud.bigquery.Table.from_api_repr(
{
"tableReference": {
"projectId": "my-project",
"datasetId": "my_dataset",
"tableId": "my_table",
},
"clustering": {
"fields": ["col1", "col2"],
},
},
)
table.schema = (
google.cloud.bigquery.SchemaField("col1", "INT64"),
google.cloud.bigquery.SchemaField("col2", "INT64"),
google.cloud.bigquery.SchemaField("col3", "INT64"),
google.cloud.bigquery.SchemaField("col4", "INT64"),
)
with pytest.warns(bigframes.exceptions.DefaultIndexWarning, match="is clustered"):
bf_read_gbq_table.get_index_cols(
table,
index_col=(),
default_index_type=bigframes.enums.DefaultIndexKind.SEQUENTIAL_INT64,
)
# Ensure that we don't raise if using a NULL index by default, such as in
# partial ordering mode. See: internal issue b/356872356.
with warnings.catch_warnings():
warnings.simplefilter(
"error", category=bigframes.exceptions.DefaultIndexWarning
)
bf_read_gbq_table.get_index_cols(
table,
index_col=(),
default_index_type=bigframes.enums.DefaultIndexKind.NULL,
)