Repository navigation
Description
Apache Iceberg version
0.12.0 (latest release)
Please describe the bug 🐞
On a format version 1 table, remove_field keeps the partition field and changes its transform to void. After that, dynamic_partition_overwrite always raises, because the check for identity transforms also rejects void. The same happens on a table that started as v1 and was upgraded to v2, since the void field stays in the spec. A table created as v2 works, because there remove_field drops the field.
I came across this while reviewing #4221.
Reproduce
import tempfile
import pyarrow as pa
import pyiceberg
from pyiceberg.catalog.sql import SqlCatalog
from pyiceberg.partitioning import PartitionField, PartitionSpec
from pyiceberg.schema import Schema
from pyiceberg.transforms import IdentityTransform
from pyiceberg.types import LongType, NestedField, StringType
print("pyiceberg", pyiceberg.__version__)
schema = Schema(
NestedField(1, "category", StringType(), required=False),
NestedField(2, "region", StringType(), required=False),
NestedField(3, "value", LongType(), required=False),
)
spec = PartitionSpec(
PartitionField(source_id=1, field_id=1000, transform=IdentityTransform(), name="category"),
PartitionField(source_id=2, field_id=1001, transform=IdentityTransform(), name="region"),
)
def run(format_version: int, upgrade_to_v2: bool = False) -> None:
label = f"v{format_version}" + (" upgraded to v2" if upgrade_to_v2 else "")
with tempfile.TemporaryDirectory() as warehouse:
catalog = SqlCatalog("default", uri=f"sqlite:///{warehouse}/catalog.db", warehouse=f"file://{warehouse}")
catalog.create_namespace("default")
table = catalog.create_table(
"default.t", schema=schema, partition_spec=spec, properties={"format-version": str(format_version)}
)
table.append(pa.table({"category": ["A", "B"], "region": ["us", "eu"], "value": [1, 2]}, schema=schema.as_arrow()))
with table.update_spec() as update:
update.remove_field("region")
if upgrade_to_v2:
with table.transaction() as tx:
tx.upgrade_table_version(2)
print(f"\n{label}: spec after remove_field = {table.spec()}")
try:
table.dynamic_partition_overwrite(
pa.table({"category": ["A"], "region": ["us"], "value": [100]}, schema=schema.as_arrow())
)
rows = sorted(zip(*table.scan().to_arrow().to_pydict().values()), key=str)
print(f"{label}: rows after overwrite = {rows}")
except ValueError as e:
print(f"{label}: ValueError: {e}")
run(1)
run(1, upgrade_to_v2=True)
run(2)Output on main at c5a39b5, and the same on 0.12.0
pyiceberg 0.12.0
v1: spec after remove_field = [
1000: category: identity(1)
1001: region: void(2)
]
v1: ValueError: For now dynamic overwrite does not support a table with non-identity-transform field in the latest partition spec: 1001: region: void(2)
v1 upgraded to v2: spec after remove_field = [
1000: category: identity(1)
1001: region: void(2)
]
v1 upgraded to v2: ValueError: For now dynamic overwrite does not support a table with non-identity-transform field in the latest partition spec: 1001: region: void(2)
v2: spec after remove_field = [
1000: category: identity(1)
]
v2: rows after overwrite = [('A', 'us', 100), ('B', 'eu', 2)]
Expected
The 2 tables with a void field should behave like the v2 table and replace category = A.
Possible fix
A void field is always null, so it can be left out of both the check and the delete filter. Skipping it only in the check is not enough, because the delete filter would then get region IS NULL and miss the old rows.
I tried this locally in Transaction.dynamic_partition_overwrite, 12 lines added and 4 removed.
- The repro above then gives the v2 result for all 3 tables.
- Old files written under the earlier spec, in both us and eu, are replaced when overwriting category = A.
- With the first field void instead, overwriting region = eu replaces only the eu rows.
- A table where every partition field is void raises the existing "Cannot apply dynamic overwrite on an unpartitioned table" error, instead of appending without deleting anything.
- tests/table/test_init.py, tests/table/test_upsert.py and tests/catalog/test_catalog_behaviors.py give 1003 passed. The 8 failures are the same with and without the change, and all 8 need extras I do not have installed (pyiceberg-core and polars).
This touches the same lines as #4204, so a fix would go in after it or be rebased on it.
Willingness to contribute
- I can contribute a fix for this bug independently
- I would be willing to contribute a fix for this bug with guidance from the Iceberg community
- I cannot contribute a fix for this bug at this time
Activity