Skip to content

Use internal values for temporal initial-defaults - #4435

Open
rahulsmahadev wants to merge 4 commits into
apache:mainfrom
rahulsmahadev:rahul/initial-default-temporal-filter
Open

rahulsmahadev wants to merge 4 commits into
apache:mainfrom
rahulsmahadev:rahul/initial-default-temporal-filter

Conversation

@rahulsmahadev

@rahulsmahadev rahulsmahadev commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Rationale for this change

Bug: when a data file lacks a date, time, timestamp or timestamptz column, = and IN miss its non-null initial-default, while != matches it. _ColumnNameTranslator compares a Python temporal value with a bound literal in days or microseconds:

# v3 table: rows 1-3 were written before `d` was added with initial-default 2020-01-01
tbl.scan(row_filter=EqualTo("d", date(2020, 1, 1))).to_arrow()  # 0 rows, expected 3

Fix: NestedField also keeps its initial-default in the literal's form, literal(initial_default).to(field_type).value computed once when the field is created (the conversion add_column uses), and exposes it as the read-only initial_default_value; the translator, the Avro default reader and Record defaults use it. Partition-projected values are already in that form and keep precedence.

Java: SchemaParser and SchemaUpdate supply typed defaults to row-group filtering; GenericReader normalizes reader output through InternalRecordWrapper before Evaluator. This PR normalizes defaults used in internal records.

  • NestedField.initial_default stays a Python object; internal default consumers share the conversion.

Are these changes tested?

Yes, filter tests in tests/expressions/test_visitors.py and tests/io/test_pyarrow.py, plus Avro and Record default tests in tests/avro/test_resolver.py and tests/test_typedef.py, and test_nested_field_initial_default_internal_value in tests/test_types.py for the converted value, also after a JSON round trip, and for a decimal default with the wrong scale. These regression tests fail on main and pass with this change.

Are there any user-facing changes?

Yes, this fixes incorrect temporal-default filter results and range errors. Avro now returns defaulted temporal fields as integer days or microseconds, matching stored fields.

Please add the changelog label.

This pull request and its description were written by Isaac.

A file that lacks a date, time, timestamp or timestamptz column with a
non-null initial-default compared the Python value with bound literals,
which hold days or microseconds.
Equality and IN dropped matching rows, != kept them, and range predicates raised TypeError.
Add a private NestedField accessor for the internal form, used by the
translator, the Avro default reader and Record defaults.
initial_default is unchanged.

Co-authored-by: Isaac <no-reply@databricks.com>
@rahulsmahadev

Copy link
Copy Markdown
Contributor Author

@kevinjqliu, you reviewed the initial-default projection in #1644. @Erigara, you worked on projected-field filtering in #2029. Could you take a look when you have a chance?

Comment thread pyiceberg/types.py Resolved
rahulsmahadev and others added 2 commits November 23, 2026 22:25
Co-authored-by: Isaac <no-reply@databricks.com>
Convert a temporal initial-default to days or microseconds once in
NestedField.__init__ instead of on every access. initial_default keeps the
date or datetime.

Co-authored-by: Isaac <no-reply@databricks.com>
@rahulsmahadev

Copy link
Copy Markdown
Contributor Author

@Fokko could you take a look when you have a chance? Thanks!

Comment thread pyiceberg/types.py
initial_default: DefaultValue | None = Field(alias="initial-default", default=None, repr=True) # type: ignore
write_default: DefaultValue | None = Field(alias="write-default", default=None, repr=True) # type: ignore
# The initial-default as bound literals hold it: days or microseconds for temporal types
_initial_default_value: Any = PrivateAttr()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The definition of a PrivateAttr is that it shouldn't be used outside of a class, while that's exactly what we're doing above.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. The readers now go through a read-only NestedField.initial_default_value property, so the private attribute is only used inside NestedField (2dd2e42).

Comment thread pyiceberg/types.py Outdated
data["write-default"] = data["write-default"] if "write-default" in data else write_default
super().__init__(**data)
self._initial_default_value = self.initial_default
if self.initial_default is not None and isinstance(self.field_type, (DateType, TimeType, TimestampType, TimestamptzType)):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we missing any types here? (Answer: Yes, we are. Hint, they are V3).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, timestamp_ns and timestamptz_ns were missing. The type list is gone. Non-null nanosecond defaults still fail in _deserialize_default_value, before this conversion, and literal conversion to those types is not implemented yet. Both are existing gaps.

Comment thread pyiceberg/types.py Outdated
Comment on lines +474 to +478
self._initial_default_value = self.initial_default
if self.initial_default is not None and isinstance(self.field_type, (DateType, TimeType, TimestampType, TimestamptzType)):
from pyiceberg.expressions.literals import literal

self._initial_default_value = literal(self.initial_default).to(self.field_type).value

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this was my suggestion, wouldn't that work?

Suggested change
self._initial_default_value = self.initial_default
if self.initial_default is not None and isinstance(self.field_type, (DateType, TimeType, TimestampType, TimestamptzType)):
from pyiceberg.expressions.literals import literal
self._initial_default_value = literal(self.initial_default).to(self.field_type).value
self._initial_default_value = literal(self.initial_default).to(self.field_type).value

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked this locally, dropping the isinstance check works. literal(v).to(field_type).value returns the value unchanged for the non-temporal primitives (boolean, int, long, float, double, decimal, string, uuid, fixed, binary), and date/time/timestamp/timestamptz still get converted to days/micros. The existing tests, plus the new ones in this PR, pass with the suggestion applied.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for checking. Done in 2dd2e42 with a None check: non-null defaults now use literal(initial_default).to(field_type).value, the same conversion as add_column, so conversion errors surface when the field is created; for example, Decimal("1.2") for decimal(9, 2) is rejected.

…operty

Readers and bound literals now use NestedField.initial_default_value instead
of the private attribute, and the internal value comes from
literal(initial_default).to(field_type) for every type, not only the
temporal ones.

Co-authored-by: Isaac <no-reply@databricks.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants