diff --git a/src/iceberg/expression/inclusive_metrics_evaluator.cc b/src/iceberg/expression/inclusive_metrics_evaluator.cc index 6902e1d32..197beb78a 100644 --- a/src/iceberg/expression/inclusive_metrics_evaluator.cc +++ b/src/iceberg/expression/inclusive_metrics_evaluator.cc @@ -19,7 +19,8 @@ #include "iceberg/expression/inclusive_metrics_evaluator.h" -#include +#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,43 +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->IsNull() || 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; + return MayMatchAny(expr, literal_set); } Result NotIn(const std::shared_ptr& expr, @@ -382,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 65fb62893..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_); @@ -928,6 +931,52 @@ 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, 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)}),