Skip to content

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

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

zhjwpku wants to merge 2 commits 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.


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.

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.

Agreed, added an inline comment. If either bound is NaN, we now treat both bounds as unreliable and skip bounds-based pruning.

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.

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;

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.

Adopted. A bound rules out a candidate only when the candidate is strictly below the lower bound or strictly above the upper bound. An unordered comparison can't establish either condition.

@zhjwpku
zhjwpku force-pushed the fix/inclusive-in-missing-lower-bound branch from 4176238 to 82deb57 Compare November 28, 2026 08:28
zhjwpku pushed a commit that referenced this pull request Nov 28, 2026
…With when lower bound is missing (#1187)

### Problem

Follow-up to #1156 and #1177.

In InclusiveMetricsEvaluator::StartsWith, if lower_bounds is missing, the evaluator returns kRowsMightMatch immediately and skips the upper_bounds check. For example, a file with no lower bound and upper_bounds = "dC" is still read for `StartsWith("required", "e")` or `StartsWith("required", "dCa")`, even though no value in the file can match.

### Fix

- Only run the lower-bound check when `lower_bounds` is present and non-null, and still fall through to prune against upper_bounds
- Use `std::string_view::compare(0, prefix.size(), prefix) > 0` for the lower bound and upper_str < prefix for the upper bound. This matches the StrictMetricsEvaluator::NotStartsWith implementation from #1177.

### Tests

Add basic unit tests.
…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.
Binding rewrites a singleton IN to equality. Use the same bounds and NaN counts for both paths so a file can be skipped only when every candidate is excluded. Ignore both bounds if either is NaN.
@zhjwpku
zhjwpku force-pushed the fix/inclusive-in-missing-lower-bound branch from 82deb57 to 7c2aa3f Compare November 29, 2026 02:55
@zhjwpku
zhjwpku requested a review from deepaknettem November 29, 2026 02:57

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