|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +"""Integration tests for Trino engine.""" |
| 18 | + |
| 19 | +import uuid |
| 20 | + |
| 21 | +import pyarrow as pa |
| 22 | +import pytest |
| 23 | +from sqlalchemy import Connection, inspect, text |
| 24 | +from sqlalchemy.engine import Engine |
| 25 | + |
| 26 | +from pyiceberg.catalog import Catalog |
| 27 | +from pyiceberg.catalog.rest import RestCatalog |
| 28 | +from pyiceberg.exceptions import NoSuchTableError |
| 29 | +from pyiceberg.partitioning import PartitionField, PartitionSpec |
| 30 | +from pyiceberg.schema import Schema |
| 31 | +from pyiceberg.transforms import BucketTransform, IdentityTransform, Transform |
| 32 | +from pyiceberg.types import NestedField, UUIDType |
| 33 | + |
| 34 | +TEST_NAMESPACE = "test_trino_namespace" |
| 35 | +TEST_NAMESPACE_IDENTIFIER = (TEST_NAMESPACE,) |
| 36 | + |
| 37 | + |
| 38 | +@pytest.mark.trino |
| 39 | +def test_schema_exists_in_trino(trino_rest_conn: Connection, catalog: RestCatalog) -> None: |
| 40 | + """Verifies that an Iceberg namespace correctly appears as a schema in Trino. |
| 41 | +
|
| 42 | + This test ensures the synchronization between Iceberg's namespace concept and |
| 43 | + Trino's schema concept, confirming that after creating a namespace in the Iceberg |
| 44 | + catalog, it becomes visible as a schema in the Trino environment. |
| 45 | + """ |
| 46 | + if catalog.namespace_exists(TEST_NAMESPACE_IDENTIFIER): |
| 47 | + catalog.drop_namespace(TEST_NAMESPACE_IDENTIFIER) |
| 48 | + catalog.create_namespace_if_not_exists(TEST_NAMESPACE_IDENTIFIER) |
| 49 | + |
| 50 | + assert catalog.namespace_exists(TEST_NAMESPACE_IDENTIFIER) |
| 51 | + assert TEST_NAMESPACE_IDENTIFIER[0].lower() in inspect(trino_rest_conn).get_schema_names() |
| 52 | + |
| 53 | + |
| 54 | +@pytest.mark.trino |
| 55 | +@pytest.mark.parametrize( |
| 56 | + "transform", |
| 57 | + [ |
| 58 | + IdentityTransform(), |
| 59 | + BucketTransform(num_buckets=32), |
| 60 | + ], |
| 61 | +) |
| 62 | +@pytest.mark.parametrize( |
| 63 | + "catalog,trino_conn", |
| 64 | + [ |
| 65 | + (pytest.lazy_fixture("session_catalog_hive"), pytest.lazy_fixture("trino_hive_conn")), |
| 66 | + (pytest.lazy_fixture("session_catalog"), pytest.lazy_fixture("trino_rest_conn")), |
| 67 | + ], |
| 68 | +) |
| 69 | +def test_uuid_partitioning_with_trino(catalog: Catalog, trino_conn: Connection, transform: Transform) -> None: # type: ignore |
| 70 | + """Test UUID partitioning using Trino engine. |
| 71 | +
|
| 72 | + This test verifies that UUID-partitioned tables created via PyIceberg can be |
| 73 | + correctly queried through Trino. It tests both Identity and Bucket transforms |
| 74 | + on UUID columns, which are not fully supported in Spark but work in Trino. |
| 75 | + """ |
| 76 | + identifier = f"default.test_uuid_partitioning_{str(transform).replace('[32]', '')}" |
| 77 | + |
| 78 | + schema = Schema(NestedField(field_id=1, name="uuid", field_type=UUIDType(), required=True)) |
| 79 | + |
| 80 | + try: |
| 81 | + catalog.drop_table(identifier=identifier) |
| 82 | + except NoSuchTableError: |
| 83 | + pass |
| 84 | + |
| 85 | + partition_spec = PartitionSpec( |
| 86 | + PartitionField(source_id=1, field_id=1000, transform=transform, name=f"uuid_{str(transform).replace('[32]', '')}") |
| 87 | + ) |
| 88 | + |
| 89 | + arr_table = pa.Table.from_pydict( |
| 90 | + { |
| 91 | + "uuid": [ |
| 92 | + uuid.UUID("00000000-0000-0000-0000-000000000000").bytes, |
| 93 | + uuid.UUID("11111111-1111-1111-1111-111111111111").bytes, |
| 94 | + ], |
| 95 | + }, |
| 96 | + schema=pa.schema( |
| 97 | + [ |
| 98 | + # Uuid not yet supported, so we have to stick with `binary(16)` |
| 99 | + # https://github.com/apache/arrow/issues/46468 |
| 100 | + pa.field("uuid", pa.binary(16), nullable=False), |
| 101 | + ] |
| 102 | + ), |
| 103 | + ) |
| 104 | + |
| 105 | + tbl = catalog.create_table( |
| 106 | + identifier=identifier, |
| 107 | + schema=schema, |
| 108 | + partition_spec=partition_spec, |
| 109 | + ) |
| 110 | + |
| 111 | + tbl.append(arr_table) |
| 112 | + rows = trino_conn.execute(text(f"SELECT * FROM {identifier}")).fetchall() |
| 113 | + lhs = sorted([r[0] for r in rows]) |
| 114 | + rhs = sorted([u.as_py() for u in tbl.scan().to_arrow()["uuid"].combine_chunks()]) |
| 115 | + assert lhs == rhs |
0 commit comments