-
Notifications
You must be signed in to change notification settings - Fork 477
Expand file tree
/
Copy pathtest_glue.py
More file actions
1182 lines (1004 loc) · 48.8 KB
/
test_glue.py
File metadata and controls
1182 lines (1004 loc) · 48.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
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
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# 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.
from unittest import mock
import boto3
import pyarrow as pa
import pytest
from moto import mock_aws
from pyiceberg.catalog.glue import GLUE_CONNECTION_S3_TABLES, GlueCatalog
from pyiceberg.exceptions import (
NamespaceAlreadyExistsError,
NamespaceNotEmptyError,
NoSuchIcebergTableError,
NoSuchNamespaceError,
NoSuchPropertyException,
NoSuchTableError,
TableAlreadyExistsError,
)
from pyiceberg.io.pyarrow import schema_to_pyarrow
from pyiceberg.partitioning import PartitionField, PartitionSpec
from pyiceberg.schema import Schema
from pyiceberg.transforms import IdentityTransform
from pyiceberg.typedef import Properties
from pyiceberg.types import IntegerType
from tests.conftest import (
BUCKET_NAME,
TABLE_METADATA_LOCATION_REGEX,
UNIFIED_AWS_SESSION_PROPERTIES,
)
S3TABLES_WAREHOUSE_LOCATION = "s3tables-warehouse-location"
def _patch_moto_for_s3tables(monkeypatch: pytest.MonkeyPatch) -> None:
"""Patch moto to simulate S3 Tables federated databases.
Moto does not support FederatedDatabase on GetDatabase responses or
auto-populating StorageDescriptor.Location for S3 Tables. These patches
simulate the S3 Tables service behavior so that the GlueCatalog S3 Tables
code path can be tested end-to-end with moto.
"""
from moto.glue.models import FakeDatabase, FakeTable
# Patch 1: Make GetDatabase return FederatedDatabase from the stored input.
_original_db_as_dict = FakeDatabase.as_dict
def _db_as_dict_with_federated(self): # type: ignore
result = _original_db_as_dict(self)
if federated := self.input.get("FederatedDatabase"):
result["FederatedDatabase"] = federated
return result
monkeypatch.setattr(FakeDatabase, "as_dict", _db_as_dict_with_federated)
# Patch 2: When a table is created with format=ICEBERG (the S3 Tables convention),
# inject a StorageDescriptor.Location to simulate S3 Tables vending a table
# warehouse location.
_original_table_init = FakeTable.__init__
def _table_init_with_location(self, database_name, table_name, table_input, catalog_id): # type: ignore
if table_input.get("Parameters", {}).get("format") == "ICEBERG" and "StorageDescriptor" not in table_input:
table_input = {
**table_input,
"StorageDescriptor": {
"Columns": [],
"Location": f"s3://{S3TABLES_WAREHOUSE_LOCATION}/{database_name}/{table_name}/",
"InputFormat": "",
"OutputFormat": "",
"SerdeInfo": {},
},
}
_original_table_init(self, database_name, table_name, table_input, catalog_id)
monkeypatch.setattr(FakeTable, "__init__", _table_init_with_location)
# Create a bucket backing the simulated table warehouse location. S3 Tables manages
# this storage internally, but in tests moto needs a real bucket for metadata file
# writes to succeed.
s3 = boto3.client("s3", region_name="us-east-1")
s3.create_bucket(Bucket=S3TABLES_WAREHOUSE_LOCATION)
@mock_aws
def test_create_table_with_database_location(
_glue: boto3.client,
_bucket_initialize: None,
moto_endpoint_url: str,
table_schema_nested: Schema,
database_name: str,
table_name: str,
) -> None:
catalog_name = "glue"
identifier = (database_name, table_name)
test_catalog = GlueCatalog(catalog_name, **{"s3.endpoint": moto_endpoint_url})
test_catalog.create_namespace(namespace=database_name, properties={"location": f"s3://{BUCKET_NAME}/{database_name}.db"})
table = test_catalog.create_table(identifier, table_schema_nested)
assert table.name() == identifier
assert TABLE_METADATA_LOCATION_REGEX.match(table.metadata_location)
assert test_catalog._parse_metadata_version(table.metadata_location) == 0
# Ensure schema is also pushed to Glue
table_info = _glue.get_table(
DatabaseName=database_name,
Name=table_name,
)
storage_descriptor = table_info["Table"]["StorageDescriptor"]
columns = storage_descriptor["Columns"]
assert len(columns) == len(table_schema_nested.fields)
assert columns[0] == {
"Name": "foo",
"Type": "string",
"Parameters": {"iceberg.field.id": "1", "iceberg.field.optional": "true", "iceberg.field.current": "true"},
}
assert storage_descriptor["Location"] == f"s3://{BUCKET_NAME}/{database_name}.db/{table_name}"
@mock_aws
def test_create_v1_table(
_bucket_initialize: None,
_glue: boto3.client,
moto_endpoint_url: str,
table_schema_nested: Schema,
database_name: str,
table_name: str,
) -> None:
catalog_name = "glue"
test_catalog = GlueCatalog(catalog_name, **{"s3.endpoint": moto_endpoint_url})
test_catalog.create_namespace(namespace=database_name, properties={"location": f"s3://{BUCKET_NAME}/{database_name}.db"})
table = test_catalog.create_table((database_name, table_name), table_schema_nested, properties={"format-version": "1"})
assert table.format_version == 1
table_info = _glue.get_table(
DatabaseName=database_name,
Name=table_name,
)
storage_descriptor = table_info["Table"]["StorageDescriptor"]
columns = storage_descriptor["Columns"]
assert len(columns) == len(table_schema_nested.fields)
assert columns[0] == {
"Name": "foo",
"Type": "string",
"Parameters": {"iceberg.field.id": "1", "iceberg.field.optional": "true", "iceberg.field.current": "true"},
}
assert storage_descriptor["Location"] == f"s3://{BUCKET_NAME}/{database_name}.db/{table_name}"
@mock_aws
def test_create_table_with_default_warehouse(
_bucket_initialize: None, moto_endpoint_url: str, table_schema_nested: Schema, database_name: str, table_name: str
) -> None:
catalog_name = "glue"
identifier = (database_name, table_name)
test_catalog = GlueCatalog(catalog_name, **{"s3.endpoint": moto_endpoint_url, "warehouse": f"s3://{BUCKET_NAME}"})
test_catalog.create_namespace(namespace=database_name)
table = test_catalog.create_table(identifier, table_schema_nested)
assert table.name() == identifier
assert TABLE_METADATA_LOCATION_REGEX.match(table.metadata_location)
assert test_catalog._parse_metadata_version(table.metadata_location) == 0
@mock_aws
def test_create_table_with_given_location(
_bucket_initialize: None, moto_endpoint_url: str, table_schema_nested: Schema, database_name: str, table_name: str
) -> None:
catalog_name = "glue"
identifier = (database_name, table_name)
test_catalog = GlueCatalog(catalog_name, **{"s3.endpoint": moto_endpoint_url})
test_catalog.create_namespace(namespace=database_name)
table = test_catalog.create_table(
identifier=identifier, schema=table_schema_nested, location=f"s3://{BUCKET_NAME}/{database_name}.db/{table_name}"
)
assert table.name() == identifier
assert TABLE_METADATA_LOCATION_REGEX.match(table.metadata_location)
assert test_catalog._parse_metadata_version(table.metadata_location) == 0
@mock_aws
def test_create_table_removes_trailing_slash_in_location(
_bucket_initialize: None, moto_endpoint_url: str, table_schema_nested: Schema, database_name: str, table_name: str
) -> None:
catalog_name = "glue"
identifier = (database_name, table_name)
test_catalog = GlueCatalog(catalog_name, **{"s3.endpoint": moto_endpoint_url})
test_catalog.create_namespace(namespace=database_name)
location = f"s3://{BUCKET_NAME}/{database_name}.db/{table_name}"
table = test_catalog.create_table(identifier=identifier, schema=table_schema_nested, location=f"{location}/")
assert table.name() == identifier
assert table.location() == location
assert TABLE_METADATA_LOCATION_REGEX.match(table.metadata_location)
assert test_catalog._parse_metadata_version(table.metadata_location) == 0
@mock_aws
def test_create_table_with_pyarrow_schema(
_bucket_initialize: None,
moto_endpoint_url: str,
pyarrow_schema_simple_without_ids: pa.Schema,
database_name: str,
table_name: str,
) -> None:
catalog_name = "glue"
identifier = (database_name, table_name)
test_catalog = GlueCatalog(catalog_name, **{"s3.endpoint": moto_endpoint_url})
test_catalog.create_namespace(namespace=database_name)
table = test_catalog.create_table(
identifier=identifier,
schema=pyarrow_schema_simple_without_ids,
location=f"s3://{BUCKET_NAME}/{database_name}.db/{table_name}",
)
assert table.name() == identifier
assert TABLE_METADATA_LOCATION_REGEX.match(table.metadata_location)
assert test_catalog._parse_metadata_version(table.metadata_location) == 0
@mock_aws
def test_create_table_with_no_location(
_bucket_initialize: None, moto_endpoint_url: str, table_schema_nested: Schema, database_name: str, table_name: str
) -> None:
catalog_name = "glue"
identifier = (database_name, table_name)
test_catalog = GlueCatalog(catalog_name, **{"s3.endpoint": moto_endpoint_url})
test_catalog.create_namespace(namespace=database_name)
with pytest.raises(ValueError):
test_catalog.create_table(identifier=identifier, schema=table_schema_nested)
@mock_aws
def test_create_table_with_strips(
_bucket_initialize: None, moto_endpoint_url: str, table_schema_nested: Schema, database_name: str, table_name: str
) -> None:
catalog_name = "glue"
identifier = (database_name, table_name)
test_catalog = GlueCatalog(catalog_name, **{"s3.endpoint": moto_endpoint_url})
test_catalog.create_namespace(namespace=database_name, properties={"location": f"s3://{BUCKET_NAME}/{database_name}.db/"})
table = test_catalog.create_table(identifier, table_schema_nested)
assert table.name() == identifier
assert TABLE_METADATA_LOCATION_REGEX.match(table.metadata_location)
assert test_catalog._parse_metadata_version(table.metadata_location) == 0
@mock_aws
def test_create_table_with_strips_bucket_root(
_bucket_initialize: None, moto_endpoint_url: str, table_schema_nested: Schema, database_name: str, table_name: str
) -> None:
identifier = (database_name, table_name)
test_catalog = GlueCatalog("glue", **{"s3.endpoint": moto_endpoint_url, "warehouse": f"s3://{BUCKET_NAME}/"})
test_catalog.create_namespace(namespace=database_name)
table_strip = test_catalog.create_table(identifier, table_schema_nested)
assert table_strip.name() == identifier
assert TABLE_METADATA_LOCATION_REGEX.match(table_strip.metadata_location)
assert test_catalog._parse_metadata_version(table_strip.metadata_location) == 0
@mock_aws
def test_create_table_with_no_database(
_bucket_initialize: None, moto_endpoint_url: str, table_schema_nested: Schema, database_name: str, table_name: str
) -> None:
identifier = (database_name, table_name)
test_catalog = GlueCatalog("glue", **{"s3.endpoint": moto_endpoint_url})
with pytest.raises(NoSuchNamespaceError):
test_catalog.create_table(identifier=identifier, schema=table_schema_nested)
@mock_aws
def test_create_table_with_glue_catalog_id(
_bucket_initialize: None, moto_endpoint_url: str, table_schema_nested: Schema, database_name: str, table_name: str
) -> None:
catalog_name = "glue"
catalog_id = "444444444444"
identifier = (database_name, table_name)
test_catalog = GlueCatalog(
catalog_name, **{"s3.endpoint": moto_endpoint_url, "warehouse": f"s3://{BUCKET_NAME}", "glue.id": catalog_id}
)
test_catalog.create_namespace(namespace=database_name)
table = test_catalog.create_table(identifier, table_schema_nested)
assert table.name() == identifier
assert TABLE_METADATA_LOCATION_REGEX.match(table.metadata_location)
assert test_catalog._parse_metadata_version(table.metadata_location) == 0
glue = boto3.client("glue")
databases = glue.get_databases()
assert databases["DatabaseList"][0]["CatalogId"] == catalog_id
@mock_aws
def test_create_duplicated_table(
_bucket_initialize: None, moto_endpoint_url: str, table_schema_nested: Schema, database_name: str, table_name: str
) -> None:
identifier = (database_name, table_name)
test_catalog = GlueCatalog("glue", **{"s3.endpoint": moto_endpoint_url, "warehouse": f"s3://{BUCKET_NAME}/"})
test_catalog.create_namespace(namespace=database_name)
test_catalog.create_table(identifier, table_schema_nested)
with pytest.raises(TableAlreadyExistsError):
test_catalog.create_table(identifier, table_schema_nested)
@mock_aws
def test_create_table_transaction_s3tables(
monkeypatch: pytest.MonkeyPatch,
_bucket_initialize: None,
moto_endpoint_url: str,
table_schema_nested: Schema,
database_name: str,
table_name: str,
) -> None:
_patch_moto_for_s3tables(monkeypatch)
identifier = (database_name, table_name)
test_catalog = GlueCatalog("s3tables", **{"s3.endpoint": moto_endpoint_url})
_create_s3tables_database(test_catalog, database_name)
with test_catalog.create_table_transaction(
identifier,
table_schema_nested,
properties={"test_key": "test_value"},
):
pass
table = test_catalog.load_table(identifier)
assert table.name() == identifier
assert table.location().rstrip("/") == f"s3://{S3TABLES_WAREHOUSE_LOCATION}/{database_name}/{table_name}"
assert table.properties["test_key"] == "test_value"
@mock_aws
def test_create_table_transaction_s3tables_with_schema_evolution(
monkeypatch: pytest.MonkeyPatch,
_bucket_initialize: None,
moto_endpoint_url: str,
table_schema_nested: Schema,
database_name: str,
table_name: str,
) -> None:
_patch_moto_for_s3tables(monkeypatch)
identifier = (database_name, table_name)
test_catalog = GlueCatalog("s3tables", **{"s3.endpoint": moto_endpoint_url})
_create_s3tables_database(test_catalog, database_name)
with test_catalog.create_table_transaction(
identifier,
table_schema_nested,
) as txn:
with txn.update_schema() as update_schema:
update_schema.add_column(path="new_col", field_type=IntegerType())
table = test_catalog.load_table(identifier)
assert table.schema().find_field("new_col").field_type == IntegerType()
@mock_aws
def test_create_table_transaction_s3tables_rejects_location(
monkeypatch: pytest.MonkeyPatch,
_bucket_initialize: None,
moto_endpoint_url: str,
table_schema_nested: Schema,
database_name: str,
table_name: str,
) -> None:
_patch_moto_for_s3tables(monkeypatch)
identifier = (database_name, table_name)
test_catalog = GlueCatalog("s3tables", **{"s3.endpoint": moto_endpoint_url})
_create_s3tables_database(test_catalog, database_name)
with pytest.raises(ValueError, match="Cannot specify a location for S3 Tables table"):
test_catalog.create_table_transaction(identifier, table_schema_nested, location="s3://some-bucket/some-path")
@mock_aws
def test_create_table_transaction_s3tables_cleanup_on_exception(
monkeypatch: pytest.MonkeyPatch,
_bucket_initialize: None,
moto_endpoint_url: str,
table_schema_nested: Schema,
database_name: str,
table_name: str,
) -> None:
"""Staging table should be cleaned up if the transaction is not committed."""
_patch_moto_for_s3tables(monkeypatch)
identifier = (database_name, table_name)
test_catalog = GlueCatalog("s3tables", **{"s3.endpoint": moto_endpoint_url})
_create_s3tables_database(test_catalog, database_name)
with pytest.raises(RuntimeError, match="intentional"):
with test_catalog.create_table_transaction(
identifier,
table_schema_nested,
):
raise RuntimeError("intentional")
# The staging table should have been cleaned up, so creating the table again should work.
table = test_catalog.create_table(identifier, table_schema_nested) # type: ignore[unreachable]
assert table.name() == identifier
@mock_aws
def test_load_table(
_bucket_initialize: None, moto_endpoint_url: str, table_schema_nested: Schema, database_name: str, table_name: str
) -> None:
catalog_name = "glue"
identifier = (database_name, table_name)
test_catalog = GlueCatalog(catalog_name, **{"s3.endpoint": moto_endpoint_url, "warehouse": f"s3://{BUCKET_NAME}/"})
test_catalog.create_namespace(namespace=database_name)
test_catalog.create_table(identifier, table_schema_nested)
table = test_catalog.load_table(identifier)
assert table.name() == identifier
assert TABLE_METADATA_LOCATION_REGEX.match(table.metadata_location)
assert test_catalog._parse_metadata_version(table.metadata_location) == 0
@mock_aws
def test_load_table_from_self_identifier(
_bucket_initialize: None, moto_endpoint_url: str, table_schema_nested: Schema, database_name: str, table_name: str
) -> None:
catalog_name = "glue"
identifier = (database_name, table_name)
test_catalog = GlueCatalog(catalog_name, **{"s3.endpoint": moto_endpoint_url, "warehouse": f"s3://{BUCKET_NAME}/"})
test_catalog.create_namespace(namespace=database_name)
intermediate = test_catalog.create_table(identifier, table_schema_nested)
table = test_catalog.load_table(intermediate.name())
assert table.name() == identifier
assert TABLE_METADATA_LOCATION_REGEX.match(table.metadata_location)
@mock_aws
def test_load_non_exist_table(_bucket_initialize: None, moto_endpoint_url: str, database_name: str, table_name: str) -> None:
identifier = (database_name, table_name)
test_catalog = GlueCatalog("glue", **{"s3.endpoint": moto_endpoint_url, "warehouse": f"s3://{BUCKET_NAME}/"})
test_catalog.create_namespace(namespace=database_name)
with pytest.raises(NoSuchTableError):
test_catalog.load_table(identifier)
@mock_aws
def test_drop_table(
_bucket_initialize: None, moto_endpoint_url: str, table_schema_nested: Schema, database_name: str, table_name: str
) -> None:
catalog_name = "glue"
identifier = (database_name, table_name)
test_catalog = GlueCatalog(catalog_name, **{"s3.endpoint": moto_endpoint_url, "warehouse": f"s3://{BUCKET_NAME}/"})
test_catalog.create_namespace(namespace=database_name)
test_catalog.create_table(identifier, table_schema_nested)
table = test_catalog.load_table(identifier)
assert table.name() == identifier
assert TABLE_METADATA_LOCATION_REGEX.match(table.metadata_location)
test_catalog.drop_table(identifier)
with pytest.raises(NoSuchTableError):
test_catalog.load_table(identifier)
@mock_aws
def test_drop_table_from_self_identifier(
_bucket_initialize: None, moto_endpoint_url: str, table_schema_nested: Schema, database_name: str, table_name: str
) -> None:
catalog_name = "glue"
identifier = (database_name, table_name)
test_catalog = GlueCatalog(catalog_name, **{"s3.endpoint": moto_endpoint_url, "warehouse": f"s3://{BUCKET_NAME}/"})
test_catalog.create_namespace(namespace=database_name)
test_catalog.create_table(identifier, table_schema_nested)
table = test_catalog.load_table(identifier)
assert table.name() == identifier
assert TABLE_METADATA_LOCATION_REGEX.match(table.metadata_location)
test_catalog.drop_table(table.name())
with pytest.raises(NoSuchTableError):
test_catalog.load_table(identifier)
with pytest.raises(NoSuchTableError):
test_catalog.load_table(table.name())
@mock_aws
def test_drop_non_exist_table(_bucket_initialize: None, moto_endpoint_url: str, database_name: str, table_name: str) -> None:
identifier = (database_name, table_name)
test_catalog = GlueCatalog("glue", **{"s3.endpoint": moto_endpoint_url, "warehouse": f"s3://{BUCKET_NAME}/"})
with pytest.raises(NoSuchTableError):
test_catalog.drop_table(identifier)
@mock_aws
def test_rename_table(
_bucket_initialize: None, moto_endpoint_url: str, table_schema_nested: Schema, database_name: str, table_name: str
) -> None:
new_table_name = f"{table_name}_new"
identifier = (database_name, table_name)
new_identifier = (database_name, new_table_name)
test_catalog = GlueCatalog("glue", **{"s3.endpoint": moto_endpoint_url, "warehouse": f"s3://{BUCKET_NAME}/"})
test_catalog.create_namespace(namespace=database_name)
table = test_catalog.create_table(identifier, table_schema_nested)
assert table.name() == identifier
assert TABLE_METADATA_LOCATION_REGEX.match(table.metadata_location)
assert test_catalog._parse_metadata_version(table.metadata_location) == 0
test_catalog.rename_table(identifier, new_identifier)
new_table = test_catalog.load_table(new_identifier)
assert new_table.name() == new_identifier
# the metadata_location should not change
assert new_table.metadata_location == table.metadata_location
# old table should be dropped
with pytest.raises(NoSuchTableError):
test_catalog.load_table(identifier)
@mock_aws
def test_rename_table_from_self_identifier(
_bucket_initialize: None, moto_endpoint_url: str, table_schema_nested: Schema, database_name: str, table_name: str
) -> None:
new_table_name = f"{table_name}_new"
identifier = (database_name, table_name)
new_identifier = (database_name, new_table_name)
test_catalog = GlueCatalog("glue", **{"s3.endpoint": moto_endpoint_url, "warehouse": f"s3://{BUCKET_NAME}/"})
test_catalog.create_namespace(namespace=database_name)
table = test_catalog.create_table(identifier, table_schema_nested)
assert table.name() == identifier
assert TABLE_METADATA_LOCATION_REGEX.match(table.metadata_location)
test_catalog.rename_table(table.name(), new_identifier)
new_table = test_catalog.load_table(new_identifier)
assert new_table.name() == new_identifier
# the metadata_location should not change
assert new_table.metadata_location == table.metadata_location
# old table should be dropped
with pytest.raises(NoSuchTableError):
test_catalog.load_table(identifier)
with pytest.raises(NoSuchTableError):
test_catalog.load_table(table.name())
@mock_aws
def test_rename_table_no_params(
_glue: boto3.client, _bucket_initialize: None, moto_endpoint_url: str, database_name: str, table_name: str
) -> None:
new_database_name = f"{database_name}_new"
new_table_name = f"{table_name}_new"
identifier = (database_name, table_name)
new_identifier = (new_database_name, new_table_name)
test_catalog = GlueCatalog("glue", **{"s3.endpoint": moto_endpoint_url, "warehouse": f"s3://{BUCKET_NAME}/"})
test_catalog.create_namespace(namespace=database_name)
test_catalog.create_namespace(namespace=new_database_name)
_glue.create_table(
DatabaseName=database_name,
TableInput={"Name": table_name, "TableType": "EXTERNAL_TABLE", "Parameters": {"table_type": "iceberg"}},
)
with pytest.raises(NoSuchPropertyException):
test_catalog.rename_table(identifier, new_identifier)
@mock_aws
def test_rename_non_iceberg_table(
_glue: boto3.client, _bucket_initialize: None, moto_endpoint_url: str, database_name: str, table_name: str
) -> None:
new_database_name = f"{database_name}_new"
new_table_name = f"{table_name}_new"
identifier = (database_name, table_name)
new_identifier = (new_database_name, new_table_name)
test_catalog = GlueCatalog("glue", **{"s3.endpoint": moto_endpoint_url, "warehouse": f"s3://{BUCKET_NAME}/"})
test_catalog.create_namespace(namespace=database_name)
test_catalog.create_namespace(namespace=new_database_name)
_glue.create_table(
DatabaseName=database_name,
TableInput={
"Name": table_name,
"TableType": "EXTERNAL_TABLE",
"Parameters": {"table_type": "noniceberg", "metadata_location": "test"},
},
)
with pytest.raises(NoSuchIcebergTableError):
test_catalog.rename_table(identifier, new_identifier)
@mock_aws
def test_list_tables(
_bucket_initialize: None,
moto_endpoint_url: str,
table_schema_nested: Schema,
database_name: str,
table_list: list[str],
) -> None:
test_catalog = GlueCatalog("glue", **{"s3.endpoint": moto_endpoint_url, "warehouse": f"s3://{BUCKET_NAME}/"})
test_catalog.create_namespace(namespace=database_name)
non_iceberg_table_name = "non_iceberg_table"
non_table_type_table_name = "non_table_type_table"
glue_client = boto3.client("glue", endpoint_url=moto_endpoint_url)
glue_client.create_table(
DatabaseName=database_name,
TableInput={
"Name": non_iceberg_table_name,
"TableType": "EXTERNAL_TABLE",
"Parameters": {"table_type": "noniceberg"},
},
)
glue_client.create_table(
DatabaseName=database_name,
TableInput={
"Name": non_table_type_table_name,
"TableType": "OTHER_TABLE_TYPE",
"Parameters": {},
},
)
for table_name in table_list:
test_catalog.create_table((database_name, table_name), table_schema_nested)
loaded_table_list = test_catalog.list_tables(database_name)
assert (database_name, non_iceberg_table_name) not in loaded_table_list
assert (database_name, non_table_type_table_name) not in loaded_table_list
for table_name in table_list:
assert (database_name, table_name) in loaded_table_list
@mock_aws
def test_list_namespaces(_bucket_initialize: None, moto_endpoint_url: str, database_list: list[str]) -> None:
test_catalog = GlueCatalog("glue", **{"s3.endpoint": moto_endpoint_url})
for database_name in database_list:
test_catalog.create_namespace(namespace=database_name)
loaded_database_list = test_catalog.list_namespaces()
for database_name in database_list:
assert (database_name,) in loaded_database_list
@mock_aws
def test_create_namespace_no_properties(_bucket_initialize: None, moto_endpoint_url: str, database_name: str) -> None:
test_catalog = GlueCatalog("glue", **{"s3.endpoint": moto_endpoint_url})
test_catalog.create_namespace(namespace=database_name)
loaded_database_list = test_catalog.list_namespaces()
assert len(loaded_database_list) == 1
assert (database_name,) in loaded_database_list
properties = test_catalog.load_namespace_properties(database_name)
assert properties == {}
@mock_aws
def test_create_namespace_with_comment_and_location(_bucket_initialize: None, moto_endpoint_url: str, database_name: str) -> None:
test_location = f"s3://{BUCKET_NAME}/{database_name}.db"
test_properties = {
"comment": "this is a test description",
"location": test_location,
}
test_catalog = GlueCatalog("glue", **{"s3.endpoint": moto_endpoint_url})
test_catalog.create_namespace(namespace=database_name, properties=test_properties)
loaded_database_list = test_catalog.list_namespaces()
assert len(loaded_database_list) == 1
assert (database_name,) in loaded_database_list
properties = test_catalog.load_namespace_properties(database_name)
assert properties["comment"] == "this is a test description"
assert properties["location"] == test_location
@mock_aws
def test_create_duplicated_namespace(_bucket_initialize: None, moto_endpoint_url: str, database_name: str) -> None:
test_catalog = GlueCatalog("glue", **{"s3.endpoint": moto_endpoint_url})
test_catalog.create_namespace(namespace=database_name)
loaded_database_list = test_catalog.list_namespaces()
assert len(loaded_database_list) == 1
assert (database_name,) in loaded_database_list
with pytest.raises(NamespaceAlreadyExistsError):
test_catalog.create_namespace(namespace=database_name, properties={"test": "test"})
@mock_aws
def test_drop_namespace(_bucket_initialize: None, moto_endpoint_url: str, database_name: str) -> None:
test_catalog = GlueCatalog("glue", **{"s3.endpoint": moto_endpoint_url})
test_catalog.create_namespace(namespace=database_name)
loaded_database_list = test_catalog.list_namespaces()
assert len(loaded_database_list) == 1
assert (database_name,) in loaded_database_list
test_catalog.drop_namespace(database_name)
loaded_database_list = test_catalog.list_namespaces()
assert len(loaded_database_list) == 0
@mock_aws
def test_drop_non_empty_namespace(
_bucket_initialize: None, moto_endpoint_url: str, table_schema_nested: Schema, database_name: str, table_name: str
) -> None:
identifier = (database_name, table_name)
test_catalog = GlueCatalog("glue", **{"s3.endpoint": moto_endpoint_url, "warehouse": f"s3://{BUCKET_NAME}/"})
test_catalog.create_namespace(namespace=database_name)
test_catalog.create_table(identifier, table_schema_nested)
assert len(test_catalog.list_tables(database_name)) == 1
with pytest.raises(NamespaceNotEmptyError):
test_catalog.drop_namespace(database_name)
@mock_aws
def test_drop_namespace_that_contains_non_iceberg_tables(
_bucket_initialize: None, moto_endpoint_url: str, table_schema_nested: Schema, database_name: str, table_name: str
) -> None:
test_catalog = GlueCatalog("glue", **{"s3.endpoint": moto_endpoint_url, "warehouse": f"s3://{BUCKET_NAME}/"})
test_catalog.create_namespace(namespace=database_name)
test_catalog.glue.create_table(DatabaseName=database_name, TableInput={"Name": "hive_table"})
with pytest.raises(NamespaceNotEmptyError):
test_catalog.drop_namespace(database_name)
@mock_aws
def test_drop_non_exist_namespace(_bucket_initialize: None, moto_endpoint_url: str, database_name: str) -> None:
test_catalog = GlueCatalog("glue", **{"s3.endpoint": moto_endpoint_url})
with pytest.raises(NoSuchNamespaceError):
test_catalog.drop_namespace(database_name)
@mock_aws
def test_load_namespace_properties(_bucket_initialize: None, moto_endpoint_url: str, database_name: str) -> None:
test_location = f"s3://{BUCKET_NAME}/{database_name}.db"
test_properties = {
"comment": "this is a test description",
"location": test_location,
"test_property1": "1",
"test_property2": "2",
"test_property3": "3",
}
test_catalog = GlueCatalog("glue", **{"s3.endpoint": moto_endpoint_url})
test_catalog.create_namespace(database_name, test_properties)
listed_properties = test_catalog.load_namespace_properties(database_name)
for k, v in listed_properties.items():
assert k in test_properties
assert v == test_properties[k]
@mock_aws
def test_load_non_exist_namespace_properties(_bucket_initialize: None, moto_endpoint_url: str, database_name: str) -> None:
test_catalog = GlueCatalog("glue", **{"s3.endpoint": moto_endpoint_url})
with pytest.raises(NoSuchNamespaceError):
test_catalog.load_namespace_properties(database_name)
@mock_aws
def test_update_namespace_properties(_bucket_initialize: None, moto_endpoint_url: str, database_name: str) -> None:
test_properties = {
"comment": "this is a test description",
"location": f"s3://{BUCKET_NAME}/{database_name}.db",
"test_property1": "1",
"test_property2": "2",
"test_property3": "3",
}
removals = {"test_property1", "test_property2", "test_property3", "should_not_removed"}
updates = {"test_property4": "4", "test_property5": "5", "comment": "updated test description"}
test_catalog = GlueCatalog("glue", **{"s3.endpoint": moto_endpoint_url})
test_catalog.create_namespace(database_name, test_properties)
update_report = test_catalog.update_namespace_properties(database_name, removals, updates)
for k in updates.keys():
assert k in update_report.updated
for k in removals:
if k == "should_not_removed":
assert k in update_report.missing
else:
assert k in update_report.removed
assert "updated test description" == test_catalog.load_namespace_properties(database_name)["comment"]
test_catalog.drop_namespace(database_name)
@mock_aws
def test_load_empty_namespace_properties(_bucket_initialize: None, moto_endpoint_url: str, database_name: str) -> None:
test_catalog = GlueCatalog("glue", **{"s3.endpoint": moto_endpoint_url})
test_catalog.create_namespace(database_name)
listed_properties = test_catalog.load_namespace_properties(database_name)
assert listed_properties == {}
@mock_aws
def test_load_default_namespace_properties(_glue, _bucket_initialize: None, moto_endpoint_url: str, database_name: str) -> None: # type: ignore
# simulate creating database with default settings through AWS Glue Web Console
_glue.create_database(DatabaseInput={"Name": database_name})
test_catalog = GlueCatalog("glue", **{"s3.endpoint": moto_endpoint_url})
listed_properties = test_catalog.load_namespace_properties(database_name)
assert listed_properties == {}
@mock_aws
def test_update_namespace_properties_overlap_update_removal(
_bucket_initialize: None, moto_endpoint_url: str, database_name: str
) -> None:
test_properties = {
"comment": "this is a test description",
"location": f"s3://{BUCKET_NAME}/{database_name}.db",
"test_property1": "1",
"test_property2": "2",
"test_property3": "3",
}
removals = {"test_property1", "test_property2", "test_property3", "should_not_removed"}
updates = {"test_property1": "4", "test_property5": "5", "comment": "updated test description"}
test_catalog = GlueCatalog("glue", **{"s3.endpoint": moto_endpoint_url})
test_catalog.create_namespace(database_name, test_properties)
with pytest.raises(ValueError):
test_catalog.update_namespace_properties(database_name, removals, updates)
# should not modify the properties
assert test_catalog.load_namespace_properties(database_name) == test_properties
@mock_aws
def test_passing_glue_session_properties() -> None:
session_properties: Properties = {
"glue.access-key-id": "glue.access-key-id",
"glue.secret-access-key": "glue.secret-access-key",
"glue.profile-name": "glue.profile-name",
"glue.region": "glue.region",
"glue.session-token": "glue.session-token",
**UNIFIED_AWS_SESSION_PROPERTIES,
}
with mock.patch("boto3.Session") as mock_session:
test_catalog = GlueCatalog("glue", **session_properties)
mock_session.assert_called_with(
aws_access_key_id="glue.access-key-id",
aws_secret_access_key="glue.secret-access-key",
aws_session_token="glue.session-token",
region_name="glue.region",
profile_name="glue.profile-name",
botocore_session=None,
)
assert test_catalog.glue is mock_session().client()
@mock_aws
def test_passing_unified_session_properties_to_glue() -> None:
session_properties: Properties = {
"glue.profile-name": "glue.profile-name",
**UNIFIED_AWS_SESSION_PROPERTIES,
}
with mock.patch("boto3.Session") as mock_session:
test_catalog = GlueCatalog("glue", **session_properties)
mock_session.assert_called_with(
aws_access_key_id="client.access-key-id",
aws_secret_access_key="client.secret-access-key",
aws_session_token="client.session-token",
region_name="client.region",
profile_name="glue.profile-name",
botocore_session=None,
)
assert test_catalog.glue is mock_session().client()
@mock_aws
def test_commit_table_update_schema(
_glue: boto3.client,
_bucket_initialize: None,
moto_endpoint_url: str,
table_schema_nested: Schema,
database_name: str,
table_name: str,
) -> None:
catalog_name = "glue"
identifier = (database_name, table_name)
test_catalog = GlueCatalog(catalog_name, **{"s3.endpoint": moto_endpoint_url, "warehouse": f"s3://{BUCKET_NAME}"})
test_catalog.create_namespace(namespace=database_name)
table = test_catalog.create_table(identifier, table_schema_nested)
original_table_metadata = table.metadata
original_table_metadata_location = table.metadata_location
original_table_last_updated_ms = table.metadata.last_updated_ms
assert TABLE_METADATA_LOCATION_REGEX.match(original_table_metadata_location)
assert test_catalog._parse_metadata_version(original_table_metadata_location) == 0
assert original_table_metadata.current_schema_id == 0
assert len(original_table_metadata.metadata_log) == 0
transaction = table.transaction()
update = transaction.update_schema()
update.add_column(path="b", field_type=IntegerType())
update.commit()
transaction.commit_transaction()
updated_table_metadata = table.metadata
assert TABLE_METADATA_LOCATION_REGEX.match(table.metadata_location)
assert test_catalog._parse_metadata_version(table.metadata_location) == 1
assert updated_table_metadata.current_schema_id == 1
assert len(updated_table_metadata.schemas) == 2
new_schema = next(schema for schema in updated_table_metadata.schemas if schema.schema_id == 1)
assert new_schema
assert new_schema == update._apply()
assert new_schema.find_field("b").field_type == IntegerType()
assert len(updated_table_metadata.metadata_log) == 1
assert updated_table_metadata.metadata_log[0].metadata_file == original_table_metadata_location
assert updated_table_metadata.metadata_log[0].timestamp_ms == original_table_last_updated_ms
# Ensure schema is also pushed to Glue
table_info = _glue.get_table(
DatabaseName=database_name,
Name=table_name,
)
storage_descriptor = table_info["Table"]["StorageDescriptor"]
columns = storage_descriptor["Columns"]
assert len(columns) == len(table_schema_nested.fields) + 1
assert columns[-1] == {
"Name": "b",
"Type": "int",
"Parameters": {"iceberg.field.id": "18", "iceberg.field.optional": "true", "iceberg.field.current": "true"},
}
assert storage_descriptor["Location"] == f"s3://{BUCKET_NAME}/{database_name}.db/{table_name}"
@mock_aws
def test_commit_table_properties(
_glue: boto3.client,
_bucket_initialize: None,
moto_endpoint_url: str,
table_schema_nested: Schema,
database_name: str,
table_name: str,
) -> None:
catalog_name = "glue"
identifier = (database_name, table_name)
test_catalog = GlueCatalog(catalog_name, **{"s3.endpoint": moto_endpoint_url, "warehouse": f"s3://{BUCKET_NAME}"})
test_catalog.create_namespace(namespace=database_name)
table = test_catalog.create_table(identifier=identifier, schema=table_schema_nested, properties={"test_a": "test_a"})
assert test_catalog._parse_metadata_version(table.metadata_location) == 0
transaction = table.transaction()
transaction.set_properties(test_a="test_aa", test_b="test_b", test_c="test_c", Description="test_description")
transaction.remove_properties("test_b")
transaction.commit_transaction()
updated_table_metadata = table.metadata
assert test_catalog._parse_metadata_version(table.metadata_location) == 1
assert updated_table_metadata.properties == {"Description": "test_description", "test_a": "test_aa", "test_c": "test_c"}
table_info = _glue.get_table(
DatabaseName=database_name,
Name=table_name,
)
assert table_info["Table"]["Description"] == "test_description"
assert table_info["Table"]["Parameters"]["test_a"] == "test_aa"
assert table_info["Table"]["Parameters"]["test_c"] == "test_c"
@mock_aws
def test_commit_append_table_snapshot_properties(
_bucket_initialize: None, moto_endpoint_url: str, table_schema_simple: Schema, database_name: str, table_name: str
) -> None:
catalog_name = "glue"
identifier = (database_name, table_name)
test_catalog = GlueCatalog(catalog_name, **{"s3.endpoint": moto_endpoint_url, "warehouse": f"s3://{BUCKET_NAME}"})
test_catalog.create_namespace(namespace=database_name)
table = test_catalog.create_table(identifier=identifier, schema=table_schema_simple)
assert test_catalog._parse_metadata_version(table.metadata_location) == 0
table.append(
pa.Table.from_pylist(
[{"foo": "foo_val", "bar": 1, "baz": False}],
schema=schema_to_pyarrow(table_schema_simple),
),
snapshot_properties={"snapshot_prop_a": "test_prop_a"},
)
updated_table_metadata = table.metadata
summary = updated_table_metadata.snapshots[-1].summary
assert test_catalog._parse_metadata_version(table.metadata_location) == 1
assert summary is not None
assert summary["snapshot_prop_a"] == "test_prop_a"
@mock_aws
def test_commit_overwrite_table_snapshot_properties(
_bucket_initialize: None, moto_endpoint_url: str, table_schema_simple: Schema, database_name: str, table_name: str
) -> None:
catalog_name = "glue"
identifier = (database_name, table_name)
test_catalog = GlueCatalog(catalog_name, **{"s3.endpoint": moto_endpoint_url, "warehouse": f"s3://{BUCKET_NAME}"})
test_catalog.create_namespace(namespace=database_name)
table = test_catalog.create_table(identifier=identifier, schema=table_schema_simple)
assert test_catalog._parse_metadata_version(table.metadata_location) == 0
table.append(
pa.Table.from_pylist(
[{"foo": "foo_val", "bar": 1, "baz": False}],
schema=schema_to_pyarrow(table_schema_simple),
),
snapshot_properties={"snapshot_prop_a": "test_prop_a"},
)
assert test_catalog._parse_metadata_version(table.metadata_location) == 1
table.overwrite(
pa.Table.from_pylist(
[{"foo": "foo_val", "bar": 2, "baz": True}],