jqin61 commented on code in PR #555: URL: https://github.com/apache/iceberg-python/pull/555#discussion_r1543796831
########## tests/integration/test_partitioned_write.py: ########## @@ -0,0 +1,533 @@ +# 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 datetime import date, datetime + +import pyarrow as pa +import pytest +import pytz +from pyspark.sql import SparkSession + +from pyiceberg.catalog import Catalog, load_catalog +from pyiceberg.exceptions import NamespaceAlreadyExistsError, NoSuchTableError +from pyiceberg.partitioning import PartitionField, PartitionSpec +from pyiceberg.schema import Schema +from pyiceberg.transforms import IdentityTransform +from pyiceberg.types import ( + BinaryType, + BooleanType, + DateType, + DoubleType, + FixedType, + FloatType, + IntegerType, + LongType, + NestedField, + StringType, + TimestampType, + TimestamptzType, +) + + +@pytest.fixture() +def catalog() -> Catalog: + catalog = load_catalog( + "local", + **{ + "type": "rest", + "uri": "http://localhost:8181", + "s3.endpoint": "http://localhost:9000", + "s3.access-key-id": "admin", + "s3.secret-access-key": "password", + }, + ) + + try: + catalog.create_namespace("default") + except NamespaceAlreadyExistsError: + pass + + return catalog + + +TEST_DATA_WITH_NULL = { + 'bool': [False, None, True], + 'string': ['a', None, 'z'], + # Go over the 16 bytes to kick in truncation + 'string_long': ['a' * 22, None, 'z' * 22], + 'int': [1, None, 9], + 'long': [1, None, 9], + 'float': [0.0, None, 0.9], + 'double': [0.0, None, 0.9], + 'timestamp': [datetime(2023, 1, 1, 19, 25, 00), None, datetime(2023, 3, 1, 19, 25, 00)], + 'timestamptz': [ + datetime(2023, 1, 1, 19, 25, 00, tzinfo=pytz.timezone('America/New_York')), + None, + datetime(2023, 3, 1, 19, 25, 00, tzinfo=pytz.timezone('America/New_York')), + ], + 'date': [date(2023, 1, 1), None, date(2023, 3, 1)], + # Not supported by Spark + # 'time': [time(1, 22, 0), None, time(19, 25, 0)], + # Not natively supported by Arrow + # 'uuid': [uuid.UUID('00000000-0000-0000-0000-000000000000').bytes, None, uuid.UUID('11111111-1111-1111-1111-111111111111').bytes], + 'binary': [b'\01', None, b'\22'], + 'fixed': [ + uuid.UUID('00000000-0000-0000-0000-000000000000').bytes, + None, + uuid.UUID('11111111-1111-1111-1111-111111111111').bytes, + ], +} + + +TABLE_SCHEMA = Schema( + NestedField(field_id=1, name="bool", field_type=BooleanType(), required=False), + NestedField(field_id=2, name="string", field_type=StringType(), required=False), + NestedField(field_id=3, name="string_long", field_type=StringType(), required=False), + NestedField(field_id=4, name="int", field_type=IntegerType(), required=False), + NestedField(field_id=5, name="long", field_type=LongType(), required=False), + NestedField(field_id=6, name="float", field_type=FloatType(), required=False), + NestedField(field_id=7, name="double", field_type=DoubleType(), required=False), + NestedField(field_id=8, name="timestamp", field_type=TimestampType(), required=False), + NestedField(field_id=9, name="timestamptz", field_type=TimestamptzType(), required=False), + NestedField(field_id=10, name="date", field_type=DateType(), required=False), + # NestedField(field_id=11, name="time", field_type=TimeType(), required=False), + # NestedField(field_id=12, name="uuid", field_type=UuidType(), required=False), + NestedField(field_id=11, name="binary", field_type=BinaryType(), required=False), + NestedField(field_id=12, name="fixed", field_type=FixedType(16), required=False), +) + + +@pytest.fixture(scope="session") +def session_catalog() -> Catalog: Review Comment: moving to conftest ########## tests/integration/test_partitioned_write.py: ########## @@ -0,0 +1,533 @@ +# 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 datetime import date, datetime + +import pyarrow as pa +import pytest +import pytz +from pyspark.sql import SparkSession + +from pyiceberg.catalog import Catalog, load_catalog +from pyiceberg.exceptions import NamespaceAlreadyExistsError, NoSuchTableError +from pyiceberg.partitioning import PartitionField, PartitionSpec +from pyiceberg.schema import Schema +from pyiceberg.transforms import IdentityTransform +from pyiceberg.types import ( + BinaryType, + BooleanType, + DateType, + DoubleType, + FixedType, + FloatType, + IntegerType, + LongType, + NestedField, + StringType, + TimestampType, + TimestamptzType, +) + + +@pytest.fixture() +def catalog() -> Catalog: + catalog = load_catalog( + "local", + **{ + "type": "rest", + "uri": "http://localhost:8181", + "s3.endpoint": "http://localhost:9000", + "s3.access-key-id": "admin", + "s3.secret-access-key": "password", + }, + ) + + try: + catalog.create_namespace("default") + except NamespaceAlreadyExistsError: + pass + + return catalog + + +TEST_DATA_WITH_NULL = { + 'bool': [False, None, True], + 'string': ['a', None, 'z'], + # Go over the 16 bytes to kick in truncation + 'string_long': ['a' * 22, None, 'z' * 22], + 'int': [1, None, 9], + 'long': [1, None, 9], + 'float': [0.0, None, 0.9], + 'double': [0.0, None, 0.9], + 'timestamp': [datetime(2023, 1, 1, 19, 25, 00), None, datetime(2023, 3, 1, 19, 25, 00)], + 'timestamptz': [ + datetime(2023, 1, 1, 19, 25, 00, tzinfo=pytz.timezone('America/New_York')), + None, + datetime(2023, 3, 1, 19, 25, 00, tzinfo=pytz.timezone('America/New_York')), + ], + 'date': [date(2023, 1, 1), None, date(2023, 3, 1)], + # Not supported by Spark + # 'time': [time(1, 22, 0), None, time(19, 25, 0)], + # Not natively supported by Arrow + # 'uuid': [uuid.UUID('00000000-0000-0000-0000-000000000000').bytes, None, uuid.UUID('11111111-1111-1111-1111-111111111111').bytes], + 'binary': [b'\01', None, b'\22'], + 'fixed': [ + uuid.UUID('00000000-0000-0000-0000-000000000000').bytes, + None, + uuid.UUID('11111111-1111-1111-1111-111111111111').bytes, + ], +} + + +TABLE_SCHEMA = Schema( + NestedField(field_id=1, name="bool", field_type=BooleanType(), required=False), + NestedField(field_id=2, name="string", field_type=StringType(), required=False), + NestedField(field_id=3, name="string_long", field_type=StringType(), required=False), + NestedField(field_id=4, name="int", field_type=IntegerType(), required=False), + NestedField(field_id=5, name="long", field_type=LongType(), required=False), + NestedField(field_id=6, name="float", field_type=FloatType(), required=False), + NestedField(field_id=7, name="double", field_type=DoubleType(), required=False), + NestedField(field_id=8, name="timestamp", field_type=TimestampType(), required=False), + NestedField(field_id=9, name="timestamptz", field_type=TimestamptzType(), required=False), + NestedField(field_id=10, name="date", field_type=DateType(), required=False), + # NestedField(field_id=11, name="time", field_type=TimeType(), required=False), + # NestedField(field_id=12, name="uuid", field_type=UuidType(), required=False), + NestedField(field_id=11, name="binary", field_type=BinaryType(), required=False), + NestedField(field_id=12, name="fixed", field_type=FixedType(16), required=False), +) + + +@pytest.fixture(scope="session") +def session_catalog() -> Catalog: + return load_catalog( + "local", + **{ + "type": "rest", + "uri": "http://localhost:8181", + "s3.endpoint": "http://localhost:9000", + "s3.access-key-id": "admin", + "s3.secret-access-key": "password", + }, + ) + + +@pytest.fixture(scope="session") +def arrow_table_with_null() -> pa.Table: + """PyArrow table with all kinds of columns""" + pa_schema = pa.schema([ + ("bool", pa.bool_()), + ("string", pa.string()), + ("string_long", pa.string()), + ("int", pa.int32()), + ("long", pa.int64()), + ("float", pa.float32()), + ("double", pa.float64()), + ("timestamp", pa.timestamp(unit="us")), + ("timestamptz", pa.timestamp(unit="us", tz="UTC")), + ("date", pa.date32()), + # Not supported by Spark + # ("time", pa.time64("us")), + # Not natively supported by Arrow + # ("uuid", pa.fixed(16)), + ("binary", pa.large_binary()), + ("fixed", pa.binary(16)), + ]) + return pa.Table.from_pydict(TEST_DATA_WITH_NULL, schema=pa_schema) + + +@pytest.fixture(scope="session", autouse=True) +def table_v1_with_null_partitioned(session_catalog: Catalog, arrow_table_with_null: pa.Table) -> None: + partition_cols = [ + 'int', + 'bool', + 'string', + "string_long", + "long", + "float", + "double", + "date", + "timestamptz", + "timestamp", + "binary", + ] + for partition_col in partition_cols: + identifier = f"default.arrow_table_v1_with_null_partitioned_on_col_{partition_col}" + + try: + session_catalog.drop_table(identifier=identifier) + except NoSuchTableError: + pass + nested_field = TABLE_SCHEMA.find_field(partition_col) + source_id = nested_field.field_id + tbl = session_catalog.create_table( + identifier=identifier, + schema=TABLE_SCHEMA, + partition_spec=PartitionSpec( + PartitionField(source_id=source_id, field_id=1001, transform=IdentityTransform(), name=partition_col) + ), + properties={'format-version': '1'}, + ) + tbl.append(arrow_table_with_null) + + assert tbl.format_version == 1, f"Expected v1, got: v{tbl.format_version}" + + +@pytest.fixture(scope="session", autouse=True) +def table_v1_appended_with_null_partitioned(session_catalog: Catalog, arrow_table_with_null: pa.Table) -> None: + partition_cols = [ + 'int', + 'bool', + 'string', + "string_long", + "long", + "float", + "double", + "date", + "timestamptz", + "timestamp", + "binary", + ] + for partition_col in partition_cols: + identifier = f"default.arrow_table_v1_appended_with_null_partitioned_on_col_{partition_col}" + + try: + session_catalog.drop_table(identifier=identifier) + except NoSuchTableError: + pass + + nested_field = TABLE_SCHEMA.find_field(partition_col) + source_id = nested_field.field_id + tbl = session_catalog.create_table( + identifier=identifier, + schema=TABLE_SCHEMA, + partition_spec=PartitionSpec( + PartitionField(source_id=source_id, field_id=1001, transform=IdentityTransform(), name=partition_col) + ), # name has to be the same for identity transform + properties={'format-version': '1'}, + ) + + for _ in range(2): + tbl.append(arrow_table_with_null) + assert tbl.format_version == 1, f"Expected v1, got: v{tbl.format_version}" + + +@pytest.fixture(scope="session", autouse=True) +def table_v2_with_null_partitioned(session_catalog: Catalog, arrow_table_with_null: pa.Table) -> None: + partition_cols = [ + 'int', + 'bool', + 'string', + "string_long", + "long", + "float", + "double", + "date", + "timestamptz", + "timestamp", + "binary", + ] + for partition_col in partition_cols: + identifier = f"default.arrow_table_v2_with_null_partitioned_on_col_{partition_col}" + + try: + session_catalog.drop_table(identifier=identifier) + except NoSuchTableError: + pass + nested_field = TABLE_SCHEMA.find_field(partition_col) + source_id = nested_field.field_id + + tbl = session_catalog.create_table( + identifier=identifier, + schema=TABLE_SCHEMA, + partition_spec=PartitionSpec( + PartitionField(source_id=source_id, field_id=1001, transform=IdentityTransform(), name=partition_col) + ), + properties={'format-version': '2'}, + ) + tbl.append(arrow_table_with_null) + + assert tbl.format_version == 2, f"Expected v2, got: v{tbl.format_version}" + + +@pytest.fixture(scope="session", autouse=True) +def table_v2_appended_with_null_partitioned(session_catalog: Catalog, arrow_table_with_null: pa.Table) -> None: + partition_cols = [ + 'int', + 'bool', + 'string', + "string_long", + "long", + "float", + "double", + "date", + "timestamptz", + "timestamp", + "binary", + ] + for partition_col in partition_cols: + identifier = f"default.arrow_table_v2_appended_with_null_partitioned_on_col_{partition_col}" + + try: + session_catalog.drop_table(identifier=identifier) + except NoSuchTableError: + pass + + nested_field = TABLE_SCHEMA.find_field(partition_col) + source_id = nested_field.field_id + tbl = session_catalog.create_table( + identifier=identifier, + schema=TABLE_SCHEMA, + partition_spec=PartitionSpec( + PartitionField(source_id=source_id, field_id=1001, transform=IdentityTransform(), name=partition_col) + ), + properties={'format-version': '2'}, + ) + + for _ in range(2): + tbl.append(arrow_table_with_null) + assert tbl.format_version == 2, f"Expected v2, got: v{tbl.format_version}" + + +@pytest.fixture(scope="session", autouse=True) +def table_v1_v2_appended_with_null(session_catalog: Catalog, arrow_table_with_null: pa.Table) -> None: + partition_cols = [ + 'int', + 'bool', + 'string', + "string_long", + "long", + "float", + "double", + "date", + "timestamptz", + "timestamp", + "binary", + ] + for partition_col in partition_cols: + identifier = f"default.arrow_table_v1_v2_appended_with_null_partitioned_on_col_{partition_col}" + + try: + session_catalog.drop_table(identifier=identifier) + except NoSuchTableError: + pass + + nested_field = TABLE_SCHEMA.find_field(partition_col) + source_id = nested_field.field_id + tbl = session_catalog.create_table( + identifier=identifier, + schema=TABLE_SCHEMA, + partition_spec=PartitionSpec( + PartitionField(source_id=source_id, field_id=1001, transform=IdentityTransform(), name=partition_col) + ), + properties={'format-version': '1'}, + ) + tbl.append(arrow_table_with_null) + + assert tbl.format_version == 1, f"Expected v1, got: v{tbl.format_version}" + + with tbl.transaction() as tx: + tx.upgrade_table_version(format_version=2) + + tbl.append(arrow_table_with_null) + + assert tbl.format_version == 2, f"Expected v2, got: v{tbl.format_version}" + + +@pytest.mark.integration +@pytest.mark.parametrize( + "part_col", ['int', 'bool', 'string', "string_long", "long", "float", "double", "date", 'timestamp', 'timestamptz'] +) +@pytest.mark.parametrize("format_version", [1, 2]) +def test_query_filter_null_partitioned(spark: SparkSession, part_col: str, format_version: int) -> None: + identifier = f"default.arrow_table_v{format_version}_with_null_partitioned_on_col_{part_col}" + df = spark.table(identifier) + df.show(20, False) + for col in TEST_DATA_WITH_NULL.keys(): + assert df.where(f"{col} is not null").count() == 2, f"Expected 2 rows for {col}" + + +@pytest.mark.integration +@pytest.mark.parametrize( + "part_col", ['int', 'bool', 'string', "string_long", "long", "float", "double", "date", "timestamptz", "timestamp", "binary"] +) +@pytest.mark.parametrize("format_version", [1, 2]) +def test_query_filter_appended_null_partitioned(spark: SparkSession, part_col: str, format_version: int) -> None: + identifier = f"default.arrow_table_v{format_version}_appended_with_null_partitioned_on_col_{part_col}" + df = spark.table(identifier) + for col in TEST_DATA_WITH_NULL.keys(): + df = spark.table(identifier) + assert df.where(f"{col} is not null").count() == 4, f"Expected 6 rows for {col}" + + +@pytest.fixture(scope="session") +def spark() -> SparkSession: Review Comment: moving to conftest -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org