forked from apache/iceberg-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_s3tables.py
332 lines (250 loc) · 13.3 KB
/
test_s3tables.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
# 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.
import string
from random import choice
import boto3
import pytest
from pyiceberg.catalog import load_catalog
from pyiceberg.catalog.s3tables import S3TablesCatalog
from pyiceberg.exceptions import NoSuchNamespaceError, NoSuchTableError, TableBucketNotFound
from pyiceberg.partitioning import PartitionField, PartitionSpec
from pyiceberg.schema import Schema
from pyiceberg.transforms import IdentityTransform
from pyiceberg.types import IntegerType
@pytest.fixture
def database_name(database_name: str) -> str:
# naming rules prevent "-" in namespaces for s3 table buckets
return database_name.replace("-", "_")
@pytest.fixture
def table_name(table_name: str) -> str:
# naming rules prevent "-" in table namees for s3 table buckets
return table_name.replace("-", "_")
@pytest.fixture()
def aws_region(_aws_credentials: None) -> str:
return "us-east-1"
@pytest.fixture
def table_bucket_arn(monkeypatch: pytest.MonkeyPatch, moto_endpoint_url: str, aws_region: str) -> str:
monkeypatch.setenv("AWS_ENDPOINT_URL", moto_endpoint_url)
prefix = "pyiceberg-table-bucket-"
random_tag = "".join(choice(string.ascii_letters) for _ in range(12))
name = (prefix + random_tag).lower()
table_bucket_arn = boto3.client("s3tables", endpoint_url=moto_endpoint_url, region_name=aws_region).create_table_bucket(
name=name
)["arn"]
return table_bucket_arn
@pytest.fixture(params=["pyiceberg.io.fsspec.FsspecFileIO", "pyiceberg.io.pyarrow.PyArrowFileIO"])
def file_io_impl(request: pytest.FixtureRequest) -> str:
return request.param
@pytest.fixture
def catalog(table_bucket_arn: str, aws_region: str, moto_endpoint_url: str, file_io_impl: str) -> S3TablesCatalog:
properties = {
"s3tables.warehouse": table_bucket_arn,
"s3tables.region": aws_region,
"py-io-impl": file_io_impl,
"s3tables.endpoint": moto_endpoint_url,
"s3.endpoint": moto_endpoint_url,
}
return S3TablesCatalog(name="test_s3tables_catalog", **properties)
def test_load_catalog(table_bucket_arn: str, aws_region: str, moto_endpoint_url: str) -> None:
properties = {
"type": "s3tables",
"s3tables.warehouse": table_bucket_arn,
"s3tables.region": aws_region,
"py-io-impl": "pyiceberg.io.pyarrow.PyArrowFileIO",
"s3tables.endpoint": moto_endpoint_url,
}
catalog = load_catalog(**properties)
assert isinstance(catalog, S3TablesCatalog)
def test_creating_catalog_validates_s3_table_bucket_exists(table_bucket_arn: str, aws_region: str) -> None:
properties = {"s3tables.warehouse": f"{table_bucket_arn}-modified", "s3tables.region": aws_region}
with pytest.raises(TableBucketNotFound):
S3TablesCatalog(name="test_s3tables_catalog", **properties)
def test_create_namespace(catalog: S3TablesCatalog, database_name: str) -> None:
catalog.create_namespace(namespace=database_name)
namespaces = catalog.list_namespaces()
assert (database_name,) in namespaces
def test_load_namespace_properties(catalog: S3TablesCatalog, database_name: str) -> None:
catalog.create_namespace(namespace=database_name)
assert database_name in catalog.load_namespace_properties(database_name)["namespace"]
def test_load_namespace_properties_for_invalid_namespace_raises_exception(catalog: S3TablesCatalog, database_name: str) -> None:
with pytest.raises(NoSuchNamespaceError):
catalog.load_namespace_properties(database_name)
def test_drop_namespace(catalog: S3TablesCatalog, database_name: str) -> None:
catalog.create_namespace(namespace=database_name)
assert (database_name,) in catalog.list_namespaces()
catalog.drop_namespace(namespace=database_name)
assert (database_name,) not in catalog.list_namespaces()
def test_create_table(catalog: S3TablesCatalog, database_name: str, table_name: str, table_schema_nested: Schema) -> None:
identifier = (database_name, table_name)
catalog.create_namespace(namespace=database_name)
table = catalog.create_table(identifier=identifier, schema=table_schema_nested)
assert table == catalog.load_table(identifier)
def test_create_table_in_invalid_namespace_raises_exception(
catalog: S3TablesCatalog, database_name: str, table_name: str, table_schema_nested: Schema
) -> None:
identifier = (database_name, table_name)
with pytest.raises(NoSuchNamespaceError):
catalog.create_table(identifier=identifier, schema=table_schema_nested)
def test_table_exists(catalog: S3TablesCatalog, database_name: str, table_name: str, table_schema_nested: Schema) -> None:
identifier = (database_name, table_name)
catalog.create_namespace(namespace=database_name)
assert not catalog.table_exists(identifier=identifier)
catalog.create_table(identifier=identifier, schema=table_schema_nested)
assert catalog.table_exists(identifier=identifier)
def test_rename_table(catalog: S3TablesCatalog, database_name: str, table_name: str, table_schema_nested: Schema) -> None:
identifier = (database_name, table_name)
catalog.create_namespace(namespace=database_name)
catalog.create_table(identifier=identifier, schema=table_schema_nested)
to_database_name = f"{database_name}new"
to_table_name = f"{table_name}new"
to_identifier = (to_database_name, to_table_name)
catalog.create_namespace(namespace=to_database_name)
catalog.rename_table(from_identifier=identifier, to_identifier=to_identifier)
assert not catalog.table_exists(identifier=identifier)
assert catalog.table_exists(identifier=to_identifier)
def test_list_tables(catalog: S3TablesCatalog, database_name: str, table_name: str, table_schema_nested: Schema) -> None:
identifier = (database_name, table_name)
catalog.create_namespace(namespace=database_name)
assert not catalog.list_tables(namespace=database_name)
catalog.create_table(identifier=identifier, schema=table_schema_nested)
assert catalog.list_tables(namespace=database_name)
def test_list_tables_for_invalid_namespace_raises_exception(catalog: S3TablesCatalog, database_name: str) -> None:
with pytest.raises(NoSuchNamespaceError):
catalog.list_tables(namespace=database_name)
def test_delete_table_raises_not_implemented(
catalog: S3TablesCatalog, database_name: str, table_name: str, table_schema_nested: Schema
) -> None:
identifier = (database_name, table_name)
catalog.create_namespace(namespace=database_name)
catalog.create_table(identifier=identifier, schema=table_schema_nested)
with pytest.raises(NotImplementedError) as exc:
catalog.drop_table(identifier=identifier)
exc.match("S3 Tables does not support the delete_table operation. You can retry with the purge_table operation.")
def test_purge_table(catalog: S3TablesCatalog, database_name: str, table_name: str, table_schema_nested: Schema) -> None:
identifier = (database_name, table_name)
catalog.create_namespace(namespace=database_name)
catalog.create_table(identifier=identifier, schema=table_schema_nested)
catalog.purge_table(identifier=identifier)
with pytest.raises(NoSuchTableError):
catalog.load_table(identifier=identifier)
def test_commit_new_column_to_table(
catalog: S3TablesCatalog, database_name: str, table_name: str, table_schema_nested: Schema
) -> None:
identifier = (database_name, table_name)
catalog.create_namespace(namespace=database_name)
table = catalog.create_table(identifier=identifier, schema=table_schema_nested)
last_updated_ms = table.metadata.last_updated_ms
original_table_metadata_location = table.metadata_location
original_table_last_updated_ms = table.metadata.last_updated_ms
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 updated_table_metadata.current_schema_id == 1
assert len(updated_table_metadata.schemas) == 2
assert updated_table_metadata.last_updated_ms > last_updated_ms
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
assert table.schema().columns[-1].name == "b"
def test_write_pyarrow_table(catalog: S3TablesCatalog, database_name: str, table_name: str) -> None:
identifier = (database_name, table_name)
catalog.create_namespace(namespace=database_name)
import pyarrow as pa
pyarrow_table = pa.Table.from_arrays(
[
pa.array([None, "A", "B", "C"]), # 'foo' column
pa.array([1, 2, 3, 4]), # 'bar' column
pa.array([True, None, False, True]), # 'baz' column
pa.array([None, "A", "B", "C"]), # 'large' column
],
schema=pa.schema(
[
pa.field("foo", pa.large_string(), nullable=True),
pa.field("bar", pa.int32(), nullable=False),
pa.field("baz", pa.bool_(), nullable=True),
pa.field("large", pa.large_string(), nullable=True),
]
),
)
table = catalog.create_table(identifier=identifier, schema=pyarrow_table.schema)
table.append(pyarrow_table)
assert table.scan().to_arrow().num_rows == pyarrow_table.num_rows
def test_commit_new_data_to_table(catalog: S3TablesCatalog, database_name: str, table_name: str) -> None:
identifier = (database_name, table_name)
catalog.create_namespace(namespace=database_name)
import pyarrow as pa
pyarrow_table = pa.Table.from_arrays(
[
pa.array([None, "A", "B", "C"]), # 'foo' column
pa.array([1, 2, 3, 4]), # 'bar' column
pa.array([True, None, False, True]), # 'baz' column
pa.array([None, "A", "B", "C"]), # 'large' column
],
schema=pa.schema(
[
pa.field("foo", pa.large_string(), nullable=True),
pa.field("bar", pa.int32(), nullable=False),
pa.field("baz", pa.bool_(), nullable=True),
pa.field("large", pa.large_string(), nullable=True),
]
),
)
table = catalog.create_table(identifier=identifier, schema=pyarrow_table.schema)
table.append(pyarrow_table)
row_count = table.scan().to_arrow().num_rows
assert row_count
last_updated_ms = table.metadata.last_updated_ms
original_table_metadata_location = table.metadata_location
original_table_last_updated_ms = table.metadata.last_updated_ms
transaction = table.transaction()
transaction.append(table.scan().to_arrow())
transaction.commit_transaction()
updated_table_metadata = table.metadata
assert updated_table_metadata.last_updated_ms > last_updated_ms
assert updated_table_metadata.metadata_log[-1].metadata_file == original_table_metadata_location
assert updated_table_metadata.metadata_log[-1].timestamp_ms == original_table_last_updated_ms
assert table.scan().to_arrow().num_rows == 2 * row_count
@pytest.mark.xfail(raises=NotImplementedError)
def test_create_table_transaction(
catalog: S3TablesCatalog, database_name: str, table_name: str, table_schema_nested: str
) -> None:
identifier = (database_name, table_name)
catalog.create_namespace(namespace=database_name)
with catalog.create_table_transaction(
identifier,
table_schema_nested,
partition_spec=PartitionSpec(PartitionField(source_id=1, field_id=1000, transform=IdentityTransform(), name="foo")),
) as txn:
last_updated_metadata = txn.table_metadata.last_updated_ms
with txn.update_schema() as update_schema:
update_schema.add_column(path="b", field_type=IntegerType())
with txn.update_spec() as update_spec:
update_spec.add_identity("bar")
txn.set_properties(test_a="test_aa", test_b="test_b", test_c="test_c")
table = catalog.load_table(identifier)
assert table.schema().find_field("b").field_type == IntegerType()
assert table.properties == {"test_a": "test_aa", "test_b": "test_b", "test_c": "test_c"}
assert table.spec().last_assigned_field_id == 1001
assert table.spec().fields_by_source_id(1)[0].name == "foo"
assert table.spec().fields_by_source_id(1)[0].field_id == 1000
assert table.spec().fields_by_source_id(1)[0].transform == IdentityTransform()
assert table.spec().fields_by_source_id(2)[0].name == "bar"
assert table.spec().fields_by_source_id(2)[0].field_id == 1001
assert table.spec().fields_by_source_id(2)[0].transform == IdentityTransform()
assert table.metadata.last_updated_ms > last_updated_metadata