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
8 changes: 4 additions & 4 deletions src/iceberg/expression/inclusive_metrics_evaluator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class InclusiveMetricsVisitor : public BoundVisitor<bool> {
}

ICEBERG_ASSIGN_OR_RAISE(auto upper, UpperBound(expr));
if (!upper.has_value() || upper->IsNull()) {
if (!upper.has_value() || upper->IsNull() || upper->IsNaN()) {
return kRowsMightMatch;
}
if (upper.value() <= lit) {
Expand All @@ -195,7 +195,7 @@ class InclusiveMetricsVisitor : public BoundVisitor<bool> {
}

ICEBERG_ASSIGN_OR_RAISE(auto upper, UpperBound(expr));
if (!upper.has_value() || upper->IsNull()) {
if (!upper.has_value() || upper->IsNull() || upper->IsNaN()) {
return kRowsMightMatch;
}
if (upper.value() < lit) {
Expand All @@ -220,7 +220,7 @@ class InclusiveMetricsVisitor : public BoundVisitor<bool> {
}

ICEBERG_ASSIGN_OR_RAISE(auto upper, UpperBound(expr));
if (!upper.has_value() || upper->IsNull()) {
if (!upper.has_value() || upper->IsNull() || upper->IsNaN()) {
return kRowsMightMatch;
}
if (upper.value() < lit) {
Expand Down Expand Up @@ -270,7 +270,7 @@ class InclusiveMetricsVisitor : public BoundVisitor<bool> {
}

ICEBERG_ASSIGN_OR_RAISE(auto upper, UpperBound(expr));
if (!upper.has_value() || upper->IsNull()) {
if (!upper.has_value() || upper->IsNull() || upper->IsNaN()) {
return kRowsMightMatch;
}
auto filtered_view = literals_view | std::views::filter([&](const Literal& lit) {
Expand Down
12 changes: 12 additions & 0 deletions src/iceberg/expression/strict_metrics_evaluator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ class StrictMetricsVisitor : public BoundVisitor<bool> {
auto it = data_file_.upper_bounds.find(id);
if (it != data_file_.upper_bounds.cend()) {
ICEBERG_ASSIGN_OR_RAISE(auto upper, ParseBound(expr, it->second));
if (upper.IsNaN()) {
return kRowsMightNotMatch;
}
if (upper < lit) {
return kRowsMustMatch;
}
Expand Down Expand Up @@ -184,6 +187,9 @@ class StrictMetricsVisitor : public BoundVisitor<bool> {
auto it = data_file_.upper_bounds.find(id);
if (it != data_file_.upper_bounds.cend()) {
ICEBERG_ASSIGN_OR_RAISE(auto upper, ParseBound(expr, it->second));
if (upper.IsNaN()) {
return kRowsMightNotMatch;
}
if (upper <= lit) {
return kRowsMustMatch;
}
Expand Down Expand Up @@ -326,6 +332,9 @@ class StrictMetricsVisitor : public BoundVisitor<bool> {
auto upper_it = data_file_.upper_bounds.find(id);
if (upper_it != data_file_.upper_bounds.cend()) {
ICEBERG_ASSIGN_OR_RAISE(auto upper, ParseBound(expr, upper_it->second));
if (upper.IsNaN()) {
return kRowsMightNotMatch;
}
if (upper < lit) {
return kRowsMustMatch;
}
Expand Down Expand Up @@ -408,6 +417,9 @@ class StrictMetricsVisitor : public BoundVisitor<bool> {
auto upper_it = data_file_.upper_bounds.find(id);
if (upper_it != data_file_.upper_bounds.cend()) {
ICEBERG_ASSIGN_OR_RAISE(auto upper, ParseBound(expr, upper_it->second));
if (upper.IsNaN()) {
return kRowsMightNotMatch;

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.

Strict NOT IN discards a usable lower bound. With lower = 10, x NOT IN (1, 2) must match every row, correct me if I'm wrong :-)

}
upper_bound = std::move(upper);
}

Expand Down
31 changes: 31 additions & 0 deletions src/iceberg/test/inclusive_metrics_evaluator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1185,4 +1185,35 @@ TEST_F(InclusiveMetricsEvaluatorMigratedTest,
Literal::Double(5.0), Literal::Double(10.0));
}

TEST_F(InclusiveMetricsEvaluatorMigratedTest, NegativeNaNUpperBound) {
auto check = [&](const std::string& col, int32_t field_id, const Literal& lower,
const Literal& upper, const Literal& lit, const Literal& other) {
auto file = std::make_shared<DataFile>();
file->file_path = "nan_bounds.orc";
file->file_format = FileFormatType::kOrc;
file->record_count = 50;
file->value_counts = {{field_id, 50L}};
file->null_value_counts = {{field_id, 0L}};
file->lower_bounds = {{field_id, lower.Serialize().value()}};
file->upper_bounds = {{field_id, upper.Serialize().value()}};

RunTest(Expressions::GreaterThan(col, lit), kRowsMightMatch, file);
RunTest(Expressions::GreaterThanOrEqual(col, lit), kRowsMightMatch, file);
RunTest(Expressions::Equal(col, lit), kRowsMightMatch, file);
RunTest(Expressions::In(col, {lit, other}), kRowsMightMatch, file);
};

// Both bounds -NaN (ORC stats when first value is -NaN).
check("no_nans", 9, Literal::Float(-kFloatNan), Literal::Float(-kFloatNan),
Literal::Float(50.0F), Literal::Float(60.0F));
check("no_nan_stats", 13, Literal::Double(-kDoubleNan), Literal::Double(-kDoubleNan),
Literal::Double(50.0), Literal::Double(60.0));

// Finite lower bound with -NaN upper bound (reaches upper check in In).
check("no_nans", 9, Literal::Float(1.0F), Literal::Float(-kFloatNan),
Literal::Float(50.0F), Literal::Float(60.0F));
check("no_nan_stats", 13, Literal::Double(1.0), Literal::Double(-kDoubleNan),
Literal::Double(50.0), Literal::Double(60.0));
}

} // namespace iceberg
43 changes: 43 additions & 0 deletions src/iceberg/test/strict_metrics_evaluator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,49 @@ TEST_F(StrictMetricsEvaluatorMigratedTest, NotStartsWithRejectsInvalidBind) {
IsError(ErrorKind::kInvalidExpression));
}

TEST_F(StrictMetricsEvaluatorMigratedTest, NegativeNaNUpperBound) {
const float neg_float_nan = -std::numeric_limits<float>::quiet_NaN();
const double neg_double_nan = -std::numeric_limits<double>::quiet_NaN();

auto check = [&](const std::string& col, int32_t field_id, std::optional<Literal> lower,
const Literal& upper, const Literal& lit, const Literal& other) {
auto file = std::make_shared<DataFile>();
file->file_path = "nan_bounds.orc";
file->file_format = FileFormatType::kOrc;
file->record_count = 50;
file->value_counts = {{field_id, 50L}};
file->null_value_counts = {{field_id, 0L}};
file->nan_value_counts = {{field_id, 0L}};
if (lower.has_value()) {
file->lower_bounds = {{field_id, lower->Serialize().value()}};
}
file->upper_bounds = {{field_id, upper.Serialize().value()}};

ExpectShouldRead(Expressions::LessThan(col, lit), kRowsMightNotMatch, file);
ExpectShouldRead(Expressions::LessThanOrEqual(col, lit), kRowsMightNotMatch, file);
ExpectShouldRead(Expressions::NotEqual(col, lit), kRowsMightNotMatch, file);
ExpectShouldRead(Expressions::NotIn(col, {lit, other}), kRowsMightNotMatch, file);
};

// Both bounds -NaN.
check("no_nans", 10, Literal::Float(neg_float_nan), Literal::Float(neg_float_nan),
Literal::Float(50.0F), Literal::Float(60.0F));
check("no_nan_stats", 14, Literal::Double(neg_double_nan),
Literal::Double(neg_double_nan), Literal::Double(50.0), Literal::Double(60.0));

// Missing lower bound with -NaN upper bound (reaches upper check in NotEq/NotIn).
check("no_nans", 10, std::nullopt, Literal::Float(neg_float_nan), Literal::Float(50.0F),
Literal::Float(60.0F));
check("no_nan_stats", 14, std::nullopt, Literal::Double(neg_double_nan),
Literal::Double(50.0), Literal::Double(60.0));

// Finite lower bound with -NaN upper bound.
check("no_nans", 10, Literal::Float(1.0F), Literal::Float(neg_float_nan),
Literal::Float(50.0F), Literal::Float(60.0F));
check("no_nan_stats", 14, Literal::Double(1.0), Literal::Double(neg_double_nan),
Literal::Double(50.0), Literal::Double(60.0));
}

TEST(StrictMetricsEvaluatorRegressionTest, MissingNullCountForField) {
// Field 14 (no_nan_stats, float64, optional) has bounds and value_counts but is
// missing from null_value_counts. The evaluator must conservatively assume nulls
Expand Down
Loading