Skip to content

fix(expression): use the upper bound for IN when the lower bound is missing - #1156

Open
zhjwpku wants to merge 1 commit into
apache:mainfrom
zhjwpku:fix/inclusive-in-missing-lower-bound
Open

zhjwpku wants to merge 1 commit into
apache:mainfrom
zhjwpku:fix/inclusive-in-missing-lower-bound

Conversation

@zhjwpku

@zhjwpku zhjwpku commented Nov 23, 2026

Copy link
Copy Markdown
Member

While reviewing #1153, I noticed that the inclusive IN evaluator stopped as soon as it found a missing lower bound. For example, a file with an upper bound of 50 was still read for id IN (51, 70).

Use each available bound to rule out candidates. Add regression tests for candidates above the upper bound, a candidate equal to it, and a NaN candidate that the upper bound cannot rule out.

…issing

While reviewing apache#1153, I noticed that the inclusive
IN evaluator stopped as soon as it found a missing lower bound. For
example, a file with an upper bound of 50 was still read for
id IN (51, 70).

Use each available bound to rule out candidates. Add regression tests
for candidates above the upper bound, a candidate equal to it, and a
NaN candidate that the upper bound cannot rule out.

return kRowsMightMatch;
const bool has_lower = lower.has_value() && !lower->IsNull();
const bool has_upper = upper.has_value() && !upper->IsNull() && !upper->IsNaN();

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 v3 file can retain a four-byte date upper bound after promotion to timestamp. The shared decoder treats the day count as micros or nanos. This bug predates #1153 and can cause its strict NOT IN path to delete files incorrectly. This patch also exposes the bug in inclusive IN when the lower bound is missing.

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.

Thanks for catching this. I opened #1157 to fix the date conversion in the shared decoder, please take a look when available.

return std::ranges::any_of(literal_set, [&](const Literal& lit) {
// Bounds exclude NaN values and may be missing on either side.
return lit.IsNaN() ||
((!has_lower || *lower <= lit) && (!has_upper || lit <= *upper));

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.

On libc++, a date upper bound in year 32768 becomes a negative year. With no lower bound, this can prune a file containing 1970 for year(d) IN (0, 1).

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.

Yeah, I opened #1158 to fix both year() and month() in TemporalUtils


ICEBERG_ASSIGN_OR_RAISE(auto lower, LowerBound(expr));
if (!lower.has_value() || lower->IsNull() || lower->IsNaN()) {
if (lower.has_value() && lower->IsNaN()) {

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.

Why this line has been changed?

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.

The old check returned early when the lower bound was missing or null, so we never checked the upper bound. For example, if the upper bound is 50, we can still skip the file for IN (51, 70). We still return early for a NaN lower bound because it means the bounds are unreliable.

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.

nit: readability, an inline comment might be helpful to explain this.

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.

if lower bound is NaN, it sounds like we're saying both bounds are unreliable. If so, should we have a defensive check: if either bounds are NaN, then don't rely on the bounds at all.

zhjwpku added a commit to zhjwpku/iceberg-cpp that referenced this pull request Nov 23, 2026
After a date column is promoted to timestamp or timestamp_ns, existing
files can still have four-byte bounds in days since 1970-01-01. For
example, day 1 represents midnight on January 2, or 86,400,000,000
microseconds since the epoch. The decoder used to read it as 1
microsecond.

With only that upper bound, the strict NOT IN check from apache#1153 can
conclude that every row matches even if the file contains an excluded
timestamp. The inclusive IN change in apache#1156 exposes the same problem
when deciding whether to skip a file.

Scale the day count to microseconds or nanoseconds in FromBytes and
check for overflow. This also fixes callers that decode manifest bounds
and compare delete-file bounds. Reject four-byte time and zoned
timestamps because Iceberg does not allow date promotion to those types.

Add coverage for both precisions, overflow in either direction, and
strict NOT IN with only a promoted upper bound. The strict regression
fails with the previous decoder. All 559 expression tests pass.
zhjwpku added a commit to zhjwpku/iceberg-cpp that referenced this pull request Nov 24, 2026
After a date column is promoted to timestamp or timestamp_ns, existing
files can still have four-byte bounds in days since 1970-01-01. For
example, day 1 represents midnight on January 2, or 86,400,000,000
microseconds since the epoch. The decoder used to read it as 1
microsecond.

With only that upper bound, the strict NOT IN check from apache#1153 can
conclude that every row matches even if the file contains an excluded
timestamp. The inclusive IN change in apache#1156 exposes the same problem
when deciding whether to skip a file.

Scale the day count to microseconds or nanoseconds in FromBytes and
check for overflow. This also fixes callers that decode manifest bounds
and compare delete-file bounds. Reject four-byte time and zoned
timestamps because Iceberg does not allow date promotion to those types.

Add coverage for both precisions, overflow in either direction, and
strict NOT IN with only a promoted upper bound. The strict regression
fails with the previous decoder. All 559 expression tests pass.

@deepaknettem deepaknettem left a comment

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.

thank you, left a couple of minor comments.

const BoundSetPredicate::LiteralSet& literal_set) override {
// all terms are null preserving. see #isNullPreserving(Bound)
int32_t id = expr->reference()->field().field_id();
if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {

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.

Can literal be NaN? For example, is a query like this possible: WHERE x IN {NaN}

If yes, then we could have a case where the file ContainsNansOnly, we return kRowCannotMatch, and incorrectly skip the file.


ICEBERG_ASSIGN_OR_RAISE(auto lower, LowerBound(expr));
if (!lower.has_value() || lower->IsNull() || lower->IsNaN()) {
if (lower.has_value() && lower->IsNaN()) {

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.

if lower bound is NaN, it sounds like we're saying both bounds are unreliable. If so, should we have a defensive check: if either bounds are NaN, then don't rely on the bounds at all.

@deepaknettem deepaknettem left a comment

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.

one more comment re: partial order check

return std::ranges::any_of(literal_set, [&](const Literal& lit) {
// Bounds exclude NaN values and may be missing on either side.
return lit.IsNaN() ||
((!has_lower || *lower <= lit) && (!has_upper || lit <= *upper));

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 don't think there's a bug here, but in terms of readability I'm wondering if we can do something better..

This evaluator is checking any row in the file could match the predicate. Anytime we return false, constexpr bool kRowCannotMatch = false; means we can prune away / skip the entire file.

Since the Literal uses partial ordering, so these comparators <= >= could willy nilly return false based on the literal value and the bound values, and if the values cannot be compared, then incorrectly prune the file.

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.

thinking through a bit more, perhaps we could structure this check like so:

auto outside_bounds = [&](<const Literal& lit>) {
  // Logic to check if `lit` is definitely outside bounds..
  // If a comparison is unordered or uncertain, return false (treat as inside bounds) to be safe.
};
return std::ranges::all_of(literal_set, outside_bounds) ? kRowCannotMatch
                                                        : kRowsMightMatch;

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.

3 participants