forked from apache/iceberg-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_base.py
771 lines (610 loc) · 29.2 KB
/
test_base.py
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
# 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=redefined-outer-name
import uuid
from pathlib import PosixPath
from typing import (
Dict,
List,
Optional,
Set,
Tuple,
Union,
)
import pyarrow as pa
import pytest
from pydantic_core import ValidationError
from pytest_lazyfixture import lazy_fixture
from pyiceberg.catalog import Catalog, MetastoreCatalog, PropertiesUpdateSummary, load_catalog
from pyiceberg.exceptions import (
NamespaceAlreadyExistsError,
NamespaceNotEmptyError,
NoSuchNamespaceError,
NoSuchTableError,
TableAlreadyExistsError,
)
from pyiceberg.io import WAREHOUSE, load_file_io
from pyiceberg.partitioning import UNPARTITIONED_PARTITION_SPEC, PartitionField, PartitionSpec
from pyiceberg.schema import Schema
from pyiceberg.table import (
CommitTableResponse,
Table,
)
from pyiceberg.table.metadata import new_table_metadata
from pyiceberg.table.sorting import UNSORTED_SORT_ORDER, SortOrder
from pyiceberg.table.update import (
AddSchemaUpdate,
SetCurrentSchemaUpdate,
TableRequirement,
TableUpdate,
update_table_metadata,
)
from pyiceberg.transforms import IdentityTransform
from pyiceberg.typedef import EMPTY_DICT, Identifier, Properties
from pyiceberg.types import IntegerType, LongType, NestedField
DEFAULT_WAREHOUSE_LOCATION = "file:///tmp/warehouse"
class InMemoryCatalog(MetastoreCatalog):
"""
An in-memory catalog implementation that uses in-memory data-structures to store the namespaces and tables.
This is useful for test, demo, and playground but not in production as data is not persisted.
"""
__tables: Dict[Identifier, Table]
__namespaces: Dict[Identifier, Properties]
def __init__(self, name: str, **properties: str) -> None:
super().__init__(name, **properties)
self.__tables = {}
self.__namespaces = {}
self._warehouse_location = properties.get(WAREHOUSE, DEFAULT_WAREHOUSE_LOCATION)
def create_table(
self,
identifier: Union[str, Identifier],
schema: Union[Schema, "pa.Schema"],
location: Optional[str] = None,
partition_spec: PartitionSpec = UNPARTITIONED_PARTITION_SPEC,
sort_order: SortOrder = UNSORTED_SORT_ORDER,
properties: Properties = EMPTY_DICT,
table_uuid: Optional[uuid.UUID] = None,
) -> Table:
schema: Schema = self._convert_schema_if_needed(schema) # type: ignore
identifier = Catalog.identifier_to_tuple(identifier)
namespace = Catalog.namespace_from(identifier)
if identifier in self.__tables:
raise TableAlreadyExistsError(f"Table already exists: {identifier}")
else:
if namespace not in self.__namespaces:
self.__namespaces[namespace] = {}
if not location:
location = f"{self._warehouse_location}/{'/'.join(identifier)}"
location = location.rstrip("/")
metadata_location = self._get_metadata_location(location=location)
metadata = new_table_metadata(
schema=schema,
partition_spec=partition_spec,
sort_order=sort_order,
location=location,
properties=properties,
table_uuid=table_uuid,
)
io = load_file_io({**self.properties, **properties}, location=location)
self._write_metadata(metadata, io, metadata_location)
table = Table(
identifier=identifier,
metadata=metadata,
metadata_location=metadata_location,
io=io,
catalog=self,
)
self.__tables[identifier] = table
return table
def register_table(self, identifier: Union[str, Identifier], metadata_location: str) -> Table:
raise NotImplementedError
def commit_table(
self, table: Table, requirements: Tuple[TableRequirement, ...], updates: Tuple[TableUpdate, ...]
) -> CommitTableResponse:
identifier_tuple = table.name()
current_table = self.load_table(identifier_tuple)
base_metadata = current_table.metadata
for requirement in requirements:
requirement.validate(base_metadata)
updated_metadata = update_table_metadata(base_metadata, updates)
if updated_metadata == base_metadata:
# no changes, do nothing
return CommitTableResponse(metadata=base_metadata, metadata_location=current_table.metadata_location)
# write new metadata
new_metadata_version = self._parse_metadata_version(current_table.metadata_location) + 1
new_metadata_location = self._get_metadata_location(current_table.metadata.location, new_metadata_version)
self._write_metadata(updated_metadata, current_table.io, new_metadata_location)
# update table state
current_table.metadata = updated_metadata
return CommitTableResponse(metadata=updated_metadata, metadata_location=new_metadata_location)
def load_table(self, identifier: Union[str, Identifier]) -> Table:
try:
identifier_tuple = Catalog.identifier_to_tuple(identifier)
return self.__tables[identifier_tuple]
except KeyError as error:
raise NoSuchTableError(f"Table does not exist: {identifier_tuple}") from error
def drop_table(self, identifier: Union[str, Identifier]) -> None:
try:
identifier_tuple = Catalog.identifier_to_tuple(identifier)
self.__tables.pop(identifier_tuple)
except KeyError as error:
raise NoSuchTableError(f"Table does not exist: {identifier_tuple}") from error
def purge_table(self, identifier: Union[str, Identifier]) -> None:
self.drop_table(identifier)
def rename_table(self, from_identifier: Union[str, Identifier], to_identifier: Union[str, Identifier]) -> Table:
try:
identifier_tuple = Catalog.identifier_to_tuple(from_identifier)
table = self.__tables.pop(identifier_tuple)
except KeyError as error:
raise NoSuchTableError(f"Table does not exist: {identifier_tuple}") from error
to_identifier = Catalog.identifier_to_tuple(to_identifier)
to_namespace = Catalog.namespace_from(to_identifier)
if to_namespace not in self.__namespaces:
self.__namespaces[to_namespace] = {}
self.__tables[to_identifier] = Table(
identifier=to_identifier,
metadata=table.metadata,
metadata_location=table.metadata_location,
io=self._load_file_io(properties=table.metadata.properties, location=table.metadata_location),
catalog=self,
)
return self.__tables[to_identifier]
def create_namespace(self, namespace: Union[str, Identifier], properties: Properties = EMPTY_DICT) -> None:
namespace = Catalog.identifier_to_tuple(namespace)
if namespace in self.__namespaces:
raise NamespaceAlreadyExistsError(f"Namespace already exists: {namespace}")
else:
self.__namespaces[namespace] = properties if properties else {}
def drop_namespace(self, namespace: Union[str, Identifier]) -> None:
namespace = Catalog.identifier_to_tuple(namespace)
if [table_identifier for table_identifier in self.__tables.keys() if namespace == table_identifier[:-1]]:
raise NamespaceNotEmptyError(f"Namespace is not empty: {namespace}")
try:
self.__namespaces.pop(namespace)
except KeyError as error:
raise NoSuchNamespaceError(f"Namespace does not exist: {namespace}") from error
def list_tables(self, namespace: Optional[Union[str, Identifier]] = None) -> List[Identifier]:
if namespace:
namespace = Catalog.identifier_to_tuple(namespace)
list_tables = [table_identifier for table_identifier in self.__tables.keys() if namespace == table_identifier[:-1]]
else:
list_tables = list(self.__tables.keys())
return list_tables
def list_namespaces(self, namespace: Union[str, Identifier] = ()) -> List[Identifier]:
# Hierarchical namespace is not supported. Return an empty list
if namespace:
return []
return list(self.__namespaces.keys())
def load_namespace_properties(self, namespace: Union[str, Identifier]) -> Properties:
namespace = Catalog.identifier_to_tuple(namespace)
try:
return self.__namespaces[namespace]
except KeyError as error:
raise NoSuchNamespaceError(f"Namespace does not exist: {namespace}") from error
def update_namespace_properties(
self, namespace: Union[str, Identifier], removals: Optional[Set[str]] = None, updates: Properties = EMPTY_DICT
) -> PropertiesUpdateSummary:
removed: Set[str] = set()
updated: Set[str] = set()
namespace = Catalog.identifier_to_tuple(namespace)
if namespace in self.__namespaces:
if removals:
for key in removals:
if key in self.__namespaces[namespace]:
del self.__namespaces[namespace][key]
removed.add(key)
if updates:
for key, value in updates.items():
self.__namespaces[namespace][key] = value
updated.add(key)
else:
raise NoSuchNamespaceError(f"Namespace does not exist: {namespace}")
expected_to_change = removed.difference(removals or set())
return PropertiesUpdateSummary(
removed=list(removed or []), updated=list(updates.keys() if updates else []), missing=list(expected_to_change)
)
def list_views(self, namespace: Optional[Union[str, Identifier]] = None) -> List[Identifier]:
raise NotImplementedError
def drop_view(self, identifier: Union[str, Identifier]) -> None:
raise NotImplementedError
def view_exists(self, identifier: Union[str, Identifier]) -> bool:
raise NotImplementedError
@pytest.fixture
def catalog(tmp_path: PosixPath) -> InMemoryCatalog:
return InMemoryCatalog("test.in_memory.catalog", **{WAREHOUSE: tmp_path.absolute().as_posix(), "test.key": "test.value"})
TEST_TABLE_IDENTIFIER = ("com", "organization", "department", "my_table")
TEST_TABLE_NAMESPACE = ("com", "organization", "department")
TEST_TABLE_NAME = "my_table"
TEST_TABLE_SCHEMA = Schema(
NestedField(1, "x", LongType(), required=True),
NestedField(2, "y", LongType(), doc="comment", required=True),
NestedField(3, "z", LongType(), required=True),
)
TEST_TABLE_PARTITION_SPEC = PartitionSpec(PartitionField(name="x", transform=IdentityTransform(), source_id=1, field_id=1000))
TEST_TABLE_PROPERTIES = {"key1": "value1", "key2": "value2"}
NO_SUCH_TABLE_ERROR = "Table does not exist: \\('com', 'organization', 'department', 'my_table'\\)"
TABLE_ALREADY_EXISTS_ERROR = "Table already exists: \\('com', 'organization', 'department', 'my_table'\\)"
NAMESPACE_ALREADY_EXISTS_ERROR = "Namespace already exists: \\('com', 'organization', 'department'\\)"
NO_SUCH_NAMESPACE_ERROR = "Namespace does not exist: \\('com', 'organization', 'department'\\)"
NAMESPACE_NOT_EMPTY_ERROR = "Namespace is not empty: \\('com', 'organization', 'department'\\)"
def given_catalog_has_a_table(
catalog: InMemoryCatalog,
properties: Properties = EMPTY_DICT,
) -> Table:
return catalog.create_table(
identifier=TEST_TABLE_IDENTIFIER,
schema=TEST_TABLE_SCHEMA,
partition_spec=TEST_TABLE_PARTITION_SPEC,
properties=properties or TEST_TABLE_PROPERTIES,
)
def test_load_catalog_impl_not_full_path() -> None:
with pytest.raises(ValueError) as exc_info:
load_catalog("catalog", **{"py-catalog-impl": "CustomCatalog"})
assert "py-catalog-impl should be full path (module.CustomCatalog), got: CustomCatalog" in str(exc_info.value)
def test_load_catalog_impl_does_not_exist() -> None:
with pytest.raises(ValueError) as exc_info:
load_catalog("catalog", **{"py-catalog-impl": "pyiceberg.does.not.exist.Catalog"})
assert "Could not initialize Catalog: pyiceberg.does.not.exist.Catalog" in str(exc_info.value)
def test_load_catalog_has_type_and_impl() -> None:
with pytest.raises(ValueError) as exc_info:
load_catalog("catalog", **{"py-catalog-impl": "pyiceberg.does.not.exist.Catalog", "type": "sql"})
assert (
"Must not set both catalog type and py-catalog-impl configurations, "
"but found type sql and py-catalog-impl pyiceberg.does.not.exist.Catalog" in str(exc_info.value)
)
def test_namespace_from_tuple() -> None:
# Given
identifier = ("com", "organization", "department", "my_table")
# When
namespace_from = Catalog.namespace_from(identifier)
# Then
assert namespace_from == ("com", "organization", "department")
def test_namespace_from_str() -> None:
# Given
identifier = "com.organization.department.my_table"
# When
namespace_from = Catalog.namespace_from(identifier)
# Then
assert namespace_from == ("com", "organization", "department")
def test_name_from_tuple() -> None:
# Given
identifier = ("com", "organization", "department", "my_table")
# When
name_from = Catalog.table_name_from(identifier)
# Then
assert name_from == "my_table"
def test_name_from_str() -> None:
# Given
identifier = "com.organization.department.my_table"
# When
name_from = Catalog.table_name_from(identifier)
# Then
assert name_from == "my_table"
def test_create_table(catalog: InMemoryCatalog) -> None:
table = catalog.create_table(
identifier=TEST_TABLE_IDENTIFIER,
schema=TEST_TABLE_SCHEMA,
partition_spec=TEST_TABLE_PARTITION_SPEC,
properties=TEST_TABLE_PROPERTIES,
)
assert catalog.load_table(TEST_TABLE_IDENTIFIER) == table
def test_create_table_location_override(catalog: InMemoryCatalog) -> None:
new_location = f"{catalog._warehouse_location}/new_location"
table = catalog.create_table(
identifier=TEST_TABLE_IDENTIFIER,
schema=TEST_TABLE_SCHEMA,
location=new_location,
partition_spec=TEST_TABLE_PARTITION_SPEC,
properties=TEST_TABLE_PROPERTIES,
)
assert catalog.load_table(TEST_TABLE_IDENTIFIER) == table
assert table.location() == new_location
def test_create_table_removes_trailing_slash_from_location(catalog: InMemoryCatalog) -> None:
new_location = f"{catalog._warehouse_location}/new_location"
table = catalog.create_table(
identifier=TEST_TABLE_IDENTIFIER,
schema=TEST_TABLE_SCHEMA,
location=f"{new_location}/",
partition_spec=TEST_TABLE_PARTITION_SPEC,
properties=TEST_TABLE_PROPERTIES,
)
assert catalog.load_table(TEST_TABLE_IDENTIFIER) == table
assert table.location() == new_location
@pytest.mark.parametrize(
"schema,expected",
[
(lazy_fixture("pyarrow_schema_simple_without_ids"), lazy_fixture("iceberg_schema_simple_no_ids")),
(lazy_fixture("iceberg_schema_simple"), lazy_fixture("iceberg_schema_simple")),
(lazy_fixture("iceberg_schema_nested"), lazy_fixture("iceberg_schema_nested")),
(lazy_fixture("pyarrow_schema_nested_without_ids"), lazy_fixture("iceberg_schema_nested_no_ids")),
],
)
def test_convert_schema_if_needed(
schema: Union[Schema, pa.Schema],
expected: Schema,
catalog: InMemoryCatalog,
) -> None:
assert expected == catalog._convert_schema_if_needed(schema)
def test_create_table_pyarrow_schema(catalog: InMemoryCatalog, pyarrow_schema_simple_without_ids: pa.Schema) -> None:
table = catalog.create_table(
identifier=TEST_TABLE_IDENTIFIER,
schema=pyarrow_schema_simple_without_ids,
properties=TEST_TABLE_PROPERTIES,
)
assert catalog.load_table(TEST_TABLE_IDENTIFIER) == table
def test_create_table_raises_error_when_table_already_exists(catalog: InMemoryCatalog) -> None:
# Given
given_catalog_has_a_table(catalog)
# When
with pytest.raises(TableAlreadyExistsError, match=TABLE_ALREADY_EXISTS_ERROR):
catalog.create_table(
identifier=TEST_TABLE_IDENTIFIER,
schema=TEST_TABLE_SCHEMA,
)
def test_load_table(catalog: InMemoryCatalog) -> None:
# Given
given_table = given_catalog_has_a_table(catalog)
# When
table = catalog.load_table(TEST_TABLE_IDENTIFIER)
# Then
assert table == given_table
def test_load_table_from_self_identifier(catalog: InMemoryCatalog) -> None:
# Given
given_table = given_catalog_has_a_table(catalog)
# When
intermediate = catalog.load_table(TEST_TABLE_IDENTIFIER)
table = catalog.load_table(intermediate._identifier)
# Then
assert table == given_table
def test_table_raises_error_on_table_not_found(catalog: InMemoryCatalog) -> None:
with pytest.raises(NoSuchTableError, match=NO_SUCH_TABLE_ERROR):
catalog.load_table(TEST_TABLE_IDENTIFIER)
def test_table_exists(catalog: InMemoryCatalog) -> None:
# Given
given_catalog_has_a_table(catalog)
# Then
assert catalog.table_exists(TEST_TABLE_IDENTIFIER)
def test_table_exists_on_table_not_found(catalog: InMemoryCatalog) -> None:
assert not catalog.table_exists(TEST_TABLE_IDENTIFIER)
def test_drop_table(catalog: InMemoryCatalog) -> None:
# Given
given_catalog_has_a_table(catalog)
# When
catalog.drop_table(TEST_TABLE_IDENTIFIER)
# Then
with pytest.raises(NoSuchTableError, match=NO_SUCH_TABLE_ERROR):
catalog.load_table(TEST_TABLE_IDENTIFIER)
def test_drop_table_from_self_identifier(catalog: InMemoryCatalog) -> None:
# Given
table = given_catalog_has_a_table(catalog)
# When
catalog.drop_table(table._identifier)
# Then
with pytest.raises(NoSuchTableError, match=NO_SUCH_TABLE_ERROR):
catalog.load_table(table._identifier)
with pytest.raises(NoSuchTableError, match=NO_SUCH_TABLE_ERROR):
catalog.load_table(TEST_TABLE_IDENTIFIER)
def test_drop_table_that_does_not_exist_raise_error(catalog: InMemoryCatalog) -> None:
with pytest.raises(NoSuchTableError, match=NO_SUCH_TABLE_ERROR):
catalog.load_table(TEST_TABLE_IDENTIFIER)
def test_purge_table(catalog: InMemoryCatalog) -> None:
# Given
given_catalog_has_a_table(catalog)
# When
catalog.purge_table(TEST_TABLE_IDENTIFIER)
# Then
with pytest.raises(NoSuchTableError, match=NO_SUCH_TABLE_ERROR):
catalog.load_table(TEST_TABLE_IDENTIFIER)
def test_rename_table(catalog: InMemoryCatalog) -> None:
# Given
given_catalog_has_a_table(catalog)
# When
new_table = "new.namespace.new_table"
table = catalog.rename_table(TEST_TABLE_IDENTIFIER, new_table)
# Then
assert table._identifier == Catalog.identifier_to_tuple(new_table)
# And
table = catalog.load_table(new_table)
assert table._identifier == Catalog.identifier_to_tuple(new_table)
# And
assert ("new", "namespace") in catalog.list_namespaces()
# And
with pytest.raises(NoSuchTableError, match=NO_SUCH_TABLE_ERROR):
catalog.load_table(TEST_TABLE_IDENTIFIER)
def test_rename_table_from_self_identifier(catalog: InMemoryCatalog) -> None:
# Given
table = given_catalog_has_a_table(catalog)
# When
new_table_name = "new.namespace.new_table"
new_table = catalog.rename_table(table._identifier, new_table_name)
# Then
assert new_table._identifier == Catalog.identifier_to_tuple(new_table_name)
# And
new_table = catalog.load_table(new_table._identifier)
assert new_table._identifier == Catalog.identifier_to_tuple(new_table_name)
# And
assert ("new", "namespace") in catalog.list_namespaces()
# And
with pytest.raises(NoSuchTableError, match=NO_SUCH_TABLE_ERROR):
catalog.load_table(table._identifier)
with pytest.raises(NoSuchTableError, match=NO_SUCH_TABLE_ERROR):
catalog.load_table(TEST_TABLE_IDENTIFIER)
def test_create_namespace(catalog: InMemoryCatalog) -> None:
# When
catalog.create_namespace(TEST_TABLE_NAMESPACE, TEST_TABLE_PROPERTIES)
# Then
assert TEST_TABLE_NAMESPACE in catalog.list_namespaces()
assert TEST_TABLE_PROPERTIES == catalog.load_namespace_properties(TEST_TABLE_NAMESPACE)
def test_create_namespace_raises_error_on_existing_namespace(catalog: InMemoryCatalog) -> None:
# Given
catalog.create_namespace(TEST_TABLE_NAMESPACE, TEST_TABLE_PROPERTIES)
# When
with pytest.raises(NamespaceAlreadyExistsError, match=NAMESPACE_ALREADY_EXISTS_ERROR):
catalog.create_namespace(TEST_TABLE_NAMESPACE, TEST_TABLE_PROPERTIES)
def test_get_namespace_metadata_raises_error_when_namespace_does_not_exist(catalog: InMemoryCatalog) -> None:
with pytest.raises(NoSuchNamespaceError, match=NO_SUCH_NAMESPACE_ERROR):
catalog.load_namespace_properties(TEST_TABLE_NAMESPACE)
def test_list_namespaces(catalog: InMemoryCatalog) -> None:
# Given
catalog.create_namespace(TEST_TABLE_NAMESPACE, TEST_TABLE_PROPERTIES)
# When
namespaces = catalog.list_namespaces()
# Then
assert TEST_TABLE_NAMESPACE in namespaces
def test_drop_namespace(catalog: InMemoryCatalog) -> None:
# Given
catalog.create_namespace(TEST_TABLE_NAMESPACE, TEST_TABLE_PROPERTIES)
# When
catalog.drop_namespace(TEST_TABLE_NAMESPACE)
# Then
assert TEST_TABLE_NAMESPACE not in catalog.list_namespaces()
def test_drop_namespace_raises_error_when_namespace_does_not_exist(catalog: InMemoryCatalog) -> None:
with pytest.raises(NoSuchNamespaceError, match=NO_SUCH_NAMESPACE_ERROR):
catalog.drop_namespace(TEST_TABLE_NAMESPACE)
def test_drop_namespace_raises_error_when_namespace_not_empty(catalog: InMemoryCatalog) -> None:
# Given
given_catalog_has_a_table(catalog)
# When
with pytest.raises(NamespaceNotEmptyError, match=NAMESPACE_NOT_EMPTY_ERROR):
catalog.drop_namespace(TEST_TABLE_NAMESPACE)
def test_list_tables(catalog: InMemoryCatalog) -> None:
# Given
given_catalog_has_a_table(catalog)
# When
tables = catalog.list_tables()
# Then
assert tables
assert TEST_TABLE_IDENTIFIER in tables
def test_list_tables_under_a_namespace(catalog: InMemoryCatalog) -> None:
# Given
given_catalog_has_a_table(catalog)
new_namespace = ("new", "namespace")
catalog.create_namespace(new_namespace)
# When
all_tables = catalog.list_tables()
new_namespace_tables = catalog.list_tables(new_namespace)
# Then
assert all_tables
assert TEST_TABLE_IDENTIFIER in all_tables
assert new_namespace_tables == []
def test_update_namespace_metadata(catalog: InMemoryCatalog) -> None:
# Given
catalog.create_namespace(TEST_TABLE_NAMESPACE, TEST_TABLE_PROPERTIES)
# When
new_metadata = {"key3": "value3", "key4": "value4"}
summary = catalog.update_namespace_properties(TEST_TABLE_NAMESPACE, updates=new_metadata)
# Then
assert TEST_TABLE_NAMESPACE in catalog.list_namespaces()
assert new_metadata.items() <= catalog.load_namespace_properties(TEST_TABLE_NAMESPACE).items()
assert summary == PropertiesUpdateSummary(removed=[], updated=["key3", "key4"], missing=[])
def test_update_namespace_metadata_removals(catalog: InMemoryCatalog) -> None:
# Given
catalog.create_namespace(TEST_TABLE_NAMESPACE, TEST_TABLE_PROPERTIES)
# When
new_metadata = {"key3": "value3", "key4": "value4"}
remove_metadata = {"key1"}
summary = catalog.update_namespace_properties(TEST_TABLE_NAMESPACE, remove_metadata, new_metadata)
# Then
assert TEST_TABLE_NAMESPACE in catalog.list_namespaces()
assert new_metadata.items() <= catalog.load_namespace_properties(TEST_TABLE_NAMESPACE).items()
assert remove_metadata.isdisjoint(catalog.load_namespace_properties(TEST_TABLE_NAMESPACE).keys())
assert summary == PropertiesUpdateSummary(removed=["key1"], updated=["key3", "key4"], missing=[])
def test_update_namespace_metadata_raises_error_when_namespace_does_not_exist(catalog: InMemoryCatalog) -> None:
with pytest.raises(NoSuchNamespaceError, match=NO_SUCH_NAMESPACE_ERROR):
catalog.update_namespace_properties(TEST_TABLE_NAMESPACE, updates=TEST_TABLE_PROPERTIES)
def test_commit_table(catalog: InMemoryCatalog) -> None:
# Given
given_table = given_catalog_has_a_table(catalog)
new_schema = Schema(
NestedField(1, "x", LongType()),
NestedField(2, "y", LongType(), doc="comment"),
NestedField(3, "z", LongType()),
NestedField(4, "add", LongType()),
)
# When
response = given_table.catalog.commit_table(
given_table,
updates=(
AddSchemaUpdate(schema=new_schema, last_column_id=new_schema.highest_field_id),
SetCurrentSchemaUpdate(schema_id=-1),
),
requirements=(),
)
# Then
assert response.metadata.table_uuid == given_table.metadata.table_uuid
assert len(response.metadata.schemas) == 2
assert response.metadata.schemas[1] == new_schema
assert response.metadata.current_schema_id == new_schema.schema_id
def test_add_column(catalog: InMemoryCatalog) -> None:
given_table = given_catalog_has_a_table(catalog)
given_table.update_schema().add_column(path="new_column1", field_type=IntegerType()).commit()
assert given_table.schema() == Schema(
NestedField(field_id=1, name="x", field_type=LongType(), required=True),
NestedField(field_id=2, name="y", field_type=LongType(), required=True, doc="comment"),
NestedField(field_id=3, name="z", field_type=LongType(), required=True),
NestedField(field_id=4, name="new_column1", field_type=IntegerType(), required=False),
identifier_field_ids=[],
)
assert given_table.schema().schema_id == 1
transaction = given_table.transaction()
transaction.update_schema().add_column(path="new_column2", field_type=IntegerType(), doc="doc").commit()
transaction.commit_transaction()
assert given_table.schema() == Schema(
NestedField(field_id=1, name="x", field_type=LongType(), required=True),
NestedField(field_id=2, name="y", field_type=LongType(), required=True, doc="comment"),
NestedField(field_id=3, name="z", field_type=LongType(), required=True),
NestedField(field_id=4, name="new_column1", field_type=IntegerType(), required=False),
NestedField(field_id=5, name="new_column2", field_type=IntegerType(), required=False, doc="doc"),
identifier_field_ids=[],
)
assert given_table.schema().schema_id == 2
def test_add_column_with_statement(catalog: InMemoryCatalog) -> None:
given_table = given_catalog_has_a_table(catalog)
with given_table.update_schema() as tx:
tx.add_column(path="new_column1", field_type=IntegerType())
assert given_table.schema() == Schema(
NestedField(field_id=1, name="x", field_type=LongType(), required=True),
NestedField(field_id=2, name="y", field_type=LongType(), required=True, doc="comment"),
NestedField(field_id=3, name="z", field_type=LongType(), required=True),
NestedField(field_id=4, name="new_column1", field_type=IntegerType(), required=False),
identifier_field_ids=[],
)
assert given_table.schema().schema_id == 1
with given_table.transaction() as tx:
tx.update_schema().add_column(path="new_column2", field_type=IntegerType(), doc="doc").commit()
assert given_table.schema() == Schema(
NestedField(field_id=1, name="x", field_type=LongType(), required=True),
NestedField(field_id=2, name="y", field_type=LongType(), required=True, doc="comment"),
NestedField(field_id=3, name="z", field_type=LongType(), required=True),
NestedField(field_id=4, name="new_column1", field_type=IntegerType(), required=False),
NestedField(field_id=5, name="new_column2", field_type=IntegerType(), required=False, doc="doc"),
identifier_field_ids=[],
)
assert given_table.schema().schema_id == 2
def test_catalog_repr(catalog: InMemoryCatalog) -> None:
s = repr(catalog)
assert s == "test.in_memory.catalog (<class 'test_base.InMemoryCatalog'>)"
def test_table_properties_int_value(catalog: InMemoryCatalog) -> None:
# table properties can be set to int, but still serialized to string
property_with_int = {"property_name": 42}
given_table = given_catalog_has_a_table(catalog, properties=property_with_int)
assert isinstance(given_table.properties["property_name"], str)
def test_table_properties_raise_for_none_value(catalog: InMemoryCatalog) -> None:
property_with_none = {"property_name": None}
with pytest.raises(ValidationError) as exc_info:
_ = given_catalog_has_a_table(catalog, properties=property_with_none)
assert "None type is not a supported value in properties: property_name" in str(exc_info.value)