Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 37 additions & 60 deletions src/iceberg/expression/inclusive_metrics_evaluator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@

#include "iceberg/expression/inclusive_metrics_evaluator.h"

#include <ranges>
#include <algorithm>
#include <span>
#include <string_view>
#include <utility>

Expand Down Expand Up @@ -207,28 +208,8 @@ class InclusiveMetricsVisitor : public BoundVisitor<bool> {
}

Result<bool> Eq(const std::shared_ptr<Bound>& 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<bool> NotEq(const std::shared_ptr<Bound>& expr, const Literal& lit) override {
Expand All @@ -246,43 +227,7 @@ class InclusiveMetricsVisitor : public BoundVisitor<bool> {

Result<bool> In(const std::shared_ptr<Bound>& 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<bool> NotIn(const std::shared_ptr<Bound>& expr,
Expand Down Expand Up @@ -382,6 +327,38 @@ class InclusiveMetricsVisitor : public BoundVisitor<bool> {
}

private:
template <typename Literals>
Result<bool> MayMatchAny(const std::shared_ptr<Bound>& 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) ||
Expand Down
49 changes: 49 additions & 0 deletions src/iceberg/test/inclusive_metrics_evaluator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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_);
Expand All @@ -928,6 +931,52 @@ TEST_F(InclusiveMetricsEvaluatorMigratedTest, IntegerInTest) {
RunTest(Expressions::In("id", ids), kRowsMightMatch, file1_);
}

TEST_F(InclusiveMetricsEvaluatorMigratedTest, InWithMissingLowerBound) {
auto file = std::make_shared<DataFile>();
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)}),
Expand Down
Loading