diff --git a/src/iceberg/expression/inclusive_metrics_evaluator.cc b/src/iceberg/expression/inclusive_metrics_evaluator.cc index cb0ea6139..b27152011 100644 --- a/src/iceberg/expression/inclusive_metrics_evaluator.cc +++ b/src/iceberg/expression/inclusive_metrics_evaluator.cc @@ -177,7 +177,7 @@ class InclusiveMetricsVisitor : public BoundVisitor { } 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) { @@ -195,7 +195,7 @@ class InclusiveMetricsVisitor : public BoundVisitor { } 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) { @@ -220,7 +220,7 @@ class InclusiveMetricsVisitor : public BoundVisitor { } 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) { @@ -270,7 +270,7 @@ class InclusiveMetricsVisitor : public BoundVisitor { } 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) { diff --git a/src/iceberg/expression/strict_metrics_evaluator.cc b/src/iceberg/expression/strict_metrics_evaluator.cc index 878a7df92..9a1b8b300 100644 --- a/src/iceberg/expression/strict_metrics_evaluator.cc +++ b/src/iceberg/expression/strict_metrics_evaluator.cc @@ -156,6 +156,9 @@ class StrictMetricsVisitor : public BoundVisitor { 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; } @@ -184,6 +187,9 @@ class StrictMetricsVisitor : public BoundVisitor { 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; } @@ -326,6 +332,9 @@ class StrictMetricsVisitor : public BoundVisitor { 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; } @@ -408,6 +417,9 @@ class StrictMetricsVisitor : public BoundVisitor { 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; + } upper_bound = std::move(upper); } diff --git a/src/iceberg/test/inclusive_metrics_evaluator_test.cc b/src/iceberg/test/inclusive_metrics_evaluator_test.cc index e8c7b75ed..baaa62f48 100644 --- a/src/iceberg/test/inclusive_metrics_evaluator_test.cc +++ b/src/iceberg/test/inclusive_metrics_evaluator_test.cc @@ -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(); + 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 diff --git a/src/iceberg/test/strict_metrics_evaluator_test.cc b/src/iceberg/test/strict_metrics_evaluator_test.cc index 70ad2d97c..d6643a19d 100644 --- a/src/iceberg/test/strict_metrics_evaluator_test.cc +++ b/src/iceberg/test/strict_metrics_evaluator_test.cc @@ -1233,6 +1233,49 @@ TEST_F(StrictMetricsEvaluatorMigratedTest, NotStartsWithRejectsInvalidBind) { IsError(ErrorKind::kInvalidExpression)); } +TEST_F(StrictMetricsEvaluatorMigratedTest, NegativeNaNUpperBound) { + const float neg_float_nan = -std::numeric_limits::quiet_NaN(); + const double neg_double_nan = -std::numeric_limits::quiet_NaN(); + + auto check = [&](const std::string& col, int32_t field_id, std::optional lower, + const Literal& upper, const Literal& lit, const Literal& other) { + auto file = std::make_shared(); + 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