From 44b77a696d5f50b694faeb39962c8fe1083534ab Mon Sep 17 00:00:00 2001 From: Junwang Zhao Date: Mon, 23 Nov 2026 10:44:24 +0800 Subject: [PATCH 1/2] fix(expression): use the upper bound for IN when the lower bound is missing While reviewing apache/iceberg-cpp#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. --- .../expression/inclusive_metrics_evaluator.cc | 30 ++++++------------- .../test/inclusive_metrics_evaluator_test.cc | 17 +++++++++++ 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/iceberg/expression/inclusive_metrics_evaluator.cc b/src/iceberg/expression/inclusive_metrics_evaluator.cc index 6902e1d32..13b54783c 100644 --- a/src/iceberg/expression/inclusive_metrics_evaluator.cc +++ b/src/iceberg/expression/inclusive_metrics_evaluator.cc @@ -19,7 +19,7 @@ #include "iceberg/expression/inclusive_metrics_evaluator.h" -#include +#include #include #include @@ -258,31 +258,19 @@ class InclusiveMetricsVisitor : public BoundVisitor { } ICEBERG_ASSIGN_OR_RAISE(auto lower, LowerBound(expr)); - if (!lower.has_value() || lower->IsNull() || lower->IsNaN()) { + if (lower.has_value() && lower->IsNaN()) { // NaN indicates unreliable bounds. See the InclusiveMetricsEvaluator docs for more. return kRowsMightMatch; } - auto literals_view = literal_set | std::views::filter([&](const Literal& lit) { - return lower.value() <= lit; - }); - // if all values are less than lower bound, rows cannot match - if (literals_view.empty()) { - return kRowCannotMatch; - } ICEBERG_ASSIGN_OR_RAISE(auto upper, UpperBound(expr)); - if (!upper.has_value() || upper->IsNull()) { - return kRowsMightMatch; - } - auto filtered_view = literals_view | std::views::filter([&](const Literal& lit) { - return upper.value() >= lit; - }); - // if remaining values are greater than upper bound, rows cannot match - if (filtered_view.empty()) { - return kRowCannotMatch; - } - - return kRowsMightMatch; + const bool has_lower = lower.has_value() && !lower->IsNull(); + const bool has_upper = upper.has_value() && !upper->IsNull() && !upper->IsNaN(); + 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)); + }); } Result NotIn(const std::shared_ptr& expr, diff --git a/src/iceberg/test/inclusive_metrics_evaluator_test.cc b/src/iceberg/test/inclusive_metrics_evaluator_test.cc index 65fb62893..a129c528a 100644 --- a/src/iceberg/test/inclusive_metrics_evaluator_test.cc +++ b/src/iceberg/test/inclusive_metrics_evaluator_test.cc @@ -928,6 +928,23 @@ TEST_F(InclusiveMetricsEvaluatorMigratedTest, IntegerInTest) { RunTest(Expressions::In("id", ids), kRowsMightMatch, file1_); } +TEST_F(InclusiveMetricsEvaluatorMigratedTest, InWithMissingLowerBound) { + auto file = std::make_shared(); + file->record_count = 1; + file->upper_bounds = {{1, Literal::Long(50).Serialize().value()}}; + RunTest(Expressions::In("id", {Literal::Long(51), Literal::Long(70)}), kRowCannotMatch, + file); + RunTest(Expressions::In("id", {Literal::Long(50), Literal::Long(70)}), kRowsMightMatch, + file); +} + +TEST_F(InclusiveMetricsEvaluatorMigratedTest, InWithNaNAndMissingLowerBound) { + file1_->upper_bounds[8] = Literal::Float(50.0F).Serialize().value(); + RunTest( + Expressions::In("some_nans", {Literal::Float(51.0F), Literal::Float(kFloatNan)}), + kRowsMightMatch, file1_); +} + TEST_F(InclusiveMetricsEvaluatorMigratedTest, IntegerNotInTest) { RunTest(Expressions::NotIn( "id", {Literal::Long(kIntMinValue - 25), Literal::Long(kIntMinValue - 24)}), From 7c2aa3f0d798c53da9ad48cdb162ca0eebe6c55d Mon Sep 17 00:00:00 2001 From: Zhao Junwang Date: Sat, 28 Nov 2026 16:28:18 +0800 Subject: [PATCH 2/2] fix(expression): keep possible NaN matches when pruning IN predicates 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. --- .../expression/inclusive_metrics_evaluator.cc | 83 ++++++++----------- .../test/inclusive_metrics_evaluator_test.cc | 32 +++++++ 2 files changed, 68 insertions(+), 47 deletions(-) diff --git a/src/iceberg/expression/inclusive_metrics_evaluator.cc b/src/iceberg/expression/inclusive_metrics_evaluator.cc index 13b54783c..197beb78a 100644 --- a/src/iceberg/expression/inclusive_metrics_evaluator.cc +++ b/src/iceberg/expression/inclusive_metrics_evaluator.cc @@ -20,6 +20,7 @@ #include "iceberg/expression/inclusive_metrics_evaluator.h" #include +#include #include #include @@ -207,28 +208,8 @@ class InclusiveMetricsVisitor : public BoundVisitor { } Result Eq(const std::shared_ptr& expr, const Literal& lit) override { - // all terms are null preserving. see #isNullPreserving(Bound) - int32_t id = expr->reference()->field().field_id(); - if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) { - return kRowCannotMatch; - } - - ICEBERG_ASSIGN_OR_RAISE(auto lower, LowerBound(expr)); - if (lower.has_value() && !lower->IsNull() && !lower->IsNaN()) { - if (lower.value() > lit) { - return kRowCannotMatch; - } - } - - ICEBERG_ASSIGN_OR_RAISE(auto upper, UpperBound(expr)); - if (!upper.has_value() || upper->IsNull()) { - return kRowsMightMatch; - } - if (upper.value() < lit) { - return kRowCannotMatch; - } - - return kRowsMightMatch; + // Binding a singleton IN produces an equality predicate. + return MayMatchAny(expr, std::span{&lit, 1}); } Result NotEq(const std::shared_ptr& expr, const Literal& lit) override { @@ -246,31 +227,7 @@ class InclusiveMetricsVisitor : public BoundVisitor { Result In(const std::shared_ptr& expr, 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)) { - return kRowCannotMatch; - } - - if (literal_set.size() > kInPredicateLimit) { - // skip evaluating the predicate if the number of values is too big - return kRowsMightMatch; - } - - ICEBERG_ASSIGN_OR_RAISE(auto lower, LowerBound(expr)); - if (lower.has_value() && lower->IsNaN()) { - // NaN indicates unreliable bounds. See the InclusiveMetricsEvaluator docs for more. - return kRowsMightMatch; - } - - ICEBERG_ASSIGN_OR_RAISE(auto upper, UpperBound(expr)); - const bool has_lower = lower.has_value() && !lower->IsNull(); - const bool has_upper = upper.has_value() && !upper->IsNull() && !upper->IsNaN(); - 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)); - }); + return MayMatchAny(expr, literal_set); } Result NotIn(const std::shared_ptr& expr, @@ -370,6 +327,38 @@ class InclusiveMetricsVisitor : public BoundVisitor { } private: + template + Result MayMatchAny(const std::shared_ptr& expr, const Literals& literals) { + const int32_t id = expr->reference()->field().field_id(); + if (ContainsNullsOnly(id)) { + return kRowCannotMatch; + } + if (literals.size() > kInPredicateLimit) { + return kRowsMightMatch; + } + if (ContainsNaNsOnly(id)) { + return std::ranges::any_of(literals, &Literal::IsNaN); + } + + ICEBERG_ASSIGN_OR_RAISE(auto lower, LowerBound(expr)); + ICEBERG_ASSIGN_OR_RAISE(auto upper, UpperBound(expr)); + if ((lower && lower->IsNaN()) || (upper && upper->IsNaN())) { + // A NaN at either end makes the file's bounds unreliable. + return kRowsMightMatch; + } + + auto outside_bounds = [&](const Literal& lit) { + if (lit.IsNaN()) { + // Min and max exclude NaNs. Only the NaN count can rule one out. + return !MayContainNaN(id); + } + // Each available bound can rule out a value. An unordered comparison cannot. + return (lower && lit < *lower) || (upper && lit > *upper); + }; + return std::ranges::all_of(literals, outside_bounds) ? kRowCannotMatch + : kRowsMightMatch; + } + bool MayContainNull(int32_t id) { return data_file_.null_value_counts.empty() || !data_file_.null_value_counts.contains(id) || diff --git a/src/iceberg/test/inclusive_metrics_evaluator_test.cc b/src/iceberg/test/inclusive_metrics_evaluator_test.cc index a129c528a..93dd5ff20 100644 --- a/src/iceberg/test/inclusive_metrics_evaluator_test.cc +++ b/src/iceberg/test/inclusive_metrics_evaluator_test.cc @@ -907,6 +907,9 @@ TEST_F(InclusiveMetricsEvaluatorMigratedTest, IntegerInTest) { RunTest(Expressions::In("id", {Literal::Long(kIntMaxValue), Literal::Long(kIntMaxValue + 1)}), kRowsMightMatch, file1_); + RunTest(Expressions::In( + "id", {Literal::Long(kIntMinValue - 1), Literal::Long(kIntMaxValue + 1)}), + kRowCannotMatch, file1_); RunTest(Expressions::In( "id", {Literal::Long(kIntMaxValue + 1), Literal::Long(kIntMaxValue + 2)}), kRowCannotMatch, file1_); @@ -945,6 +948,35 @@ TEST_F(InclusiveMetricsEvaluatorMigratedTest, InWithNaNAndMissingLowerBound) { kRowsMightMatch, file1_); } +TEST_F(InclusiveMetricsEvaluatorMigratedTest, InWithNaNValueCounts) { + RunTest(Expressions::In("all_nans", {Literal::Double(kDoubleNan)}), kRowsMightMatch, + file1_); + RunTest(Expressions::In("all_nans", {Literal::Double(kDoubleNan), Literal::Double(1)}), + kRowsMightMatch, file1_); + RunTest(Expressions::In("all_nans", {Literal::Double(1), Literal::Double(2)}), + kRowCannotMatch, file1_); + + file1_->upper_bounds[9] = Literal::Float(0).Serialize().value(); + auto expr = Expressions::In("no_nans", {Literal::Float(kFloatNan), Literal::Float(1)}); + RunTest(expr, kRowCannotMatch, file1_); + file1_->nan_value_counts.erase(9); + RunTest(expr, kRowsMightMatch, file1_); +} + +TEST_F(InclusiveMetricsEvaluatorMigratedTest, InWithNaNBound) { + auto expr = Expressions::In("some_nans", {Literal::Float(1), Literal::Float(2)}); + auto singleton = Expressions::In("some_nans", {Literal::Float(1)}); + file1_->lower_bounds[8] = Literal::Float(kFloatNan).Serialize().value(); + file1_->upper_bounds[8] = Literal::Float(0).Serialize().value(); + RunTest(expr, kRowsMightMatch, file1_); + RunTest(singleton, kRowsMightMatch, file1_); + + file1_->lower_bounds[8] = Literal::Float(3).Serialize().value(); + file1_->upper_bounds[8] = Literal::Float(kFloatNan).Serialize().value(); + RunTest(expr, kRowsMightMatch, file1_); + RunTest(singleton, kRowsMightMatch, file1_); +} + TEST_F(InclusiveMetricsEvaluatorMigratedTest, IntegerNotInTest) { RunTest(Expressions::NotIn( "id", {Literal::Long(kIntMinValue - 25), Literal::Long(kIntMinValue - 24)}),