advancedxy commented on code in PR #956:
URL: https://github.com/apache/iceberg-cpp/pull/956#discussion_r4236715425
##########
src/iceberg/data/file_scan_task_reader.h:
##########
@@ -55,6 +55,8 @@ class ICEBERG_DATA_EXPORT FileScanTaskReader {
/// The output schema for the returned ArrowArrayStream. Must be a
/// projection of table_schema.
std::shared_ptr<Schema> projected_schema;
+ /// Case sensitivity for filter binding. Pass
TableScan::is_case_sensitive().
+ bool filter_case_sensitive = true;
Review Comment:
hmm, this should be called as `case_sensitive`?
Generally speaking, the case sensitivity applies to schema pruning and
predicate pushdown and maybe other things, I don't it's a good idea to bound
this to filter only. iceberg-java/rust both call it `case_sensitive`. Other
places should be modified accordingly/
##########
src/iceberg/expression/binder.h:
##########
@@ -61,19 +62,25 @@ class ICEBERG_EXPORT Binder : public
ExpressionVisitor<std::shared_ptr<Expressio
const bool case_sensitive_;
};
-class ICEBERG_EXPORT IsBoundVisitor : public ExpressionVisitor<bool> {
+class ICEBERG_EXPORT IsBoundVisitor : public
ExpressionVisitor<std::optional<bool>> {
Review Comment:
why this change?
I think whether an expression is bounded or not should be deterministic
##########
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) {
+ return kRowsCannotMatch;
+ }
+ const auto upper = MaxValue(ref);
+ if (!upper || upper->type()->type_id() != TypeId::kString) {
+ return kRowsMightMatch;
+ }
+ return std::get<std::string>(upper->value()).compare(0, prefix.size(),
prefix) >= 0;
+ }
+
+ Result<bool> NotStartsWith(const std::shared_ptr<Bound>& expr,
+ const Literal& value) override {
+ const auto ref = std::dynamic_pointer_cast<BoundReference>(expr);
+ if (MayContainNull(ref)) {
+ return kRowsMightMatch;
+ }
+ 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()).starts_with(prefix)) {
+ return kRowsMightMatch;
+ }
+ const auto upper = MaxValue(ref);
+ if (!upper || upper->type()->type_id() != TypeId::kString) {
+ return kRowsMightMatch;
+ }
+ return !std::get<std::string>(upper->value()).starts_with(prefix);
+ }
+
+ private:
+ bool ContainsNullsOnly(const std::shared_ptr<BoundReference>& ref) const {
+ const auto stats = GetStatistics(ref);
+ // GetStatistics excludes repeated columns, so each row contributes one
value.
+ return stats && stats->HasNullCount() && stats->null_count() ==
row_group_.num_rows();
+ }
+
+ bool MayContainNull(const std::shared_ptr<BoundReference>& ref) const {
+ const auto stats = GetStatistics(ref);
+ return !stats || !stats->HasNullCount() || stats->null_count() != 0;
+ }
+
+ std::shared_ptr<::parquet::Statistics> GetStatistics(
+ const std::shared_ptr<BoundReference>& ref) const {
+ if (!ref || !ref->type()->is_primitive() ||
+ MetadataColumns::IsMetadataColumn(ref->field_id()) ||
+ MetadataColumns::IsRowLineageColumn(ref->field_id())) {
+ return nullptr;
+ }
Review Comment:
this seems unnecessary? if the ref column is nested, or meta column, then it
should not be presented in the field_id_column_idx_ mapping.
##########
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:
1. I'm not sure we can use std::string to compare string columns directly.
Per iceberg's spec String column must be encoded as UTF-8 bytes. we may compare
unsigned utf-8 bytes directly. cc @wgtmac if you have more insights.
##########
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) {
+ return kRowsCannotMatch;
+ }
+ const auto upper = MaxValue(ref);
+ if (!upper || upper->type()->type_id() != TypeId::kString) {
+ return kRowsMightMatch;
+ }
+ return std::get<std::string>(upper->value()).compare(0, prefix.size(),
prefix) >= 0;
+ }
+
+ Result<bool> NotStartsWith(const std::shared_ptr<Bound>& expr,
+ const Literal& value) override {
+ const auto ref = std::dynamic_pointer_cast<BoundReference>(expr);
+ if (MayContainNull(ref)) {
+ return kRowsMightMatch;
+ }
+ 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()).starts_with(prefix)) {
+ return kRowsMightMatch;
+ }
+ const auto upper = MaxValue(ref);
+ if (!upper || upper->type()->type_id() != TypeId::kString) {
+ return kRowsMightMatch;
+ }
+ return !std::get<std::string>(upper->value()).starts_with(prefix);
+ }
+
+ private:
+ bool ContainsNullsOnly(const std::shared_ptr<BoundReference>& ref) const {
+ const auto stats = GetStatistics(ref);
+ // GetStatistics excludes repeated columns, so each row contributes one
value.
+ return stats && stats->HasNullCount() && stats->null_count() ==
row_group_.num_rows();
+ }
+
+ bool MayContainNull(const std::shared_ptr<BoundReference>& ref) const {
+ const auto stats = GetStatistics(ref);
+ return !stats || !stats->HasNullCount() || stats->null_count() != 0;
+ }
+
+ std::shared_ptr<::parquet::Statistics> GetStatistics(
+ const std::shared_ptr<BoundReference>& ref) const {
+ if (!ref || !ref->type()->is_primitive() ||
+ MetadataColumns::IsMetadataColumn(ref->field_id()) ||
+ MetadataColumns::IsRowLineageColumn(ref->field_id())) {
+ return nullptr;
+ }
+ auto column = column_indices_.find(ref->field_id());
+ // Missing columns can have initial defaults, so do not assume all nulls.
+ if (column == column_indices_.end()) {
+ return nullptr;
+ }
+ const auto& descriptor = *manifest_.descr->Column(column->second);
+ auto field = manifest_.column_index_to_field.find(column->second);
+ // Repeated-column statistics describe elements rather than rows.
+ if (descriptor.max_repetition_level() != 0 ||
Review Comment:
what does this mean? I didn't see any related logic in the iceberg-java side.
##########
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; }
Review Comment:
why is this?
it should be `Not(bool result) override { return !result }`?
If this is on purpose, then at least a comment should be added to justify.
##########
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) {
+ return kRowsCannotMatch;
+ }
+ const auto upper = MaxValue(ref);
+ if (!upper || upper->type()->type_id() != TypeId::kString) {
+ return kRowsMightMatch;
+ }
+ return std::get<std::string>(upper->value()).compare(0, prefix.size(),
prefix) >= 0;
+ }
+
+ Result<bool> NotStartsWith(const std::shared_ptr<Bound>& expr,
+ const Literal& value) override {
+ const auto ref = std::dynamic_pointer_cast<BoundReference>(expr);
+ if (MayContainNull(ref)) {
+ return kRowsMightMatch;
+ }
+ 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()).starts_with(prefix)) {
+ return kRowsMightMatch;
+ }
+ const auto upper = MaxValue(ref);
+ if (!upper || upper->type()->type_id() != TypeId::kString) {
+ return kRowsMightMatch;
+ }
+ return !std::get<std::string>(upper->value()).starts_with(prefix);
+ }
+
+ private:
+ bool ContainsNullsOnly(const std::shared_ptr<BoundReference>& ref) const {
+ const auto stats = GetStatistics(ref);
+ // GetStatistics excludes repeated columns, so each row contributes one
value.
+ return stats && stats->HasNullCount() && stats->null_count() ==
row_group_.num_rows();
+ }
+
+ bool MayContainNull(const std::shared_ptr<BoundReference>& ref) const {
+ const auto stats = GetStatistics(ref);
+ return !stats || !stats->HasNullCount() || stats->null_count() != 0;
+ }
+
+ std::shared_ptr<::parquet::Statistics> GetStatistics(
+ const std::shared_ptr<BoundReference>& ref) const {
+ if (!ref || !ref->type()->is_primitive() ||
+ MetadataColumns::IsMetadataColumn(ref->field_id()) ||
+ MetadataColumns::IsRowLineageColumn(ref->field_id())) {
+ return nullptr;
+ }
+ auto column = column_indices_.find(ref->field_id());
+ // Missing columns can have initial defaults, so do not assume all nulls.
+ if (column == column_indices_.end()) {
+ return nullptr;
+ }
+ const auto& descriptor = *manifest_.descr->Column(column->second);
+ auto field = manifest_.column_index_to_field.find(column->second);
+ // Repeated-column statistics describe elements rather than rows.
+ if (descriptor.max_repetition_level() != 0 ||
+ field == manifest_.column_index_to_field.end() ||
+ !ValidateParquetTypeCompatibility(*ref->type(), *field->second)) {
+ return nullptr;
+ }
+ return row_group_.ColumnChunk(column->second)->statistics();
+ }
+
+ std::optional<Literal> MinValue(const std::shared_ptr<BoundReference>& ref)
const {
+ return GetBound(ref, /*is_min=*/true);
+ }
+
+ std::optional<Literal> MaxValue(const std::shared_ptr<BoundReference>& ref)
const {
+ return GetBound(ref, /*is_min=*/false);
+ }
+
+ std::optional<Literal> GetBound(const std::shared_ptr<BoundReference>& ref,
+ bool is_min) const {
+ const auto stats = GetStatistics(ref);
+ if (!stats || !stats->HasMinMax()) {
+ return std::nullopt;
+ }
+ const auto& type = static_cast<const PrimitiveType&>(*ref->type());
+ // Parquet floating-point bounds can exclude NaNs.
+ // Treat both bounds as undefined, preserving null-count pruning.
+ if (type.type_id() == TypeId::kFloat || type.type_id() == TypeId::kDouble)
{
+ return std::nullopt;
+ }
+ auto result =
+ ParquetMetrics::StatsValueToLiteral(*stats->descr(), type, *stats,
is_min);
+ if (!result || result->IsNaN()) {
+ return std::nullopt;
+ }
+ return std::move(*result);
+ }
+
+ template <typename Comparator>
+ bool VisitInequality(const std::shared_ptr<BoundReference>& ref, const
Literal& value,
Review Comment:
nit: `VisitInequality` is not a good name. There's no visit logic in the
method.
##########
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;
+ }
Review Comment:
hmmm. this is strange. I think you are mixing validation and metrics
evaluation here. I'm not sure it is good way to add such check here.
##########
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) {
+ return kRowsCannotMatch;
+ }
+ const auto upper = MaxValue(ref);
+ if (!upper || upper->type()->type_id() != TypeId::kString) {
+ return kRowsMightMatch;
+ }
+ return std::get<std::string>(upper->value()).compare(0, prefix.size(),
prefix) >= 0;
+ }
+
+ Result<bool> NotStartsWith(const std::shared_ptr<Bound>& expr,
+ const Literal& value) override {
+ const auto ref = std::dynamic_pointer_cast<BoundReference>(expr);
+ if (MayContainNull(ref)) {
+ return kRowsMightMatch;
+ }
+ 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()).starts_with(prefix)) {
+ return kRowsMightMatch;
+ }
+ const auto upper = MaxValue(ref);
+ if (!upper || upper->type()->type_id() != TypeId::kString) {
+ return kRowsMightMatch;
+ }
+ return !std::get<std::string>(upper->value()).starts_with(prefix);
+ }
+
+ private:
+ bool ContainsNullsOnly(const std::shared_ptr<BoundReference>& ref) const {
+ const auto stats = GetStatistics(ref);
+ // GetStatistics excludes repeated columns, so each row contributes one
value.
+ return stats && stats->HasNullCount() && stats->null_count() ==
row_group_.num_rows();
+ }
+
+ bool MayContainNull(const std::shared_ptr<BoundReference>& ref) const {
+ const auto stats = GetStatistics(ref);
+ return !stats || !stats->HasNullCount() || stats->null_count() != 0;
+ }
+
+ std::shared_ptr<::parquet::Statistics> GetStatistics(
+ const std::shared_ptr<BoundReference>& ref) const {
+ if (!ref || !ref->type()->is_primitive() ||
+ MetadataColumns::IsMetadataColumn(ref->field_id()) ||
+ MetadataColumns::IsRowLineageColumn(ref->field_id())) {
+ return nullptr;
+ }
+ auto column = column_indices_.find(ref->field_id());
+ // Missing columns can have initial defaults, so do not assume all nulls.
+ if (column == column_indices_.end()) {
+ return nullptr;
+ }
+ const auto& descriptor = *manifest_.descr->Column(column->second);
+ auto field = manifest_.column_index_to_field.find(column->second);
+ // Repeated-column statistics describe elements rather than rows.
+ if (descriptor.max_repetition_level() != 0 ||
+ field == manifest_.column_index_to_field.end() ||
+ !ValidateParquetTypeCompatibility(*ref->type(), *field->second)) {
+ return nullptr;
+ }
+ return row_group_.ColumnChunk(column->second)->statistics();
+ }
+
+ std::optional<Literal> MinValue(const std::shared_ptr<BoundReference>& ref)
const {
+ return GetBound(ref, /*is_min=*/true);
+ }
+
+ std::optional<Literal> MaxValue(const std::shared_ptr<BoundReference>& ref)
const {
+ return GetBound(ref, /*is_min=*/false);
+ }
+
+ std::optional<Literal> GetBound(const std::shared_ptr<BoundReference>& ref,
+ bool is_min) const {
+ const auto stats = GetStatistics(ref);
+ if (!stats || !stats->HasMinMax()) {
+ return std::nullopt;
+ }
+ const auto& type = static_cast<const PrimitiveType&>(*ref->type());
+ // Parquet floating-point bounds can exclude NaNs.
+ // Treat both bounds as undefined, preserving null-count pruning.
+ if (type.type_id() == TypeId::kFloat || type.type_id() == TypeId::kDouble)
{
+ return std::nullopt;
+ }
+ auto result =
+ ParquetMetrics::StatsValueToLiteral(*stats->descr(), type, *stats,
is_min);
+ if (!result || result->IsNaN()) {
+ return std::nullopt;
+ }
+ return std::move(*result);
+ }
+
+ template <typename Comparator>
+ bool VisitInequality(const std::shared_ptr<BoundReference>& ref, const
Literal& value,
+ Comparator compare, bool lower_bound) const {
+ if (ContainsNullsOnly(ref)) {
+ return kRowsCannotMatch;
+ }
+ if (value.IsNaN()) {
+ return kRowsMightMatch;
+ }
+ const auto bound = lower_bound ? MinValue(ref) : MaxValue(ref);
+ if (!bound) {
+ return kRowsMightMatch;
+ }
+ // An unordered comparison cannot rule out matching rows.
+ if ((*bound <=> value) == std::partial_ordering::unordered) {
+ return kRowsMightMatch;
+ }
+ return compare(*bound, value);
+ }
+
+ const ::parquet::arrow::SchemaManifest& manifest_;
+ const ::parquet::RowGroupMetaData& row_group_;
+ const std::unordered_map<int32_t, int>& column_indices_;
Review Comment:
this should be called `field_id_to_column_idx_`, which could be more self
descriptive.
##########
src/iceberg/parquet/parquet_reader.cc:
##########
@@ -387,39 +406,58 @@ class ParquetReader::Impl {
}
private:
+ Status OpenNextRowGroupRange() {
+ const size_t begin = context_->next_row_group_;
+ size_t end = begin + 1;
+ std::vector<int> row_groups{context_->row_groups_[begin].index};
+ // Share a reader across adjacent row groups so Arrow can coalesce
pre-buffered
+ // reads. Stop at gaps to keep physical row positions contiguous within
batches.
+ while (end < context_->row_groups_.size() &&
+ context_->row_groups_[end].index == context_->row_groups_[end -
1].index + 1) {
+ row_groups.push_back(context_->row_groups_[end].index);
+ ++end;
+ }
Review Comment:
why this change?
Seems like unrelated change to the metrics filter?
##########
src/iceberg/parquet/parquet_reader.cc:
##########
@@ -387,39 +406,58 @@ class ParquetReader::Impl {
}
private:
+ Status OpenNextRowGroupRange() {
+ const size_t begin = context_->next_row_group_;
+ size_t end = begin + 1;
+ std::vector<int> row_groups{context_->row_groups_[begin].index};
+ // Share a reader across adjacent row groups so Arrow can coalesce
pre-buffered
+ // reads. Stop at gaps to keep physical row positions contiguous within
batches.
+ while (end < context_->row_groups_.size() &&
+ context_->row_groups_[end].index == context_->row_groups_[end -
1].index + 1) {
+ row_groups.push_back(context_->row_groups_[end].index);
+ ++end;
+ }
+ ICEBERG_ARROW_ASSIGN_OR_RETURN(
+ context_->record_batch_reader_,
+ reader_->GetRecordBatchReader(row_groups,
SelectedColumnIndices(projection_)));
+ context_->next_row_group_ = end;
+ metadata_context_.next_file_pos = context_->row_groups_[begin].first_row;
Review Comment:
hmmm. It looks like that how iceberg-cpp currently support _pos metadata
column is problematic. It tracks `next_file_pos` in MetadataColumnContext and
updates/incrs per batch. This might work for parquet reader without data
skipping.. Once row group filtering, and later page skipping is kicked in, it
would be hard to track that correctly. I think we should follow how parquet-rs:
https://github.com/apache/arrow-rs/pull/8715 or iceberg-java's
approach(org.apache.iceberg.parquet.ParquetValueReaders.PositionReader) to
support `_pos` metadata column. WDYT @wgtmac
--
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]