YinZheng-Sun commented on code in PR #956: URL: https://github.com/apache/iceberg-cpp/pull/956#discussion_r4241947973
########## src/iceberg/parquet/parquet_metrics_row_group_filter.cc: ########## @@ -0,0 +1,327 @@ +/* + * 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 <algorithm> +#include <compare> +#include <functional> +#include <optional> +#include <unordered_map> + +#include <parquet/statistics.h> + +#include "iceberg/expression/expression_visitor.h" +#include "iceberg/expression/rewrite_not.h" +#include "iceberg/metadata_columns.h" +#include "iceberg/parquet/parquet_metrics_internal.h" +#include "iceberg/parquet/parquet_metrics_row_group_filter_internal.h" +#include "iceberg/parquet/parquet_schema_util_internal.h" +#include "iceberg/type.h" +#include "iceberg/util/macros.h" + +namespace iceberg::parquet { + +namespace { + +constexpr size_t kInPredicateLimit = 200; +// True means a matching row may exist; false means the group can be skipped. +constexpr bool kRowsMightMatch = true; +constexpr bool kRowsCannotMatch = false; + +class MetricsVisitor : public BoundVisitor<bool> { + public: + MetricsVisitor(const ::parquet::arrow::SchemaManifest& manifest, + const ::parquet::RowGroupMetaData& row_group, + const std::unordered_map<int32_t, int>& column_indices) + : manifest_(manifest), row_group_(row_group), column_indices_(column_indices) {} + + Result<bool> AlwaysTrue() override { return kRowsMightMatch; } + + Result<bool> AlwaysFalse() override { return kRowsCannotMatch; } + + Result<bool> Not(bool) override { return kRowsMightMatch; } + + Result<bool> And(bool left, bool right) override { return left && right; } + + Result<bool> Or(bool left, bool right) override { return left || right; } + + Result<bool> IsNull(const std::shared_ptr<Bound>& expr) override { + return MayContainNull(std::dynamic_pointer_cast<BoundReference>(expr)); + } + + Result<bool> NotNull(const std::shared_ptr<Bound>& expr) override { + if (ContainsNullsOnly(std::dynamic_pointer_cast<BoundReference>(expr))) { + return kRowsCannotMatch; + } + return kRowsMightMatch; + } + + Result<bool> IsNaN(const std::shared_ptr<Bound>& expr) override { + if (ContainsNullsOnly(std::dynamic_pointer_cast<BoundReference>(expr))) { + return kRowsCannotMatch; + } + return kRowsMightMatch; + } + + Result<bool> NotNaN(const std::shared_ptr<Bound>&) override { return kRowsMightMatch; } + + Result<bool> Lt(const std::shared_ptr<Bound>& expr, const Literal& value) override { + return VisitInequality(std::dynamic_pointer_cast<BoundReference>(expr), value, + std::less<Literal>{}, /*use_lower_bound=*/true); + } + + Result<bool> LtEq(const std::shared_ptr<Bound>& expr, const Literal& value) override { + return VisitInequality(std::dynamic_pointer_cast<BoundReference>(expr), value, + std::less_equal<Literal>{}, /*use_lower_bound=*/true); + } + + Result<bool> Gt(const std::shared_ptr<Bound>& expr, const Literal& value) override { + return VisitInequality(std::dynamic_pointer_cast<BoundReference>(expr), value, + std::greater<Literal>{}, /*use_lower_bound=*/false); + } + + Result<bool> GtEq(const std::shared_ptr<Bound>& expr, const Literal& value) override { + return VisitInequality(std::dynamic_pointer_cast<BoundReference>(expr), value, + std::greater_equal<Literal>{}, /*use_lower_bound=*/false); + } + + Result<bool> Eq(const std::shared_ptr<Bound>& expr, const Literal& value) override { + const auto ref = std::dynamic_pointer_cast<BoundReference>(expr); + if (ContainsNullsOnly(ref)) { + return kRowsCannotMatch; + } + const auto lower = MinValue(ref); + if (!lower) { + return kRowsMightMatch; + } + if (*lower > value) { + return kRowsCannotMatch; + } + const auto upper = MaxValue(ref); + if (!upper) { + return kRowsMightMatch; + } + return !(*upper < value); + } + + Result<bool> NotEq(const std::shared_ptr<Bound>&, const Literal&) override { + // Like Java, keep negative membership predicates inclusive. + return kRowsMightMatch; + } + + Result<bool> In(const std::shared_ptr<Bound>& expr, + const BoundSetPredicate::LiteralSet& values) override { + const auto ref = std::dynamic_pointer_cast<BoundReference>(expr); + if (ContainsNullsOnly(ref)) { + return kRowsCannotMatch; + } + if (values.size() > kInPredicateLimit) { + return kRowsMightMatch; + } + const auto lower = MinValue(ref); + if (!lower) { + return kRowsMightMatch; + } + if (std::ranges::all_of(values, [&](const auto& value) { return value < *lower; })) { + return kRowsCannotMatch; + } + const auto upper = MaxValue(ref); + if (!upper) { + return kRowsMightMatch; + } + // Like Java, a single candidate must satisfy both bounds. + for (const auto& value : values) { + if (!(value < *lower || value > *upper)) { + return kRowsMightMatch; + } + } + return kRowsCannotMatch; + } + + Result<bool> NotIn(const std::shared_ptr<Bound>&, + const BoundSetPredicate::LiteralSet&) override { + return kRowsMightMatch; + } + + Result<bool> StartsWith(const std::shared_ptr<Bound>& expr, + const Literal& value) override { + const auto ref = std::dynamic_pointer_cast<BoundReference>(expr); + if (ContainsNullsOnly(ref)) { + return kRowsCannotMatch; + } + const auto lower = MinValue(ref); + if (!lower || lower->type()->type_id() != TypeId::kString) { + return kRowsMightMatch; + } + const auto& prefix = std::get<std::string>(value.value()); + if (std::get<std::string>(lower->value()).compare(0, prefix.size(), prefix) > 0) { Review Comment: std::string::compare uses std::char_traits<char>, which the standard specifies compares as unsigned char, so this is an unsigned byte-wise comparison and matches UTF-8 code point order. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
