Skip to content

Commit 6f841db

Browse files
committed
Support reading initial-defaults
1 parent 4ff1558 commit 6f841db

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

pyiceberg/io/pyarrow.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,9 +1768,12 @@ def struct(
17681768
array = self._cast_if_needed(field, field_array)
17691769
field_arrays.append(array)
17701770
fields.append(self._construct_field(field, array.type))
1771-
elif field.optional:
1771+
elif field.optional or field.initial_default is not None:
17721772
arrow_type = schema_to_pyarrow(field.field_type, include_field_ids=False)
1773-
field_arrays.append(pa.nulls(len(struct_array), type=arrow_type))
1773+
if field.initial_default is None:
1774+
field_arrays.append(pa.nulls(len(struct_array), type=arrow_type))
1775+
else:
1776+
field_arrays.append(pa.repeat(field.initial_default, len(struct_array)))
17741777
fields.append(self._construct_field(field, arrow_type))
17751778
else:
17761779
raise ResolveError(f"Field is required, and could not be found in the file: {field}")

tests/io/test_pyarrow.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2203,6 +2203,17 @@ def test_identity_partition_on_multi_columns() -> None:
22032203
) == arrow_table.sort_by([("born_year", "ascending"), ("n_legs", "ascending"), ("animal", "ascending")])
22042204

22052205

2206+
def test_initial_value() -> None:
2207+
# Have some fake data, otherwise it will generate a table without records
2208+
data = pa.record_batch([pa.nulls(10, pa.int32())], ["some_field"])
2209+
result = _to_requested_schema(
2210+
Schema(NestedField(1, "so-true", BooleanType(), required=True, initial_default=True)), Schema(), data
2211+
)
2212+
assert result.column_names == ["so-true"]
2213+
for val in result[0]:
2214+
assert val.as_py() is True
2215+
2216+
22062217
def test__to_requested_schema_timestamps(
22072218
arrow_table_schema_with_all_timestamp_precisions: pa.Schema,
22082219
arrow_table_with_all_timestamp_precisions: pa.Table,

0 commit comments

Comments
 (0)