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
29 changes: 25 additions & 4 deletions src/iceberg/expression/strict_metrics_evaluator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -436,18 +436,39 @@ class StrictMetricsVisitor : public BoundVisitor<bool> {
}

bool CanContainNulls(int32_t id) {
auto field_result = schema_.GetFieldById(id);
if (field_result.has_value() && field_result->has_value() &&
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.

We should preserve error returned by schema_.GetFieldById, so we need to change the return type to Result<bool>. BTW, why calling GetFieldById instead of FindFieldById? They are semantically different.

!field_result->value().get().optional()) {
return false;
}

if (data_file_.null_value_counts.empty()) {
return true;
}
auto it = data_file_.null_value_counts.find(id);
return it != data_file_.null_value_counts.cend() && it->second > 0;
if (it == data_file_.null_value_counts.cend()) {
return true;
}
return it->second > 0;
}

bool CanContainNaNs(int32_t id) {
// nan counts might be null for early version writers when nan counters are not
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.

I think the original implementation matches the Java parity exactly, which assumes null_value_counts should populate all fields but nan_value_counts don't (when they are not empty). I agree current PR is a nice fix. I still suggest keeping this comment since it still holds.

// populated.
auto field_result = schema_.GetFieldById(id);
if (field_result.has_value() && field_result->has_value()) {
auto type_id = field_result->value().get().type()->type_id();
if (type_id != TypeId::kFloat && type_id != TypeId::kDouble) {
return false;
}
}

if (data_file_.nan_value_counts.empty()) {
return true;
}
auto it = data_file_.nan_value_counts.find(id);
return it != data_file_.nan_value_counts.cend() && it->second > 0;
if (it == data_file_.nan_value_counts.cend()) {
return true;
}
return it->second > 0;
}

bool ContainsNullsOnly(int32_t id) {
Expand Down
46 changes: 46 additions & 0 deletions src/iceberg/test/strict_metrics_evaluator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -846,4 +846,50 @@ TEST_F(StrictMetricsEvaluatorMigratedTest, EvaluateOnNestedColumnWithStats) {
ExpectShouldRead(Expressions::NotNull("struct.nested_col_with_stats"), false);
}

TEST_F(StrictMetricsEvaluatorMigratedTest, MissingNullCountForField) {
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.

This is not migrated from Java implementation so please do not use this confusing name.

// 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
// may exist and return kRowsMightNotMatch for comparison operators.
auto data_file = std::make_shared<DataFile>();
data_file->file_path = "null_test.parquet";
data_file->file_format = FileFormatType::kParquet;
data_file->record_count = 50;
data_file->value_counts = {{14, 50L}};
data_file->null_value_counts = {{4, 0L}, {5, 0L}};
data_file->nan_value_counts = {{14, 0L}};
data_file->lower_bounds = {{14, Literal::Double(1.0).Serialize().value()}};
data_file->upper_bounds = {{14, Literal::Double(100.0).Serialize().value()}};

ExpectShouldRead(Expressions::LessThan("no_nan_stats", Literal::Double(200.0)), false,
data_file);
ExpectShouldRead(Expressions::LessThanOrEqual("no_nan_stats", Literal::Double(200.0)),
false, data_file);
ExpectShouldRead(Expressions::GreaterThan("no_nan_stats", Literal::Double(-1.0)), false,
data_file);
ExpectShouldRead(Expressions::GreaterThanOrEqual("no_nan_stats", Literal::Double(-1.0)),
false, data_file);
ExpectShouldRead(Expressions::Equal("no_nan_stats", Literal::Double(50.0)), false,
data_file);
}
Comment thread
wgtmac marked this conversation as resolved.

TEST_F(StrictMetricsEvaluatorMigratedTest, MissingNanCountForField) {
// Field 14 (no_nan_stats, float64, optional) is missing from nan_value_counts.
// For a floating-point field, the evaluator must conservatively assume NaNs may
// exist and return kRowsMightNotMatch for comparison operators.
auto data_file = std::make_shared<DataFile>();
data_file->file_path = "nan_test.parquet";
data_file->file_format = FileFormatType::kParquet;
data_file->record_count = 50;
data_file->value_counts = {{14, 50L}};
data_file->null_value_counts = {{14, 0L}};
data_file->nan_value_counts = {{8, 0L}};
data_file->lower_bounds = {{14, Literal::Double(1.0).Serialize().value()}};
data_file->upper_bounds = {{14, Literal::Double(100.0).Serialize().value()}};

ExpectShouldRead(Expressions::LessThan("no_nan_stats", Literal::Double(200.0)), false,
data_file);
ExpectShouldRead(Expressions::GreaterThan("no_nan_stats", Literal::Double(-1.0)), false,
data_file);
}

} // namespace iceberg
Loading