forked from googleapis/google-cloud-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_reader.py
More file actions
810 lines (667 loc) · 26.1 KB
/
test_reader.py
File metadata and controls
810 lines (667 loc) · 26.1 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
# -*- coding: utf-8 -*-
#
# Copyright 2018 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
#
# https://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 datetime
import decimal
import itertools
import json
import fastavro
import pyarrow
import mock
import pandas
import pandas.testing
import pytest
import pytz
import six
import google.api_core.exceptions
from google.cloud import bigquery_storage_v1beta1
PROJECT = "my-project"
BQ_TO_AVRO_TYPES = {
"int64": "long",
"float64": "double",
"bool": "boolean",
"numeric": {"type": "bytes", "logicalType": "decimal", "precision": 38, "scale": 9},
"string": "string",
"bytes": "bytes",
"date": {"type": "int", "logicalType": "date"},
"datetime": {"type": "string", "sqlType": "DATETIME"},
"time": {"type": "long", "logicalType": "time-micros"},
"timestamp": {"type": "long", "logicalType": "timestamp-micros"},
}
# This dictionary is duplicated in bigquery/google/cloud/bigquery/_pandas_helpers.py
# When modifying it be sure to update it there as well.
BQ_TO_ARROW_TYPES = {
"int64": pyarrow.int64(),
"float64": pyarrow.float64(),
"bool": pyarrow.bool_(),
"numeric": pyarrow.decimal128(38, 9),
"string": pyarrow.utf8(),
"bytes": pyarrow.binary(),
"date": pyarrow.date32(), # int32 days since epoch
"datetime": pyarrow.timestamp("us"),
"time": pyarrow.time64("us"),
"timestamp": pyarrow.timestamp("us", tz="UTC"),
}
SCALAR_COLUMNS = [
{"name": "int_col", "type": "int64"},
{"name": "float_col", "type": "float64"},
{"name": "num_col", "type": "numeric"},
{"name": "bool_col", "type": "bool"},
{"name": "str_col", "type": "string"},
{"name": "bytes_col", "type": "bytes"},
{"name": "date_col", "type": "date"},
{"name": "time_col", "type": "time"},
{"name": "ts_col", "type": "timestamp"},
]
SCALAR_COLUMN_NAMES = [field["name"] for field in SCALAR_COLUMNS]
SCALAR_BLOCKS = [
[
{
"int_col": 123,
"float_col": 3.14,
"num_col": decimal.Decimal("9.99"),
"bool_col": True,
"str_col": "hello world",
"bytes_col": b"ascii bytes",
"date_col": datetime.date(1998, 9, 4),
"time_col": datetime.time(12, 0),
"ts_col": datetime.datetime(2000, 1, 1, 5, 0, tzinfo=pytz.utc),
},
{
"int_col": 456,
"float_col": 2.72,
"num_col": decimal.Decimal("0.99"),
"bool_col": False,
"str_col": "hallo welt",
"bytes_col": b"\xbb\xee\xff",
"date_col": datetime.date(1995, 3, 2),
"time_col": datetime.time(13, 37),
"ts_col": datetime.datetime(1965, 4, 3, 2, 1, tzinfo=pytz.utc),
},
],
[
{
"int_col": 789,
"float_col": 1.23,
"num_col": decimal.Decimal("5.67"),
"bool_col": True,
"str_col": u"こんにちは世界",
"bytes_col": b"\x54\x69\x6d",
"date_col": datetime.date(1970, 1, 1),
"time_col": datetime.time(16, 20),
"ts_col": datetime.datetime(1991, 8, 25, 20, 57, 8, tzinfo=pytz.utc),
}
],
]
@pytest.fixture()
def mut():
from google.cloud.bigquery_storage_v1beta1 import reader
return reader
@pytest.fixture()
def class_under_test(mut):
return mut.ReadRowsStream
@pytest.fixture()
def mock_client():
from google.cloud.bigquery_storage_v1beta1.gapic import big_query_storage_client
return mock.create_autospec(big_query_storage_client.BigQueryStorageClient)
def _bq_to_avro_blocks(bq_blocks, avro_schema_json):
avro_schema = fastavro.parse_schema(avro_schema_json)
avro_blocks = []
for block in bq_blocks:
blockio = six.BytesIO()
for row in block:
fastavro.schemaless_writer(blockio, avro_schema, row)
response = bigquery_storage_v1beta1.types.ReadRowsResponse()
response.row_count = len(block)
response.avro_rows.serialized_binary_rows = blockio.getvalue()
avro_blocks.append(response)
return avro_blocks
def _bq_to_arrow_batch_objects(bq_blocks, arrow_schema):
arrow_batches = []
for block in bq_blocks:
arrays = []
for name in arrow_schema.names:
arrays.append(
pyarrow.array(
(row[name] for row in block),
type=arrow_schema.field_by_name(name).type,
size=len(block),
)
)
arrow_batches.append(pyarrow.RecordBatch.from_arrays(arrays, arrow_schema))
return arrow_batches
def _bq_to_arrow_batches(bq_blocks, arrow_schema):
arrow_batches = []
for record_batch in _bq_to_arrow_batch_objects(bq_blocks, arrow_schema):
response = bigquery_storage_v1beta1.types.ReadRowsResponse()
response.arrow_record_batch.serialized_record_batch = (
record_batch.serialize().to_pybytes()
)
arrow_batches.append(response)
return arrow_batches
def _pages_w_unavailable(pages):
for page in pages:
yield page
raise google.api_core.exceptions.ServiceUnavailable("test: please reconnect")
def _avro_blocks_w_deadline(avro_blocks):
for block in avro_blocks:
yield block
raise google.api_core.exceptions.DeadlineExceeded("test: timeout, don't reconnect")
def _generate_avro_read_session(avro_schema_json):
schema = json.dumps(avro_schema_json)
return bigquery_storage_v1beta1.types.ReadSession(avro_schema={"schema": schema})
def _generate_arrow_read_session(arrow_schema):
return bigquery_storage_v1beta1.types.ReadSession(
arrow_schema={"serialized_schema": arrow_schema.serialize().to_pybytes()}
)
def _bq_to_avro_schema(bq_columns):
fields = []
avro_schema = {"type": "record", "name": "__root__", "fields": fields}
for column in bq_columns:
doc = column.get("description")
name = column["name"]
type_ = BQ_TO_AVRO_TYPES[column["type"]]
mode = column.get("mode", "nullable").lower()
if mode == "nullable":
type_ = ["null", type_]
fields.append({"name": name, "type": type_, "doc": doc})
return avro_schema
def _bq_to_arrow_schema(bq_columns):
def bq_col_as_field(column):
doc = column.get("description")
name = column["name"]
type_ = BQ_TO_ARROW_TYPES[column["type"]]
mode = column.get("mode", "nullable").lower()
return pyarrow.field(name, type_, mode == "nullable", {"description": doc})
return pyarrow.schema(bq_col_as_field(c) for c in bq_columns)
def _get_avro_bytes(rows, avro_schema):
avro_file = six.BytesIO()
for row in rows:
fastavro.schemaless_writer(avro_file, avro_schema, row)
return avro_file.getvalue()
def test_avro_rows_raises_import_error(mut, class_under_test, mock_client, monkeypatch):
monkeypatch.setattr(mut, "fastavro", None)
reader = class_under_test(
[], mock_client, bigquery_storage_v1beta1.types.StreamPosition(), {}
)
bq_columns = [{"name": "int_col", "type": "int64"}]
avro_schema = _bq_to_avro_schema(bq_columns)
read_session = _generate_avro_read_session(avro_schema)
with pytest.raises(ImportError):
reader.rows(read_session)
def test_pyarrow_rows_raises_import_error(
mut, class_under_test, mock_client, monkeypatch
):
monkeypatch.setattr(mut, "pyarrow", None)
reader = class_under_test(
[], mock_client, bigquery_storage_v1beta1.types.StreamPosition(), {}
)
bq_columns = [{"name": "int_col", "type": "int64"}]
arrow_schema = _bq_to_arrow_schema(bq_columns)
read_session = _generate_arrow_read_session(arrow_schema)
with pytest.raises(ImportError):
reader.rows(read_session)
def test_rows_no_schema_set_raises_type_error(
mut, class_under_test, mock_client, monkeypatch
):
reader = class_under_test(
[], mock_client, bigquery_storage_v1beta1.types.StreamPosition(), {}
)
read_session = bigquery_storage_v1beta1.types.ReadSession()
with pytest.raises(TypeError):
reader.rows(read_session)
def test_rows_w_empty_stream(class_under_test, mock_client):
bq_columns = [{"name": "int_col", "type": "int64"}]
avro_schema = _bq_to_avro_schema(bq_columns)
read_session = _generate_avro_read_session(avro_schema)
reader = class_under_test(
[], mock_client, bigquery_storage_v1beta1.types.StreamPosition(), {}
)
got = reader.rows(read_session)
assert got.total_rows is None
assert tuple(got) == ()
def test_rows_w_empty_stream_arrow(class_under_test, mock_client):
bq_columns = [{"name": "int_col", "type": "int64"}]
arrow_schema = _bq_to_arrow_schema(bq_columns)
read_session = _generate_arrow_read_session(arrow_schema)
reader = class_under_test(
[], mock_client, bigquery_storage_v1beta1.types.StreamPosition(), {}
)
got = reader.rows(read_session)
assert got.total_rows is None
assert tuple(got) == ()
def test_rows_w_scalars(class_under_test, mock_client):
avro_schema = _bq_to_avro_schema(SCALAR_COLUMNS)
read_session = _generate_avro_read_session(avro_schema)
avro_blocks = _bq_to_avro_blocks(SCALAR_BLOCKS, avro_schema)
reader = class_under_test(
avro_blocks, mock_client, bigquery_storage_v1beta1.types.StreamPosition(), {}
)
got = tuple(reader.rows(read_session))
expected = tuple(itertools.chain.from_iterable(SCALAR_BLOCKS))
assert got == expected
def test_rows_w_scalars_arrow(class_under_test, mock_client):
arrow_schema = _bq_to_arrow_schema(SCALAR_COLUMNS)
read_session = _generate_arrow_read_session(arrow_schema)
arrow_batches = _bq_to_arrow_batches(SCALAR_BLOCKS, arrow_schema)
reader = class_under_test(
arrow_batches, mock_client, bigquery_storage_v1beta1.types.StreamPosition(), {}
)
got = tuple(reader.rows(read_session))
expected = tuple(itertools.chain.from_iterable(SCALAR_BLOCKS))
assert got == expected
def test_rows_w_timeout(class_under_test, mock_client):
bq_columns = [{"name": "int_col", "type": "int64"}]
avro_schema = _bq_to_avro_schema(bq_columns)
read_session = _generate_avro_read_session(avro_schema)
bq_blocks_1 = [
[{"int_col": 123}, {"int_col": 234}],
[{"int_col": 345}, {"int_col": 456}],
]
avro_blocks_1 = _avro_blocks_w_deadline(
_bq_to_avro_blocks(bq_blocks_1, avro_schema)
)
bq_blocks_2 = [[{"int_col": 567}, {"int_col": 789}], [{"int_col": 890}]]
avro_blocks_2 = _bq_to_avro_blocks(bq_blocks_2, avro_schema)
mock_client.read_rows.return_value = avro_blocks_2
stream_position = bigquery_storage_v1beta1.types.StreamPosition(
stream={"name": "test"}
)
reader = class_under_test(
avro_blocks_1,
mock_client,
stream_position,
{"metadata": {"test-key": "test-value"}},
)
with pytest.raises(google.api_core.exceptions.DeadlineExceeded):
list(reader.rows(read_session))
# Don't reconnect on DeadlineException. This allows user-specified timeouts
# to be respected.
mock_client.read_rows.assert_not_called()
def test_rows_w_reconnect(class_under_test, mock_client):
bq_columns = [{"name": "int_col", "type": "int64"}]
avro_schema = _bq_to_avro_schema(bq_columns)
read_session = _generate_avro_read_session(avro_schema)
bq_blocks_1 = [
[{"int_col": 123}, {"int_col": 234}],
[{"int_col": 345}, {"int_col": 456}],
]
avro_blocks_1 = _pages_w_unavailable(_bq_to_avro_blocks(bq_blocks_1, avro_schema))
bq_blocks_2 = [[{"int_col": 567}, {"int_col": 789}], [{"int_col": 890}]]
avro_blocks_2 = _bq_to_avro_blocks(bq_blocks_2, avro_schema)
for block in avro_blocks_2:
block.status.estimated_row_count = 7
mock_client.read_rows.return_value = avro_blocks_2
stream_position = bigquery_storage_v1beta1.types.StreamPosition(
stream={"name": "test"}
)
reader = class_under_test(
avro_blocks_1,
mock_client,
stream_position,
{"metadata": {"test-key": "test-value"}},
)
got = reader.rows(read_session)
expected = tuple(
itertools.chain(
itertools.chain.from_iterable(bq_blocks_1),
itertools.chain.from_iterable(bq_blocks_2),
)
)
assert tuple(got) == expected
assert got.total_rows == 7
mock_client.read_rows.assert_called_once_with(
bigquery_storage_v1beta1.types.StreamPosition(
stream={"name": "test"}, offset=4
),
metadata={"test-key": "test-value"},
)
def test_rows_w_reconnect_by_page(class_under_test, mock_client):
bq_columns = [{"name": "int_col", "type": "int64"}]
avro_schema = _bq_to_avro_schema(bq_columns)
read_session = _generate_avro_read_session(avro_schema)
bq_blocks_1 = [
[{"int_col": 123}, {"int_col": 234}],
[{"int_col": 345}, {"int_col": 456}],
]
avro_blocks_1 = _bq_to_avro_blocks(bq_blocks_1, avro_schema)
bq_blocks_2 = [[{"int_col": 567}, {"int_col": 789}], [{"int_col": 890}]]
avro_blocks_2 = _bq_to_avro_blocks(bq_blocks_2, avro_schema)
avro_blocks_1[0].status.estimated_row_count = 8
avro_blocks_1[1].status.estimated_row_count = 6
avro_blocks_2[0].status.estimated_row_count = 9
avro_blocks_2[1].status.estimated_row_count = 7
mock_client.read_rows.return_value = avro_blocks_2
stream_position = bigquery_storage_v1beta1.types.StreamPosition(
stream={"name": "test"}
)
reader = class_under_test(
_pages_w_unavailable(avro_blocks_1),
mock_client,
stream_position,
{"metadata": {"test-key": "test-value"}},
)
got = reader.rows(read_session)
pages = iter(got.pages)
assert got.total_rows is None
page_1 = next(pages)
assert got.total_rows == 8
assert page_1.num_items == 2
assert page_1.remaining == 2
assert tuple(page_1) == tuple(bq_blocks_1[0])
assert page_1.num_items == 2
assert page_1.remaining == 0
page_2 = next(pages)
assert got.total_rows == 6
assert next(page_2) == bq_blocks_1[1][0]
assert page_2.num_items == 2
assert page_2.remaining == 1
assert next(page_2) == bq_blocks_1[1][1]
page_3 = next(pages)
assert tuple(page_3) == tuple(bq_blocks_2[0])
assert page_3.num_items == 2
assert page_3.remaining == 0
assert got.total_rows == 9
page_4 = next(pages)
assert got.total_rows == 7
assert tuple(page_4) == tuple(bq_blocks_2[1])
assert page_4.num_items == 1
assert page_4.remaining == 0
def test_to_arrow_no_pyarrow_raises_import_error(
mut, class_under_test, mock_client, monkeypatch
):
monkeypatch.setattr(mut, "pyarrow", None)
arrow_schema = _bq_to_arrow_schema(SCALAR_COLUMNS)
read_session = _generate_arrow_read_session(arrow_schema)
arrow_batches = _bq_to_arrow_batches(SCALAR_BLOCKS, arrow_schema)
reader = class_under_test(
arrow_batches, mock_client, bigquery_storage_v1beta1.types.StreamPosition(), {}
)
with pytest.raises(ImportError):
reader.to_arrow(read_session)
with pytest.raises(ImportError):
reader.rows(read_session).to_arrow()
with pytest.raises(ImportError):
next(reader.rows(read_session).pages).to_arrow()
def test_to_arrow_w_scalars_arrow(class_under_test):
arrow_schema = _bq_to_arrow_schema(SCALAR_COLUMNS)
read_session = _generate_arrow_read_session(arrow_schema)
arrow_batches = _bq_to_arrow_batches(SCALAR_BLOCKS, arrow_schema)
reader = class_under_test(
arrow_batches, mock_client, bigquery_storage_v1beta1.types.StreamPosition(), {}
)
actual_table = reader.to_arrow(read_session)
expected_table = pyarrow.Table.from_batches(
_bq_to_arrow_batch_objects(SCALAR_BLOCKS, arrow_schema)
)
assert actual_table == expected_table
def test_to_dataframe_no_pandas_raises_import_error(
mut, class_under_test, mock_client, monkeypatch
):
monkeypatch.setattr(mut, "pandas", None)
avro_schema = _bq_to_avro_schema(SCALAR_COLUMNS)
read_session = _generate_avro_read_session(avro_schema)
avro_blocks = _bq_to_avro_blocks(SCALAR_BLOCKS, avro_schema)
reader = class_under_test(
avro_blocks, mock_client, bigquery_storage_v1beta1.types.StreamPosition(), {}
)
with pytest.raises(ImportError):
reader.to_dataframe(read_session)
with pytest.raises(ImportError):
reader.rows(read_session).to_dataframe()
with pytest.raises(ImportError):
next(reader.rows(read_session).pages).to_dataframe()
def test_to_dataframe_no_schema_set_raises_type_error(
mut, class_under_test, mock_client, monkeypatch
):
reader = class_under_test(
[], mock_client, bigquery_storage_v1beta1.types.StreamPosition(), {}
)
read_session = bigquery_storage_v1beta1.types.ReadSession()
with pytest.raises(TypeError):
reader.to_dataframe(read_session)
def test_to_dataframe_w_scalars(class_under_test):
avro_schema = _bq_to_avro_schema(SCALAR_COLUMNS)
read_session = _generate_avro_read_session(avro_schema)
avro_blocks = _bq_to_avro_blocks(SCALAR_BLOCKS, avro_schema)
reader = class_under_test(
avro_blocks, mock_client, bigquery_storage_v1beta1.types.StreamPosition(), {}
)
got = reader.to_dataframe(read_session)
expected = pandas.DataFrame(
list(itertools.chain.from_iterable(SCALAR_BLOCKS)), columns=SCALAR_COLUMN_NAMES
)
# fastavro provides its own UTC definition, so
# compare the timestamp columns separately.
got_ts = got["ts_col"]
got = got.drop(columns=["ts_col"])
expected_ts = expected["ts_col"]
expected = expected.drop(columns=["ts_col"])
pandas.testing.assert_frame_equal(
got.reset_index(drop=True), # reset_index to ignore row labels
expected.reset_index(drop=True),
)
pandas.testing.assert_series_equal(
got_ts.reset_index(drop=True),
expected_ts.reset_index(drop=True),
check_dtype=False, # fastavro's UTC means different dtype
check_datetimelike_compat=True,
)
def test_to_dataframe_w_scalars_arrow(class_under_test):
arrow_schema = _bq_to_arrow_schema(SCALAR_COLUMNS)
read_session = _generate_arrow_read_session(arrow_schema)
arrow_batches = _bq_to_arrow_batches(SCALAR_BLOCKS, arrow_schema)
reader = class_under_test(
arrow_batches, mock_client, bigquery_storage_v1beta1.types.StreamPosition(), {}
)
got = reader.to_dataframe(read_session)
expected = pandas.DataFrame(
list(itertools.chain.from_iterable(SCALAR_BLOCKS)), columns=SCALAR_COLUMN_NAMES
)
pandas.testing.assert_frame_equal(
got.reset_index(drop=True), # reset_index to ignore row labels
expected.reset_index(drop=True),
)
def test_to_dataframe_w_dtypes(class_under_test):
avro_schema = _bq_to_avro_schema(
[
{"name": "bigfloat", "type": "float64"},
{"name": "lilfloat", "type": "float64"},
]
)
read_session = _generate_avro_read_session(avro_schema)
blocks = [
[{"bigfloat": 1.25, "lilfloat": 30.5}, {"bigfloat": 2.5, "lilfloat": 21.125}],
[{"bigfloat": 3.75, "lilfloat": 11.0}],
]
avro_blocks = _bq_to_avro_blocks(blocks, avro_schema)
reader = class_under_test(
avro_blocks, mock_client, bigquery_storage_v1beta1.types.StreamPosition(), {}
)
got = reader.to_dataframe(read_session, dtypes={"lilfloat": "float16"})
expected = pandas.DataFrame(
{
"bigfloat": [1.25, 2.5, 3.75],
"lilfloat": pandas.Series([30.5, 21.125, 11.0], dtype="float16"),
},
columns=["bigfloat", "lilfloat"],
)
pandas.testing.assert_frame_equal(
got.reset_index(drop=True), # reset_index to ignore row labels
expected.reset_index(drop=True),
)
def test_to_dataframe_w_dtypes_arrow(class_under_test):
arrow_schema = _bq_to_arrow_schema(
[
{"name": "bigfloat", "type": "float64"},
{"name": "lilfloat", "type": "float64"},
]
)
read_session = _generate_arrow_read_session(arrow_schema)
blocks = [
[{"bigfloat": 1.25, "lilfloat": 30.5}, {"bigfloat": 2.5, "lilfloat": 21.125}],
[{"bigfloat": 3.75, "lilfloat": 11.0}],
]
arrow_batches = _bq_to_arrow_batches(blocks, arrow_schema)
reader = class_under_test(
arrow_batches, mock_client, bigquery_storage_v1beta1.types.StreamPosition(), {}
)
got = reader.to_dataframe(read_session, dtypes={"lilfloat": "float16"})
expected = pandas.DataFrame(
{
"bigfloat": [1.25, 2.5, 3.75],
"lilfloat": pandas.Series([30.5, 21.125, 11.0], dtype="float16"),
},
columns=["bigfloat", "lilfloat"],
)
pandas.testing.assert_frame_equal(
got.reset_index(drop=True), # reset_index to ignore row labels
expected.reset_index(drop=True),
)
def test_to_dataframe_by_page(class_under_test, mock_client):
bq_columns = [
{"name": "int_col", "type": "int64"},
{"name": "bool_col", "type": "bool"},
]
avro_schema = _bq_to_avro_schema(bq_columns)
read_session = _generate_avro_read_session(avro_schema)
block_1 = [{"int_col": 123, "bool_col": True}, {"int_col": 234, "bool_col": False}]
block_2 = [{"int_col": 345, "bool_col": True}, {"int_col": 456, "bool_col": False}]
block_3 = [{"int_col": 567, "bool_col": True}, {"int_col": 789, "bool_col": False}]
block_4 = [{"int_col": 890, "bool_col": True}]
# Break blocks into two groups to test that iteration continues across
# reconnection.
bq_blocks_1 = [block_1, block_2]
bq_blocks_2 = [block_3, block_4]
avro_blocks_1 = _bq_to_avro_blocks(bq_blocks_1, avro_schema)
avro_blocks_2 = _bq_to_avro_blocks(bq_blocks_2, avro_schema)
mock_client.read_rows.return_value = avro_blocks_2
stream_position = bigquery_storage_v1beta1.types.StreamPosition(
stream={"name": "test"}
)
reader = class_under_test(
_pages_w_unavailable(avro_blocks_1),
mock_client,
stream_position,
{"metadata": {"test-key": "test-value"}},
)
got = reader.rows(read_session)
pages = iter(got.pages)
page_1 = next(pages)
pandas.testing.assert_frame_equal(
page_1.to_dataframe().reset_index(drop=True),
pandas.DataFrame(block_1, columns=["int_col", "bool_col"]).reset_index(
drop=True
),
)
page_2 = next(pages)
pandas.testing.assert_frame_equal(
page_2.to_dataframe().reset_index(drop=True),
pandas.DataFrame(block_2, columns=["int_col", "bool_col"]).reset_index(
drop=True
),
)
page_3 = next(pages)
pandas.testing.assert_frame_equal(
page_3.to_dataframe().reset_index(drop=True),
pandas.DataFrame(block_3, columns=["int_col", "bool_col"]).reset_index(
drop=True
),
)
page_4 = next(pages)
pandas.testing.assert_frame_equal(
page_4.to_dataframe().reset_index(drop=True),
pandas.DataFrame(block_4, columns=["int_col", "bool_col"]).reset_index(
drop=True
),
)
def test_to_dataframe_by_page_arrow(class_under_test, mock_client):
bq_columns = [
{"name": "int_col", "type": "int64"},
{"name": "bool_col", "type": "bool"},
]
arrow_schema = _bq_to_arrow_schema(bq_columns)
read_session = _generate_arrow_read_session(arrow_schema)
bq_block_1 = [
{"int_col": 123, "bool_col": True},
{"int_col": 234, "bool_col": False},
]
bq_block_2 = [
{"int_col": 345, "bool_col": True},
{"int_col": 456, "bool_col": False},
]
bq_block_3 = [
{"int_col": 567, "bool_col": True},
{"int_col": 789, "bool_col": False},
]
bq_block_4 = [{"int_col": 890, "bool_col": True}]
# Break blocks into two groups to test that iteration continues across
# reconnection.
bq_blocks_1 = [bq_block_1, bq_block_2]
bq_blocks_2 = [bq_block_3, bq_block_4]
batch_1 = _bq_to_arrow_batches(bq_blocks_1, arrow_schema)
batch_2 = _bq_to_arrow_batches(bq_blocks_2, arrow_schema)
mock_client.read_rows.return_value = batch_2
reader = class_under_test(
_pages_w_unavailable(batch_1),
mock_client,
bigquery_storage_v1beta1.types.StreamPosition(),
{},
)
got = reader.rows(read_session)
pages = iter(got.pages)
page_1 = next(pages)
pandas.testing.assert_frame_equal(
page_1.to_dataframe(
dtypes={"int_col": "int64", "bool_col": "bool"}
).reset_index(drop=True),
pandas.DataFrame(bq_block_1, columns=["int_col", "bool_col"]).reset_index(
drop=True
),
)
page_2 = next(pages)
pandas.testing.assert_frame_equal(
page_2.to_dataframe().reset_index(drop=True),
pandas.DataFrame(bq_block_2, columns=["int_col", "bool_col"]).reset_index(
drop=True
),
)
page_3 = next(pages)
pandas.testing.assert_frame_equal(
page_3.to_dataframe().reset_index(drop=True),
pandas.DataFrame(bq_block_3, columns=["int_col", "bool_col"]).reset_index(
drop=True
),
)
page_4 = next(pages)
pandas.testing.assert_frame_equal(
page_4.to_dataframe().reset_index(drop=True),
pandas.DataFrame(bq_block_4, columns=["int_col", "bool_col"]).reset_index(
drop=True
),
)
def test_copy_stream_position(mut):
read_position = bigquery_storage_v1beta1.types.StreamPosition(
stream={"name": "test"}, offset=41
)
got = mut._copy_stream_position(read_position)
assert got == read_position
got.offset = 42
assert read_position.offset == 41
def test_copy_stream_position_w_dict(mut):
read_position = {"stream": {"name": "test"}, "offset": 42}
got = mut._copy_stream_position(read_position)
assert got.stream.name == "test"
assert got.offset == 42