You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by "dongxiao1198 (via GitHub)" <gi...@apache.org> on 2025/11/27 07:44:24 UTC
[PR] feat: add inclusive metrics evaluator [iceberg-cpp]
dongxiao1198 opened a new pull request, #357:
URL: https://github.com/apache/iceberg-cpp/pull/357
feat: add inclusive metrics evaluator
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: add inclusive metrics evaluator [iceberg-cpp]
Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac commented on code in PR #357:
URL: https://github.com/apache/iceberg-cpp/pull/357#discussion_r2579505628
##########
src/iceberg/expression/inclusive_metrics_evaluator.cc:
##########
@@ -0,0 +1,546 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "iceberg/expression/inclusive_metrics_evaluator.h"
+
+#include "iceberg/expression/binder.h"
+#include "iceberg/expression/expression_visitor.h"
+#include "iceberg/expression/rewrite_not.h"
+#include "iceberg/manifest/manifest_entry.h"
+#include "iceberg/schema.h"
+#include "iceberg/transform.h"
+#include "iceberg/util/macros.h"
+
+namespace iceberg {
+
+namespace {
+constexpr bool kRowsMightMatch = true;
+constexpr bool kRowCannotMatch = false;
+constexpr int32_t kInPredicateLimit = 200;
+} // namespace
+
+class InclusiveMetricsVisitor : public BoundVisitor<bool> {
+ public:
+ explicit InclusiveMetricsVisitor(const DataFile& data_file) : data_file_(data_file) {}
+
+ Result<bool> AlwaysTrue() override { return kRowsMightMatch; }
+
+ Result<bool> AlwaysFalse() override { return kRowCannotMatch; }
+
+ Result<bool> Not(bool child_result) override { return !child_result; }
+
+ Result<bool> And(bool left_result, bool right_result) override {
+ return left_result && right_result;
+ }
+
+ Result<bool> Or(bool left_result, bool right_result) override {
+ return left_result || right_result;
+ }
+
+ Result<bool> IsNull(const std::shared_ptr<Bound>& expr) override {
+ // no need to check whether the field is required because binding evaluates that case
+ // if the column has no null values, the expression cannot match
+ if (IsNonNullPreserving(expr)) {
+ // number of non-nulls is the same as for the ref
+ int32_t id = expr->reference()->field().field_id();
+ if (!MayContainNull(id)) {
+ return kRowCannotMatch;
+ }
+ }
+ return kRowsMightMatch;
+ }
+
+ Result<bool> NotNull(const std::shared_ptr<Bound>& expr) override {
+ // no need to check whether the field is required because binding evaluates that case
+ // if the column has no non-null values, the expression cannot match
+
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int32_t id = expr->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return kRowCannotMatch;
+ }
+
+ return kRowsMightMatch;
+ }
+
+ Result<bool> IsNaN(const std::shared_ptr<Bound>& expr) override {
+ // when there's no nanCounts information, but we already know the column only contains
+ // null, it's guaranteed that there's no NaN value
+ int32_t id = expr->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return kRowCannotMatch;
+ }
+ if (std::dynamic_pointer_cast<BoundReference>(expr) == nullptr) {
+ return kRowsMightMatch;
+ }
+ auto it = data_file_.nan_value_counts.find(id);
+ if (it != data_file_.nan_value_counts.end() && it->second == 0) {
+ return kRowCannotMatch;
+ }
+ return kRowsMightMatch;
+ }
+
+ Result<bool> NotNaN(const std::shared_ptr<Bound>& expr) override {
+ if (std::dynamic_pointer_cast<BoundReference>(expr) == nullptr) {
+ // identity transforms are already removed by this time
+ return kRowsMightMatch;
+ }
+
+ int32_t id = expr->reference()->field().field_id();
+
+ if (ContainsNaNsOnly(id)) {
+ return kRowCannotMatch;
+ }
+
+ return kRowsMightMatch;
+ }
+
+ Result<bool> Lt(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_result, LowerBound(expr));
+ if (!lower_result.has_value() || lower_result.value().IsNull() ||
+ lower_result.value().IsNaN()) {
+ // NaN indicates unreliable bounds. See the InclusiveMetricsEvaluator docs for more.
+ return kRowsMightMatch;
+ }
+ const auto& lower = lower_result.value();
+
+ // this also works for transforms that are order preserving:
+ // if a transform f is order preserving, a < b means that f(a) <= f(b).
+ // because lower <= a for all values of a in the file, f(lower) <= f(a).
+ // when f(lower) >= X then f(a) >= f(lower) >= X, so there is no a such that f(a) < X
+ // f(lower) >= X means rows cannot match
+ if (lower >= lit) {
+ return kRowCannotMatch;
+ }
+
+ return kRowsMightMatch;
+ }
+
+ Result<bool> LtEq(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_result, LowerBound(expr));
+ if (!lower_result.has_value() || lower_result.value().IsNull() ||
+ lower_result.value().IsNaN()) {
+ // NaN indicates unreliable bounds. See the InclusiveMetricsEvaluator docs for more.
+ return kRowsMightMatch;
+ }
+ const auto& lower = lower_result.value();
+
+ // this also works for transforms that are order preserving:
+ // if a transform f is order preserving, a < b means that f(a) <= f(b).
+ // because lower <= a for all values of a in the file, f(lower) <= f(a).
+ // when f(lower) > X then f(a) >= f(lower) > X, so there is no a such that f(a) <= X
+ // f(lower) > X means rows cannot match
+ if (lower > lit) {
+ return kRowCannotMatch;
+ }
+
+ return kRowsMightMatch;
+ }
+
+ Result<bool> Gt(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 upper_result, UpperBound(expr));
+ if (!upper_result.has_value() || upper_result.value().IsNull()) {
+ return kRowsMightMatch;
+ }
+ const auto& upper = upper_result.value();
+
+ if (upper <= lit) {
+ return kRowCannotMatch;
+ }
+
+ return kRowsMightMatch;
+ }
+
+ Result<bool> GtEq(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 upper_result, UpperBound(expr));
+ if (!upper_result.has_value() || upper_result.value().IsNull()) {
+ return kRowsMightMatch;
+ }
+ const auto& upper = upper_result.value();
+ if (upper < lit) {
+ return kRowCannotMatch;
+ }
+
+ return kRowsMightMatch;
+ }
+
+ 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_result, LowerBound(expr));
+ if (lower_result.has_value() && !lower_result.value().IsNull() &&
+ !lower_result.value().IsNaN()) {
+ const auto& lower = lower_result.value();
+ if (!lower.IsNaN() && lower > lit) {
+ return kRowCannotMatch;
+ }
+ }
+
+ ICEBERG_ASSIGN_OR_RAISE(auto upper_result, UpperBound(expr));
+ if (!upper_result.has_value() || upper_result.value().IsNull()) {
+ return kRowsMightMatch;
+ }
+ const auto& upper = upper_result.value();
+ if (upper < lit) {
+ return kRowCannotMatch;
+ }
+
+ return kRowsMightMatch;
+ }
+
+ Result<bool> NotEq(const std::shared_ptr<Bound>& expr, const Literal& lit) override {
+ // because the bounds are not necessarily a min or max value, this cannot be answered
+ // using them. notEq(col, X) with (X, Y) doesn't guarantee that X is a value in col.
+ return kRowsMightMatch;
+ }
+
+ 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_result, LowerBound(expr));
+ if (!lower_result.has_value() || lower_result.value().IsNull() ||
+ lower_result.value().IsNaN()) {
+ // NaN indicates unreliable bounds. See the InclusiveMetricsEvaluator docs for more.
+ return kRowsMightMatch;
+ }
+ const auto& lower = lower_result.value();
+ auto literals_view = literal_set | std::views::filter([&](const Literal& lit) {
+ return lower <= lit;
+ });
+ // if all values are less than lower bound, rows cannot match
+ if (literals_view.empty()) {
+ return kRowCannotMatch;
+ }
+
+ ICEBERG_ASSIGN_OR_RAISE(auto upper_result, UpperBound(expr));
+ if (!upper_result.has_value() || upper_result.value().IsNull()) {
+ return kRowsMightMatch;
+ }
+ const auto& upper = upper_result.value();
+ auto filtered_view = literals_view | std::views::filter([&](const Literal& lit) {
+ return upper >= lit;
+ });
+ // if remaining values are greater than upper bound, rows cannot match
+ if (filtered_view.empty()) {
+ return kRowCannotMatch;
+ }
+
+ return kRowsMightMatch;
+ }
+
+ Result<bool> NotIn(const std::shared_ptr<Bound>& expr,
+ const BoundSetPredicate::LiteralSet& literal_set) override {
+ // because the bounds are not necessarily a min or max value, this cannot be answered
+ // using them. notIn(col, {X, ...}) with (X, Y) doesn't guarantee that X is a value in
+ // col.
+ return kRowsMightMatch;
+ }
+
+ Result<bool> StartsWith(const std::shared_ptr<Bound>& expr,
+ const Literal& lit) override {
+ auto transform = std::dynamic_pointer_cast<BoundTransform>(expr);
+ if (transform != nullptr &&
+ transform->transform()->transform_type() != TransformType::kIdentity) {
+ // truncate must be rewritten in binding. the result is either always or never
+ // compatible
+ return kRowsMightMatch;
+ }
+
+ int32_t id = expr->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return kRowCannotMatch;
+ }
+ if (lit.type()->type_id() != TypeId::kString) {
+ return kRowCannotMatch;
+ }
+ const auto& prefix = get<std::string>(lit.value());
+
+ ICEBERG_ASSIGN_OR_RAISE(auto lower_result, LowerBound(expr));
+ if (!lower_result.has_value() || lower_result.value().IsNull()) {
+ return kRowsMightMatch;
+ }
+ const auto& lower = lower_result.value();
+ const auto& lower_str = get<std::string>(lower.value());
+ // truncate lower bound so that its length in bytes is not greater than the length of
+ // prefix
+ int length = std::min(prefix.size(), lower_str.size());
+ // if prefix of lower bound is greater than prefix, rows cannot match
+ if (lower_str.substr(0, length) > prefix) {
+ return kRowCannotMatch;
+ }
+
+ ICEBERG_ASSIGN_OR_RAISE(auto upper_result, UpperBound(expr));
+ if (!upper_result.has_value() || upper_result.value().IsNull()) {
+ return kRowsMightMatch;
+ }
+ const auto& upper = upper_result.value();
+ const auto& upper_str = get<std::string>(upper.value());
+ // truncate upper bound so that its length in bytes is not greater than the length of
+ // prefix
+ length = std::min(prefix.size(), upper_str.size());
+ // if prefix of upper bound is less than prefix, rows cannot match
+ if (upper_str.substr(0, length) < prefix) {
+ return kRowCannotMatch;
+ }
+
+ return kRowsMightMatch;
+ }
+
+ Result<bool> NotStartsWith(const std::shared_ptr<Bound>& expr,
+ const Literal& lit) override {
+ // the only transforms that produce strings are truncate and identity, which work with
+ // this
+ int32_t id = expr->reference()->field().field_id();
+ if (MayContainNull(id)) {
+ return kRowsMightMatch;
+ }
+
+ if (lit.type()->type_id() != TypeId::kString) {
+ return kRowCannotMatch;
+ }
+ const auto& prefix = get<std::string>(lit.value());
+
+ // notStartsWith will match unless all values must start with the prefix. This happens
+ // when the lower and upper bounds both start with the prefix.
+ ICEBERG_ASSIGN_OR_RAISE(auto lower_result, LowerBound(expr));
+ ICEBERG_ASSIGN_OR_RAISE(auto upper_result, UpperBound(expr));
+ if (!lower_result.has_value() || lower_result.value().IsNull() ||
+ !upper_result.has_value() || upper_result.value().IsNull()) {
+ return kRowsMightMatch;
+ }
+ const auto& lower = lower_result.value();
+ const auto& upper = upper_result.value();
+ const auto& lower_str = get<std::string>(lower.value());
+ const auto& upper_str = get<std::string>(upper.value());
+
+ // if lower is shorter than the prefix then lower doesn't start with the prefix
+ if (lower_str.size() < prefix.size()) {
+ return kRowsMightMatch;
+ }
+
+ if (lower_str.starts_with(prefix)) {
+ // if upper is shorter than the prefix then upper can't start with the prefix
+ if (upper_str.size() < prefix.size()) {
+ return kRowsMightMatch;
+ }
+ if (upper_str.starts_with(prefix)) {
+ // both bounds match the prefix, so all rows must match the prefix and therefore
+ // do not satisfy the predicate
+ return kRowCannotMatch;
+ }
+ }
+
+ return kRowsMightMatch;
+ }
+
+ private:
+ bool MayContainNull(int32_t id) {
+ return data_file_.null_value_counts.empty() ||
+ !data_file_.null_value_counts.contains(id) ||
+ data_file_.null_value_counts.at(id) != 0;
+ }
+
+ bool ContainsNullsOnly(int32_t id) {
+ auto val_it = data_file_.value_counts.find(id);
+ auto null_it = data_file_.null_value_counts.find(id);
+ return val_it != data_file_.value_counts.end() &&
+ null_it != data_file_.null_value_counts.end() &&
+ val_it->second - null_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);
+ return val_it != data_file_.value_counts.end() &&
+ nan_it != data_file_.nan_value_counts.end() &&
+ val_it->second == nan_it->second;
+ }
+
+ Result<std::optional<Literal>> LowerBound(const std::shared_ptr<Bound>& expr) {
+ if (auto reference = std::dynamic_pointer_cast<BoundReference>(expr);
+ reference != nullptr) {
+ return ParseLowerBound(*reference);
+ } else if (auto transform = std::dynamic_pointer_cast<BoundTransform>(expr);
+ transform != nullptr) {
+ return TransformLowerBound(*transform);
+ } else {
+ return std::nullopt;
+ }
+ // TODO(xiao.dong) handle extract lower and upper bounds
+ }
+
+ Result<std::optional<Literal>> UpperBound(const std::shared_ptr<Bound>& expr) {
+ if (auto reference = std::dynamic_pointer_cast<BoundReference>(expr);
+ reference != nullptr) {
+ return ParseUpperBound(*reference);
+ } else if (auto transform = std::dynamic_pointer_cast<BoundTransform>(expr);
+ transform != nullptr) {
+ return TransformUpperBound(*transform);
+ } else {
+ return std::nullopt;
+ }
+ // TODO(xiao.dong) handle extract lower and upper bounds
+ }
+
+ Result<std::optional<Literal>> ParseLowerBound(const BoundReference& ref) {
+ int32_t id = ref.field().field_id();
+ auto type = ref.type();
+ if (!type->is_primitive()) {
+ return NotSupported("Lower bound of non-primitive type is not supported.");
+ }
+ auto primitive_type = internal::checked_pointer_cast<PrimitiveType>(type);
+ if (!data_file_.lower_bounds.empty() && data_file_.lower_bounds.contains(id)) {
+ ICEBERG_ASSIGN_OR_RAISE(
+ auto lower,
+ Literal::Deserialize(data_file_.lower_bounds.at(id), primitive_type));
+ return lower;
+ }
+
+ return std::nullopt;
+ }
+
+ Result<std::optional<Literal>> ParseUpperBound(const BoundReference& ref) {
+ int32_t id = ref.field().field_id();
+ auto type = ref.type();
+ if (!type->is_primitive()) {
+ return NotSupported("Upper bound of non-primitive type is not supported.");
+ }
+ auto primitive_type = internal::checked_pointer_cast<PrimitiveType>(type);
+ if (!data_file_.upper_bounds.empty() && data_file_.upper_bounds.contains(id)) {
+ ICEBERG_ASSIGN_OR_RAISE(
+ auto upper,
+ Literal::Deserialize(data_file_.upper_bounds.at(id), primitive_type));
+ return upper;
+ }
+
+ return std::nullopt;
+ }
+
+ Result<std::optional<Literal>> TransformLowerBound(BoundTransform& boundTransform) {
+ auto transform = boundTransform.transform();
+ if (transform->PreservesOrder()) {
+ ICEBERG_ASSIGN_OR_RAISE(auto lower, ParseLowerBound(*boundTransform.reference()));
+ ICEBERG_ASSIGN_OR_RAISE(auto transform_func,
+ transform->Bind(boundTransform.reference()->type()));
+ if (lower.has_value()) {
+ return transform_func->Transform(lower.value());
+ }
+ }
+
+ return std::nullopt;
+ }
+
+ Result<std::optional<Literal>> TransformUpperBound(BoundTransform& boundTransform) {
+ auto transform = boundTransform.transform();
+ if (transform->PreservesOrder()) {
+ ICEBERG_ASSIGN_OR_RAISE(auto upper, ParseLowerBound(*boundTransform.reference()));
Review Comment:
```suggestion
ICEBERG_ASSIGN_OR_RAISE(auto upper, ParseUpperBound(*boundTransform.reference()));
```
We might need to port the TestInclusiveMetricsEvaluatorWithTransforms as well.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: add inclusive metrics evaluator [iceberg-cpp]
Posted by "HeartLinked (via GitHub)" <gi...@apache.org>.
HeartLinked commented on code in PR #357:
URL: https://github.com/apache/iceberg-cpp/pull/357#discussion_r2572105075
##########
src/iceberg/test/inclusive_metrics_evaluator_test.cc:
##########
@@ -0,0 +1,385 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "iceberg/expression/inclusive_metrics_evaluator.h"
+
+#include <gtest/gtest.h>
+
+#include "iceberg/expression/binder.h"
+#include "iceberg/expression/expressions.h"
+#include "iceberg/manifest_entry.h"
+#include "iceberg/schema.h"
+#include "iceberg/test/matchers.h"
+#include "iceberg/type.h"
+
+namespace iceberg {
+
+namespace {
+static const bool ROWS_MIGHT_MATCH = true;
+static const bool ROWS_CANNOT_MATCH = false;
+} // namespace
+using TestVariant = std::variant<bool, int32_t, int64_t, double, std::string>;
+
+class InclusiveMetricsEvaluatorTest : public ::testing::Test {
+ protected:
+ void SetUp() override {
+ schema_ = std::make_shared<Schema>(
+ std::vector<SchemaField>{
+ SchemaField::MakeRequired(1, "id", int64()),
+ SchemaField::MakeOptional(2, "name", string()),
+ SchemaField::MakeRequired(3, "age", int32()),
+ SchemaField::MakeOptional(4, "salary", float64()),
+ SchemaField::MakeRequired(5, "active", boolean()),
+ SchemaField::MakeRequired(6, "date", string()),
+ },
+ /*schema_id=*/0);
+ }
+
+ Result<std::shared_ptr<Expression>> Bind(const std::shared_ptr<Expression>& expr,
+ bool case_sensitive = true) {
+ return Binder::Bind(*schema_, expr, case_sensitive);
+ }
+
+ std::shared_ptr<DataFile> PrepareDataFile(
+ const std::string& partition, int64_t record_count, int64_t file_size_in_bytes,
+ const std::map<std::string, TestVariant>& lower_bounds,
+ const std::map<std::string, TestVariant>& upper_bounds,
+ const std::map<int32_t, int64_t>& value_counts = {},
+ const std::map<int32_t, int64_t>& null_counts = {},
+ const std::map<int32_t, int64_t>& nan_counts = {}) {
+ auto parse_bound = [&](const std::map<std::string, TestVariant>& bounds,
+ std::map<int32_t, std::vector<uint8_t>>& bound_values) {
+ for (const auto& [key, value] : bounds) {
+ if (key == "id") {
+ bound_values[1] = Literal::Long(std::get<int64_t>(value)).Serialize().value();
+ } else if (key == "name") {
+ bound_values[2] =
+ Literal::String(std::get<std::string>(value)).Serialize().value();
+ } else if (key == "age") {
+ bound_values[3] = Literal::Int(std::get<int32_t>(value)).Serialize().value();
+ } else if (key == "salary") {
+ bound_values[4] = Literal::Double(std::get<double>(value)).Serialize().value();
+ } else if (key == "active") {
+ bound_values[5] = Literal::Boolean(std::get<bool>(value)).Serialize().value();
+ }
+ }
+ };
+
+ auto data_file = std::make_shared<DataFile>();
Review Comment:
You can use designated initializers here.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: add inclusive metrics evaluator [iceberg-cpp]
Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac commented on code in PR #357:
URL: https://github.com/apache/iceberg-cpp/pull/357#discussion_r2575523873
##########
src/iceberg/expression/inclusive_metrics_evaluator.cc:
##########
@@ -0,0 +1,544 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "iceberg/expression/inclusive_metrics_evaluator.h"
+
+#include "iceberg/expression/binder.h"
+#include "iceberg/expression/expression_visitor.h"
+#include "iceberg/expression/rewrite_not.h"
+#include "iceberg/manifest_entry.h"
+#include "iceberg/schema.h"
+#include "iceberg/transform.h"
+#include "iceberg/util/macros.h"
+
+namespace iceberg {
+
+namespace {
+static const bool ROWS_MIGHT_MATCH = true;
+static const bool ROWS_CANNOT_MATCH = false;
+static const int32_t IN_PREDICATE_LIMIT = 200;
+} // namespace
+
+class InclusiveMetricsVisitor : public BoundVisitor<bool> {
+ public:
+ explicit InclusiveMetricsVisitor(const DataFile& data_file) : data_file_(data_file) {}
+
+ Result<bool> AlwaysTrue() override { return ROWS_MIGHT_MATCH; }
+
+ Result<bool> AlwaysFalse() override { return ROWS_CANNOT_MATCH; }
+
+ Result<bool> Not(bool child_result) override { return !child_result; }
+
+ Result<bool> And(bool left_result, bool right_result) override {
+ return left_result && right_result;
+ }
+
+ Result<bool> Or(bool left_result, bool right_result) override {
+ return left_result || right_result;
+ }
+
+ Result<bool> IsNull(const std::shared_ptr<BoundTerm>& term) override {
+ if (IsNonNullPreserving(term)) {
+ // number of non-nulls is the same as for the ref
+ int id = term->reference()->field().field_id();
+ if (!MayContainNull(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ }
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotNull(const std::shared_ptr<BoundTerm>& term) override {
+ // no need to check whether the field is required because binding evaluates that case
+ // if the column has no non-null values, the expression cannot match
+
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> IsNaN(const std::shared_ptr<BoundTerm>& term) override {
+ // when there's no nanCounts information, but we already know the column only contains
+ // null, it's guaranteed that there's no NaN value
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ auto ptr = std::dynamic_pointer_cast<BoundReference>(term);
+ if (ptr == nullptr) {
+ return ROWS_MIGHT_MATCH;
+ }
+ if (!data_file_.nan_value_counts.empty() &&
+ data_file_.nan_value_counts.contains(id) &&
+ data_file_.nan_value_counts.at(id) == 0) {
+ return ROWS_CANNOT_MATCH;
+ }
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotNaN(const std::shared_ptr<BoundTerm>& term) override {
+ auto ptr = std::dynamic_pointer_cast<BoundReference>(term);
+ if (ptr == nullptr) {
+ // identity transforms are already removed by this time
+ return ROWS_MIGHT_MATCH;
+ }
+
+ int id = term->reference()->field().field_id();
+
+ if (ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> Lt(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value() || lower_result.value().IsNaN()) {
+ // NaN indicates unreliable bounds. See the InclusiveMetricsEvaluator docs for more.
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+
+ // this also works for transforms that are order preserving:
+ // if a transform f is order preserving, a < b means that f(a) <= f(b).
+ // because lower <= a for all values of a in the file, f(lower) <= f(a).
+ // when f(lower) >= X then f(a) >= f(lower) >= X, so there is no a such that f(a) < X
+ // f(lower) >= X means rows cannot match
+ if (lower >= lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> LtEq(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value() || lower_result.value().IsNaN()) {
+ // NaN indicates unreliable bounds. See the InclusiveMetricsEvaluator docs for more.
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+
+ // this also works for transforms that are order preserving:
+ // if a transform f is order preserving, a < b means that f(a) <= f(b).
+ // because lower <= a for all values of a in the file, f(lower) <= f(a).
+ // when f(lower) > X then f(a) >= f(lower) > X, so there is no a such that f(a) <= X
+ // f(lower) > X means rows cannot match
+ if (lower > lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> Gt(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+
+ if (upper <= lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> GtEq(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ if (upper < lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> Eq(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto lower_result = LowerBound(term);
+ if (lower_result.has_value()) {
+ const auto& lower = lower_result.value();
+ if (!lower.IsNaN() && lower > lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ if (upper < lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotEq(const std::shared_ptr<BoundTerm>& term,
+ const Literal& lit) override {
+ // because the bounds are not necessarily a min or max value, this cannot be answered
+ // using them. notEq(col, X) with (X, Y) doesn't guarantee that X is a value in col.
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> In(const std::shared_ptr<BoundTerm>& term,
+ const BoundSetPredicate::LiteralSet& literal_set) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ if (literal_set.size() > IN_PREDICATE_LIMIT) {
+ // skip evaluating the predicate if the number of values is too big
+ return ROWS_MIGHT_MATCH;
+ }
+
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value() || lower_result.value().IsNaN()) {
+ // NaN indicates unreliable bounds. See the InclusiveMetricsEvaluator docs for more.
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+ std::vector<Literal> literals;
+ for (const auto& lit : literal_set) {
+ if (lower <= lit) {
+ literals.emplace_back(lit);
+ }
+ }
+ // if all values are less than lower bound, rows cannot match
+ if (literals.empty()) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ std::erase_if(literals, [&](const Literal& x) { return x > upper; });
+ // if remaining values are greater than upper bound, rows cannot match
+ if (literals.empty()) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotIn(const std::shared_ptr<BoundTerm>& term,
+ const BoundSetPredicate::LiteralSet& literal_set) override {
+ // because the bounds are not necessarily a min or max value, this cannot be answered
+ // using them. notIn(col, {X, ...}) with (X, Y) doesn't guarantee that X is a value in
+ // col.
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> StartsWith(const std::shared_ptr<BoundTerm>& term,
+ const Literal& lit) override {
+ if (term->kind() == BoundTerm::Kind::kTransform &&
+ std::dynamic_pointer_cast<BoundTransform>(term)->transform()->transform_type() !=
+ TransformType::kIdentity) {
+ // truncate must be rewritten in binding. the result is either always or never
+ // compatible
+ return ROWS_MIGHT_MATCH;
+ }
+
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ if (lit.type()->type_id() != TypeId::kString) {
+ return ROWS_CANNOT_MATCH;
+ }
+ std::string prefix = get<std::string>(lit.value());
Review Comment:
```suggestion
const std::string& prefix = get<std::string>(lit.value());
```
##########
src/iceberg/expression/inclusive_metrics_evaluator.cc:
##########
@@ -0,0 +1,544 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "iceberg/expression/inclusive_metrics_evaluator.h"
+
+#include "iceberg/expression/binder.h"
+#include "iceberg/expression/expression_visitor.h"
+#include "iceberg/expression/rewrite_not.h"
+#include "iceberg/manifest_entry.h"
+#include "iceberg/schema.h"
+#include "iceberg/transform.h"
+#include "iceberg/util/macros.h"
+
+namespace iceberg {
+
+namespace {
+static const bool ROWS_MIGHT_MATCH = true;
+static const bool ROWS_CANNOT_MATCH = false;
+static const int32_t IN_PREDICATE_LIMIT = 200;
+} // namespace
+
+class InclusiveMetricsVisitor : public BoundVisitor<bool> {
+ public:
+ explicit InclusiveMetricsVisitor(const DataFile& data_file) : data_file_(data_file) {}
+
+ Result<bool> AlwaysTrue() override { return ROWS_MIGHT_MATCH; }
+
+ Result<bool> AlwaysFalse() override { return ROWS_CANNOT_MATCH; }
+
+ Result<bool> Not(bool child_result) override { return !child_result; }
+
+ Result<bool> And(bool left_result, bool right_result) override {
+ return left_result && right_result;
+ }
+
+ Result<bool> Or(bool left_result, bool right_result) override {
+ return left_result || right_result;
+ }
+
+ Result<bool> IsNull(const std::shared_ptr<BoundTerm>& term) override {
+ if (IsNonNullPreserving(term)) {
+ // number of non-nulls is the same as for the ref
+ int id = term->reference()->field().field_id();
+ if (!MayContainNull(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ }
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotNull(const std::shared_ptr<BoundTerm>& term) override {
+ // no need to check whether the field is required because binding evaluates that case
+ // if the column has no non-null values, the expression cannot match
+
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> IsNaN(const std::shared_ptr<BoundTerm>& term) override {
+ // when there's no nanCounts information, but we already know the column only contains
+ // null, it's guaranteed that there's no NaN value
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ auto ptr = std::dynamic_pointer_cast<BoundReference>(term);
Review Comment:
This may be slow. At least we should use `dynamic_cast<BoundReference*>(term.get())`. A better solution is to add some virtual functions to `Bound` so we can know exactly the type.
##########
src/iceberg/test/inclusive_metrics_evaluator_test.cc:
##########
@@ -0,0 +1,385 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "iceberg/expression/inclusive_metrics_evaluator.h"
+
+#include <gtest/gtest.h>
+
+#include "iceberg/expression/binder.h"
+#include "iceberg/expression/expressions.h"
+#include "iceberg/manifest_entry.h"
+#include "iceberg/schema.h"
+#include "iceberg/test/matchers.h"
+#include "iceberg/type.h"
+
+namespace iceberg {
+
+namespace {
+static const bool ROWS_MIGHT_MATCH = true;
Review Comment:
ditto, use `k` prefix for constants.
##########
src/iceberg/expression/inclusive_metrics_evaluator.cc:
##########
@@ -0,0 +1,544 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "iceberg/expression/inclusive_metrics_evaluator.h"
+
+#include "iceberg/expression/binder.h"
+#include "iceberg/expression/expression_visitor.h"
+#include "iceberg/expression/rewrite_not.h"
+#include "iceberg/manifest_entry.h"
+#include "iceberg/schema.h"
+#include "iceberg/transform.h"
+#include "iceberg/util/macros.h"
+
+namespace iceberg {
+
+namespace {
+static const bool ROWS_MIGHT_MATCH = true;
+static const bool ROWS_CANNOT_MATCH = false;
+static const int32_t IN_PREDICATE_LIMIT = 200;
+} // namespace
+
+class InclusiveMetricsVisitor : public BoundVisitor<bool> {
+ public:
+ explicit InclusiveMetricsVisitor(const DataFile& data_file) : data_file_(data_file) {}
+
+ Result<bool> AlwaysTrue() override { return ROWS_MIGHT_MATCH; }
+
+ Result<bool> AlwaysFalse() override { return ROWS_CANNOT_MATCH; }
+
+ Result<bool> Not(bool child_result) override { return !child_result; }
+
+ Result<bool> And(bool left_result, bool right_result) override {
+ return left_result && right_result;
+ }
+
+ Result<bool> Or(bool left_result, bool right_result) override {
+ return left_result || right_result;
+ }
+
+ Result<bool> IsNull(const std::shared_ptr<BoundTerm>& term) override {
+ if (IsNonNullPreserving(term)) {
+ // number of non-nulls is the same as for the ref
+ int id = term->reference()->field().field_id();
+ if (!MayContainNull(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ }
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotNull(const std::shared_ptr<BoundTerm>& term) override {
+ // no need to check whether the field is required because binding evaluates that case
+ // if the column has no non-null values, the expression cannot match
+
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> IsNaN(const std::shared_ptr<BoundTerm>& term) override {
+ // when there's no nanCounts information, but we already know the column only contains
+ // null, it's guaranteed that there's no NaN value
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ auto ptr = std::dynamic_pointer_cast<BoundReference>(term);
+ if (ptr == nullptr) {
+ return ROWS_MIGHT_MATCH;
+ }
+ if (!data_file_.nan_value_counts.empty() &&
+ data_file_.nan_value_counts.contains(id) &&
+ data_file_.nan_value_counts.at(id) == 0) {
+ return ROWS_CANNOT_MATCH;
+ }
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotNaN(const std::shared_ptr<BoundTerm>& term) override {
+ auto ptr = std::dynamic_pointer_cast<BoundReference>(term);
+ if (ptr == nullptr) {
+ // identity transforms are already removed by this time
+ return ROWS_MIGHT_MATCH;
+ }
+
+ int id = term->reference()->field().field_id();
+
+ if (ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> Lt(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ auto lower_result = LowerBound(term);
Review Comment:
Isn't it an error if `!lower_result.has_value()`? Should we directly return it?
##########
src/iceberg/test/inclusive_metrics_evaluator_test.cc:
##########
@@ -0,0 +1,385 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "iceberg/expression/inclusive_metrics_evaluator.h"
+
+#include <gtest/gtest.h>
+
+#include "iceberg/expression/binder.h"
+#include "iceberg/expression/expressions.h"
+#include "iceberg/manifest_entry.h"
+#include "iceberg/schema.h"
+#include "iceberg/test/matchers.h"
+#include "iceberg/type.h"
+
+namespace iceberg {
+
+namespace {
+static const bool ROWS_MIGHT_MATCH = true;
+static const bool ROWS_CANNOT_MATCH = false;
+} // namespace
+using TestVariant = std::variant<bool, int32_t, int64_t, double, std::string>;
+
+class InclusiveMetricsEvaluatorTest : public ::testing::Test {
Review Comment:
Is it possible to port all cases from the Java `TestInclusiveMetricsEvaluator`? We did same thing for manifest reader/writer and evaluator to have better coverage.
##########
src/iceberg/expression/inclusive_metrics_evaluator.cc:
##########
@@ -0,0 +1,544 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "iceberg/expression/inclusive_metrics_evaluator.h"
+
+#include "iceberg/expression/binder.h"
+#include "iceberg/expression/expression_visitor.h"
+#include "iceberg/expression/rewrite_not.h"
+#include "iceberg/manifest_entry.h"
+#include "iceberg/schema.h"
+#include "iceberg/transform.h"
+#include "iceberg/util/macros.h"
+
+namespace iceberg {
+
+namespace {
+static const bool ROWS_MIGHT_MATCH = true;
+static const bool ROWS_CANNOT_MATCH = false;
+static const int32_t IN_PREDICATE_LIMIT = 200;
+} // namespace
+
+class InclusiveMetricsVisitor : public BoundVisitor<bool> {
+ public:
+ explicit InclusiveMetricsVisitor(const DataFile& data_file) : data_file_(data_file) {}
+
+ Result<bool> AlwaysTrue() override { return ROWS_MIGHT_MATCH; }
+
+ Result<bool> AlwaysFalse() override { return ROWS_CANNOT_MATCH; }
+
+ Result<bool> Not(bool child_result) override { return !child_result; }
+
+ Result<bool> And(bool left_result, bool right_result) override {
+ return left_result && right_result;
+ }
+
+ Result<bool> Or(bool left_result, bool right_result) override {
+ return left_result || right_result;
+ }
+
+ Result<bool> IsNull(const std::shared_ptr<BoundTerm>& term) override {
+ if (IsNonNullPreserving(term)) {
+ // number of non-nulls is the same as for the ref
+ int id = term->reference()->field().field_id();
+ if (!MayContainNull(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ }
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotNull(const std::shared_ptr<BoundTerm>& term) override {
+ // no need to check whether the field is required because binding evaluates that case
+ // if the column has no non-null values, the expression cannot match
+
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> IsNaN(const std::shared_ptr<BoundTerm>& term) override {
+ // when there's no nanCounts information, but we already know the column only contains
+ // null, it's guaranteed that there's no NaN value
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ auto ptr = std::dynamic_pointer_cast<BoundReference>(term);
+ if (ptr == nullptr) {
+ return ROWS_MIGHT_MATCH;
+ }
+ if (!data_file_.nan_value_counts.empty() &&
+ data_file_.nan_value_counts.contains(id) &&
+ data_file_.nan_value_counts.at(id) == 0) {
+ return ROWS_CANNOT_MATCH;
+ }
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotNaN(const std::shared_ptr<BoundTerm>& term) override {
+ auto ptr = std::dynamic_pointer_cast<BoundReference>(term);
+ if (ptr == nullptr) {
+ // identity transforms are already removed by this time
+ return ROWS_MIGHT_MATCH;
+ }
+
+ int id = term->reference()->field().field_id();
+
+ if (ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> Lt(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value() || lower_result.value().IsNaN()) {
+ // NaN indicates unreliable bounds. See the InclusiveMetricsEvaluator docs for more.
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+
+ // this also works for transforms that are order preserving:
+ // if a transform f is order preserving, a < b means that f(a) <= f(b).
+ // because lower <= a for all values of a in the file, f(lower) <= f(a).
+ // when f(lower) >= X then f(a) >= f(lower) >= X, so there is no a such that f(a) < X
+ // f(lower) >= X means rows cannot match
+ if (lower >= lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> LtEq(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value() || lower_result.value().IsNaN()) {
+ // NaN indicates unreliable bounds. See the InclusiveMetricsEvaluator docs for more.
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+
+ // this also works for transforms that are order preserving:
+ // if a transform f is order preserving, a < b means that f(a) <= f(b).
+ // because lower <= a for all values of a in the file, f(lower) <= f(a).
+ // when f(lower) > X then f(a) >= f(lower) > X, so there is no a such that f(a) <= X
+ // f(lower) > X means rows cannot match
+ if (lower > lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> Gt(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+
+ if (upper <= lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> GtEq(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ if (upper < lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> Eq(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto lower_result = LowerBound(term);
+ if (lower_result.has_value()) {
+ const auto& lower = lower_result.value();
+ if (!lower.IsNaN() && lower > lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ if (upper < lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotEq(const std::shared_ptr<BoundTerm>& term,
+ const Literal& lit) override {
+ // because the bounds are not necessarily a min or max value, this cannot be answered
+ // using them. notEq(col, X) with (X, Y) doesn't guarantee that X is a value in col.
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> In(const std::shared_ptr<BoundTerm>& term,
+ const BoundSetPredicate::LiteralSet& literal_set) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ if (literal_set.size() > IN_PREDICATE_LIMIT) {
+ // skip evaluating the predicate if the number of values is too big
+ return ROWS_MIGHT_MATCH;
+ }
+
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value() || lower_result.value().IsNaN()) {
+ // NaN indicates unreliable bounds. See the InclusiveMetricsEvaluator docs for more.
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+ std::vector<Literal> literals;
+ for (const auto& lit : literal_set) {
+ if (lower <= lit) {
+ literals.emplace_back(lit);
+ }
+ }
+ // if all values are less than lower bound, rows cannot match
+ if (literals.empty()) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ std::erase_if(literals, [&](const Literal& x) { return x > upper; });
+ // if remaining values are greater than upper bound, rows cannot match
+ if (literals.empty()) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotIn(const std::shared_ptr<BoundTerm>& term,
+ const BoundSetPredicate::LiteralSet& literal_set) override {
+ // because the bounds are not necessarily a min or max value, this cannot be answered
+ // using them. notIn(col, {X, ...}) with (X, Y) doesn't guarantee that X is a value in
+ // col.
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> StartsWith(const std::shared_ptr<BoundTerm>& term,
+ const Literal& lit) override {
+ if (term->kind() == BoundTerm::Kind::kTransform &&
+ std::dynamic_pointer_cast<BoundTransform>(term)->transform()->transform_type() !=
+ TransformType::kIdentity) {
+ // truncate must be rewritten in binding. the result is either always or never
+ // compatible
+ return ROWS_MIGHT_MATCH;
+ }
+
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ if (lit.type()->type_id() != TypeId::kString) {
+ return ROWS_CANNOT_MATCH;
+ }
+ std::string prefix = get<std::string>(lit.value());
+
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+ auto lower_str = get<std::string>(lower.value());
+ // truncate lower bound so that its length in bytes is not greater than the length of
+ // prefix
+ int length = std::min(prefix.size(), lower_str.size());
+ // if prefix of lower bound is greater than prefix, rows cannot match
+ if (lower_str.substr(0, length) > prefix) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ auto upper_str = get<std::string>(upper.value());
+ // truncate upper bound so that its length in bytes is not greater than the length of
+ // prefix
+ length = std::min(prefix.size(), upper_str.size());
+ // if prefix of upper bound is less than prefix, rows cannot match
+ if (upper_str.substr(0, length) < prefix) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotStartsWith(const std::shared_ptr<BoundTerm>& term,
+ const Literal& lit) override {
+ // the only transforms that produce strings are truncate and identity, which work with
+ // this
+ int id = term->reference()->field().field_id();
+ if (MayContainNull(id)) {
+ return ROWS_MIGHT_MATCH;
+ }
+
+ if (lit.type()->type_id() != TypeId::kString) {
+ return ROWS_CANNOT_MATCH;
+ }
+ std::string prefix = get<std::string>(lit.value());
+
+ // notStartsWith will match unless all values must start with the prefix. This happens
+ // when the lower and upper bounds both start with the prefix.
+ auto lower_result = LowerBound(term);
+ auto upper_result = UpperBound(term);
+ if (!lower_result.has_value() || !upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+ const auto& upper = upper_result.value();
+ auto lower_str = get<std::string>(lower.value());
+ auto upper_str = get<std::string>(upper.value());
+
+ // if lower is shorter than the prefix then lower doesn't start with the prefix
+ if (lower_str.size() < prefix.size()) {
+ return ROWS_MIGHT_MATCH;
+ }
+
+ if (lower_str.starts_with(prefix)) {
+ // if upper is shorter than the prefix then upper can't start with the prefix
+ if (upper_str.size() < prefix.size()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ if (upper_str.starts_with(prefix)) {
+ // both bounds match the prefix, so all rows must match the prefix and therefore
+ // do not satisfy the predicate
+ return ROWS_CANNOT_MATCH;
+ }
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ private:
+ bool MayContainNull(int32_t id) {
+ return data_file_.null_value_counts.empty() ||
+ !data_file_.null_value_counts.contains(id) ||
+ data_file_.null_value_counts.at(id) != 0;
+ }
+
+ bool ContainsNullsOnly(int32_t id) {
+ return !data_file_.value_counts.empty() && data_file_.value_counts.contains(id) &&
+ !data_file_.null_value_counts.empty() &&
+ data_file_.null_value_counts.contains(id) &&
+ data_file_.value_counts.at(id) - data_file_.null_value_counts.at(id) == 0;
+ }
+
+ bool ContainsNaNsOnly(int32_t id) {
+ return !data_file_.nan_value_counts.empty() &&
+ data_file_.nan_value_counts.contains(id) && !data_file_.value_counts.empty() &&
+ data_file_.value_counts.at(id) == data_file_.nan_value_counts.at(id);
+ }
+
+ Result<Literal> LowerBound(const std::shared_ptr<BoundTerm>& term) {
+ if (term->kind() == BoundTerm::Kind::kReference) {
+ return ParseLowerBound(*std::dynamic_pointer_cast<BoundReference>(term));
Review Comment:
use internal::checked_cast
##########
src/iceberg/expression/inclusive_metrics_evaluator.cc:
##########
@@ -0,0 +1,544 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "iceberg/expression/inclusive_metrics_evaluator.h"
+
+#include "iceberg/expression/binder.h"
+#include "iceberg/expression/expression_visitor.h"
+#include "iceberg/expression/rewrite_not.h"
+#include "iceberg/manifest_entry.h"
+#include "iceberg/schema.h"
+#include "iceberg/transform.h"
+#include "iceberg/util/macros.h"
+
+namespace iceberg {
+
+namespace {
+static const bool ROWS_MIGHT_MATCH = true;
+static const bool ROWS_CANNOT_MATCH = false;
+static const int32_t IN_PREDICATE_LIMIT = 200;
+} // namespace
+
+class InclusiveMetricsVisitor : public BoundVisitor<bool> {
+ public:
+ explicit InclusiveMetricsVisitor(const DataFile& data_file) : data_file_(data_file) {}
+
+ Result<bool> AlwaysTrue() override { return ROWS_MIGHT_MATCH; }
+
+ Result<bool> AlwaysFalse() override { return ROWS_CANNOT_MATCH; }
+
+ Result<bool> Not(bool child_result) override { return !child_result; }
+
+ Result<bool> And(bool left_result, bool right_result) override {
+ return left_result && right_result;
+ }
+
+ Result<bool> Or(bool left_result, bool right_result) override {
+ return left_result || right_result;
+ }
+
+ Result<bool> IsNull(const std::shared_ptr<BoundTerm>& term) override {
+ if (IsNonNullPreserving(term)) {
+ // number of non-nulls is the same as for the ref
+ int id = term->reference()->field().field_id();
Review Comment:
```suggestion
int32_t id = term->reference()->field().field_id();
```
##########
src/iceberg/expression/inclusive_metrics_evaluator.h:
##########
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#pragma once
+
+/// \file iceberg/expression/inclusive_metrics_evaluator.h
+///
+/// Evaluates an Expression on a DataFile to test whether rows in the file may match.
+///
+/// This evaluation is inclusive: it returns true if a file may match and false if it
+/// cannot match.
+///
+/// Files are passed to #eval(ContentFile), which returns true if the file may contain
+/// matching rows and false if the file cannot contain matching rows. Files may be skipped
+/// if and only if the return value of eval is false.
+///
+/// Due to the comparison implementation of ORC stats, for float/double columns in ORC
+/// files, if the first value in a file is NaN, metrics of this file will report NaN for
+/// both upper and lower bound despite that the column could contain non-NaN data. Thus in
+/// some scenarios explicitly checks for NaN is necessary in order to not skip files that
+/// may contain matching data.
+///
+
+#include <memory>
+
+#include "iceberg/expression/expression.h"
+#include "iceberg/iceberg_export.h"
+#include "iceberg/result.h"
+#include "iceberg/type_fwd.h"
+
+namespace iceberg {
+
+class ICEBERG_EXPORT InclusiveMetricsEvaluator {
+ public:
+ /// \brief Make a inclusive metrics evaluator
+ ///
+ /// \param expr The expression to evaluate
+ /// \param schema The schema of the table
+ /// \param case_sensitive Whether field name matching is case-sensitive
+ static Result<std::unique_ptr<InclusiveMetricsEvaluator>> Make(
+ std::shared_ptr<Expression> expr, const std::shared_ptr<Schema>& schema,
Review Comment:
```suggestion
std::shared_ptr<Expression> expr, const Schema& schema,
```
##########
src/iceberg/expression/inclusive_metrics_evaluator.cc:
##########
@@ -0,0 +1,544 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "iceberg/expression/inclusive_metrics_evaluator.h"
+
+#include "iceberg/expression/binder.h"
+#include "iceberg/expression/expression_visitor.h"
+#include "iceberg/expression/rewrite_not.h"
+#include "iceberg/manifest_entry.h"
+#include "iceberg/schema.h"
+#include "iceberg/transform.h"
+#include "iceberg/util/macros.h"
+
+namespace iceberg {
+
+namespace {
+static const bool ROWS_MIGHT_MATCH = true;
+static const bool ROWS_CANNOT_MATCH = false;
+static const int32_t IN_PREDICATE_LIMIT = 200;
+} // namespace
+
+class InclusiveMetricsVisitor : public BoundVisitor<bool> {
+ public:
+ explicit InclusiveMetricsVisitor(const DataFile& data_file) : data_file_(data_file) {}
+
+ Result<bool> AlwaysTrue() override { return ROWS_MIGHT_MATCH; }
+
+ Result<bool> AlwaysFalse() override { return ROWS_CANNOT_MATCH; }
+
+ Result<bool> Not(bool child_result) override { return !child_result; }
+
+ Result<bool> And(bool left_result, bool right_result) override {
+ return left_result && right_result;
+ }
+
+ Result<bool> Or(bool left_result, bool right_result) override {
+ return left_result || right_result;
+ }
+
+ Result<bool> IsNull(const std::shared_ptr<BoundTerm>& term) override {
+ if (IsNonNullPreserving(term)) {
+ // number of non-nulls is the same as for the ref
+ int id = term->reference()->field().field_id();
+ if (!MayContainNull(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ }
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotNull(const std::shared_ptr<BoundTerm>& term) override {
+ // no need to check whether the field is required because binding evaluates that case
+ // if the column has no non-null values, the expression cannot match
+
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> IsNaN(const std::shared_ptr<BoundTerm>& term) override {
+ // when there's no nanCounts information, but we already know the column only contains
+ // null, it's guaranteed that there's no NaN value
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ auto ptr = std::dynamic_pointer_cast<BoundReference>(term);
+ if (ptr == nullptr) {
+ return ROWS_MIGHT_MATCH;
+ }
+ if (!data_file_.nan_value_counts.empty() &&
+ data_file_.nan_value_counts.contains(id) &&
Review Comment:
nit: use `find` to combine `contains` and `at`
##########
src/iceberg/expression/inclusive_metrics_evaluator.cc:
##########
@@ -0,0 +1,544 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "iceberg/expression/inclusive_metrics_evaluator.h"
+
+#include "iceberg/expression/binder.h"
+#include "iceberg/expression/expression_visitor.h"
+#include "iceberg/expression/rewrite_not.h"
+#include "iceberg/manifest_entry.h"
+#include "iceberg/schema.h"
+#include "iceberg/transform.h"
+#include "iceberg/util/macros.h"
+
+namespace iceberg {
+
+namespace {
+static const bool ROWS_MIGHT_MATCH = true;
+static const bool ROWS_CANNOT_MATCH = false;
+static const int32_t IN_PREDICATE_LIMIT = 200;
+} // namespace
+
+class InclusiveMetricsVisitor : public BoundVisitor<bool> {
+ public:
+ explicit InclusiveMetricsVisitor(const DataFile& data_file) : data_file_(data_file) {}
+
+ Result<bool> AlwaysTrue() override { return ROWS_MIGHT_MATCH; }
+
+ Result<bool> AlwaysFalse() override { return ROWS_CANNOT_MATCH; }
+
+ Result<bool> Not(bool child_result) override { return !child_result; }
+
+ Result<bool> And(bool left_result, bool right_result) override {
+ return left_result && right_result;
+ }
+
+ Result<bool> Or(bool left_result, bool right_result) override {
+ return left_result || right_result;
+ }
+
+ Result<bool> IsNull(const std::shared_ptr<BoundTerm>& term) override {
+ if (IsNonNullPreserving(term)) {
+ // number of non-nulls is the same as for the ref
+ int id = term->reference()->field().field_id();
+ if (!MayContainNull(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ }
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotNull(const std::shared_ptr<BoundTerm>& term) override {
+ // no need to check whether the field is required because binding evaluates that case
+ // if the column has no non-null values, the expression cannot match
+
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> IsNaN(const std::shared_ptr<BoundTerm>& term) override {
+ // when there's no nanCounts information, but we already know the column only contains
+ // null, it's guaranteed that there's no NaN value
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ auto ptr = std::dynamic_pointer_cast<BoundReference>(term);
+ if (ptr == nullptr) {
+ return ROWS_MIGHT_MATCH;
+ }
+ if (!data_file_.nan_value_counts.empty() &&
+ data_file_.nan_value_counts.contains(id) &&
+ data_file_.nan_value_counts.at(id) == 0) {
+ return ROWS_CANNOT_MATCH;
+ }
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotNaN(const std::shared_ptr<BoundTerm>& term) override {
+ auto ptr = std::dynamic_pointer_cast<BoundReference>(term);
+ if (ptr == nullptr) {
+ // identity transforms are already removed by this time
+ return ROWS_MIGHT_MATCH;
+ }
+
+ int id = term->reference()->field().field_id();
+
+ if (ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> Lt(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value() || lower_result.value().IsNaN()) {
+ // NaN indicates unreliable bounds. See the InclusiveMetricsEvaluator docs for more.
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+
+ // this also works for transforms that are order preserving:
+ // if a transform f is order preserving, a < b means that f(a) <= f(b).
+ // because lower <= a for all values of a in the file, f(lower) <= f(a).
+ // when f(lower) >= X then f(a) >= f(lower) >= X, so there is no a such that f(a) < X
+ // f(lower) >= X means rows cannot match
+ if (lower >= lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> LtEq(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value() || lower_result.value().IsNaN()) {
+ // NaN indicates unreliable bounds. See the InclusiveMetricsEvaluator docs for more.
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+
+ // this also works for transforms that are order preserving:
+ // if a transform f is order preserving, a < b means that f(a) <= f(b).
+ // because lower <= a for all values of a in the file, f(lower) <= f(a).
+ // when f(lower) > X then f(a) >= f(lower) > X, so there is no a such that f(a) <= X
+ // f(lower) > X means rows cannot match
+ if (lower > lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> Gt(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+
+ if (upper <= lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> GtEq(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ if (upper < lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> Eq(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto lower_result = LowerBound(term);
+ if (lower_result.has_value()) {
+ const auto& lower = lower_result.value();
+ if (!lower.IsNaN() && lower > lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ if (upper < lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotEq(const std::shared_ptr<BoundTerm>& term,
+ const Literal& lit) override {
+ // because the bounds are not necessarily a min or max value, this cannot be answered
+ // using them. notEq(col, X) with (X, Y) doesn't guarantee that X is a value in col.
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> In(const std::shared_ptr<BoundTerm>& term,
+ const BoundSetPredicate::LiteralSet& literal_set) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ if (literal_set.size() > IN_PREDICATE_LIMIT) {
+ // skip evaluating the predicate if the number of values is too big
+ return ROWS_MIGHT_MATCH;
+ }
+
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value() || lower_result.value().IsNaN()) {
+ // NaN indicates unreliable bounds. See the InclusiveMetricsEvaluator docs for more.
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+ std::vector<Literal> literals;
+ for (const auto& lit : literal_set) {
+ if (lower <= lit) {
+ literals.emplace_back(lit);
+ }
+ }
+ // if all values are less than lower bound, rows cannot match
+ if (literals.empty()) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ std::erase_if(literals, [&](const Literal& x) { return x > upper; });
+ // if remaining values are greater than upper bound, rows cannot match
+ if (literals.empty()) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotIn(const std::shared_ptr<BoundTerm>& term,
+ const BoundSetPredicate::LiteralSet& literal_set) override {
+ // because the bounds are not necessarily a min or max value, this cannot be answered
+ // using them. notIn(col, {X, ...}) with (X, Y) doesn't guarantee that X is a value in
+ // col.
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> StartsWith(const std::shared_ptr<BoundTerm>& term,
+ const Literal& lit) override {
+ if (term->kind() == BoundTerm::Kind::kTransform &&
+ std::dynamic_pointer_cast<BoundTransform>(term)->transform()->transform_type() !=
Review Comment:
use internal::checked_cast
##########
src/iceberg/expression/inclusive_metrics_evaluator.cc:
##########
@@ -0,0 +1,544 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "iceberg/expression/inclusive_metrics_evaluator.h"
+
+#include "iceberg/expression/binder.h"
+#include "iceberg/expression/expression_visitor.h"
+#include "iceberg/expression/rewrite_not.h"
+#include "iceberg/manifest_entry.h"
+#include "iceberg/schema.h"
+#include "iceberg/transform.h"
+#include "iceberg/util/macros.h"
+
+namespace iceberg {
+
+namespace {
+static const bool ROWS_MIGHT_MATCH = true;
+static const bool ROWS_CANNOT_MATCH = false;
+static const int32_t IN_PREDICATE_LIMIT = 200;
+} // namespace
+
+class InclusiveMetricsVisitor : public BoundVisitor<bool> {
+ public:
+ explicit InclusiveMetricsVisitor(const DataFile& data_file) : data_file_(data_file) {}
+
+ Result<bool> AlwaysTrue() override { return ROWS_MIGHT_MATCH; }
+
+ Result<bool> AlwaysFalse() override { return ROWS_CANNOT_MATCH; }
+
+ Result<bool> Not(bool child_result) override { return !child_result; }
+
+ Result<bool> And(bool left_result, bool right_result) override {
+ return left_result && right_result;
+ }
+
+ Result<bool> Or(bool left_result, bool right_result) override {
+ return left_result || right_result;
+ }
+
+ Result<bool> IsNull(const std::shared_ptr<BoundTerm>& term) override {
+ if (IsNonNullPreserving(term)) {
Review Comment:
```suggestion
// if the column has no null values, the expression cannot match
if (IsNonNullPreserving(term)) {
```
Let's preserve the original comment
##########
src/iceberg/expression/inclusive_metrics_evaluator.cc:
##########
@@ -0,0 +1,544 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "iceberg/expression/inclusive_metrics_evaluator.h"
+
+#include "iceberg/expression/binder.h"
+#include "iceberg/expression/expression_visitor.h"
+#include "iceberg/expression/rewrite_not.h"
+#include "iceberg/manifest_entry.h"
+#include "iceberg/schema.h"
+#include "iceberg/transform.h"
+#include "iceberg/util/macros.h"
+
+namespace iceberg {
+
+namespace {
+static const bool ROWS_MIGHT_MATCH = true;
+static const bool ROWS_CANNOT_MATCH = false;
+static const int32_t IN_PREDICATE_LIMIT = 200;
+} // namespace
+
+class InclusiveMetricsVisitor : public BoundVisitor<bool> {
+ public:
+ explicit InclusiveMetricsVisitor(const DataFile& data_file) : data_file_(data_file) {}
+
+ Result<bool> AlwaysTrue() override { return ROWS_MIGHT_MATCH; }
+
+ Result<bool> AlwaysFalse() override { return ROWS_CANNOT_MATCH; }
+
+ Result<bool> Not(bool child_result) override { return !child_result; }
+
+ Result<bool> And(bool left_result, bool right_result) override {
+ return left_result && right_result;
+ }
+
+ Result<bool> Or(bool left_result, bool right_result) override {
+ return left_result || right_result;
+ }
+
+ Result<bool> IsNull(const std::shared_ptr<BoundTerm>& term) override {
+ if (IsNonNullPreserving(term)) {
+ // number of non-nulls is the same as for the ref
+ int id = term->reference()->field().field_id();
+ if (!MayContainNull(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ }
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotNull(const std::shared_ptr<BoundTerm>& term) override {
+ // no need to check whether the field is required because binding evaluates that case
+ // if the column has no non-null values, the expression cannot match
+
+ // all terms are null preserving. see #isNullPreserving(Bound)
Review Comment:
```suggestion
// if the column has no non-null values, the expression cannot match.
// all terms are null preserving.
```
##########
src/iceberg/expression/inclusive_metrics_evaluator.cc:
##########
@@ -0,0 +1,544 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "iceberg/expression/inclusive_metrics_evaluator.h"
+
+#include "iceberg/expression/binder.h"
+#include "iceberg/expression/expression_visitor.h"
+#include "iceberg/expression/rewrite_not.h"
+#include "iceberg/manifest_entry.h"
+#include "iceberg/schema.h"
+#include "iceberg/transform.h"
+#include "iceberg/util/macros.h"
+
+namespace iceberg {
+
+namespace {
+static const bool ROWS_MIGHT_MATCH = true;
+static const bool ROWS_CANNOT_MATCH = false;
+static const int32_t IN_PREDICATE_LIMIT = 200;
Review Comment:
```suggestion
constexpr bool kRowsMightMatch = true;
constexpr bool kRowCannotMatch = false;
constexpr int32_t kInPredicateLimit = 200;
```
##########
src/iceberg/expression/inclusive_metrics_evaluator.cc:
##########
@@ -0,0 +1,544 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "iceberg/expression/inclusive_metrics_evaluator.h"
+
+#include "iceberg/expression/binder.h"
+#include "iceberg/expression/expression_visitor.h"
+#include "iceberg/expression/rewrite_not.h"
+#include "iceberg/manifest_entry.h"
+#include "iceberg/schema.h"
+#include "iceberg/transform.h"
+#include "iceberg/util/macros.h"
+
+namespace iceberg {
+
+namespace {
+static const bool ROWS_MIGHT_MATCH = true;
+static const bool ROWS_CANNOT_MATCH = false;
+static const int32_t IN_PREDICATE_LIMIT = 200;
+} // namespace
+
+class InclusiveMetricsVisitor : public BoundVisitor<bool> {
+ public:
+ explicit InclusiveMetricsVisitor(const DataFile& data_file) : data_file_(data_file) {}
+
+ Result<bool> AlwaysTrue() override { return ROWS_MIGHT_MATCH; }
+
+ Result<bool> AlwaysFalse() override { return ROWS_CANNOT_MATCH; }
+
+ Result<bool> Not(bool child_result) override { return !child_result; }
+
+ Result<bool> And(bool left_result, bool right_result) override {
+ return left_result && right_result;
+ }
+
+ Result<bool> Or(bool left_result, bool right_result) override {
+ return left_result || right_result;
+ }
+
+ Result<bool> IsNull(const std::shared_ptr<BoundTerm>& term) override {
+ if (IsNonNullPreserving(term)) {
+ // number of non-nulls is the same as for the ref
+ int id = term->reference()->field().field_id();
+ if (!MayContainNull(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ }
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotNull(const std::shared_ptr<BoundTerm>& term) override {
+ // no need to check whether the field is required because binding evaluates that case
+ // if the column has no non-null values, the expression cannot match
+
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> IsNaN(const std::shared_ptr<BoundTerm>& term) override {
+ // when there's no nanCounts information, but we already know the column only contains
+ // null, it's guaranteed that there's no NaN value
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ auto ptr = std::dynamic_pointer_cast<BoundReference>(term);
+ if (ptr == nullptr) {
+ return ROWS_MIGHT_MATCH;
+ }
+ if (!data_file_.nan_value_counts.empty() &&
+ data_file_.nan_value_counts.contains(id) &&
+ data_file_.nan_value_counts.at(id) == 0) {
+ return ROWS_CANNOT_MATCH;
+ }
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotNaN(const std::shared_ptr<BoundTerm>& term) override {
+ auto ptr = std::dynamic_pointer_cast<BoundReference>(term);
+ if (ptr == nullptr) {
+ // identity transforms are already removed by this time
+ return ROWS_MIGHT_MATCH;
+ }
+
+ int id = term->reference()->field().field_id();
+
+ if (ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> Lt(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value() || lower_result.value().IsNaN()) {
+ // NaN indicates unreliable bounds. See the InclusiveMetricsEvaluator docs for more.
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+
+ // this also works for transforms that are order preserving:
+ // if a transform f is order preserving, a < b means that f(a) <= f(b).
+ // because lower <= a for all values of a in the file, f(lower) <= f(a).
+ // when f(lower) >= X then f(a) >= f(lower) >= X, so there is no a such that f(a) < X
+ // f(lower) >= X means rows cannot match
+ if (lower >= lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> LtEq(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value() || lower_result.value().IsNaN()) {
+ // NaN indicates unreliable bounds. See the InclusiveMetricsEvaluator docs for more.
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+
+ // this also works for transforms that are order preserving:
+ // if a transform f is order preserving, a < b means that f(a) <= f(b).
+ // because lower <= a for all values of a in the file, f(lower) <= f(a).
+ // when f(lower) > X then f(a) >= f(lower) > X, so there is no a such that f(a) <= X
+ // f(lower) > X means rows cannot match
+ if (lower > lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> Gt(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+
+ if (upper <= lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> GtEq(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ if (upper < lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> Eq(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto lower_result = LowerBound(term);
+ if (lower_result.has_value()) {
+ const auto& lower = lower_result.value();
+ if (!lower.IsNaN() && lower > lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ if (upper < lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotEq(const std::shared_ptr<BoundTerm>& term,
+ const Literal& lit) override {
+ // because the bounds are not necessarily a min or max value, this cannot be answered
+ // using them. notEq(col, X) with (X, Y) doesn't guarantee that X is a value in col.
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> In(const std::shared_ptr<BoundTerm>& term,
+ const BoundSetPredicate::LiteralSet& literal_set) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ if (literal_set.size() > IN_PREDICATE_LIMIT) {
+ // skip evaluating the predicate if the number of values is too big
+ return ROWS_MIGHT_MATCH;
+ }
+
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value() || lower_result.value().IsNaN()) {
+ // NaN indicates unreliable bounds. See the InclusiveMetricsEvaluator docs for more.
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+ std::vector<Literal> literals;
+ for (const auto& lit : literal_set) {
+ if (lower <= lit) {
+ literals.emplace_back(lit);
+ }
+ }
+ // if all values are less than lower bound, rows cannot match
+ if (literals.empty()) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ std::erase_if(literals, [&](const Literal& x) { return x > upper; });
+ // if remaining values are greater than upper bound, rows cannot match
+ if (literals.empty()) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotIn(const std::shared_ptr<BoundTerm>& term,
+ const BoundSetPredicate::LiteralSet& literal_set) override {
+ // because the bounds are not necessarily a min or max value, this cannot be answered
+ // using them. notIn(col, {X, ...}) with (X, Y) doesn't guarantee that X is a value in
+ // col.
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> StartsWith(const std::shared_ptr<BoundTerm>& term,
+ const Literal& lit) override {
+ if (term->kind() == BoundTerm::Kind::kTransform &&
+ std::dynamic_pointer_cast<BoundTransform>(term)->transform()->transform_type() !=
+ TransformType::kIdentity) {
+ // truncate must be rewritten in binding. the result is either always or never
+ // compatible
+ return ROWS_MIGHT_MATCH;
+ }
+
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ if (lit.type()->type_id() != TypeId::kString) {
+ return ROWS_CANNOT_MATCH;
+ }
+ std::string prefix = get<std::string>(lit.value());
+
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+ auto lower_str = get<std::string>(lower.value());
+ // truncate lower bound so that its length in bytes is not greater than the length of
+ // prefix
+ int length = std::min(prefix.size(), lower_str.size());
+ // if prefix of lower bound is greater than prefix, rows cannot match
+ if (lower_str.substr(0, length) > prefix) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ auto upper_str = get<std::string>(upper.value());
+ // truncate upper bound so that its length in bytes is not greater than the length of
+ // prefix
+ length = std::min(prefix.size(), upper_str.size());
+ // if prefix of upper bound is less than prefix, rows cannot match
+ if (upper_str.substr(0, length) < prefix) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotStartsWith(const std::shared_ptr<BoundTerm>& term,
+ const Literal& lit) override {
+ // the only transforms that produce strings are truncate and identity, which work with
+ // this
+ int id = term->reference()->field().field_id();
+ if (MayContainNull(id)) {
+ return ROWS_MIGHT_MATCH;
+ }
+
+ if (lit.type()->type_id() != TypeId::kString) {
+ return ROWS_CANNOT_MATCH;
+ }
+ std::string prefix = get<std::string>(lit.value());
+
+ // notStartsWith will match unless all values must start with the prefix. This happens
+ // when the lower and upper bounds both start with the prefix.
+ auto lower_result = LowerBound(term);
+ auto upper_result = UpperBound(term);
+ if (!lower_result.has_value() || !upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+ const auto& upper = upper_result.value();
+ auto lower_str = get<std::string>(lower.value());
+ auto upper_str = get<std::string>(upper.value());
+
+ // if lower is shorter than the prefix then lower doesn't start with the prefix
+ if (lower_str.size() < prefix.size()) {
+ return ROWS_MIGHT_MATCH;
+ }
+
+ if (lower_str.starts_with(prefix)) {
+ // if upper is shorter than the prefix then upper can't start with the prefix
+ if (upper_str.size() < prefix.size()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ if (upper_str.starts_with(prefix)) {
+ // both bounds match the prefix, so all rows must match the prefix and therefore
+ // do not satisfy the predicate
+ return ROWS_CANNOT_MATCH;
+ }
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ private:
+ bool MayContainNull(int32_t id) {
+ return data_file_.null_value_counts.empty() ||
+ !data_file_.null_value_counts.contains(id) ||
+ data_file_.null_value_counts.at(id) != 0;
+ }
+
+ bool ContainsNullsOnly(int32_t id) {
+ return !data_file_.value_counts.empty() && data_file_.value_counts.contains(id) &&
+ !data_file_.null_value_counts.empty() &&
+ data_file_.null_value_counts.contains(id) &&
+ data_file_.value_counts.at(id) - data_file_.null_value_counts.at(id) == 0;
+ }
+
+ bool ContainsNaNsOnly(int32_t id) {
+ return !data_file_.nan_value_counts.empty() &&
+ data_file_.nan_value_counts.contains(id) && !data_file_.value_counts.empty() &&
+ data_file_.value_counts.at(id) == data_file_.nan_value_counts.at(id);
+ }
+
+ Result<Literal> LowerBound(const std::shared_ptr<BoundTerm>& term) {
+ if (term->kind() == BoundTerm::Kind::kReference) {
+ return ParseLowerBound(*std::dynamic_pointer_cast<BoundReference>(term));
+ } else if (term->kind() == BoundTerm::Kind::kTransform) {
+ return TransformLowerBound(*std::dynamic_pointer_cast<BoundTransform>(term));
+ } else if (term->kind() == BoundTerm::Kind::kExtract) {
+ // TODO(xiao.dong) handle extract lower and upper bounds
+ return NotImplemented("Extract lower bound not implemented.");
+ } else {
+ return NotFound("Lower bound not found.");
+ }
+ }
+
+ Result<Literal> UpperBound(const std::shared_ptr<BoundTerm>& term) {
+ if (term->kind() == BoundTerm::Kind::kReference) {
+ return ParseUpperBound(*std::dynamic_pointer_cast<BoundReference>(term));
+ } else if (term->kind() == BoundTerm::Kind::kTransform) {
+ return TransformUpperBound(*std::dynamic_pointer_cast<BoundTransform>(term));
+ } else if (term->kind() == BoundTerm::Kind::kExtract) {
+ // TODO(xiao.dong) handle extract lower and upper bounds
+ return NotImplemented("Extract upper bound not implemented.");
+ } else {
+ return NotFound("Upper bound not found.");
+ }
+ }
+
+ Result<Literal> ParseLowerBound(const BoundReference& ref) {
+ int id = ref.field().field_id();
+ auto type = ref.type();
+ if (!type->is_primitive()) {
+ return Invalid("Lower bound of non-primitive type is not supported.");
Review Comment:
Return `InvalidArgument` or `NotSupported`?
##########
src/iceberg/expression/inclusive_metrics_evaluator.cc:
##########
@@ -0,0 +1,544 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "iceberg/expression/inclusive_metrics_evaluator.h"
+
+#include "iceberg/expression/binder.h"
+#include "iceberg/expression/expression_visitor.h"
+#include "iceberg/expression/rewrite_not.h"
+#include "iceberg/manifest_entry.h"
+#include "iceberg/schema.h"
+#include "iceberg/transform.h"
+#include "iceberg/util/macros.h"
+
+namespace iceberg {
+
+namespace {
+static const bool ROWS_MIGHT_MATCH = true;
+static const bool ROWS_CANNOT_MATCH = false;
+static const int32_t IN_PREDICATE_LIMIT = 200;
+} // namespace
+
+class InclusiveMetricsVisitor : public BoundVisitor<bool> {
+ public:
+ explicit InclusiveMetricsVisitor(const DataFile& data_file) : data_file_(data_file) {}
+
+ Result<bool> AlwaysTrue() override { return ROWS_MIGHT_MATCH; }
+
+ Result<bool> AlwaysFalse() override { return ROWS_CANNOT_MATCH; }
+
+ Result<bool> Not(bool child_result) override { return !child_result; }
+
+ Result<bool> And(bool left_result, bool right_result) override {
+ return left_result && right_result;
+ }
+
+ Result<bool> Or(bool left_result, bool right_result) override {
+ return left_result || right_result;
+ }
+
+ Result<bool> IsNull(const std::shared_ptr<BoundTerm>& term) override {
+ if (IsNonNullPreserving(term)) {
+ // number of non-nulls is the same as for the ref
+ int id = term->reference()->field().field_id();
+ if (!MayContainNull(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ }
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotNull(const std::shared_ptr<BoundTerm>& term) override {
+ // no need to check whether the field is required because binding evaluates that case
+ // if the column has no non-null values, the expression cannot match
+
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> IsNaN(const std::shared_ptr<BoundTerm>& term) override {
+ // when there's no nanCounts information, but we already know the column only contains
+ // null, it's guaranteed that there's no NaN value
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ auto ptr = std::dynamic_pointer_cast<BoundReference>(term);
+ if (ptr == nullptr) {
+ return ROWS_MIGHT_MATCH;
+ }
+ if (!data_file_.nan_value_counts.empty() &&
+ data_file_.nan_value_counts.contains(id) &&
+ data_file_.nan_value_counts.at(id) == 0) {
+ return ROWS_CANNOT_MATCH;
+ }
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotNaN(const std::shared_ptr<BoundTerm>& term) override {
+ auto ptr = std::dynamic_pointer_cast<BoundReference>(term);
+ if (ptr == nullptr) {
+ // identity transforms are already removed by this time
+ return ROWS_MIGHT_MATCH;
+ }
+
+ int id = term->reference()->field().field_id();
+
+ if (ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> Lt(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value() || lower_result.value().IsNaN()) {
+ // NaN indicates unreliable bounds. See the InclusiveMetricsEvaluator docs for more.
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+
+ // this also works for transforms that are order preserving:
+ // if a transform f is order preserving, a < b means that f(a) <= f(b).
+ // because lower <= a for all values of a in the file, f(lower) <= f(a).
+ // when f(lower) >= X then f(a) >= f(lower) >= X, so there is no a such that f(a) < X
+ // f(lower) >= X means rows cannot match
+ if (lower >= lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> LtEq(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value() || lower_result.value().IsNaN()) {
+ // NaN indicates unreliable bounds. See the InclusiveMetricsEvaluator docs for more.
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+
+ // this also works for transforms that are order preserving:
+ // if a transform f is order preserving, a < b means that f(a) <= f(b).
+ // because lower <= a for all values of a in the file, f(lower) <= f(a).
+ // when f(lower) > X then f(a) >= f(lower) > X, so there is no a such that f(a) <= X
+ // f(lower) > X means rows cannot match
+ if (lower > lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> Gt(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+
+ if (upper <= lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> GtEq(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ if (upper < lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> Eq(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto lower_result = LowerBound(term);
+ if (lower_result.has_value()) {
+ const auto& lower = lower_result.value();
+ if (!lower.IsNaN() && lower > lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ if (upper < lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotEq(const std::shared_ptr<BoundTerm>& term,
+ const Literal& lit) override {
+ // because the bounds are not necessarily a min or max value, this cannot be answered
+ // using them. notEq(col, X) with (X, Y) doesn't guarantee that X is a value in col.
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> In(const std::shared_ptr<BoundTerm>& term,
+ const BoundSetPredicate::LiteralSet& literal_set) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ if (literal_set.size() > IN_PREDICATE_LIMIT) {
+ // skip evaluating the predicate if the number of values is too big
+ return ROWS_MIGHT_MATCH;
+ }
+
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value() || lower_result.value().IsNaN()) {
+ // NaN indicates unreliable bounds. See the InclusiveMetricsEvaluator docs for more.
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+ std::vector<Literal> literals;
+ for (const auto& lit : literal_set) {
+ if (lower <= lit) {
+ literals.emplace_back(lit);
+ }
+ }
+ // if all values are less than lower bound, rows cannot match
+ if (literals.empty()) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ std::erase_if(literals, [&](const Literal& x) { return x > upper; });
+ // if remaining values are greater than upper bound, rows cannot match
+ if (literals.empty()) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotIn(const std::shared_ptr<BoundTerm>& term,
+ const BoundSetPredicate::LiteralSet& literal_set) override {
+ // because the bounds are not necessarily a min or max value, this cannot be answered
+ // using them. notIn(col, {X, ...}) with (X, Y) doesn't guarantee that X is a value in
+ // col.
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> StartsWith(const std::shared_ptr<BoundTerm>& term,
+ const Literal& lit) override {
+ if (term->kind() == BoundTerm::Kind::kTransform &&
+ std::dynamic_pointer_cast<BoundTransform>(term)->transform()->transform_type() !=
+ TransformType::kIdentity) {
+ // truncate must be rewritten in binding. the result is either always or never
+ // compatible
+ return ROWS_MIGHT_MATCH;
+ }
+
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ if (lit.type()->type_id() != TypeId::kString) {
+ return ROWS_CANNOT_MATCH;
+ }
+ std::string prefix = get<std::string>(lit.value());
+
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+ auto lower_str = get<std::string>(lower.value());
+ // truncate lower bound so that its length in bytes is not greater than the length of
+ // prefix
+ int length = std::min(prefix.size(), lower_str.size());
+ // if prefix of lower bound is greater than prefix, rows cannot match
+ if (lower_str.substr(0, length) > prefix) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ auto upper_str = get<std::string>(upper.value());
+ // truncate upper bound so that its length in bytes is not greater than the length of
+ // prefix
+ length = std::min(prefix.size(), upper_str.size());
+ // if prefix of upper bound is less than prefix, rows cannot match
+ if (upper_str.substr(0, length) < prefix) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotStartsWith(const std::shared_ptr<BoundTerm>& term,
+ const Literal& lit) override {
+ // the only transforms that produce strings are truncate and identity, which work with
+ // this
+ int id = term->reference()->field().field_id();
+ if (MayContainNull(id)) {
+ return ROWS_MIGHT_MATCH;
+ }
+
+ if (lit.type()->type_id() != TypeId::kString) {
+ return ROWS_CANNOT_MATCH;
+ }
+ std::string prefix = get<std::string>(lit.value());
+
+ // notStartsWith will match unless all values must start with the prefix. This happens
+ // when the lower and upper bounds both start with the prefix.
+ auto lower_result = LowerBound(term);
+ auto upper_result = UpperBound(term);
+ if (!lower_result.has_value() || !upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+ const auto& upper = upper_result.value();
+ auto lower_str = get<std::string>(lower.value());
+ auto upper_str = get<std::string>(upper.value());
+
+ // if lower is shorter than the prefix then lower doesn't start with the prefix
+ if (lower_str.size() < prefix.size()) {
+ return ROWS_MIGHT_MATCH;
+ }
+
+ if (lower_str.starts_with(prefix)) {
+ // if upper is shorter than the prefix then upper can't start with the prefix
+ if (upper_str.size() < prefix.size()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ if (upper_str.starts_with(prefix)) {
+ // both bounds match the prefix, so all rows must match the prefix and therefore
+ // do not satisfy the predicate
+ return ROWS_CANNOT_MATCH;
+ }
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ private:
+ bool MayContainNull(int32_t id) {
+ return data_file_.null_value_counts.empty() ||
+ !data_file_.null_value_counts.contains(id) ||
+ data_file_.null_value_counts.at(id) != 0;
+ }
+
+ bool ContainsNullsOnly(int32_t id) {
+ return !data_file_.value_counts.empty() && data_file_.value_counts.contains(id) &&
+ !data_file_.null_value_counts.empty() &&
+ data_file_.null_value_counts.contains(id) &&
+ data_file_.value_counts.at(id) - data_file_.null_value_counts.at(id) == 0;
+ }
+
+ bool ContainsNaNsOnly(int32_t id) {
+ return !data_file_.nan_value_counts.empty() &&
+ data_file_.nan_value_counts.contains(id) && !data_file_.value_counts.empty() &&
+ data_file_.value_counts.at(id) == data_file_.nan_value_counts.at(id);
+ }
+
+ Result<Literal> LowerBound(const std::shared_ptr<BoundTerm>& term) {
+ if (term->kind() == BoundTerm::Kind::kReference) {
+ return ParseLowerBound(*std::dynamic_pointer_cast<BoundReference>(term));
+ } else if (term->kind() == BoundTerm::Kind::kTransform) {
+ return TransformLowerBound(*std::dynamic_pointer_cast<BoundTransform>(term));
+ } else if (term->kind() == BoundTerm::Kind::kExtract) {
+ // TODO(xiao.dong) handle extract lower and upper bounds
+ return NotImplemented("Extract lower bound not implemented.");
+ } else {
+ return NotFound("Lower bound not found.");
+ }
+ }
+
+ Result<Literal> UpperBound(const std::shared_ptr<BoundTerm>& term) {
+ if (term->kind() == BoundTerm::Kind::kReference) {
+ return ParseUpperBound(*std::dynamic_pointer_cast<BoundReference>(term));
+ } else if (term->kind() == BoundTerm::Kind::kTransform) {
+ return TransformUpperBound(*std::dynamic_pointer_cast<BoundTransform>(term));
+ } else if (term->kind() == BoundTerm::Kind::kExtract) {
+ // TODO(xiao.dong) handle extract lower and upper bounds
+ return NotImplemented("Extract upper bound not implemented.");
+ } else {
+ return NotFound("Upper bound not found.");
+ }
+ }
+
+ Result<Literal> ParseLowerBound(const BoundReference& ref) {
+ int id = ref.field().field_id();
+ auto type = ref.type();
+ if (!type->is_primitive()) {
+ return Invalid("Lower bound of non-primitive type is not supported.");
+ }
+ auto primitive_type = std::dynamic_pointer_cast<PrimitiveType>(type);
+ if (!data_file_.lower_bounds.empty() && data_file_.lower_bounds.contains(id)) {
+ ICEBERG_ASSIGN_OR_RAISE(
+ auto lower,
+ Literal::Deserialize(data_file_.lower_bounds.at(id), primitive_type));
+ return lower;
+ }
+
+ return NotFound("Lower bound not found.");
+ }
+
+ Result<Literal> ParseUpperBound(const BoundReference& ref) {
+ int id = ref.field().field_id();
+ auto type = ref.type();
+ if (!type->is_primitive()) {
+ return Invalid("Upper bound of non-primitive type is not supported.");
+ }
+ auto primitive_type = std::dynamic_pointer_cast<PrimitiveType>(type);
+ if (!data_file_.upper_bounds.empty() && data_file_.upper_bounds.contains(id)) {
+ ICEBERG_ASSIGN_OR_RAISE(
+ auto upper,
+ Literal::Deserialize(data_file_.upper_bounds.at(id), primitive_type));
+ return upper;
+ }
+
+ return NotFound("Upper bound not found.");
+ }
+
+ Result<Literal> TransformLowerBound(BoundTransform& boundTransform) {
+ auto transform = boundTransform.transform();
+ if (transform->PreservesOrder()) {
+ ICEBERG_ASSIGN_OR_RAISE(auto lower, ParseLowerBound(*boundTransform.reference()));
+ ICEBERG_ASSIGN_OR_RAISE(auto transform_func,
+ transform->Bind(boundTransform.reference()->type()));
+ return transform_func->Transform(lower);
+ }
+
+ return NotFound("Transform lower bound not found.");
+ }
+
+ Result<Literal> TransformUpperBound(BoundTransform& boundTransform) {
+ auto transform = boundTransform.transform();
+ if (transform->PreservesOrder()) {
+ ICEBERG_ASSIGN_OR_RAISE(auto upper, ParseLowerBound(*boundTransform.reference()));
+ ICEBERG_ASSIGN_OR_RAISE(auto transform_func,
+ transform->Bind(boundTransform.reference()->type()));
+ return transform_func->Transform(upper);
+ }
+
+ return NotFound("Transform upper bound not found.");
+ }
+
+ // TODO(xiao.dong) handle extract lower and upper bounds
+ /*
+ Literal extractLowerBound(const BoundExtract& bound) {
+
+ }
+
+ Literal extractUpperBound(const BoundExtract& bound) {
+
+ }
+*/
+
+ /** Returns true if the expression term produces a non-null value for non-null input. */
+ bool IsNonNullPreserving(const std::shared_ptr<BoundTerm>& term) {
+ if (term->kind() == BoundTerm::Kind::kReference) {
+ return true;
+ } else if (term->kind() == BoundTerm::Kind::kTransform) {
+ return std::dynamic_pointer_cast<BoundTransform>(term)
+ ->transform()
+ ->PreservesOrder();
+ }
+ // a non-null variant does not necessarily contain a specific field
+ // and unknown bound terms are not non-null preserving
+ return false;
+ }
+
+ private:
+ const DataFile& data_file_;
+};
+
+InclusiveMetricsEvaluator::InclusiveMetricsEvaluator(
+ const std::shared_ptr<Expression>& expr)
+ : expr_(std::move(expr)) {}
+
+InclusiveMetricsEvaluator::~InclusiveMetricsEvaluator() = default;
+
+Result<std::unique_ptr<InclusiveMetricsEvaluator>> InclusiveMetricsEvaluator::Make(
+ std::shared_ptr<Expression> expr, const std::shared_ptr<Schema>& schema,
+ bool case_sensitive) {
+ ICEBERG_ASSIGN_OR_RAISE(auto rewrite_expr, RewriteNot::Visit(std::move(expr)));
+ ICEBERG_ASSIGN_OR_RAISE(auto bound_expr,
+ Binder::Bind(*schema, rewrite_expr, case_sensitive));
+ return std::unique_ptr<InclusiveMetricsEvaluator>(
+ new InclusiveMetricsEvaluator(std::move(bound_expr)));
+}
+
+Result<bool> InclusiveMetricsEvaluator::Eval(const DataFile& data_file) const {
+ if (data_file.record_count <= 0) {
+ return ROWS_CANNOT_MATCH;
+ }
Review Comment:
```suggestion
if (data_file.record_count == 0) {
return kRowsCannotMatch;
}
if (data_file.record_count < 0) {
return kRowsMightMatch;
}
```
##########
src/iceberg/expression/inclusive_metrics_evaluator.cc:
##########
@@ -0,0 +1,544 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "iceberg/expression/inclusive_metrics_evaluator.h"
+
+#include "iceberg/expression/binder.h"
+#include "iceberg/expression/expression_visitor.h"
+#include "iceberg/expression/rewrite_not.h"
+#include "iceberg/manifest_entry.h"
+#include "iceberg/schema.h"
+#include "iceberg/transform.h"
+#include "iceberg/util/macros.h"
+
+namespace iceberg {
+
+namespace {
+static const bool ROWS_MIGHT_MATCH = true;
+static const bool ROWS_CANNOT_MATCH = false;
+static const int32_t IN_PREDICATE_LIMIT = 200;
+} // namespace
+
+class InclusiveMetricsVisitor : public BoundVisitor<bool> {
+ public:
+ explicit InclusiveMetricsVisitor(const DataFile& data_file) : data_file_(data_file) {}
+
+ Result<bool> AlwaysTrue() override { return ROWS_MIGHT_MATCH; }
+
+ Result<bool> AlwaysFalse() override { return ROWS_CANNOT_MATCH; }
+
+ Result<bool> Not(bool child_result) override { return !child_result; }
+
+ Result<bool> And(bool left_result, bool right_result) override {
+ return left_result && right_result;
+ }
+
+ Result<bool> Or(bool left_result, bool right_result) override {
+ return left_result || right_result;
+ }
+
+ Result<bool> IsNull(const std::shared_ptr<BoundTerm>& term) override {
+ if (IsNonNullPreserving(term)) {
+ // number of non-nulls is the same as for the ref
+ int id = term->reference()->field().field_id();
+ if (!MayContainNull(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ }
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotNull(const std::shared_ptr<BoundTerm>& term) override {
+ // no need to check whether the field is required because binding evaluates that case
+ // if the column has no non-null values, the expression cannot match
+
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> IsNaN(const std::shared_ptr<BoundTerm>& term) override {
+ // when there's no nanCounts information, but we already know the column only contains
+ // null, it's guaranteed that there's no NaN value
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ auto ptr = std::dynamic_pointer_cast<BoundReference>(term);
+ if (ptr == nullptr) {
+ return ROWS_MIGHT_MATCH;
+ }
+ if (!data_file_.nan_value_counts.empty() &&
+ data_file_.nan_value_counts.contains(id) &&
+ data_file_.nan_value_counts.at(id) == 0) {
+ return ROWS_CANNOT_MATCH;
+ }
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotNaN(const std::shared_ptr<BoundTerm>& term) override {
+ auto ptr = std::dynamic_pointer_cast<BoundReference>(term);
+ if (ptr == nullptr) {
+ // identity transforms are already removed by this time
+ return ROWS_MIGHT_MATCH;
+ }
+
+ int id = term->reference()->field().field_id();
+
+ if (ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> Lt(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value() || lower_result.value().IsNaN()) {
+ // NaN indicates unreliable bounds. See the InclusiveMetricsEvaluator docs for more.
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+
+ // this also works for transforms that are order preserving:
+ // if a transform f is order preserving, a < b means that f(a) <= f(b).
+ // because lower <= a for all values of a in the file, f(lower) <= f(a).
+ // when f(lower) >= X then f(a) >= f(lower) >= X, so there is no a such that f(a) < X
+ // f(lower) >= X means rows cannot match
+ if (lower >= lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> LtEq(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value() || lower_result.value().IsNaN()) {
+ // NaN indicates unreliable bounds. See the InclusiveMetricsEvaluator docs for more.
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+
+ // this also works for transforms that are order preserving:
+ // if a transform f is order preserving, a < b means that f(a) <= f(b).
+ // because lower <= a for all values of a in the file, f(lower) <= f(a).
+ // when f(lower) > X then f(a) >= f(lower) > X, so there is no a such that f(a) <= X
+ // f(lower) > X means rows cannot match
+ if (lower > lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> Gt(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+
+ if (upper <= lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> GtEq(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ if (upper < lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> Eq(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto lower_result = LowerBound(term);
+ if (lower_result.has_value()) {
+ const auto& lower = lower_result.value();
+ if (!lower.IsNaN() && lower > lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ if (upper < lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotEq(const std::shared_ptr<BoundTerm>& term,
+ const Literal& lit) override {
+ // because the bounds are not necessarily a min or max value, this cannot be answered
+ // using them. notEq(col, X) with (X, Y) doesn't guarantee that X is a value in col.
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> In(const std::shared_ptr<BoundTerm>& term,
+ const BoundSetPredicate::LiteralSet& literal_set) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ if (literal_set.size() > IN_PREDICATE_LIMIT) {
+ // skip evaluating the predicate if the number of values is too big
+ return ROWS_MIGHT_MATCH;
+ }
+
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value() || lower_result.value().IsNaN()) {
+ // NaN indicates unreliable bounds. See the InclusiveMetricsEvaluator docs for more.
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+ std::vector<Literal> literals;
+ for (const auto& lit : literal_set) {
Review Comment:
nit: use ranges to only execute these on views.
##########
src/iceberg/expression/inclusive_metrics_evaluator.cc:
##########
@@ -0,0 +1,544 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "iceberg/expression/inclusive_metrics_evaluator.h"
+
+#include "iceberg/expression/binder.h"
+#include "iceberg/expression/expression_visitor.h"
+#include "iceberg/expression/rewrite_not.h"
+#include "iceberg/manifest_entry.h"
+#include "iceberg/schema.h"
+#include "iceberg/transform.h"
+#include "iceberg/util/macros.h"
+
+namespace iceberg {
+
+namespace {
+static const bool ROWS_MIGHT_MATCH = true;
+static const bool ROWS_CANNOT_MATCH = false;
+static const int32_t IN_PREDICATE_LIMIT = 200;
+} // namespace
+
+class InclusiveMetricsVisitor : public BoundVisitor<bool> {
+ public:
+ explicit InclusiveMetricsVisitor(const DataFile& data_file) : data_file_(data_file) {}
+
+ Result<bool> AlwaysTrue() override { return ROWS_MIGHT_MATCH; }
+
+ Result<bool> AlwaysFalse() override { return ROWS_CANNOT_MATCH; }
+
+ Result<bool> Not(bool child_result) override { return !child_result; }
+
+ Result<bool> And(bool left_result, bool right_result) override {
+ return left_result && right_result;
+ }
+
+ Result<bool> Or(bool left_result, bool right_result) override {
+ return left_result || right_result;
+ }
+
+ Result<bool> IsNull(const std::shared_ptr<BoundTerm>& term) override {
+ if (IsNonNullPreserving(term)) {
+ // number of non-nulls is the same as for the ref
+ int id = term->reference()->field().field_id();
+ if (!MayContainNull(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ }
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotNull(const std::shared_ptr<BoundTerm>& term) override {
+ // no need to check whether the field is required because binding evaluates that case
+ // if the column has no non-null values, the expression cannot match
+
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> IsNaN(const std::shared_ptr<BoundTerm>& term) override {
+ // when there's no nanCounts information, but we already know the column only contains
+ // null, it's guaranteed that there's no NaN value
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ auto ptr = std::dynamic_pointer_cast<BoundReference>(term);
+ if (ptr == nullptr) {
+ return ROWS_MIGHT_MATCH;
+ }
+ if (!data_file_.nan_value_counts.empty() &&
+ data_file_.nan_value_counts.contains(id) &&
+ data_file_.nan_value_counts.at(id) == 0) {
+ return ROWS_CANNOT_MATCH;
+ }
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotNaN(const std::shared_ptr<BoundTerm>& term) override {
+ auto ptr = std::dynamic_pointer_cast<BoundReference>(term);
+ if (ptr == nullptr) {
+ // identity transforms are already removed by this time
+ return ROWS_MIGHT_MATCH;
+ }
+
+ int id = term->reference()->field().field_id();
+
+ if (ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> Lt(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value() || lower_result.value().IsNaN()) {
+ // NaN indicates unreliable bounds. See the InclusiveMetricsEvaluator docs for more.
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+
+ // this also works for transforms that are order preserving:
+ // if a transform f is order preserving, a < b means that f(a) <= f(b).
+ // because lower <= a for all values of a in the file, f(lower) <= f(a).
+ // when f(lower) >= X then f(a) >= f(lower) >= X, so there is no a such that f(a) < X
+ // f(lower) >= X means rows cannot match
+ if (lower >= lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> LtEq(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value() || lower_result.value().IsNaN()) {
Review Comment:
ditto
##########
src/iceberg/expression/inclusive_metrics_evaluator.cc:
##########
@@ -0,0 +1,544 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "iceberg/expression/inclusive_metrics_evaluator.h"
+
+#include "iceberg/expression/binder.h"
+#include "iceberg/expression/expression_visitor.h"
+#include "iceberg/expression/rewrite_not.h"
+#include "iceberg/manifest_entry.h"
+#include "iceberg/schema.h"
+#include "iceberg/transform.h"
+#include "iceberg/util/macros.h"
+
+namespace iceberg {
+
+namespace {
+static const bool ROWS_MIGHT_MATCH = true;
+static const bool ROWS_CANNOT_MATCH = false;
+static const int32_t IN_PREDICATE_LIMIT = 200;
+} // namespace
+
+class InclusiveMetricsVisitor : public BoundVisitor<bool> {
+ public:
+ explicit InclusiveMetricsVisitor(const DataFile& data_file) : data_file_(data_file) {}
+
+ Result<bool> AlwaysTrue() override { return ROWS_MIGHT_MATCH; }
+
+ Result<bool> AlwaysFalse() override { return ROWS_CANNOT_MATCH; }
+
+ Result<bool> Not(bool child_result) override { return !child_result; }
+
+ Result<bool> And(bool left_result, bool right_result) override {
+ return left_result && right_result;
+ }
+
+ Result<bool> Or(bool left_result, bool right_result) override {
+ return left_result || right_result;
+ }
+
+ Result<bool> IsNull(const std::shared_ptr<BoundTerm>& term) override {
+ if (IsNonNullPreserving(term)) {
+ // number of non-nulls is the same as for the ref
+ int id = term->reference()->field().field_id();
+ if (!MayContainNull(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ }
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotNull(const std::shared_ptr<BoundTerm>& term) override {
+ // no need to check whether the field is required because binding evaluates that case
+ // if the column has no non-null values, the expression cannot match
+
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> IsNaN(const std::shared_ptr<BoundTerm>& term) override {
+ // when there's no nanCounts information, but we already know the column only contains
+ // null, it's guaranteed that there's no NaN value
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ auto ptr = std::dynamic_pointer_cast<BoundReference>(term);
+ if (ptr == nullptr) {
+ return ROWS_MIGHT_MATCH;
+ }
+ if (!data_file_.nan_value_counts.empty() &&
+ data_file_.nan_value_counts.contains(id) &&
+ data_file_.nan_value_counts.at(id) == 0) {
+ return ROWS_CANNOT_MATCH;
+ }
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotNaN(const std::shared_ptr<BoundTerm>& term) override {
+ auto ptr = std::dynamic_pointer_cast<BoundReference>(term);
+ if (ptr == nullptr) {
+ // identity transforms are already removed by this time
+ return ROWS_MIGHT_MATCH;
+ }
+
+ int id = term->reference()->field().field_id();
+
+ if (ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> Lt(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value() || lower_result.value().IsNaN()) {
Review Comment:
We need to check `lower_result.value().IsNull()` here.
##########
src/iceberg/expression/inclusive_metrics_evaluator.cc:
##########
@@ -0,0 +1,544 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "iceberg/expression/inclusive_metrics_evaluator.h"
+
+#include "iceberg/expression/binder.h"
+#include "iceberg/expression/expression_visitor.h"
+#include "iceberg/expression/rewrite_not.h"
+#include "iceberg/manifest_entry.h"
+#include "iceberg/schema.h"
+#include "iceberg/transform.h"
+#include "iceberg/util/macros.h"
+
+namespace iceberg {
+
+namespace {
+static const bool ROWS_MIGHT_MATCH = true;
+static const bool ROWS_CANNOT_MATCH = false;
+static const int32_t IN_PREDICATE_LIMIT = 200;
+} // namespace
+
+class InclusiveMetricsVisitor : public BoundVisitor<bool> {
+ public:
+ explicit InclusiveMetricsVisitor(const DataFile& data_file) : data_file_(data_file) {}
+
+ Result<bool> AlwaysTrue() override { return ROWS_MIGHT_MATCH; }
+
+ Result<bool> AlwaysFalse() override { return ROWS_CANNOT_MATCH; }
+
+ Result<bool> Not(bool child_result) override { return !child_result; }
+
+ Result<bool> And(bool left_result, bool right_result) override {
+ return left_result && right_result;
+ }
+
+ Result<bool> Or(bool left_result, bool right_result) override {
+ return left_result || right_result;
+ }
+
+ Result<bool> IsNull(const std::shared_ptr<BoundTerm>& term) override {
+ if (IsNonNullPreserving(term)) {
+ // number of non-nulls is the same as for the ref
+ int id = term->reference()->field().field_id();
+ if (!MayContainNull(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ }
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotNull(const std::shared_ptr<BoundTerm>& term) override {
+ // no need to check whether the field is required because binding evaluates that case
+ // if the column has no non-null values, the expression cannot match
+
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> IsNaN(const std::shared_ptr<BoundTerm>& term) override {
+ // when there's no nanCounts information, but we already know the column only contains
+ // null, it's guaranteed that there's no NaN value
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ auto ptr = std::dynamic_pointer_cast<BoundReference>(term);
Review Comment:
Same for below
##########
src/iceberg/expression/inclusive_metrics_evaluator.cc:
##########
@@ -0,0 +1,544 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "iceberg/expression/inclusive_metrics_evaluator.h"
+
+#include "iceberg/expression/binder.h"
+#include "iceberg/expression/expression_visitor.h"
+#include "iceberg/expression/rewrite_not.h"
+#include "iceberg/manifest_entry.h"
+#include "iceberg/schema.h"
+#include "iceberg/transform.h"
+#include "iceberg/util/macros.h"
+
+namespace iceberg {
+
+namespace {
+static const bool ROWS_MIGHT_MATCH = true;
+static const bool ROWS_CANNOT_MATCH = false;
+static const int32_t IN_PREDICATE_LIMIT = 200;
+} // namespace
+
+class InclusiveMetricsVisitor : public BoundVisitor<bool> {
+ public:
+ explicit InclusiveMetricsVisitor(const DataFile& data_file) : data_file_(data_file) {}
+
+ Result<bool> AlwaysTrue() override { return ROWS_MIGHT_MATCH; }
+
+ Result<bool> AlwaysFalse() override { return ROWS_CANNOT_MATCH; }
+
+ Result<bool> Not(bool child_result) override { return !child_result; }
+
+ Result<bool> And(bool left_result, bool right_result) override {
+ return left_result && right_result;
+ }
+
+ Result<bool> Or(bool left_result, bool right_result) override {
+ return left_result || right_result;
+ }
+
+ Result<bool> IsNull(const std::shared_ptr<BoundTerm>& term) override {
+ if (IsNonNullPreserving(term)) {
+ // number of non-nulls is the same as for the ref
+ int id = term->reference()->field().field_id();
+ if (!MayContainNull(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ }
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotNull(const std::shared_ptr<BoundTerm>& term) override {
+ // no need to check whether the field is required because binding evaluates that case
+ // if the column has no non-null values, the expression cannot match
+
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> IsNaN(const std::shared_ptr<BoundTerm>& term) override {
+ // when there's no nanCounts information, but we already know the column only contains
+ // null, it's guaranteed that there's no NaN value
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ auto ptr = std::dynamic_pointer_cast<BoundReference>(term);
+ if (ptr == nullptr) {
+ return ROWS_MIGHT_MATCH;
+ }
+ if (!data_file_.nan_value_counts.empty() &&
+ data_file_.nan_value_counts.contains(id) &&
+ data_file_.nan_value_counts.at(id) == 0) {
+ return ROWS_CANNOT_MATCH;
+ }
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotNaN(const std::shared_ptr<BoundTerm>& term) override {
+ auto ptr = std::dynamic_pointer_cast<BoundReference>(term);
+ if (ptr == nullptr) {
+ // identity transforms are already removed by this time
+ return ROWS_MIGHT_MATCH;
+ }
+
+ int id = term->reference()->field().field_id();
+
+ if (ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> Lt(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value() || lower_result.value().IsNaN()) {
+ // NaN indicates unreliable bounds. See the InclusiveMetricsEvaluator docs for more.
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+
+ // this also works for transforms that are order preserving:
+ // if a transform f is order preserving, a < b means that f(a) <= f(b).
+ // because lower <= a for all values of a in the file, f(lower) <= f(a).
+ // when f(lower) >= X then f(a) >= f(lower) >= X, so there is no a such that f(a) < X
+ // f(lower) >= X means rows cannot match
+ if (lower >= lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> LtEq(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value() || lower_result.value().IsNaN()) {
+ // NaN indicates unreliable bounds. See the InclusiveMetricsEvaluator docs for more.
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+
+ // this also works for transforms that are order preserving:
+ // if a transform f is order preserving, a < b means that f(a) <= f(b).
+ // because lower <= a for all values of a in the file, f(lower) <= f(a).
+ // when f(lower) > X then f(a) >= f(lower) > X, so there is no a such that f(a) <= X
+ // f(lower) > X means rows cannot match
+ if (lower > lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> Gt(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+
+ if (upper <= lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> GtEq(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ if (upper < lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> Eq(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto lower_result = LowerBound(term);
+ if (lower_result.has_value()) {
+ const auto& lower = lower_result.value();
+ if (!lower.IsNaN() && lower > lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ if (upper < lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotEq(const std::shared_ptr<BoundTerm>& term,
+ const Literal& lit) override {
+ // because the bounds are not necessarily a min or max value, this cannot be answered
+ // using them. notEq(col, X) with (X, Y) doesn't guarantee that X is a value in col.
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> In(const std::shared_ptr<BoundTerm>& term,
+ const BoundSetPredicate::LiteralSet& literal_set) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ if (literal_set.size() > IN_PREDICATE_LIMIT) {
+ // skip evaluating the predicate if the number of values is too big
+ return ROWS_MIGHT_MATCH;
+ }
+
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value() || lower_result.value().IsNaN()) {
+ // NaN indicates unreliable bounds. See the InclusiveMetricsEvaluator docs for more.
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+ std::vector<Literal> literals;
+ for (const auto& lit : literal_set) {
+ if (lower <= lit) {
+ literals.emplace_back(lit);
+ }
+ }
+ // if all values are less than lower bound, rows cannot match
+ if (literals.empty()) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ std::erase_if(literals, [&](const Literal& x) { return x > upper; });
+ // if remaining values are greater than upper bound, rows cannot match
+ if (literals.empty()) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotIn(const std::shared_ptr<BoundTerm>& term,
+ const BoundSetPredicate::LiteralSet& literal_set) override {
+ // because the bounds are not necessarily a min or max value, this cannot be answered
+ // using them. notIn(col, {X, ...}) with (X, Y) doesn't guarantee that X is a value in
+ // col.
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> StartsWith(const std::shared_ptr<BoundTerm>& term,
+ const Literal& lit) override {
+ if (term->kind() == BoundTerm::Kind::kTransform &&
+ std::dynamic_pointer_cast<BoundTransform>(term)->transform()->transform_type() !=
+ TransformType::kIdentity) {
+ // truncate must be rewritten in binding. the result is either always or never
+ // compatible
+ return ROWS_MIGHT_MATCH;
+ }
+
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ if (lit.type()->type_id() != TypeId::kString) {
+ return ROWS_CANNOT_MATCH;
+ }
+ std::string prefix = get<std::string>(lit.value());
+
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+ auto lower_str = get<std::string>(lower.value());
+ // truncate lower bound so that its length in bytes is not greater than the length of
+ // prefix
+ int length = std::min(prefix.size(), lower_str.size());
+ // if prefix of lower bound is greater than prefix, rows cannot match
+ if (lower_str.substr(0, length) > prefix) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ auto upper_str = get<std::string>(upper.value());
+ // truncate upper bound so that its length in bytes is not greater than the length of
+ // prefix
+ length = std::min(prefix.size(), upper_str.size());
+ // if prefix of upper bound is less than prefix, rows cannot match
+ if (upper_str.substr(0, length) < prefix) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotStartsWith(const std::shared_ptr<BoundTerm>& term,
+ const Literal& lit) override {
+ // the only transforms that produce strings are truncate and identity, which work with
+ // this
+ int id = term->reference()->field().field_id();
+ if (MayContainNull(id)) {
+ return ROWS_MIGHT_MATCH;
+ }
+
+ if (lit.type()->type_id() != TypeId::kString) {
+ return ROWS_CANNOT_MATCH;
+ }
+ std::string prefix = get<std::string>(lit.value());
+
+ // notStartsWith will match unless all values must start with the prefix. This happens
+ // when the lower and upper bounds both start with the prefix.
+ auto lower_result = LowerBound(term);
+ auto upper_result = UpperBound(term);
+ if (!lower_result.has_value() || !upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+ const auto& upper = upper_result.value();
+ auto lower_str = get<std::string>(lower.value());
+ auto upper_str = get<std::string>(upper.value());
+
+ // if lower is shorter than the prefix then lower doesn't start with the prefix
+ if (lower_str.size() < prefix.size()) {
+ return ROWS_MIGHT_MATCH;
+ }
+
+ if (lower_str.starts_with(prefix)) {
+ // if upper is shorter than the prefix then upper can't start with the prefix
+ if (upper_str.size() < prefix.size()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ if (upper_str.starts_with(prefix)) {
+ // both bounds match the prefix, so all rows must match the prefix and therefore
+ // do not satisfy the predicate
+ return ROWS_CANNOT_MATCH;
+ }
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ private:
+ bool MayContainNull(int32_t id) {
+ return data_file_.null_value_counts.empty() ||
+ !data_file_.null_value_counts.contains(id) ||
+ data_file_.null_value_counts.at(id) != 0;
+ }
+
+ bool ContainsNullsOnly(int32_t id) {
+ return !data_file_.value_counts.empty() && data_file_.value_counts.contains(id) &&
+ !data_file_.null_value_counts.empty() &&
+ data_file_.null_value_counts.contains(id) &&
+ data_file_.value_counts.at(id) - data_file_.null_value_counts.at(id) == 0;
+ }
+
+ bool ContainsNaNsOnly(int32_t id) {
+ return !data_file_.nan_value_counts.empty() &&
+ data_file_.nan_value_counts.contains(id) && !data_file_.value_counts.empty() &&
+ data_file_.value_counts.at(id) == data_file_.nan_value_counts.at(id);
+ }
+
+ Result<Literal> LowerBound(const std::shared_ptr<BoundTerm>& term) {
+ if (term->kind() == BoundTerm::Kind::kReference) {
+ return ParseLowerBound(*std::dynamic_pointer_cast<BoundReference>(term));
+ } else if (term->kind() == BoundTerm::Kind::kTransform) {
+ return TransformLowerBound(*std::dynamic_pointer_cast<BoundTransform>(term));
+ } else if (term->kind() == BoundTerm::Kind::kExtract) {
+ // TODO(xiao.dong) handle extract lower and upper bounds
+ return NotImplemented("Extract lower bound not implemented.");
+ } else {
+ return NotFound("Lower bound not found.");
+ }
+ }
+
+ Result<Literal> UpperBound(const std::shared_ptr<BoundTerm>& term) {
+ if (term->kind() == BoundTerm::Kind::kReference) {
+ return ParseUpperBound(*std::dynamic_pointer_cast<BoundReference>(term));
+ } else if (term->kind() == BoundTerm::Kind::kTransform) {
+ return TransformUpperBound(*std::dynamic_pointer_cast<BoundTransform>(term));
+ } else if (term->kind() == BoundTerm::Kind::kExtract) {
+ // TODO(xiao.dong) handle extract lower and upper bounds
+ return NotImplemented("Extract upper bound not implemented.");
+ } else {
+ return NotFound("Upper bound not found.");
+ }
+ }
+
+ Result<Literal> ParseLowerBound(const BoundReference& ref) {
+ int id = ref.field().field_id();
+ auto type = ref.type();
+ if (!type->is_primitive()) {
+ return Invalid("Lower bound of non-primitive type is not supported.");
+ }
+ auto primitive_type = std::dynamic_pointer_cast<PrimitiveType>(type);
+ if (!data_file_.lower_bounds.empty() && data_file_.lower_bounds.contains(id)) {
+ ICEBERG_ASSIGN_OR_RAISE(
+ auto lower,
+ Literal::Deserialize(data_file_.lower_bounds.at(id), primitive_type));
+ return lower;
+ }
+
+ return NotFound("Lower bound not found.");
+ }
+
+ Result<Literal> ParseUpperBound(const BoundReference& ref) {
+ int id = ref.field().field_id();
+ auto type = ref.type();
+ if (!type->is_primitive()) {
+ return Invalid("Upper bound of non-primitive type is not supported.");
+ }
+ auto primitive_type = std::dynamic_pointer_cast<PrimitiveType>(type);
+ if (!data_file_.upper_bounds.empty() && data_file_.upper_bounds.contains(id)) {
+ ICEBERG_ASSIGN_OR_RAISE(
+ auto upper,
+ Literal::Deserialize(data_file_.upper_bounds.at(id), primitive_type));
+ return upper;
+ }
+
+ return NotFound("Upper bound not found.");
+ }
+
+ Result<Literal> TransformLowerBound(BoundTransform& boundTransform) {
+ auto transform = boundTransform.transform();
+ if (transform->PreservesOrder()) {
+ ICEBERG_ASSIGN_OR_RAISE(auto lower, ParseLowerBound(*boundTransform.reference()));
+ ICEBERG_ASSIGN_OR_RAISE(auto transform_func,
+ transform->Bind(boundTransform.reference()->type()));
+ return transform_func->Transform(lower);
+ }
+
+ return NotFound("Transform lower bound not found.");
+ }
+
+ Result<Literal> TransformUpperBound(BoundTransform& boundTransform) {
+ auto transform = boundTransform.transform();
+ if (transform->PreservesOrder()) {
+ ICEBERG_ASSIGN_OR_RAISE(auto upper, ParseLowerBound(*boundTransform.reference()));
+ ICEBERG_ASSIGN_OR_RAISE(auto transform_func,
+ transform->Bind(boundTransform.reference()->type()));
+ return transform_func->Transform(upper);
+ }
+
+ return NotFound("Transform upper bound not found.");
+ }
+
+ // TODO(xiao.dong) handle extract lower and upper bounds
+ /*
+ Literal extractLowerBound(const BoundExtract& bound) {
+
+ }
+
+ Literal extractUpperBound(const BoundExtract& bound) {
+
+ }
+*/
+
+ /** Returns true if the expression term produces a non-null value for non-null input. */
+ bool IsNonNullPreserving(const std::shared_ptr<BoundTerm>& term) {
+ if (term->kind() == BoundTerm::Kind::kReference) {
+ return true;
+ } else if (term->kind() == BoundTerm::Kind::kTransform) {
+ return std::dynamic_pointer_cast<BoundTransform>(term)
+ ->transform()
+ ->PreservesOrder();
+ }
+ // a non-null variant does not necessarily contain a specific field
+ // and unknown bound terms are not non-null preserving
+ return false;
+ }
+
+ private:
+ const DataFile& data_file_;
+};
+
+InclusiveMetricsEvaluator::InclusiveMetricsEvaluator(
+ const std::shared_ptr<Expression>& expr)
Review Comment:
```suggestion
std::shared_ptr<Expression> expr)
```
We shouldn't move a const ref.
##########
src/iceberg/expression/inclusive_metrics_evaluator.cc:
##########
@@ -0,0 +1,544 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "iceberg/expression/inclusive_metrics_evaluator.h"
+
+#include "iceberg/expression/binder.h"
+#include "iceberg/expression/expression_visitor.h"
+#include "iceberg/expression/rewrite_not.h"
+#include "iceberg/manifest_entry.h"
+#include "iceberg/schema.h"
+#include "iceberg/transform.h"
+#include "iceberg/util/macros.h"
+
+namespace iceberg {
+
+namespace {
+static const bool ROWS_MIGHT_MATCH = true;
+static const bool ROWS_CANNOT_MATCH = false;
+static const int32_t IN_PREDICATE_LIMIT = 200;
+} // namespace
+
+class InclusiveMetricsVisitor : public BoundVisitor<bool> {
+ public:
+ explicit InclusiveMetricsVisitor(const DataFile& data_file) : data_file_(data_file) {}
+
+ Result<bool> AlwaysTrue() override { return ROWS_MIGHT_MATCH; }
+
+ Result<bool> AlwaysFalse() override { return ROWS_CANNOT_MATCH; }
+
+ Result<bool> Not(bool child_result) override { return !child_result; }
+
+ Result<bool> And(bool left_result, bool right_result) override {
+ return left_result && right_result;
+ }
+
+ Result<bool> Or(bool left_result, bool right_result) override {
+ return left_result || right_result;
+ }
+
+ Result<bool> IsNull(const std::shared_ptr<BoundTerm>& term) override {
+ if (IsNonNullPreserving(term)) {
+ // number of non-nulls is the same as for the ref
+ int id = term->reference()->field().field_id();
+ if (!MayContainNull(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ }
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotNull(const std::shared_ptr<BoundTerm>& term) override {
+ // no need to check whether the field is required because binding evaluates that case
+ // if the column has no non-null values, the expression cannot match
+
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> IsNaN(const std::shared_ptr<BoundTerm>& term) override {
+ // when there's no nanCounts information, but we already know the column only contains
+ // null, it's guaranteed that there's no NaN value
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ auto ptr = std::dynamic_pointer_cast<BoundReference>(term);
+ if (ptr == nullptr) {
+ return ROWS_MIGHT_MATCH;
+ }
+ if (!data_file_.nan_value_counts.empty() &&
+ data_file_.nan_value_counts.contains(id) &&
+ data_file_.nan_value_counts.at(id) == 0) {
+ return ROWS_CANNOT_MATCH;
+ }
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotNaN(const std::shared_ptr<BoundTerm>& term) override {
+ auto ptr = std::dynamic_pointer_cast<BoundReference>(term);
+ if (ptr == nullptr) {
+ // identity transforms are already removed by this time
+ return ROWS_MIGHT_MATCH;
+ }
+
+ int id = term->reference()->field().field_id();
+
+ if (ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> Lt(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value() || lower_result.value().IsNaN()) {
+ // NaN indicates unreliable bounds. See the InclusiveMetricsEvaluator docs for more.
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+
+ // this also works for transforms that are order preserving:
+ // if a transform f is order preserving, a < b means that f(a) <= f(b).
+ // because lower <= a for all values of a in the file, f(lower) <= f(a).
+ // when f(lower) >= X then f(a) >= f(lower) >= X, so there is no a such that f(a) < X
+ // f(lower) >= X means rows cannot match
+ if (lower >= lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> LtEq(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value() || lower_result.value().IsNaN()) {
+ // NaN indicates unreliable bounds. See the InclusiveMetricsEvaluator docs for more.
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+
+ // this also works for transforms that are order preserving:
+ // if a transform f is order preserving, a < b means that f(a) <= f(b).
+ // because lower <= a for all values of a in the file, f(lower) <= f(a).
+ // when f(lower) > X then f(a) >= f(lower) > X, so there is no a such that f(a) <= X
+ // f(lower) > X means rows cannot match
+ if (lower > lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> Gt(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+
+ if (upper <= lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> GtEq(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ if (upper < lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> Eq(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto lower_result = LowerBound(term);
+ if (lower_result.has_value()) {
+ const auto& lower = lower_result.value();
+ if (!lower.IsNaN() && lower > lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ if (upper < lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotEq(const std::shared_ptr<BoundTerm>& term,
+ const Literal& lit) override {
+ // because the bounds are not necessarily a min or max value, this cannot be answered
+ // using them. notEq(col, X) with (X, Y) doesn't guarantee that X is a value in col.
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> In(const std::shared_ptr<BoundTerm>& term,
+ const BoundSetPredicate::LiteralSet& literal_set) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ if (literal_set.size() > IN_PREDICATE_LIMIT) {
+ // skip evaluating the predicate if the number of values is too big
+ return ROWS_MIGHT_MATCH;
+ }
+
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value() || lower_result.value().IsNaN()) {
+ // NaN indicates unreliable bounds. See the InclusiveMetricsEvaluator docs for more.
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+ std::vector<Literal> literals;
+ for (const auto& lit : literal_set) {
+ if (lower <= lit) {
+ literals.emplace_back(lit);
+ }
+ }
+ // if all values are less than lower bound, rows cannot match
+ if (literals.empty()) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ std::erase_if(literals, [&](const Literal& x) { return x > upper; });
+ // if remaining values are greater than upper bound, rows cannot match
+ if (literals.empty()) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotIn(const std::shared_ptr<BoundTerm>& term,
+ const BoundSetPredicate::LiteralSet& literal_set) override {
+ // because the bounds are not necessarily a min or max value, this cannot be answered
+ // using them. notIn(col, {X, ...}) with (X, Y) doesn't guarantee that X is a value in
+ // col.
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> StartsWith(const std::shared_ptr<BoundTerm>& term,
+ const Literal& lit) override {
+ if (term->kind() == BoundTerm::Kind::kTransform &&
+ std::dynamic_pointer_cast<BoundTransform>(term)->transform()->transform_type() !=
+ TransformType::kIdentity) {
+ // truncate must be rewritten in binding. the result is either always or never
+ // compatible
+ return ROWS_MIGHT_MATCH;
+ }
+
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ if (lit.type()->type_id() != TypeId::kString) {
+ return ROWS_CANNOT_MATCH;
+ }
+ std::string prefix = get<std::string>(lit.value());
+
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+ auto lower_str = get<std::string>(lower.value());
+ // truncate lower bound so that its length in bytes is not greater than the length of
+ // prefix
+ int length = std::min(prefix.size(), lower_str.size());
+ // if prefix of lower bound is greater than prefix, rows cannot match
+ if (lower_str.substr(0, length) > prefix) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ auto upper_str = get<std::string>(upper.value());
+ // truncate upper bound so that its length in bytes is not greater than the length of
+ // prefix
+ length = std::min(prefix.size(), upper_str.size());
+ // if prefix of upper bound is less than prefix, rows cannot match
+ if (upper_str.substr(0, length) < prefix) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotStartsWith(const std::shared_ptr<BoundTerm>& term,
+ const Literal& lit) override {
+ // the only transforms that produce strings are truncate and identity, which work with
+ // this
+ int id = term->reference()->field().field_id();
+ if (MayContainNull(id)) {
+ return ROWS_MIGHT_MATCH;
+ }
+
+ if (lit.type()->type_id() != TypeId::kString) {
+ return ROWS_CANNOT_MATCH;
+ }
+ std::string prefix = get<std::string>(lit.value());
+
+ // notStartsWith will match unless all values must start with the prefix. This happens
+ // when the lower and upper bounds both start with the prefix.
+ auto lower_result = LowerBound(term);
+ auto upper_result = UpperBound(term);
+ if (!lower_result.has_value() || !upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+ const auto& upper = upper_result.value();
+ auto lower_str = get<std::string>(lower.value());
+ auto upper_str = get<std::string>(upper.value());
+
+ // if lower is shorter than the prefix then lower doesn't start with the prefix
+ if (lower_str.size() < prefix.size()) {
+ return ROWS_MIGHT_MATCH;
+ }
+
+ if (lower_str.starts_with(prefix)) {
+ // if upper is shorter than the prefix then upper can't start with the prefix
+ if (upper_str.size() < prefix.size()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ if (upper_str.starts_with(prefix)) {
+ // both bounds match the prefix, so all rows must match the prefix and therefore
+ // do not satisfy the predicate
+ return ROWS_CANNOT_MATCH;
+ }
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ private:
+ bool MayContainNull(int32_t id) {
+ return data_file_.null_value_counts.empty() ||
+ !data_file_.null_value_counts.contains(id) ||
+ data_file_.null_value_counts.at(id) != 0;
+ }
+
+ bool ContainsNullsOnly(int32_t id) {
+ return !data_file_.value_counts.empty() && data_file_.value_counts.contains(id) &&
+ !data_file_.null_value_counts.empty() &&
+ data_file_.null_value_counts.contains(id) &&
+ data_file_.value_counts.at(id) - data_file_.null_value_counts.at(id) == 0;
+ }
+
+ bool ContainsNaNsOnly(int32_t id) {
+ return !data_file_.nan_value_counts.empty() &&
+ data_file_.nan_value_counts.contains(id) && !data_file_.value_counts.empty() &&
+ data_file_.value_counts.at(id) == data_file_.nan_value_counts.at(id);
+ }
+
+ Result<Literal> LowerBound(const std::shared_ptr<BoundTerm>& term) {
+ if (term->kind() == BoundTerm::Kind::kReference) {
+ return ParseLowerBound(*std::dynamic_pointer_cast<BoundReference>(term));
+ } else if (term->kind() == BoundTerm::Kind::kTransform) {
+ return TransformLowerBound(*std::dynamic_pointer_cast<BoundTransform>(term));
+ } else if (term->kind() == BoundTerm::Kind::kExtract) {
+ // TODO(xiao.dong) handle extract lower and upper bounds
+ return NotImplemented("Extract lower bound not implemented.");
+ } else {
+ return NotFound("Lower bound not found.");
+ }
+ }
+
+ Result<Literal> UpperBound(const std::shared_ptr<BoundTerm>& term) {
+ if (term->kind() == BoundTerm::Kind::kReference) {
+ return ParseUpperBound(*std::dynamic_pointer_cast<BoundReference>(term));
+ } else if (term->kind() == BoundTerm::Kind::kTransform) {
+ return TransformUpperBound(*std::dynamic_pointer_cast<BoundTransform>(term));
+ } else if (term->kind() == BoundTerm::Kind::kExtract) {
+ // TODO(xiao.dong) handle extract lower and upper bounds
+ return NotImplemented("Extract upper bound not implemented.");
+ } else {
+ return NotFound("Upper bound not found.");
+ }
+ }
+
+ Result<Literal> ParseLowerBound(const BoundReference& ref) {
+ int id = ref.field().field_id();
+ auto type = ref.type();
+ if (!type->is_primitive()) {
+ return Invalid("Lower bound of non-primitive type is not supported.");
+ }
+ auto primitive_type = std::dynamic_pointer_cast<PrimitiveType>(type);
+ if (!data_file_.lower_bounds.empty() && data_file_.lower_bounds.contains(id)) {
+ ICEBERG_ASSIGN_OR_RAISE(
+ auto lower,
+ Literal::Deserialize(data_file_.lower_bounds.at(id), primitive_type));
+ return lower;
+ }
+
+ return NotFound("Lower bound not found.");
Review Comment:
The `Error` status in the `Result` usually means an error has been found and cannot proceed. We can either change the return type to `Result<std::optional<Literal>>` or return `Literal::Null()` for a missing value (which is a valid case).
##########
src/iceberg/expression/inclusive_metrics_evaluator.cc:
##########
@@ -0,0 +1,544 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "iceberg/expression/inclusive_metrics_evaluator.h"
+
+#include "iceberg/expression/binder.h"
+#include "iceberg/expression/expression_visitor.h"
+#include "iceberg/expression/rewrite_not.h"
+#include "iceberg/manifest_entry.h"
+#include "iceberg/schema.h"
+#include "iceberg/transform.h"
+#include "iceberg/util/macros.h"
+
+namespace iceberg {
+
+namespace {
+static const bool ROWS_MIGHT_MATCH = true;
+static const bool ROWS_CANNOT_MATCH = false;
+static const int32_t IN_PREDICATE_LIMIT = 200;
+} // namespace
+
+class InclusiveMetricsVisitor : public BoundVisitor<bool> {
+ public:
+ explicit InclusiveMetricsVisitor(const DataFile& data_file) : data_file_(data_file) {}
+
+ Result<bool> AlwaysTrue() override { return ROWS_MIGHT_MATCH; }
+
+ Result<bool> AlwaysFalse() override { return ROWS_CANNOT_MATCH; }
+
+ Result<bool> Not(bool child_result) override { return !child_result; }
+
+ Result<bool> And(bool left_result, bool right_result) override {
+ return left_result && right_result;
+ }
+
+ Result<bool> Or(bool left_result, bool right_result) override {
+ return left_result || right_result;
+ }
+
+ Result<bool> IsNull(const std::shared_ptr<BoundTerm>& term) override {
+ if (IsNonNullPreserving(term)) {
+ // number of non-nulls is the same as for the ref
+ int id = term->reference()->field().field_id();
+ if (!MayContainNull(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ }
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotNull(const std::shared_ptr<BoundTerm>& term) override {
+ // no need to check whether the field is required because binding evaluates that case
+ // if the column has no non-null values, the expression cannot match
+
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> IsNaN(const std::shared_ptr<BoundTerm>& term) override {
+ // when there's no nanCounts information, but we already know the column only contains
+ // null, it's guaranteed that there's no NaN value
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ auto ptr = std::dynamic_pointer_cast<BoundReference>(term);
+ if (ptr == nullptr) {
+ return ROWS_MIGHT_MATCH;
+ }
+ if (!data_file_.nan_value_counts.empty() &&
+ data_file_.nan_value_counts.contains(id) &&
+ data_file_.nan_value_counts.at(id) == 0) {
+ return ROWS_CANNOT_MATCH;
+ }
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotNaN(const std::shared_ptr<BoundTerm>& term) override {
+ auto ptr = std::dynamic_pointer_cast<BoundReference>(term);
+ if (ptr == nullptr) {
+ // identity transforms are already removed by this time
+ return ROWS_MIGHT_MATCH;
+ }
+
+ int id = term->reference()->field().field_id();
+
+ if (ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> Lt(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value() || lower_result.value().IsNaN()) {
+ // NaN indicates unreliable bounds. See the InclusiveMetricsEvaluator docs for more.
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+
+ // this also works for transforms that are order preserving:
+ // if a transform f is order preserving, a < b means that f(a) <= f(b).
+ // because lower <= a for all values of a in the file, f(lower) <= f(a).
+ // when f(lower) >= X then f(a) >= f(lower) >= X, so there is no a such that f(a) < X
+ // f(lower) >= X means rows cannot match
+ if (lower >= lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> LtEq(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value() || lower_result.value().IsNaN()) {
+ // NaN indicates unreliable bounds. See the InclusiveMetricsEvaluator docs for more.
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+
+ // this also works for transforms that are order preserving:
+ // if a transform f is order preserving, a < b means that f(a) <= f(b).
+ // because lower <= a for all values of a in the file, f(lower) <= f(a).
+ // when f(lower) > X then f(a) >= f(lower) > X, so there is no a such that f(a) <= X
+ // f(lower) > X means rows cannot match
+ if (lower > lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> Gt(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+
+ if (upper <= lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> GtEq(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ if (upper < lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> Eq(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto lower_result = LowerBound(term);
+ if (lower_result.has_value()) {
+ const auto& lower = lower_result.value();
+ if (!lower.IsNaN() && lower > lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ if (upper < lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotEq(const std::shared_ptr<BoundTerm>& term,
+ const Literal& lit) override {
+ // because the bounds are not necessarily a min or max value, this cannot be answered
+ // using them. notEq(col, X) with (X, Y) doesn't guarantee that X is a value in col.
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> In(const std::shared_ptr<BoundTerm>& term,
+ const BoundSetPredicate::LiteralSet& literal_set) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ if (literal_set.size() > IN_PREDICATE_LIMIT) {
+ // skip evaluating the predicate if the number of values is too big
+ return ROWS_MIGHT_MATCH;
+ }
+
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value() || lower_result.value().IsNaN()) {
+ // NaN indicates unreliable bounds. See the InclusiveMetricsEvaluator docs for more.
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+ std::vector<Literal> literals;
+ for (const auto& lit : literal_set) {
+ if (lower <= lit) {
+ literals.emplace_back(lit);
+ }
+ }
+ // if all values are less than lower bound, rows cannot match
+ if (literals.empty()) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ std::erase_if(literals, [&](const Literal& x) { return x > upper; });
+ // if remaining values are greater than upper bound, rows cannot match
+ if (literals.empty()) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotIn(const std::shared_ptr<BoundTerm>& term,
+ const BoundSetPredicate::LiteralSet& literal_set) override {
+ // because the bounds are not necessarily a min or max value, this cannot be answered
+ // using them. notIn(col, {X, ...}) with (X, Y) doesn't guarantee that X is a value in
+ // col.
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> StartsWith(const std::shared_ptr<BoundTerm>& term,
+ const Literal& lit) override {
+ if (term->kind() == BoundTerm::Kind::kTransform &&
+ std::dynamic_pointer_cast<BoundTransform>(term)->transform()->transform_type() !=
+ TransformType::kIdentity) {
+ // truncate must be rewritten in binding. the result is either always or never
+ // compatible
+ return ROWS_MIGHT_MATCH;
+ }
+
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ if (lit.type()->type_id() != TypeId::kString) {
+ return ROWS_CANNOT_MATCH;
+ }
+ std::string prefix = get<std::string>(lit.value());
+
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+ auto lower_str = get<std::string>(lower.value());
+ // truncate lower bound so that its length in bytes is not greater than the length of
+ // prefix
+ int length = std::min(prefix.size(), lower_str.size());
+ // if prefix of lower bound is greater than prefix, rows cannot match
+ if (lower_str.substr(0, length) > prefix) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ auto upper_str = get<std::string>(upper.value());
+ // truncate upper bound so that its length in bytes is not greater than the length of
+ // prefix
+ length = std::min(prefix.size(), upper_str.size());
+ // if prefix of upper bound is less than prefix, rows cannot match
+ if (upper_str.substr(0, length) < prefix) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotStartsWith(const std::shared_ptr<BoundTerm>& term,
+ const Literal& lit) override {
+ // the only transforms that produce strings are truncate and identity, which work with
+ // this
+ int id = term->reference()->field().field_id();
+ if (MayContainNull(id)) {
+ return ROWS_MIGHT_MATCH;
+ }
+
+ if (lit.type()->type_id() != TypeId::kString) {
+ return ROWS_CANNOT_MATCH;
+ }
+ std::string prefix = get<std::string>(lit.value());
+
+ // notStartsWith will match unless all values must start with the prefix. This happens
+ // when the lower and upper bounds both start with the prefix.
+ auto lower_result = LowerBound(term);
+ auto upper_result = UpperBound(term);
+ if (!lower_result.has_value() || !upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+ const auto& upper = upper_result.value();
+ auto lower_str = get<std::string>(lower.value());
+ auto upper_str = get<std::string>(upper.value());
+
+ // if lower is shorter than the prefix then lower doesn't start with the prefix
+ if (lower_str.size() < prefix.size()) {
+ return ROWS_MIGHT_MATCH;
+ }
+
+ if (lower_str.starts_with(prefix)) {
+ // if upper is shorter than the prefix then upper can't start with the prefix
+ if (upper_str.size() < prefix.size()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ if (upper_str.starts_with(prefix)) {
+ // both bounds match the prefix, so all rows must match the prefix and therefore
+ // do not satisfy the predicate
+ return ROWS_CANNOT_MATCH;
+ }
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ private:
+ bool MayContainNull(int32_t id) {
+ return data_file_.null_value_counts.empty() ||
+ !data_file_.null_value_counts.contains(id) ||
+ data_file_.null_value_counts.at(id) != 0;
+ }
+
+ bool ContainsNullsOnly(int32_t id) {
+ return !data_file_.value_counts.empty() && data_file_.value_counts.contains(id) &&
+ !data_file_.null_value_counts.empty() &&
+ data_file_.null_value_counts.contains(id) &&
+ data_file_.value_counts.at(id) - data_file_.null_value_counts.at(id) == 0;
+ }
+
+ bool ContainsNaNsOnly(int32_t id) {
+ return !data_file_.nan_value_counts.empty() &&
+ data_file_.nan_value_counts.contains(id) && !data_file_.value_counts.empty() &&
+ data_file_.value_counts.at(id) == data_file_.nan_value_counts.at(id);
+ }
+
+ Result<Literal> LowerBound(const std::shared_ptr<BoundTerm>& term) {
+ if (term->kind() == BoundTerm::Kind::kReference) {
+ return ParseLowerBound(*std::dynamic_pointer_cast<BoundReference>(term));
+ } else if (term->kind() == BoundTerm::Kind::kTransform) {
+ return TransformLowerBound(*std::dynamic_pointer_cast<BoundTransform>(term));
+ } else if (term->kind() == BoundTerm::Kind::kExtract) {
+ // TODO(xiao.dong) handle extract lower and upper bounds
+ return NotImplemented("Extract lower bound not implemented.");
+ } else {
+ return NotFound("Lower bound not found.");
+ }
+ }
+
+ Result<Literal> UpperBound(const std::shared_ptr<BoundTerm>& term) {
+ if (term->kind() == BoundTerm::Kind::kReference) {
+ return ParseUpperBound(*std::dynamic_pointer_cast<BoundReference>(term));
+ } else if (term->kind() == BoundTerm::Kind::kTransform) {
+ return TransformUpperBound(*std::dynamic_pointer_cast<BoundTransform>(term));
+ } else if (term->kind() == BoundTerm::Kind::kExtract) {
+ // TODO(xiao.dong) handle extract lower and upper bounds
+ return NotImplemented("Extract upper bound not implemented.");
+ } else {
+ return NotFound("Upper bound not found.");
+ }
+ }
+
+ Result<Literal> ParseLowerBound(const BoundReference& ref) {
+ int id = ref.field().field_id();
+ auto type = ref.type();
+ if (!type->is_primitive()) {
+ return Invalid("Lower bound of non-primitive type is not supported.");
+ }
+ auto primitive_type = std::dynamic_pointer_cast<PrimitiveType>(type);
+ if (!data_file_.lower_bounds.empty() && data_file_.lower_bounds.contains(id)) {
+ ICEBERG_ASSIGN_OR_RAISE(
+ auto lower,
+ Literal::Deserialize(data_file_.lower_bounds.at(id), primitive_type));
+ return lower;
+ }
+
+ return NotFound("Lower bound not found.");
+ }
+
+ Result<Literal> ParseUpperBound(const BoundReference& ref) {
+ int id = ref.field().field_id();
+ auto type = ref.type();
+ if (!type->is_primitive()) {
+ return Invalid("Upper bound of non-primitive type is not supported.");
+ }
+ auto primitive_type = std::dynamic_pointer_cast<PrimitiveType>(type);
+ if (!data_file_.upper_bounds.empty() && data_file_.upper_bounds.contains(id)) {
+ ICEBERG_ASSIGN_OR_RAISE(
+ auto upper,
+ Literal::Deserialize(data_file_.upper_bounds.at(id), primitive_type));
+ return upper;
+ }
+
+ return NotFound("Upper bound not found.");
+ }
+
+ Result<Literal> TransformLowerBound(BoundTransform& boundTransform) {
+ auto transform = boundTransform.transform();
+ if (transform->PreservesOrder()) {
+ ICEBERG_ASSIGN_OR_RAISE(auto lower, ParseLowerBound(*boundTransform.reference()));
+ ICEBERG_ASSIGN_OR_RAISE(auto transform_func,
+ transform->Bind(boundTransform.reference()->type()));
+ return transform_func->Transform(lower);
+ }
+
+ return NotFound("Transform lower bound not found.");
Review Comment:
Same here
##########
src/iceberg/expression/inclusive_metrics_evaluator.cc:
##########
@@ -0,0 +1,544 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "iceberg/expression/inclusive_metrics_evaluator.h"
+
+#include "iceberg/expression/binder.h"
+#include "iceberg/expression/expression_visitor.h"
+#include "iceberg/expression/rewrite_not.h"
+#include "iceberg/manifest_entry.h"
+#include "iceberg/schema.h"
+#include "iceberg/transform.h"
+#include "iceberg/util/macros.h"
+
+namespace iceberg {
+
+namespace {
+static const bool ROWS_MIGHT_MATCH = true;
+static const bool ROWS_CANNOT_MATCH = false;
+static const int32_t IN_PREDICATE_LIMIT = 200;
+} // namespace
+
+class InclusiveMetricsVisitor : public BoundVisitor<bool> {
+ public:
+ explicit InclusiveMetricsVisitor(const DataFile& data_file) : data_file_(data_file) {}
+
+ Result<bool> AlwaysTrue() override { return ROWS_MIGHT_MATCH; }
+
+ Result<bool> AlwaysFalse() override { return ROWS_CANNOT_MATCH; }
+
+ Result<bool> Not(bool child_result) override { return !child_result; }
+
+ Result<bool> And(bool left_result, bool right_result) override {
+ return left_result && right_result;
+ }
+
+ Result<bool> Or(bool left_result, bool right_result) override {
+ return left_result || right_result;
+ }
+
+ Result<bool> IsNull(const std::shared_ptr<BoundTerm>& term) override {
+ if (IsNonNullPreserving(term)) {
+ // number of non-nulls is the same as for the ref
+ int id = term->reference()->field().field_id();
+ if (!MayContainNull(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ }
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotNull(const std::shared_ptr<BoundTerm>& term) override {
+ // no need to check whether the field is required because binding evaluates that case
+ // if the column has no non-null values, the expression cannot match
+
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> IsNaN(const std::shared_ptr<BoundTerm>& term) override {
+ // when there's no nanCounts information, but we already know the column only contains
+ // null, it's guaranteed that there's no NaN value
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ auto ptr = std::dynamic_pointer_cast<BoundReference>(term);
+ if (ptr == nullptr) {
+ return ROWS_MIGHT_MATCH;
+ }
+ if (!data_file_.nan_value_counts.empty() &&
+ data_file_.nan_value_counts.contains(id) &&
+ data_file_.nan_value_counts.at(id) == 0) {
+ return ROWS_CANNOT_MATCH;
+ }
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotNaN(const std::shared_ptr<BoundTerm>& term) override {
+ auto ptr = std::dynamic_pointer_cast<BoundReference>(term);
+ if (ptr == nullptr) {
+ // identity transforms are already removed by this time
+ return ROWS_MIGHT_MATCH;
+ }
+
+ int id = term->reference()->field().field_id();
+
+ if (ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> Lt(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value() || lower_result.value().IsNaN()) {
+ // NaN indicates unreliable bounds. See the InclusiveMetricsEvaluator docs for more.
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+
+ // this also works for transforms that are order preserving:
+ // if a transform f is order preserving, a < b means that f(a) <= f(b).
+ // because lower <= a for all values of a in the file, f(lower) <= f(a).
+ // when f(lower) >= X then f(a) >= f(lower) >= X, so there is no a such that f(a) < X
+ // f(lower) >= X means rows cannot match
+ if (lower >= lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> LtEq(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value() || lower_result.value().IsNaN()) {
+ // NaN indicates unreliable bounds. See the InclusiveMetricsEvaluator docs for more.
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+
+ // this also works for transforms that are order preserving:
+ // if a transform f is order preserving, a < b means that f(a) <= f(b).
+ // because lower <= a for all values of a in the file, f(lower) <= f(a).
+ // when f(lower) > X then f(a) >= f(lower) > X, so there is no a such that f(a) <= X
+ // f(lower) > X means rows cannot match
+ if (lower > lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> Gt(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+
+ if (upper <= lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> GtEq(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ if (upper < lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> Eq(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto lower_result = LowerBound(term);
+ if (lower_result.has_value()) {
+ const auto& lower = lower_result.value();
+ if (!lower.IsNaN() && lower > lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ if (upper < lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotEq(const std::shared_ptr<BoundTerm>& term,
+ const Literal& lit) override {
+ // because the bounds are not necessarily a min or max value, this cannot be answered
+ // using them. notEq(col, X) with (X, Y) doesn't guarantee that X is a value in col.
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> In(const std::shared_ptr<BoundTerm>& term,
+ const BoundSetPredicate::LiteralSet& literal_set) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ if (literal_set.size() > IN_PREDICATE_LIMIT) {
+ // skip evaluating the predicate if the number of values is too big
+ return ROWS_MIGHT_MATCH;
+ }
+
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value() || lower_result.value().IsNaN()) {
+ // NaN indicates unreliable bounds. See the InclusiveMetricsEvaluator docs for more.
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+ std::vector<Literal> literals;
+ for (const auto& lit : literal_set) {
+ if (lower <= lit) {
+ literals.emplace_back(lit);
+ }
+ }
+ // if all values are less than lower bound, rows cannot match
+ if (literals.empty()) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ std::erase_if(literals, [&](const Literal& x) { return x > upper; });
+ // if remaining values are greater than upper bound, rows cannot match
+ if (literals.empty()) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotIn(const std::shared_ptr<BoundTerm>& term,
+ const BoundSetPredicate::LiteralSet& literal_set) override {
+ // because the bounds are not necessarily a min or max value, this cannot be answered
+ // using them. notIn(col, {X, ...}) with (X, Y) doesn't guarantee that X is a value in
+ // col.
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> StartsWith(const std::shared_ptr<BoundTerm>& term,
+ const Literal& lit) override {
+ if (term->kind() == BoundTerm::Kind::kTransform &&
+ std::dynamic_pointer_cast<BoundTransform>(term)->transform()->transform_type() !=
+ TransformType::kIdentity) {
+ // truncate must be rewritten in binding. the result is either always or never
+ // compatible
+ return ROWS_MIGHT_MATCH;
+ }
+
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ if (lit.type()->type_id() != TypeId::kString) {
+ return ROWS_CANNOT_MATCH;
+ }
+ std::string prefix = get<std::string>(lit.value());
+
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+ auto lower_str = get<std::string>(lower.value());
+ // truncate lower bound so that its length in bytes is not greater than the length of
+ // prefix
+ int length = std::min(prefix.size(), lower_str.size());
+ // if prefix of lower bound is greater than prefix, rows cannot match
+ if (lower_str.substr(0, length) > prefix) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ auto upper_str = get<std::string>(upper.value());
+ // truncate upper bound so that its length in bytes is not greater than the length of
+ // prefix
+ length = std::min(prefix.size(), upper_str.size());
+ // if prefix of upper bound is less than prefix, rows cannot match
+ if (upper_str.substr(0, length) < prefix) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotStartsWith(const std::shared_ptr<BoundTerm>& term,
+ const Literal& lit) override {
+ // the only transforms that produce strings are truncate and identity, which work with
+ // this
+ int id = term->reference()->field().field_id();
+ if (MayContainNull(id)) {
+ return ROWS_MIGHT_MATCH;
+ }
+
+ if (lit.type()->type_id() != TypeId::kString) {
+ return ROWS_CANNOT_MATCH;
+ }
+ std::string prefix = get<std::string>(lit.value());
+
+ // notStartsWith will match unless all values must start with the prefix. This happens
+ // when the lower and upper bounds both start with the prefix.
+ auto lower_result = LowerBound(term);
+ auto upper_result = UpperBound(term);
+ if (!lower_result.has_value() || !upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+ const auto& upper = upper_result.value();
+ auto lower_str = get<std::string>(lower.value());
+ auto upper_str = get<std::string>(upper.value());
+
+ // if lower is shorter than the prefix then lower doesn't start with the prefix
+ if (lower_str.size() < prefix.size()) {
+ return ROWS_MIGHT_MATCH;
+ }
+
+ if (lower_str.starts_with(prefix)) {
+ // if upper is shorter than the prefix then upper can't start with the prefix
+ if (upper_str.size() < prefix.size()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ if (upper_str.starts_with(prefix)) {
+ // both bounds match the prefix, so all rows must match the prefix and therefore
+ // do not satisfy the predicate
+ return ROWS_CANNOT_MATCH;
+ }
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ private:
+ bool MayContainNull(int32_t id) {
+ return data_file_.null_value_counts.empty() ||
+ !data_file_.null_value_counts.contains(id) ||
+ data_file_.null_value_counts.at(id) != 0;
+ }
+
+ bool ContainsNullsOnly(int32_t id) {
+ return !data_file_.value_counts.empty() && data_file_.value_counts.contains(id) &&
+ !data_file_.null_value_counts.empty() &&
+ data_file_.null_value_counts.contains(id) &&
+ data_file_.value_counts.at(id) - data_file_.null_value_counts.at(id) == 0;
+ }
+
+ bool ContainsNaNsOnly(int32_t id) {
+ return !data_file_.nan_value_counts.empty() &&
+ data_file_.nan_value_counts.contains(id) && !data_file_.value_counts.empty() &&
+ data_file_.value_counts.at(id) == data_file_.nan_value_counts.at(id);
+ }
+
+ Result<Literal> LowerBound(const std::shared_ptr<BoundTerm>& term) {
+ if (term->kind() == BoundTerm::Kind::kReference) {
+ return ParseLowerBound(*std::dynamic_pointer_cast<BoundReference>(term));
+ } else if (term->kind() == BoundTerm::Kind::kTransform) {
+ return TransformLowerBound(*std::dynamic_pointer_cast<BoundTransform>(term));
+ } else if (term->kind() == BoundTerm::Kind::kExtract) {
+ // TODO(xiao.dong) handle extract lower and upper bounds
+ return NotImplemented("Extract lower bound not implemented.");
+ } else {
+ return NotFound("Lower bound not found.");
+ }
+ }
+
+ Result<Literal> UpperBound(const std::shared_ptr<BoundTerm>& term) {
+ if (term->kind() == BoundTerm::Kind::kReference) {
+ return ParseUpperBound(*std::dynamic_pointer_cast<BoundReference>(term));
+ } else if (term->kind() == BoundTerm::Kind::kTransform) {
+ return TransformUpperBound(*std::dynamic_pointer_cast<BoundTransform>(term));
+ } else if (term->kind() == BoundTerm::Kind::kExtract) {
+ // TODO(xiao.dong) handle extract lower and upper bounds
+ return NotImplemented("Extract upper bound not implemented.");
+ } else {
+ return NotFound("Upper bound not found.");
+ }
+ }
+
+ Result<Literal> ParseLowerBound(const BoundReference& ref) {
+ int id = ref.field().field_id();
+ auto type = ref.type();
+ if (!type->is_primitive()) {
+ return Invalid("Lower bound of non-primitive type is not supported.");
+ }
+ auto primitive_type = std::dynamic_pointer_cast<PrimitiveType>(type);
+ if (!data_file_.lower_bounds.empty() && data_file_.lower_bounds.contains(id)) {
+ ICEBERG_ASSIGN_OR_RAISE(
+ auto lower,
+ Literal::Deserialize(data_file_.lower_bounds.at(id), primitive_type));
+ return lower;
+ }
+
+ return NotFound("Lower bound not found.");
+ }
+
+ Result<Literal> ParseUpperBound(const BoundReference& ref) {
+ int id = ref.field().field_id();
+ auto type = ref.type();
+ if (!type->is_primitive()) {
+ return Invalid("Upper bound of non-primitive type is not supported.");
+ }
+ auto primitive_type = std::dynamic_pointer_cast<PrimitiveType>(type);
+ if (!data_file_.upper_bounds.empty() && data_file_.upper_bounds.contains(id)) {
+ ICEBERG_ASSIGN_OR_RAISE(
+ auto upper,
+ Literal::Deserialize(data_file_.upper_bounds.at(id), primitive_type));
+ return upper;
+ }
+
+ return NotFound("Upper bound not found.");
+ }
+
+ Result<Literal> TransformLowerBound(BoundTransform& boundTransform) {
+ auto transform = boundTransform.transform();
+ if (transform->PreservesOrder()) {
+ ICEBERG_ASSIGN_OR_RAISE(auto lower, ParseLowerBound(*boundTransform.reference()));
+ ICEBERG_ASSIGN_OR_RAISE(auto transform_func,
+ transform->Bind(boundTransform.reference()->type()));
+ return transform_func->Transform(lower);
+ }
+
+ return NotFound("Transform lower bound not found.");
+ }
+
+ Result<Literal> TransformUpperBound(BoundTransform& boundTransform) {
+ auto transform = boundTransform.transform();
+ if (transform->PreservesOrder()) {
+ ICEBERG_ASSIGN_OR_RAISE(auto upper, ParseLowerBound(*boundTransform.reference()));
+ ICEBERG_ASSIGN_OR_RAISE(auto transform_func,
+ transform->Bind(boundTransform.reference()->type()));
+ return transform_func->Transform(upper);
+ }
+
+ return NotFound("Transform upper bound not found.");
+ }
+
+ // TODO(xiao.dong) handle extract lower and upper bounds
Review Comment:
Remove these for now?
##########
src/iceberg/expression/inclusive_metrics_evaluator.cc:
##########
@@ -0,0 +1,544 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "iceberg/expression/inclusive_metrics_evaluator.h"
+
+#include "iceberg/expression/binder.h"
+#include "iceberg/expression/expression_visitor.h"
+#include "iceberg/expression/rewrite_not.h"
+#include "iceberg/manifest_entry.h"
+#include "iceberg/schema.h"
+#include "iceberg/transform.h"
+#include "iceberg/util/macros.h"
+
+namespace iceberg {
+
+namespace {
+static const bool ROWS_MIGHT_MATCH = true;
+static const bool ROWS_CANNOT_MATCH = false;
+static const int32_t IN_PREDICATE_LIMIT = 200;
+} // namespace
+
+class InclusiveMetricsVisitor : public BoundVisitor<bool> {
+ public:
+ explicit InclusiveMetricsVisitor(const DataFile& data_file) : data_file_(data_file) {}
+
+ Result<bool> AlwaysTrue() override { return ROWS_MIGHT_MATCH; }
+
+ Result<bool> AlwaysFalse() override { return ROWS_CANNOT_MATCH; }
+
+ Result<bool> Not(bool child_result) override { return !child_result; }
+
+ Result<bool> And(bool left_result, bool right_result) override {
+ return left_result && right_result;
+ }
+
+ Result<bool> Or(bool left_result, bool right_result) override {
+ return left_result || right_result;
+ }
+
+ Result<bool> IsNull(const std::shared_ptr<BoundTerm>& term) override {
+ if (IsNonNullPreserving(term)) {
+ // number of non-nulls is the same as for the ref
+ int id = term->reference()->field().field_id();
+ if (!MayContainNull(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ }
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotNull(const std::shared_ptr<BoundTerm>& term) override {
+ // no need to check whether the field is required because binding evaluates that case
+ // if the column has no non-null values, the expression cannot match
+
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> IsNaN(const std::shared_ptr<BoundTerm>& term) override {
+ // when there's no nanCounts information, but we already know the column only contains
+ // null, it's guaranteed that there's no NaN value
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ auto ptr = std::dynamic_pointer_cast<BoundReference>(term);
+ if (ptr == nullptr) {
+ return ROWS_MIGHT_MATCH;
+ }
+ if (!data_file_.nan_value_counts.empty() &&
+ data_file_.nan_value_counts.contains(id) &&
+ data_file_.nan_value_counts.at(id) == 0) {
+ return ROWS_CANNOT_MATCH;
+ }
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotNaN(const std::shared_ptr<BoundTerm>& term) override {
+ auto ptr = std::dynamic_pointer_cast<BoundReference>(term);
+ if (ptr == nullptr) {
+ // identity transforms are already removed by this time
+ return ROWS_MIGHT_MATCH;
+ }
+
+ int id = term->reference()->field().field_id();
+
+ if (ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> Lt(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value() || lower_result.value().IsNaN()) {
+ // NaN indicates unreliable bounds. See the InclusiveMetricsEvaluator docs for more.
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+
+ // this also works for transforms that are order preserving:
+ // if a transform f is order preserving, a < b means that f(a) <= f(b).
+ // because lower <= a for all values of a in the file, f(lower) <= f(a).
+ // when f(lower) >= X then f(a) >= f(lower) >= X, so there is no a such that f(a) < X
+ // f(lower) >= X means rows cannot match
+ if (lower >= lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> LtEq(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value() || lower_result.value().IsNaN()) {
+ // NaN indicates unreliable bounds. See the InclusiveMetricsEvaluator docs for more.
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+
+ // this also works for transforms that are order preserving:
+ // if a transform f is order preserving, a < b means that f(a) <= f(b).
+ // because lower <= a for all values of a in the file, f(lower) <= f(a).
+ // when f(lower) > X then f(a) >= f(lower) > X, so there is no a such that f(a) <= X
+ // f(lower) > X means rows cannot match
+ if (lower > lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> Gt(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+
+ if (upper <= lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> GtEq(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ if (upper < lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> Eq(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto lower_result = LowerBound(term);
+ if (lower_result.has_value()) {
+ const auto& lower = lower_result.value();
+ if (!lower.IsNaN() && lower > lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ if (upper < lit) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotEq(const std::shared_ptr<BoundTerm>& term,
+ const Literal& lit) override {
+ // because the bounds are not necessarily a min or max value, this cannot be answered
+ // using them. notEq(col, X) with (X, Y) doesn't guarantee that X is a value in col.
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> In(const std::shared_ptr<BoundTerm>& term,
+ const BoundSetPredicate::LiteralSet& literal_set) override {
+ // all terms are null preserving. see #isNullPreserving(Bound)
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id) || ContainsNaNsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ if (literal_set.size() > IN_PREDICATE_LIMIT) {
+ // skip evaluating the predicate if the number of values is too big
+ return ROWS_MIGHT_MATCH;
+ }
+
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value() || lower_result.value().IsNaN()) {
+ // NaN indicates unreliable bounds. See the InclusiveMetricsEvaluator docs for more.
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+ std::vector<Literal> literals;
+ for (const auto& lit : literal_set) {
+ if (lower <= lit) {
+ literals.emplace_back(lit);
+ }
+ }
+ // if all values are less than lower bound, rows cannot match
+ if (literals.empty()) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ std::erase_if(literals, [&](const Literal& x) { return x > upper; });
+ // if remaining values are greater than upper bound, rows cannot match
+ if (literals.empty()) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotIn(const std::shared_ptr<BoundTerm>& term,
+ const BoundSetPredicate::LiteralSet& literal_set) override {
+ // because the bounds are not necessarily a min or max value, this cannot be answered
+ // using them. notIn(col, {X, ...}) with (X, Y) doesn't guarantee that X is a value in
+ // col.
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> StartsWith(const std::shared_ptr<BoundTerm>& term,
+ const Literal& lit) override {
+ if (term->kind() == BoundTerm::Kind::kTransform &&
+ std::dynamic_pointer_cast<BoundTransform>(term)->transform()->transform_type() !=
+ TransformType::kIdentity) {
+ // truncate must be rewritten in binding. the result is either always or never
+ // compatible
+ return ROWS_MIGHT_MATCH;
+ }
+
+ int id = term->reference()->field().field_id();
+ if (ContainsNullsOnly(id)) {
+ return ROWS_CANNOT_MATCH;
+ }
+ if (lit.type()->type_id() != TypeId::kString) {
+ return ROWS_CANNOT_MATCH;
+ }
+ std::string prefix = get<std::string>(lit.value());
+
+ auto lower_result = LowerBound(term);
+ if (!lower_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+ auto lower_str = get<std::string>(lower.value());
+ // truncate lower bound so that its length in bytes is not greater than the length of
+ // prefix
+ int length = std::min(prefix.size(), lower_str.size());
+ // if prefix of lower bound is greater than prefix, rows cannot match
+ if (lower_str.substr(0, length) > prefix) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ auto upper_result = UpperBound(term);
+ if (!upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& upper = upper_result.value();
+ auto upper_str = get<std::string>(upper.value());
+ // truncate upper bound so that its length in bytes is not greater than the length of
+ // prefix
+ length = std::min(prefix.size(), upper_str.size());
+ // if prefix of upper bound is less than prefix, rows cannot match
+ if (upper_str.substr(0, length) < prefix) {
+ return ROWS_CANNOT_MATCH;
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ Result<bool> NotStartsWith(const std::shared_ptr<BoundTerm>& term,
+ const Literal& lit) override {
+ // the only transforms that produce strings are truncate and identity, which work with
+ // this
+ int id = term->reference()->field().field_id();
+ if (MayContainNull(id)) {
+ return ROWS_MIGHT_MATCH;
+ }
+
+ if (lit.type()->type_id() != TypeId::kString) {
+ return ROWS_CANNOT_MATCH;
+ }
+ std::string prefix = get<std::string>(lit.value());
+
+ // notStartsWith will match unless all values must start with the prefix. This happens
+ // when the lower and upper bounds both start with the prefix.
+ auto lower_result = LowerBound(term);
+ auto upper_result = UpperBound(term);
+ if (!lower_result.has_value() || !upper_result.has_value()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ const auto& lower = lower_result.value();
+ const auto& upper = upper_result.value();
+ auto lower_str = get<std::string>(lower.value());
+ auto upper_str = get<std::string>(upper.value());
+
+ // if lower is shorter than the prefix then lower doesn't start with the prefix
+ if (lower_str.size() < prefix.size()) {
+ return ROWS_MIGHT_MATCH;
+ }
+
+ if (lower_str.starts_with(prefix)) {
+ // if upper is shorter than the prefix then upper can't start with the prefix
+ if (upper_str.size() < prefix.size()) {
+ return ROWS_MIGHT_MATCH;
+ }
+ if (upper_str.starts_with(prefix)) {
+ // both bounds match the prefix, so all rows must match the prefix and therefore
+ // do not satisfy the predicate
+ return ROWS_CANNOT_MATCH;
+ }
+ }
+
+ return ROWS_MIGHT_MATCH;
+ }
+
+ private:
+ bool MayContainNull(int32_t id) {
+ return data_file_.null_value_counts.empty() ||
+ !data_file_.null_value_counts.contains(id) ||
+ data_file_.null_value_counts.at(id) != 0;
+ }
+
+ bool ContainsNullsOnly(int32_t id) {
+ return !data_file_.value_counts.empty() && data_file_.value_counts.contains(id) &&
+ !data_file_.null_value_counts.empty() &&
+ data_file_.null_value_counts.contains(id) &&
+ data_file_.value_counts.at(id) - data_file_.null_value_counts.at(id) == 0;
+ }
+
+ bool ContainsNaNsOnly(int32_t id) {
+ return !data_file_.nan_value_counts.empty() &&
+ data_file_.nan_value_counts.contains(id) && !data_file_.value_counts.empty() &&
+ data_file_.value_counts.at(id) == data_file_.nan_value_counts.at(id);
+ }
+
+ Result<Literal> LowerBound(const std::shared_ptr<BoundTerm>& term) {
+ if (term->kind() == BoundTerm::Kind::kReference) {
+ return ParseLowerBound(*std::dynamic_pointer_cast<BoundReference>(term));
+ } else if (term->kind() == BoundTerm::Kind::kTransform) {
+ return TransformLowerBound(*std::dynamic_pointer_cast<BoundTransform>(term));
+ } else if (term->kind() == BoundTerm::Kind::kExtract) {
+ // TODO(xiao.dong) handle extract lower and upper bounds
+ return NotImplemented("Extract lower bound not implemented.");
+ } else {
+ return NotFound("Lower bound not found.");
+ }
+ }
+
+ Result<Literal> UpperBound(const std::shared_ptr<BoundTerm>& term) {
+ if (term->kind() == BoundTerm::Kind::kReference) {
+ return ParseUpperBound(*std::dynamic_pointer_cast<BoundReference>(term));
+ } else if (term->kind() == BoundTerm::Kind::kTransform) {
+ return TransformUpperBound(*std::dynamic_pointer_cast<BoundTransform>(term));
+ } else if (term->kind() == BoundTerm::Kind::kExtract) {
+ // TODO(xiao.dong) handle extract lower and upper bounds
+ return NotImplemented("Extract upper bound not implemented.");
+ } else {
+ return NotFound("Upper bound not found.");
+ }
+ }
+
+ Result<Literal> ParseLowerBound(const BoundReference& ref) {
+ int id = ref.field().field_id();
+ auto type = ref.type();
+ if (!type->is_primitive()) {
+ return Invalid("Lower bound of non-primitive type is not supported.");
+ }
+ auto primitive_type = std::dynamic_pointer_cast<PrimitiveType>(type);
+ if (!data_file_.lower_bounds.empty() && data_file_.lower_bounds.contains(id)) {
+ ICEBERG_ASSIGN_OR_RAISE(
+ auto lower,
+ Literal::Deserialize(data_file_.lower_bounds.at(id), primitive_type));
+ return lower;
+ }
+
+ return NotFound("Lower bound not found.");
+ }
+
+ Result<Literal> ParseUpperBound(const BoundReference& ref) {
+ int id = ref.field().field_id();
+ auto type = ref.type();
+ if (!type->is_primitive()) {
+ return Invalid("Upper bound of non-primitive type is not supported.");
+ }
+ auto primitive_type = std::dynamic_pointer_cast<PrimitiveType>(type);
+ if (!data_file_.upper_bounds.empty() && data_file_.upper_bounds.contains(id)) {
+ ICEBERG_ASSIGN_OR_RAISE(
+ auto upper,
+ Literal::Deserialize(data_file_.upper_bounds.at(id), primitive_type));
+ return upper;
+ }
+
+ return NotFound("Upper bound not found.");
+ }
+
+ Result<Literal> TransformLowerBound(BoundTransform& boundTransform) {
+ auto transform = boundTransform.transform();
+ if (transform->PreservesOrder()) {
+ ICEBERG_ASSIGN_OR_RAISE(auto lower, ParseLowerBound(*boundTransform.reference()));
+ ICEBERG_ASSIGN_OR_RAISE(auto transform_func,
+ transform->Bind(boundTransform.reference()->type()));
+ return transform_func->Transform(lower);
+ }
+
+ return NotFound("Transform lower bound not found.");
+ }
+
+ Result<Literal> TransformUpperBound(BoundTransform& boundTransform) {
+ auto transform = boundTransform.transform();
+ if (transform->PreservesOrder()) {
+ ICEBERG_ASSIGN_OR_RAISE(auto upper, ParseLowerBound(*boundTransform.reference()));
+ ICEBERG_ASSIGN_OR_RAISE(auto transform_func,
+ transform->Bind(boundTransform.reference()->type()));
+ return transform_func->Transform(upper);
+ }
+
+ return NotFound("Transform upper bound not found.");
+ }
+
+ // TODO(xiao.dong) handle extract lower and upper bounds
+ /*
+ Literal extractLowerBound(const BoundExtract& bound) {
+
+ }
+
+ Literal extractUpperBound(const BoundExtract& bound) {
+
+ }
+*/
+
+ /** Returns true if the expression term produces a non-null value for non-null input. */
+ bool IsNonNullPreserving(const std::shared_ptr<BoundTerm>& term) {
+ if (term->kind() == BoundTerm::Kind::kReference) {
+ return true;
+ } else if (term->kind() == BoundTerm::Kind::kTransform) {
+ return std::dynamic_pointer_cast<BoundTransform>(term)
Review Comment:
ditto
##########
src/iceberg/CMakeLists.txt:
##########
@@ -28,6 +28,7 @@ set(ICEBERG_SOURCES
expression/literal.cc
expression/predicate.cc
expression/rewrite_not.cc
+ expression/inclusive_metrics_evaluator.cc
Review Comment:
sort alphabetically.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: add inclusive metrics evaluator [iceberg-cpp]
Posted by "dongxiao1198 (via GitHub)" <gi...@apache.org>.
dongxiao1198 commented on PR #357:
URL: https://github.com/apache/iceberg-cpp/pull/357#issuecomment-3594195444
> Modify meson files too?
done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: add inclusive metrics evaluator [iceberg-cpp]
Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac merged PR #357:
URL: https://github.com/apache/iceberg-cpp/pull/357
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: add inclusive metrics evaluator [iceberg-cpp]
Posted by "dongxiao1198 (via GitHub)" <gi...@apache.org>.
dongxiao1198 commented on code in PR #357:
URL: https://github.com/apache/iceberg-cpp/pull/357#discussion_r2576778354
##########
src/iceberg/test/inclusive_metrics_evaluator_test.cc:
##########
@@ -0,0 +1,385 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "iceberg/expression/inclusive_metrics_evaluator.h"
+
+#include <gtest/gtest.h>
+
+#include "iceberg/expression/binder.h"
+#include "iceberg/expression/expressions.h"
+#include "iceberg/manifest_entry.h"
+#include "iceberg/schema.h"
+#include "iceberg/test/matchers.h"
+#include "iceberg/type.h"
+
+namespace iceberg {
+
+namespace {
+static const bool ROWS_MIGHT_MATCH = true;
+static const bool ROWS_CANNOT_MATCH = false;
+} // namespace
+using TestVariant = std::variant<bool, int32_t, int64_t, double, std::string>;
+
+class InclusiveMetricsEvaluatorTest : public ::testing::Test {
Review Comment:
I migrated all java case into this ut, but the cases I write before were kept anyway, the ut file is a little bit large, but the coverage should be better than java cases.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org