This repository was archived by the owner on Mar 13, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathtest_core_sample.py
More file actions
332 lines (286 loc) · 12.2 KB
/
test_core_sample.py
File metadata and controls
332 lines (286 loc) · 12.2 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# Copyright (c) 2025 pandas-gbq Authors All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
from typing import Sequence
from unittest import mock
import google.cloud.bigquery
import pytest
import pandas_gbq.constants
import pandas_gbq.core.sample
@pytest.mark.parametrize(
"schema, expected_size",
[
pytest.param(
[
google.cloud.bigquery.SchemaField("id", "INT64"), # 8
google.cloud.bigquery.SchemaField("is_valid", "BOOL"), # 1
google.cloud.bigquery.SchemaField("price", "NUMERIC"), # 16
google.cloud.bigquery.SchemaField("big_value", "BIGNUMERIC"), # 32
],
8 + 1 + 16 + 32, # 57
id="Fixed_Size_Types",
),
pytest.param(
[
google.cloud.bigquery.SchemaField(
"coords",
"RECORD",
fields=[
google.cloud.bigquery.SchemaField("lat", "FLOAT64"), # 8
google.cloud.bigquery.SchemaField("lon", "FLOAT64"), # 8
],
),
],
16, # 8 + 8
id="Simple_Struct",
),
pytest.param(
[
google.cloud.bigquery.SchemaField(
"history", "TIMESTAMP", mode="REPEATED"
), # 5 * 8
],
pandas_gbq.core.sample._ARRAY_LENGTH_ESTIMATE * 8, # 40
id="Simple_Array",
),
pytest.param(
[
google.cloud.bigquery.SchemaField(
"addresses",
"RECORD",
mode="REPEATED",
fields=[
google.cloud.bigquery.SchemaField("street", "STRING"), # 1KIB
google.cloud.bigquery.SchemaField("zip", "INT64"), # 8
],
),
],
pandas_gbq.core.sample._ARRAY_LENGTH_ESTIMATE
* (pandas_gbq.constants.BYTES_IN_KIB + 8),
id="Repeated_Struct",
),
pytest.param(
[
google.cloud.bigquery.SchemaField(
"empty_struct", "RECORD", fields=[]
), # 0
google.cloud.bigquery.SchemaField("simple_int", "INT64"), # 8
],
8, # 0 + 8
id="empty-struct",
),
pytest.param(
[
google.cloud.bigquery.SchemaField("bytes", "BYTES"),
]
* 9_999,
pandas_gbq.core.sample._MAX_ROW_BYTES,
id="many-bytes",
),
# Case 8: Complex Mix (Combining multiple cases)
pytest.param(
[
google.cloud.bigquery.SchemaField("key", "INT64"), # 8
google.cloud.bigquery.SchemaField("notes", "STRING"), # 1KIB
google.cloud.bigquery.SchemaField(
"history", "TIMESTAMP", mode="REPEATED"
), # 40
google.cloud.bigquery.SchemaField(
"details",
"RECORD",
fields=[
google.cloud.bigquery.SchemaField("d1", "NUMERIC"), # 16
google.cloud.bigquery.SchemaField("d2", "BYTES"), # 1MB
],
),
],
8
+ pandas_gbq.constants.BYTES_IN_KIB
+ 40
+ (16 + pandas_gbq.constants.BYTES_IN_MIB),
id="Complex_Mix",
),
],
)
def test_estimate_row_size_parametrized(
schema: Sequence[google.cloud.bigquery.SchemaField], expected_size: int
):
actual_size = pandas_gbq.core.sample._estimate_row_bytes(schema)
assert actual_size == expected_size
def test_calculate_target_bytes_with_target_mb():
target_mb = 200
expected_bytes = target_mb * pandas_gbq.constants.BYTES_IN_MIB
actual_bytes = pandas_gbq.core.sample._calculate_target_bytes(target_mb)
assert actual_bytes == expected_bytes
@mock.patch("psutil.virtual_memory")
def test_calculate_target_bytes_with_available_memory(mock_virtual_memory):
# Mock psutil.virtual_memory to return a mock object with an 'available' attribute.
available_memory = 2 * pandas_gbq.constants.BYTES_IN_GIB # 2 GB
mock_virtual_memory.return_value = mock.Mock(available=available_memory)
# Expected bytes is available memory / 4, as it falls between _MAX_ROW_BYTES and _MAX_AUTO_TARGET_BYTES
expected_bytes = available_memory // 4
actual_bytes = pandas_gbq.core.sample._calculate_target_bytes(None)
assert actual_bytes == expected_bytes
@mock.patch("psutil.virtual_memory")
def test_calculate_target_bytes_low_memory_uses_max_row_bytes(mock_virtual_memory):
# Mock psutil.virtual_memory to return a mock object with an 'available' attribute.
# Set available memory to a low value.
available_memory = 100 # 100 bytes
mock_virtual_memory.return_value = mock.Mock(available=available_memory)
# Expected bytes should be _MAX_ROW_BYTES because available // 4 is less.
expected_bytes = pandas_gbq.core.sample._MAX_ROW_BYTES
actual_bytes = pandas_gbq.core.sample._calculate_target_bytes(None)
assert actual_bytes == expected_bytes
@mock.patch("psutil.virtual_memory")
def test_calculate_target_bytes_caps_at_max_auto_target_bytes(mock_virtual_memory):
# Mock psutil.virtual_memory to return a mock object with an 'available' attribute.
# Set available memory to a high value (e.g., 8 GB) so that available // 4 > _MAX_AUTO_TARGET_BYTES.
available_memory = 8 * pandas_gbq.constants.BYTES_IN_GIB # 8 GB
mock_virtual_memory.return_value = mock.Mock(available=available_memory)
# Expected bytes should be _MAX_AUTO_TARGET_BYTES (1 GiB) because available // 4 (2 GiB) is capped.
expected_bytes = pandas_gbq.core.sample._MAX_AUTO_TARGET_BYTES
actual_bytes = pandas_gbq.core.sample._calculate_target_bytes(None)
assert actual_bytes == expected_bytes
@pytest.mark.parametrize(
"target_bytes, table_bytes, table_rows, fields, expected_limit",
[
# With table_bytes and table_rows, should use proportion
pytest.param(
1000, 10000, 100, [], 10, id="with-stats-simple"
), # 100 * (1000/10000)
pytest.param(1, 10000, 100, [], 1, id="with-stats-min-1"), # min is 1
# Without stats, should estimate from schema
pytest.param(
1000,
None,
None,
[google.cloud.bigquery.SchemaField("col1", "INT64")], # 8 bytes
125, # 1000 // 8
id="no-stats-simple",
),
pytest.param(
10,
None,
None,
[google.cloud.bigquery.SchemaField("col1", "NUMERIC")], # 16 bytes
1, # max(1, 10 // 16)
id="no-stats-min-1",
),
# Edge case: row_bytes_estimate is 0
pytest.param(
1000,
None,
None,
[],
1000,
id="no-stats-zero-row-size", # empty schema -> 0 bytes
),
],
)
def test_estimate_limit(target_bytes, table_bytes, table_rows, fields, expected_limit):
limit = pandas_gbq.core.sample._estimate_limit(
target_bytes=target_bytes,
table_bytes=table_bytes,
table_rows=table_rows,
fields=fields,
)
assert limit == expected_limit
@mock.patch("pandas_gbq.core.read.download_results")
def test_sample_with_tablesample(mock_download_results, mock_bigquery_client):
proportion = 0.1
target_row_count = 100
pandas_gbq.core.sample._sample_with_tablesample(
"test-project.test_dataset.test_table",
bqclient=mock_bigquery_client,
proportion=proportion,
target_row_count=target_row_count,
)
mock_bigquery_client.query_and_wait.assert_called_once()
query = mock_bigquery_client.query_and_wait.call_args[0][0]
assert "TABLESAMPLE SYSTEM (10.0 PERCENT)" in query
assert "LIMIT 100" in query
assert "FROM `test-project.test_dataset.test_table`" in query
mock_download_results.assert_called_once()
@mock.patch("pandas_gbq.core.read.download_results")
def test_sample_with_limit(mock_download_results, mock_bigquery_client):
target_row_count = 200
pandas_gbq.core.sample._sample_with_limit(
"test-project.test_dataset.test_table",
bqclient=mock_bigquery_client,
target_row_count=target_row_count,
)
mock_bigquery_client.query_and_wait.assert_called_once()
query = mock_bigquery_client.query_and_wait.call_args[0][0]
assert "TABLESAMPLE" not in query
assert "LIMIT 200" in query
assert "FROM `test-project.test_dataset.test_table`" in query
mock_download_results.assert_called_once()
@pytest.fixture
def mock_gbq_connector(mock_bigquery_client):
with mock.patch("pandas_gbq.gbq_connector.GbqConnector") as mock_connector_class:
mock_connector = mock_connector_class.return_value
mock_connector.get_client.return_value = mock_bigquery_client
mock_connector.credentials = mock.Mock()
yield mock_connector
@mock.patch("pandas_gbq.core.read.download_results")
def test_sample_small_table_downloads_all(
mock_download_results, mock_gbq_connector, mock_bigquery_client
):
mock_table = mock.Mock(spec=google.cloud.bigquery.Table)
type(mock_table).table_type = mock.PropertyMock(return_value="TABLE")
type(mock_table).num_bytes = mock.PropertyMock(return_value=1000)
type(mock_table).num_rows = mock.PropertyMock(return_value=10)
type(mock_table).schema = mock.PropertyMock(return_value=[])
mock_bigquery_client.get_table.return_value = mock_table
with mock.patch(
"pandas_gbq.core.sample._calculate_target_bytes", return_value=2000
):
pandas_gbq.core.sample.sample("my-project.my_dataset.my_table")
mock_bigquery_client.list_rows.assert_called_once_with(mock_table)
mock_download_results.assert_called_once()
# Check that we didn't try to run a query for sampling
mock_bigquery_client.query_and_wait.assert_not_called()
@mock.patch("pandas_gbq.core.sample._sample_with_tablesample")
def test_sample_uses_tablesample(
mock_sample_with_tablesample, mock_gbq_connector, mock_bigquery_client
):
mock_table = mock.Mock(spec=google.cloud.bigquery.Table)
type(mock_table).table_type = mock.PropertyMock(return_value="TABLE")
type(mock_table).num_bytes = mock.PropertyMock(return_value=1_000_000_000_000)
type(mock_table).num_rows = mock.PropertyMock(return_value=1_000)
type(mock_table).schema = mock.PropertyMock(
return_value=[google.cloud.bigquery.SchemaField("col1", "INT64")]
)
mock_bigquery_client.get_table.return_value = mock_table
pandas_gbq.core.sample.sample("my-project.my_dataset.my_table", target_mb=1)
mock_sample_with_tablesample.assert_called_once()
@mock.patch("pandas_gbq.core.sample._sample_with_limit")
def test_sample_uses_limit_fallback(
mock_sample_with_limit, mock_gbq_connector, mock_bigquery_client
):
mock_table = mock.Mock(spec=google.cloud.bigquery.Table)
mock_table.num_bytes = 10000
mock_table.num_rows = 100
mock_table.table_type = "VIEW" # Not eligible for TABLESAMPLE
mock_table.schema = [google.cloud.bigquery.SchemaField("col1", "INT64")]
mock_bigquery_client.get_table.return_value = mock_table
with mock.patch(
"pandas_gbq.core.sample._calculate_target_bytes", return_value=1000
):
pandas_gbq.core.sample.sample("my-project.my_dataset.my_table")
mock_sample_with_limit.assert_called_once()
@mock.patch("pandas_gbq.core.sample._sample_with_limit")
def test_sample_uses_limit_fallback_no_bytes(
mock_sample_with_limit, mock_gbq_connector, mock_bigquery_client
):
mock_table = mock.Mock(spec=google.cloud.bigquery.Table)
mock_table.num_bytes = None # num_bytes can be None
mock_table.num_rows = 100
mock_table.table_type = "TABLE"
mock_table.schema = [google.cloud.bigquery.SchemaField("col1", "INT64")]
mock_bigquery_client.get_table.return_value = mock_table
with mock.patch(
"pandas_gbq.core.sample._calculate_target_bytes", return_value=1000
):
pandas_gbq.core.sample.sample("my-project.my_dataset.my_table")
mock_sample_with_limit.assert_called_once()