This is an automated email from the ASF dual-hosted git repository.

github-merge-queue[bot] pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg-python.git


The following commit(s) were added to refs/heads/main by this push:
     new 49413d1e4 Relabel UTC aliases when projecting timestamptz_ns columns 
(#4468)
49413d1e4 is described below

commit 49413d1e4b40fc6e9a147c1450968c7bbc30d3d4
Author: g0itb4 <[email protected]>
AuthorDate: Thu Nov 26 16:27:57 2026 +0000

    Relabel UTC aliases when projecting timestamptz_ns columns (#4468)
    
    * Relabel UTC aliases when projecting timestamptz_ns columns
    
    * Reinstate `raise`
    
    ---------
    
    Co-authored-by: g0itb4 <[email protected]>
    Co-authored-by: Fokko Driesprong <[email protected]>
---
 pyiceberg/io/pyarrow.py  | 11 +++++++++++
 tests/io/test_pyarrow.py | 29 +++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+)

diff --git a/pyiceberg/io/pyarrow.py b/pyiceberg/io/pyarrow.py
index e345e040f..8aff1f7e7 100644
--- a/pyiceberg/io/pyarrow.py
+++ b/pyiceberg/io/pyarrow.py
@@ -2286,6 +2286,17 @@ class 
ArrowProjectionVisitor(SchemaWithPartnerVisitor[pa.Array, pa.Array | None]
                         elif target_type.unit == "us" and values.type.unit in 
{"s", "ms", "us"}:
                             return values.cast(target_type)
                     raise ValueError(f"Unsupported schema projection from 
{values.type} to {target_type}")
+                elif field.field_type == TimestamptzNanoType():
+                    # Writers label UTC differently (e.g. "+00:00"); relabel 
so files can be concatenated
+                    if (
+                        pa.types.is_timestamp(target_type)
+                        and target_type.tz == "UTC"
+                        and pa.types.is_timestamp(values.type)
+                        and values.type.unit == "ns"
+                        and (values.type.tz in UTC_ALIASES or values.type.tz 
is None)
+                    ):
+                        return values.cast(target_type)
+                    raise ValueError(f"Unsupported schema projection from 
{values.type} to {target_type}")
                 elif field.field_type == TimeType():
                     if pa.types.is_time(target_type) and 
pa.types.is_time(values.type):
                         # Downcasting of nanoseconds to microseconds
diff --git a/tests/io/test_pyarrow.py b/tests/io/test_pyarrow.py
index 40ad3bd42..a2b39f0cc 100644
--- a/tests/io/test_pyarrow.py
+++ b/tests/io/test_pyarrow.py
@@ -139,6 +139,7 @@ from pyiceberg.types import (
     StructType,
     TimestampNanoType,
     TimestampType,
+    TimestamptzNanoType,
     TimestamptzType,
     TimeType,
     UUIDType,
@@ -4022,6 +4023,34 @@ def 
test__to_requested_schema_timestamptz_to_timestamp_projection() -> None:
     assert expected.equals(actual_result)
 
 
+def test__to_requested_schema_timestamptz_ns_utc_alias_concatenates() -> None:
+    table_schema = Schema(
+        NestedField(1, "id", LongType(), required=False),
+        NestedField(2, "ts_field", TimestamptzNanoType(), required=False),
+    )
+
+    # file written before the column was added
+    old_file_schema = Schema(NestedField(1, "id", LongType(), required=False))
+    old_batch = pa.record_batch([pa.array([1], type=pa.int64())], names=["id"])
+
+    # file written by another writer that labels UTC as "+00:00"
+    new_batch = pa.record_batch(
+        [
+            pa.array([2], type=pa.int64()),
+            pa.array([1755172800123456789], type=pa.timestamp("ns", 
tz="+00:00")),
+        ],
+        names=["id", "ts_field"],
+    )
+
+    old_result = _to_requested_schema(table_schema, old_file_schema, old_batch)
+    new_result = _to_requested_schema(table_schema, table_schema, new_batch)
+
+    assert new_result.schema.field("ts_field").type == pa.timestamp("ns", 
tz="UTC")
+    combined = pa.concat_tables([pa.Table.from_batches([old_result]), 
pa.Table.from_batches([new_result])])
+    assert combined.column("ts_field").to_pylist()[0] is None
+    assert combined.column("ts_field").cast(pa.int64()).to_pylist()[1] == 
1755172800123456789
+
+
 def test__to_requested_schema_timestamptz_to_timestamp_write_rejects() -> None:
     """Test that the write path (default) rejects timestamptz to timestamp 
casting.
 

Reply via email to