This is an automated email from the ASF dual-hosted git repository.

zhjwpku pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg-cpp.git


The following commit(s) were added to refs/heads/main by this push:
     new ce9b4f6a fix(expression): handle missing nan_value_counts in 
InclusiveMetricsEvaluator for float/double (#1155)
ce9b4f6a is described below

commit ce9b4f6ae055299ae1050f96a4aa60a5a1e4e33e
Author: Deepak N <[email protected]>
AuthorDate: Sun Nov 22 18:15:02 2026 -0800

    fix(expression): handle missing nan_value_counts in 
InclusiveMetricsEvaluator for float/double (#1155)
    
    ### **Problem**
    
    This is a corner-case correctness issue in 
InclusiveMetricsEvaluator::UniqueValue for float and double columns when 
nan_value_counts is unset by a writer.
    
    Per the Iceberg spec 
(https://iceberg.apache.org/spec/#field-level-metrics-and-statistics), 
lower_bounds and upper_bounds don't consider NaNs, and nan_value_counts is 
optional (a missing entry means unknown, not 0).
    
    **As an example**: Let's assume a file has only two float values {5.0, NaN} 
with null_value_counts = 0 and nan_value_counts is **not set**.
    
    In this case, both lower and upper bounds are 5.0. The existing 
implementation for UniqueValue is matching it when it should not, and both 
NotEq(col, 5.0) and NotIn(col, {5.0}) incorrectly prune the file.
    
    ### **Fix**
    
    Mostly ported from Java's InclusiveEvalVisitor.uniqueValue / 
InclusiveMetricsEvaluator.mayContainNaN.
    
    Added a MayContainNaN() helper.
    
    References:
    * Java [uniqueValue]( 
https://github.com/apache/iceberg/blob/main/api/src/main/java/org/apache/iceberg/expressions/InclusiveEvalVisitor.java#L437-L459)
    * Java 
[mayContainNaNs](https://github.com/apache/iceberg/blob/main/api/src/main/java/org/apache/iceberg/expressions/InclusiveMetricsEvaluator.java#L113-L116)
    * C++ 
[CanContainNaNs](https://github.com/apache/iceberg-cpp/blob/main/src/iceberg/expression/strict_metrics_evaluator.cc#L472-L491)
    
    ### **Tests**
    
    Added a new unit test covering both float and double columns across zero, 
empty, missing field ID, and non-zero nan_value_counts and null_value_counts.
    
    Java TestInclusiveMetricsEvaluator also seems to be missing a test for 
missing nan_value_counts on float/double (but the implementation looks 
correct). We can open a separate test PR in apache/iceberg for the test.
---
 .../expression/inclusive_metrics_evaluator.cc      | 13 +++-
 .../test/inclusive_metrics_evaluator_test.cc       | 90 +++++++++++++++++++++-
 2 files changed, 97 insertions(+), 6 deletions(-)

diff --git a/src/iceberg/expression/inclusive_metrics_evaluator.cc 
b/src/iceberg/expression/inclusive_metrics_evaluator.cc
index aa79472f..d16e58ee 100644
--- a/src/iceberg/expression/inclusive_metrics_evaluator.cc
+++ b/src/iceberg/expression/inclusive_metrics_evaluator.cc
@@ -91,8 +91,7 @@ class InclusiveMetricsVisitor : public BoundVisitor<bool> {
     if (dynamic_cast<const BoundReference*>(expr.get()) == nullptr) {
       return kRowsMightMatch;
     }
-    auto it = data_file_.nan_value_counts.find(id);
-    if (it != data_file_.nan_value_counts.end() && it->second == 0) {
+    if (!MayContainNaN(id)) {
       return kRowCannotMatch;
     }
     return kRowsMightMatch;
@@ -397,6 +396,11 @@ class InclusiveMetricsVisitor : public BoundVisitor<bool> {
            val_it->second == null_it->second;
   }
 
+  bool MayContainNaN(int32_t id) {
+    auto it = data_file_.nan_value_counts.find(id);
+    return it == data_file_.nan_value_counts.cend() || it->second != 0;
+  }
+
   bool ContainsNaNsOnly(int32_t id) {
     auto val_it = data_file_.value_counts.find(id);
     auto nan_it = data_file_.nan_value_counts.find(id);
@@ -447,8 +451,9 @@ class InclusiveMetricsVisitor : public BoundVisitor<bool> {
       return std::nullopt;
     }
 
-    auto nan_it = data_file_.nan_value_counts.find(id);
-    if (nan_it != data_file_.nan_value_counts.cend() && nan_it->second != 0) {
+    // Float and double bounds exclude NaNs, so require nan_value_counts == 0.
+    const auto type_id = lower->type()->type_id();
+    if ((type_id == TypeId::kFloat || type_id == TypeId::kDouble) && 
MayContainNaN(id)) {
       return std::nullopt;
     }
 
diff --git a/src/iceberg/test/inclusive_metrics_evaluator_test.cc 
b/src/iceberg/test/inclusive_metrics_evaluator_test.cc
index f4e24ffe..e8c7b75e 100644
--- a/src/iceberg/test/inclusive_metrics_evaluator_test.cc
+++ b/src/iceberg/test/inclusive_metrics_evaluator_test.cc
@@ -19,6 +19,10 @@
 
 #include "iceberg/expression/inclusive_metrics_evaluator.h"
 
+#include <map>
+#include <string>
+#include <vector>
+
 #include <gtest/gtest.h>
 
 #include "iceberg/expression/binder.h"
@@ -966,7 +970,6 @@ TEST_F(InclusiveMetricsEvaluatorMigratedTest, 
NotEqWithSingleValue) {
   single_value->record_count = 10;
   single_value->value_counts = {{3, 10L}};
   single_value->null_value_counts = {{3, 0L}};
-  single_value->nan_value_counts = {{3, 0L}};
   single_value->lower_bounds = {{3, 
Literal::String("abc").Serialize().value()}};
   single_value->upper_bounds = {{3, 
Literal::String("abc").Serialize().value()}};
   // single value equals the literal -> rows cannot match
@@ -1042,7 +1045,6 @@ TEST_F(InclusiveMetricsEvaluatorMigratedTest, 
NotInWithSingleValue) {
   single_value->record_count = 10;
   single_value->value_counts = {{3, 10L}};
   single_value->null_value_counts = {{3, 0L}};
-  single_value->nan_value_counts = {{3, 0L}};
   single_value->lower_bounds = {{3, 
Literal::String("abc").Serialize().value()}};
   single_value->upper_bounds = {{3, 
Literal::String("abc").Serialize().value()}};
   // single value is in the exclusion set -> rows cannot match
@@ -1099,4 +1101,88 @@ TEST_F(InclusiveMetricsEvaluatorMigratedTest, 
NotInWithSingleValue) {
           single_value_nan_bounds);
 }
 
+// Per the spec:
+// 1. float/double upper and lower bounds don't cover nulls or NaNs.
+// 2. null_value_counts and nan_value_counts are optional.
+// 3. only float/double values can have NaNs, and nan_value_counts set.
+// Therefore, single-value (aka UniqueValue) NotEq/NotIn pruning for 
float/double is only
+// possible when both null_value_counts and nan_value_counts are explicitly 
set to 0 for
+// the column, and must not prune when either count is missing or non-zero.
+TEST_F(InclusiveMetricsEvaluatorMigratedTest,
+       UniqueValuePruningFloatAndDoubleNaNAndNullCounts) {
+  auto check_column = [&](const std::string& col_name, int32_t field_id,
+                          int32_t other_field_id, const Literal& match_val,
+                          const Literal& other_val) {
+    SCOPED_TRACE(col_name);
+    auto file = std::make_shared<DataFile>();
+    file->file_path = "single_value";
+    file->file_format = FileFormatType::kParquet;
+    file->record_count = 10;
+    file->value_counts = {{field_id, 10L}};
+    auto bound = match_val.Serialize().value();
+    file->lower_bounds = {{field_id, bound}};
+    file->upper_bounds = {{field_id, bound}};
+
+    struct Case {
+      std::string label;
+      std::map<int32_t, int64_t> null_counts;
+      std::map<int32_t, int64_t> nan_counts;
+      bool expected;
+    };
+
+    const std::vector<Case> cases = {
+        // Explicit 0 nulls and 0 NaNs: safe to prune when the single value is 
excluded.
+        {.label = "zero_nulls_and_nans",
+         .null_counts = {{field_id, 0L}},
+         .nan_counts = {{field_id, 0L}},
+         .expected = kRowCannotMatch},
+        // Missing or non-zero nan_value_counts: file may contain NaN rows 
outside
+        // [lower, upper], so must not prune.
+        {.label = "empty_nan_counts",
+         .null_counts = {{field_id, 0L}},
+         .nan_counts = {},
+         .expected = kRowsMightMatch},
+        {.label = "missing_field_nan_count",
+         .null_counts = {{field_id, 0L}},
+         .nan_counts = {{other_field_id, 0L}},
+         .expected = kRowsMightMatch},
+        {.label = "non_zero_nans",
+         .null_counts = {{field_id, 0L}},
+         .nan_counts = {{field_id, 2L}},
+         .expected = kRowsMightMatch},
+        // Missing or non-zero null_value_counts: file may contain nulls even 
when
+        // nan_value_counts is 0, so must not prune.
+        {.label = "empty_null_counts",
+         .null_counts = {},
+         .nan_counts = {{field_id, 0L}},
+         .expected = kRowsMightMatch},
+        {.label = "missing_field_null_count",
+         .null_counts = {{other_field_id, 0L}},
+         .nan_counts = {{field_id, 0L}},
+         .expected = kRowsMightMatch},
+        {.label = "non_zero_nulls",
+         .null_counts = {{field_id, 2L}},
+         .nan_counts = {{field_id, 0L}},
+         .expected = kRowsMightMatch},
+    };
+
+    for (const auto& tc : cases) {
+      SCOPED_TRACE(tc.label);
+      file->null_value_counts = tc.null_counts;
+      file->nan_value_counts = tc.nan_counts;
+      RunTest(Expressions::NotEqual(col_name, match_val), tc.expected, file);
+      RunTest(Expressions::NotIn(col_name, {match_val, other_val}), 
tc.expected, file);
+      // A literal different from the file's single value can still match rows.
+      RunTest(Expressions::NotEqual(col_name, other_val), kRowsMightMatch, 
file);
+      RunTest(Expressions::NotIn(col_name, {other_val}), kRowsMightMatch, 
file);
+    }
+  };
+
+  // Check for both float and double columns.
+  check_column("no_nans", /*field_id=*/9, /*other_field_id=*/13, 
Literal::Float(5.0F),
+               Literal::Float(10.0F));
+  check_column("no_nan_stats", /*field_id=*/13, /*other_field_id=*/9,
+               Literal::Double(5.0), Literal::Double(10.0));
+}
+
 }  // namespace iceberg

Reply via email to