forked from apache/iceberg-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_pyarrow_visitor.py
More file actions
499 lines (417 loc) · 18.2 KB
/
test_pyarrow_visitor.py
File metadata and controls
499 lines (417 loc) · 18.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
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
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
# pylint: disable=protected-access,unused-argument,redefined-outer-name
import re
import pyarrow as pa
import pytest
from pyiceberg.io.pyarrow import (
_ConvertToArrowSchema,
_ConvertToIceberg,
_ConvertToIcebergWithoutIDs,
_HasIds,
pyarrow_to_schema,
schema_to_pyarrow,
visit_pyarrow,
)
from pyiceberg.schema import Schema, visit
from pyiceberg.table.name_mapping import MappedField, NameMapping
from pyiceberg.types import (
BinaryType,
BooleanType,
DateType,
DecimalType,
DoubleType,
FixedType,
FloatType,
IntegerType,
ListType,
LongType,
MapType,
NestedField,
StringType,
StructType,
TimestampType,
TimestamptzType,
TimeType,
)
def test_pyarrow_binary_to_iceberg() -> None:
length = 23
pyarrow_type = pa.binary(length)
converted_iceberg_type = visit_pyarrow(pyarrow_type, _ConvertToIceberg())
assert converted_iceberg_type == FixedType(length)
assert visit(converted_iceberg_type, _ConvertToArrowSchema()) == pyarrow_type
def test_pyarrow_decimal128_to_iceberg() -> None:
precision = 26
scale = 20
pyarrow_type = pa.decimal128(precision, scale)
converted_iceberg_type = visit_pyarrow(pyarrow_type, _ConvertToIceberg())
assert converted_iceberg_type == DecimalType(precision, scale)
assert visit(converted_iceberg_type, _ConvertToArrowSchema()) == pyarrow_type
def test_pyarrow_decimal256_to_iceberg() -> None:
precision = 26
scale = 20
pyarrow_type = pa.decimal256(precision, scale)
with pytest.raises(TypeError, match=re.escape("Unsupported type: decimal256(26, 20)")):
visit_pyarrow(pyarrow_type, _ConvertToIceberg())
def test_pyarrow_boolean_to_iceberg() -> None:
pyarrow_type = pa.bool_()
converted_iceberg_type = visit_pyarrow(pyarrow_type, _ConvertToIceberg())
assert converted_iceberg_type == BooleanType()
assert visit(converted_iceberg_type, _ConvertToArrowSchema()) == pyarrow_type
def test_pyarrow_int32_to_iceberg() -> None:
pyarrow_type = pa.int32()
converted_iceberg_type = visit_pyarrow(pyarrow_type, _ConvertToIceberg())
assert converted_iceberg_type == IntegerType()
assert visit(converted_iceberg_type, _ConvertToArrowSchema()) == pyarrow_type
def test_pyarrow_int64_to_iceberg() -> None:
pyarrow_type = pa.int64()
converted_iceberg_type = visit_pyarrow(pyarrow_type, _ConvertToIceberg())
assert converted_iceberg_type == LongType()
assert visit(converted_iceberg_type, _ConvertToArrowSchema()) == pyarrow_type
def test_pyarrow_float32_to_iceberg() -> None:
pyarrow_type = pa.float32()
converted_iceberg_type = visit_pyarrow(pyarrow_type, _ConvertToIceberg())
assert converted_iceberg_type == FloatType()
assert visit(converted_iceberg_type, _ConvertToArrowSchema()) == pyarrow_type
def test_pyarrow_float64_to_iceberg() -> None:
pyarrow_type = pa.float64()
converted_iceberg_type = visit_pyarrow(pyarrow_type, _ConvertToIceberg())
assert converted_iceberg_type == DoubleType()
assert visit(converted_iceberg_type, _ConvertToArrowSchema()) == pyarrow_type
def test_pyarrow_date32_to_iceberg() -> None:
pyarrow_type = pa.date32()
converted_iceberg_type = visit_pyarrow(pyarrow_type, _ConvertToIceberg())
assert converted_iceberg_type == DateType()
assert visit(converted_iceberg_type, _ConvertToArrowSchema()) == pyarrow_type
def test_pyarrow_date64_to_iceberg() -> None:
pyarrow_type = pa.date64()
with pytest.raises(TypeError, match=re.escape("Unsupported type: date64")):
visit_pyarrow(pyarrow_type, _ConvertToIceberg())
def test_pyarrow_time32_to_iceberg() -> None:
pyarrow_type = pa.time32("ms")
with pytest.raises(TypeError, match=re.escape("Unsupported type: time32[ms]")):
visit_pyarrow(pyarrow_type, _ConvertToIceberg())
pyarrow_type = pa.time32("s")
with pytest.raises(TypeError, match=re.escape("Unsupported type: time32[s]")):
visit_pyarrow(pyarrow_type, _ConvertToIceberg())
def test_pyarrow_time64_us_to_iceberg() -> None:
pyarrow_type = pa.time64("us")
converted_iceberg_type = visit_pyarrow(pyarrow_type, _ConvertToIceberg())
assert converted_iceberg_type == TimeType()
assert visit(converted_iceberg_type, _ConvertToArrowSchema()) == pyarrow_type
def test_pyarrow_time64_ns_to_iceberg() -> None:
pyarrow_type = pa.time64("ns")
with pytest.raises(TypeError, match=re.escape("Unsupported type: time64[ns]")):
visit_pyarrow(pyarrow_type, _ConvertToIceberg())
def test_pyarrow_timestamp_to_iceberg() -> None:
pyarrow_type = pa.timestamp(unit="us")
converted_iceberg_type = visit_pyarrow(pyarrow_type, _ConvertToIceberg())
assert converted_iceberg_type == TimestampType()
assert visit(converted_iceberg_type, _ConvertToArrowSchema()) == pyarrow_type
def test_pyarrow_timestamp_invalid_units() -> None:
pyarrow_type = pa.timestamp(unit="ms")
with pytest.raises(TypeError, match=re.escape("Unsupported type: timestamp[ms]")):
visit_pyarrow(pyarrow_type, _ConvertToIceberg())
pyarrow_type = pa.timestamp(unit="s")
with pytest.raises(TypeError, match=re.escape("Unsupported type: timestamp[s]")):
visit_pyarrow(pyarrow_type, _ConvertToIceberg())
pyarrow_type = pa.timestamp(unit="ns")
with pytest.raises(TypeError, match=re.escape("Unsupported type: timestamp[ns]")):
visit_pyarrow(pyarrow_type, _ConvertToIceberg())
def test_pyarrow_timestamp_tz_to_iceberg() -> None:
pyarrow_type = pa.timestamp(unit="us", tz="UTC")
pyarrow_type_zero_offset = pa.timestamp(unit="us", tz="+00:00")
converted_iceberg_type = visit_pyarrow(pyarrow_type, _ConvertToIceberg())
converted_iceberg_type_zero_offset = visit_pyarrow(pyarrow_type_zero_offset, _ConvertToIceberg())
assert converted_iceberg_type == TimestamptzType()
assert converted_iceberg_type_zero_offset == TimestamptzType()
assert visit(converted_iceberg_type, _ConvertToArrowSchema()) == pyarrow_type
assert visit(converted_iceberg_type_zero_offset, _ConvertToArrowSchema()) == pyarrow_type
def test_pyarrow_timestamp_tz_invalid_units() -> None:
pyarrow_type = pa.timestamp(unit="ms", tz="UTC")
with pytest.raises(TypeError, match=re.escape("Unsupported type: timestamp[ms, tz=UTC]")):
visit_pyarrow(pyarrow_type, _ConvertToIceberg())
pyarrow_type = pa.timestamp(unit="s", tz="UTC")
with pytest.raises(TypeError, match=re.escape("Unsupported type: timestamp[s, tz=UTC]")):
visit_pyarrow(pyarrow_type, _ConvertToIceberg())
pyarrow_type = pa.timestamp(unit="ns", tz="UTC")
with pytest.raises(TypeError, match=re.escape("Unsupported type: timestamp[ns, tz=UTC]")):
visit_pyarrow(pyarrow_type, _ConvertToIceberg())
def test_pyarrow_timestamp_tz_invalid_tz() -> None:
pyarrow_type = pa.timestamp(unit="us", tz="US/Pacific")
with pytest.raises(TypeError, match=re.escape("Unsupported type: timestamp[us, tz=US/Pacific]")):
visit_pyarrow(pyarrow_type, _ConvertToIceberg())
def test_pyarrow_string_to_iceberg() -> None:
pyarrow_type = pa.string()
converted_iceberg_type = visit_pyarrow(pyarrow_type, _ConvertToIceberg())
assert converted_iceberg_type == StringType()
assert visit(converted_iceberg_type, _ConvertToArrowSchema()) == pyarrow_type
def test_pyarrow_variable_binary_to_iceberg() -> None:
pyarrow_type = pa.binary()
converted_iceberg_type = visit_pyarrow(pyarrow_type, _ConvertToIceberg())
assert converted_iceberg_type == BinaryType()
assert visit(converted_iceberg_type, _ConvertToArrowSchema()) == pyarrow_type
def test_pyarrow_struct_to_iceberg() -> None:
pyarrow_struct = pa.struct([
pa.field("foo", pa.string(), nullable=True, metadata={"PARQUET:field_id": "1", "doc": "foo doc"}),
pa.field("bar", pa.int32(), nullable=False, metadata={"PARQUET:field_id": "2"}),
pa.field("baz", pa.bool_(), nullable=True, metadata={"PARQUET:field_id": "3"}),
])
expected = StructType(
NestedField(field_id=1, name="foo", field_type=StringType(), required=False, doc="foo doc"),
NestedField(field_id=2, name="bar", field_type=IntegerType(), required=True),
NestedField(field_id=3, name="baz", field_type=BooleanType(), required=False),
)
assert visit_pyarrow(pyarrow_struct, _ConvertToIceberg()) == expected
def test_pyarrow_list_to_iceberg() -> None:
pyarrow_list = pa.list_(pa.field("element", pa.int32(), nullable=False, metadata={"PARQUET:field_id": "1"}))
expected = ListType(
element_id=1,
element_type=IntegerType(),
element_required=True,
)
assert visit_pyarrow(pyarrow_list, _ConvertToIceberg()) == expected
def test_pyarrow_map_to_iceberg() -> None:
pyarrow_map = pa.map_(
pa.field("key", pa.int32(), nullable=False, metadata={"PARQUET:field_id": "1"}),
pa.field("value", pa.string(), nullable=False, metadata={"PARQUET:field_id": "2"}),
)
expected = MapType(
key_id=1,
key_type=IntegerType(),
value_id=2,
value_type=StringType(),
value_required=True,
)
assert visit_pyarrow(pyarrow_map, _ConvertToIceberg()) == expected
def test_round_schema_conversion_simple(table_schema_simple: Schema) -> None:
actual = str(pyarrow_to_schema(schema_to_pyarrow(table_schema_simple)))
expected = """table {
1: foo: optional string
2: bar: required int
3: baz: optional boolean
}"""
assert actual == expected
def test_round_schema_conversion_nested(table_schema_nested: Schema) -> None:
actual = str(pyarrow_to_schema(schema_to_pyarrow(table_schema_nested)))
expected = """table {
1: foo: optional string
2: bar: required int
3: baz: optional boolean
4: qux: required list<string>
6: quux: required map<string, map<string, int>>
11: location: required list<struct<13: latitude: optional float, 14: longitude: optional float>>
15: person: optional struct<16: name: optional string, 17: age: required int>
}"""
assert actual == expected
def test_round_schema_large_string() -> None:
schema = pa.schema([pa.field("animals", pa.large_string())])
actual = str(pyarrow_to_schema(schema, name_mapping=NameMapping([MappedField(field_id=1, names=["animals"])])))
expected = """table {
1: animals: optional string
}"""
assert actual == expected
def test_simple_schema_has_missing_ids() -> None:
schema = pa.schema([
pa.field('foo', pa.string(), nullable=False),
])
visitor = _HasIds()
has_ids = visit_pyarrow(schema, visitor)
assert not has_ids
def test_simple_schema_has_missing_ids_partial() -> None:
schema = pa.schema([
pa.field('foo', pa.string(), nullable=False, metadata={"PARQUET:field_id": "1", "doc": "foo doc"}),
pa.field('bar', pa.int32(), nullable=False),
])
visitor = _HasIds()
has_ids = visit_pyarrow(schema, visitor)
assert not has_ids
def test_nested_schema_has_missing_ids() -> None:
schema = pa.schema([
pa.field('foo', pa.string(), nullable=False),
pa.field(
'quux',
pa.map_(
pa.string(),
pa.map_(pa.string(), pa.int32()),
),
nullable=False,
),
])
visitor = _HasIds()
has_ids = visit_pyarrow(schema, visitor)
assert not has_ids
def test_nested_schema_has_ids() -> None:
schema = pa.schema([
pa.field('foo', pa.string(), nullable=False, metadata={"PARQUET:field_id": "1", "doc": "foo doc"}),
pa.field(
'quux',
pa.map_(
pa.field("key", pa.string(), nullable=False, metadata={"PARQUET:field_id": "7"}),
pa.field(
"value",
pa.map_(
pa.field('key', pa.string(), nullable=False, metadata={"PARQUET:field_id": "9"}),
pa.field('value', pa.int32(), metadata={"PARQUET:field_id": "10"}),
),
nullable=False,
metadata={"PARQUET:field_id": "8"},
),
),
nullable=False,
metadata={"PARQUET:field_id": "6", "doc": "quux doc"},
),
])
visitor = _HasIds()
has_ids = visit_pyarrow(schema, visitor)
assert has_ids
def test_nested_schema_has_partial_missing_ids() -> None:
schema = pa.schema([
pa.field('foo', pa.string(), nullable=False, metadata={"PARQUET:field_id": "1", "doc": "foo doc"}),
pa.field(
'quux',
pa.map_(
pa.field("key", pa.string(), nullable=False, metadata={"PARQUET:field_id": "7"}),
pa.field(
"value",
pa.map_(pa.field('key', pa.string(), nullable=False), pa.field('value', pa.int32())),
nullable=False,
),
),
nullable=False,
metadata={"PARQUET:field_id": "6", "doc": "quux doc"},
),
])
visitor = _HasIds()
has_ids = visit_pyarrow(schema, visitor)
assert not has_ids
def test_pyarrow_schema_to_schema_missing_ids_and_name_mapping(pyarrow_schema_simple_without_ids: pa.Schema) -> None:
schema = pyarrow_schema_simple_without_ids
with pytest.raises(ValueError) as exc_info:
_ = pyarrow_to_schema(schema)
assert (
"Parquet file does not have field-ids and the Iceberg table does not have 'schema.name-mapping.default' defined"
in str(exc_info.value)
)
def test_simple_pyarrow_schema_to_schema_missing_ids_using_name_mapping(
pyarrow_schema_simple_without_ids: pa.Schema, iceberg_schema_simple: Schema
) -> None:
schema = pyarrow_schema_simple_without_ids
name_mapping = NameMapping([
MappedField(field_id=1, names=['foo']),
MappedField(field_id=2, names=['bar']),
MappedField(field_id=3, names=['baz']),
])
assert pyarrow_to_schema(schema, name_mapping) == iceberg_schema_simple
def test_simple_pyarrow_schema_to_schema_missing_ids_using_name_mapping_partial_exception(
pyarrow_schema_simple_without_ids: pa.Schema,
) -> None:
schema = pyarrow_schema_simple_without_ids
name_mapping = NameMapping([
MappedField(field_id=1, names=['foo']),
])
with pytest.raises(ValueError) as exc_info:
_ = pyarrow_to_schema(schema, name_mapping)
assert "Could not find field with name: bar" in str(exc_info.value)
def test_nested_pyarrow_schema_to_schema_missing_ids_using_name_mapping(
pyarrow_schema_nested_without_ids: pa.Schema, iceberg_schema_nested: Schema
) -> None:
schema = pyarrow_schema_nested_without_ids
name_mapping = NameMapping([
MappedField(field_id=1, names=['foo']),
MappedField(field_id=2, names=['bar']),
MappedField(field_id=3, names=['baz']),
MappedField(field_id=4, names=['qux'], fields=[MappedField(field_id=5, names=['element'])]),
MappedField(
field_id=6,
names=['quux'],
fields=[
MappedField(field_id=7, names=['key']),
MappedField(
field_id=8,
names=['value'],
fields=[
MappedField(field_id=9, names=['key']),
MappedField(field_id=10, names=['value']),
],
),
],
),
MappedField(
field_id=11,
names=['location'],
fields=[
MappedField(
field_id=12,
names=['element'],
fields=[
MappedField(field_id=13, names=['latitude']),
MappedField(field_id=14, names=['longitude']),
],
)
],
),
MappedField(
field_id=15,
names=['person'],
fields=[
MappedField(field_id=16, names=['name']),
MappedField(field_id=17, names=['age']),
],
),
])
assert pyarrow_to_schema(schema, name_mapping) == iceberg_schema_nested
def test_pyarrow_schema_to_schema_missing_ids_using_name_mapping_nested_missing_id() -> None:
schema = pa.schema([
pa.field('foo', pa.string(), nullable=False),
pa.field(
'quux',
pa.map_(
pa.string(),
pa.map_(pa.string(), pa.int32()),
),
nullable=False,
),
])
name_mapping = NameMapping([
MappedField(field_id=1, names=['foo']),
MappedField(
field_id=6,
names=['quux'],
fields=[
MappedField(field_id=7, names=['key']),
MappedField(
field_id=8,
names=['value'],
fields=[
MappedField(field_id=10, names=['value']),
],
),
],
),
])
with pytest.raises(ValueError) as exc_info:
_ = pyarrow_to_schema(schema, name_mapping)
assert "Could not find field with name: quux.value.key" in str(exc_info.value)
def test_pyarrow_schema_to_schema_fresh_ids_simple_schema(
pyarrow_schema_simple_without_ids: pa.Schema, iceberg_schema_simple_no_ids: Schema
) -> None:
assert visit_pyarrow(pyarrow_schema_simple_without_ids, _ConvertToIcebergWithoutIDs()) == iceberg_schema_simple_no_ids
def test_pyarrow_schema_to_schema_fresh_ids_nested_schema(
pyarrow_schema_nested_without_ids: pa.Schema, iceberg_schema_nested_no_ids: Schema
) -> None:
assert visit_pyarrow(pyarrow_schema_nested_without_ids, _ConvertToIcebergWithoutIDs()) == iceberg_schema_nested_no_ids