Skip to content

fix: decode promoted date bounds in timestamp units - #1157

Open
zhjwpku wants to merge 2 commits into
apache:mainfrom
zhjwpku:fix/decode-promoted-date-bounds
Open

zhjwpku wants to merge 2 commits into
apache:mainfrom
zhjwpku:fix/decode-promoted-date-bounds

Conversation

@zhjwpku

@zhjwpku zhjwpku commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

After a date column is promoted to timestamp or timestamp_ns, existing files
keep four-byte bounds in days. Per the spec's schema evolution rules, decode them
as midnight in the target unit instead of reading the day count as micros/nanos.

Without this, inclusive pruning can drop files that contain matching rows, and
strict evaluation can report that all rows match when they do not.

Changes:

  • Conversions::FromBytes scales four-byte bounds for timestamp and
    timestamp_ns, and returns an error on overflow.
  • Four-byte values for time, timestamptz, and timestamptz_ns are now
    rejected; the spec does not allow date promotion to those types.
  • File-level evaluation (inclusive, strict, aggregates, equality deletes) uses an
    internal DeserializeBound that treats an out-of-range date bound as unknown
    rather than failing planning. Malformed bounds are still errors.
  • Min/max aggregates decode bounds with the source column type, which also fixes
    type-changing transforms such as max(day(ts)).

Related to #1156 and #1153.

Comment thread src/iceberg/util/conversions.cc Outdated
// Before date promotion, bounds were stored as days since the epoch.
const auto units_per_day = type_id == TypeId::kTimestamp ? internal::kMicrosPerDay
: internal::kNanosPerDay;
ICEBERG_ASSIGN_OR_RAISE(auto timestamp, MultiplyExact(value, units_per_day));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A file containing only epoch day 0 can have a valid loose date upper bound of day 106752. After promotion to timestamp_ns, the row is still representable, but MultiplyExact rejects the upper bound and aborts planning. Could we treat this bound as unknown while retaining overflow errors for actual row values?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Bounds are now decoded through an internal DeserializeBound() that returns
std::nullopt when a four-byte date bound does not fit the promoted type.
Callers treat it as a missing bound, so planning continues and the other bound
stays usable. Literal::Deserialize() still returns an error on overflow, and
malformed bounds are still propagated as errors.

@zhjwpku
zhjwpku force-pushed the fix/decode-promoted-date-bounds branch from e689e15 to 953a517 Compare November 24, 2026 02:34
Read four-byte bounds for timestamp and timestamp_ns as epoch days. Treat date bounds outside the target range as unknown when evaluating file and manifest statistics, while propagating malformed bounds as errors.
@zhjwpku
zhjwpku force-pushed the fix/decode-promoted-date-bounds branch from 3306bcc to 4bb3a9f Compare November 24, 2026 06:38
Require all four bounds for equality delete range checks. Keep partition summary evaluation on its existing path and cover timestamp source bounds in transformed aggregates and inclusive file pruning.
auto primitive_type = internal::checked_pointer_cast<PrimitiveType>(type);
if (data_file_.upper_bounds.contains(id)) {
return Literal::Deserialize(data_file_.upper_bounds.at(id), primitive_type);
return internal::DeserializeBound(data_file_.upper_bounds.at(id), primitive_type);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A date upper bound of 89,478,486 days fits in timestamp micros, but its hour count wraps to a negative int32. This makes hour(ts) >= 0 prune a file containing epoch. Skip hour-based pruning when the transformed range can wrap.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good catch, I reproduced the same issue with ordinary 8-byte timestamp bounds. The upper bound wraps to a negative hour, so hour(ts) >= 0 can skip a file containing the epoch. I opened #1176 to fix this for both timestamp and timestamptz.

return bound;
}
SingleValueStructLike data(std::move(*bound));
return term.Evaluate(data);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The later of the two timestamp examples in Appendix B maps to bucket 0, while the earlier one maps to bucket 1. Taking the bucket of the upper bound returns the wrong MAX. Require an order-preserving transform before deriving an extremum from one endpoint.

ICEBERG_UNWRAP_OR_FAIL(auto upper,
Literal::Timestamp(2 * internal::kMicrosPerDay + 1).Serialize());
DataFile file{.record_count = 1, .upper_bounds = {{1, upper}}};
EXPECT_TRUE(max_day->HasValue(file));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A file containing only day 1 may have an upper bound on day 2. What guarantees this bound is exact? Please cover that case and fall back when the metrics cannot establish the MAX.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Nothing in DataFile says whether a bound is exact, so the aggregate cannot
detect this case. This is the same contract as Java's MaxAggregate, which
returns the bound as is and leaves it to the caller to decide whether bounds can
be trusted. This PR does not change that; the test only checks that the bound is
decoded with the source column type.

// https://iceberg.apache.org/spec/#schema-evolution
auto timestamp = PromoteDate(value, type_id);
if (!timestamp) {
return InvalidArgument("Date {} is outside the range of {}", value,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ManifestEvaluator still reaches this error for partition summaries. An old identity-date manifest may contain day 1 with a loose upper bound of day 106752. After promotion to timestamp_ns, keep that manifest for ts > epoch and distinguish an unrepresentable bound from an all-null summary.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think promoting an identity-partitioned date to timestamp_ns is disallowed because it changes the partition value. The day, month, and year transforms keep the same partition values across the promotion.

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