Repository navigation
Use internal values for temporal initial-defaults - #4435
rahulsmahadev wants to merge 4 commits into
Conversation
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>
|
@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? |
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>
|
@Fokko could you take a look when you have a chance? Thanks! |
| 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() |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Agreed. The readers now go through a read-only NestedField.initial_default_value property, so the private attribute is only used inside NestedField (2dd2e42).
| 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)): |
There was a problem hiding this comment.
Are we missing any types here? (Answer: Yes, we are. Hint, they are V3).
There was a problem hiding this comment.
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.
| 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 |
There was a problem hiding this comment.
I think this was my suggestion, wouldn't that work?
| 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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>
Rationale for this change
Bug: when a data file lacks a date, time, timestamp or timestamptz column,
=andINmiss its non-nullinitial-default, while!=matches it._ColumnNameTranslatorcompares a Python temporal value with a bound literal in days or microseconds:Fix:
NestedFieldalso keeps its initial-default in the literal's form,literal(initial_default).to(field_type).valuecomputed once when the field is created (the conversionadd_columnuses), and exposes it as the read-onlyinitial_default_value; the translator, the Avro default reader andRecorddefaults use it. Partition-projected values are already in that form and keep precedence.Java:
SchemaParserandSchemaUpdatesupply typed defaults to row-group filtering;GenericReadernormalizes reader output throughInternalRecordWrapperbeforeEvaluator. This PR normalizes defaults used in internal records.NestedField.initial_defaultstays a Python object; internal default consumers share the conversion.Are these changes tested?
Yes, filter tests in
tests/expressions/test_visitors.pyandtests/io/test_pyarrow.py, plus Avro andRecorddefault tests intests/avro/test_resolver.pyandtests/test_typedef.py, andtest_nested_field_initial_default_internal_valueintests/test_types.pyfor 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
changeloglabel.This pull request and its description were written by Isaac.