Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions src/iceberg/delete_file_index.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "iceberg/util/cache_internal.h"
#include "iceberg/util/checked_cast.h"
#include "iceberg/util/content_file_util.h"
#include "iceberg/util/conversions_internal.h"
#include "iceberg/util/executor_util_internal.h"
#include "iceberg/util/macros.h"

Expand Down Expand Up @@ -104,17 +105,19 @@ Status EqualityDeleteFile::ConvertBoundsIfNeeded() const {
// Convert lower bound
if (auto it = wrapped.data_file->lower_bounds.find(field_id);
it != wrapped.data_file->lower_bounds.cend() && !it->second.empty()) {
ICEBERG_ASSIGN_OR_RAISE(auto lower,
Literal::Deserialize(it->second, primitive_type));
lower_bounds.emplace(field_id, std::move(lower));
ICEBERG_ASSIGN_OR_RAISE(auto lower, DeserializeBound(it->second, primitive_type));
if (lower) {
lower_bounds.emplace(field_id, std::move(*lower));
}
}

// Convert upper bound
if (auto it = wrapped.data_file->upper_bounds.find(field_id);
it != wrapped.data_file->upper_bounds.cend() && !it->second.empty()) {
ICEBERG_ASSIGN_OR_RAISE(auto upper,
Literal::Deserialize(it->second, primitive_type));
upper_bounds.emplace(field_id, std::move(upper));
ICEBERG_ASSIGN_OR_RAISE(auto upper, DeserializeBound(it->second, primitive_type));
if (upper) {
upper_bounds.emplace(field_id, std::move(*upper));
}
}
}

Expand Down Expand Up @@ -183,18 +186,19 @@ Result<bool> CanContainEqDeletesForFile(const DataFile& data_file,

ICEBERG_ASSIGN_OR_RAISE(auto delete_lower, delete_file.LowerBound(field_id));
ICEBERG_ASSIGN_OR_RAISE(auto delete_upper, delete_file.UpperBound(field_id));
if (!delete_lower.has_value() || !delete_upper.has_value()) {
if (!delete_lower || !delete_upper) {
continue; // Missing bounds, assume may match
}

// Convert data bounds
auto primitive_type = checked_pointer_cast<PrimitiveType>(field.type());
ICEBERG_ASSIGN_OR_RAISE(auto data_lower,
Literal::Deserialize(data_lower_it->second, primitive_type));
DeserializeBound(data_lower_it->second, primitive_type));
ICEBERG_ASSIGN_OR_RAISE(auto data_upper,
Literal::Deserialize(data_upper_it->second, primitive_type));
DeserializeBound(data_upper_it->second, primitive_type));

if (!RangesOverlap(data_lower, data_upper, delete_lower->get(),
if (data_lower && data_upper &&
!RangesOverlap(*data_lower, *data_upper, delete_lower->get(),
delete_upper->get())) {
return false; // Ranges don't overlap - cannot match
}
Expand Down
112 changes: 69 additions & 43 deletions src/iceberg/expression/aggregate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@
#include "iceberg/expression/literal.h"
#include "iceberg/manifest/manifest_entry.h"
#include "iceberg/row/struct_like.h"
#include "iceberg/transform.h"
#include "iceberg/type.h"
#include "iceberg/util/checked_cast.h"
#include "iceberg/util/conversions_internal.h"
#include "iceberg/util/macros.h"

namespace iceberg {
Expand All @@ -56,17 +58,57 @@ class SingleValueStructLike : public StructLike {
Literal literal_;
};

Result<Literal> EvaluateBoundTerm(const BoundTerm& term,
const std::optional<std::vector<uint8_t>>& bound) {
auto ptype = GetPrimitiveType(term);
if (!bound.has_value()) {
SingleValueStructLike data(Literal::Null(ptype));
return term.Evaluate(data);
// A disengaged optional means the file metrics cannot determine the extremum.
Result<std::optional<Literal>> EvaluateFileExtremum(const BoundAggregate& aggregate,
const DataFile& file) {
const auto& term = *aggregate.term();
bool preserves_order = term.kind() == Term::Kind::kReference;
if (term.kind() == Term::Kind::kTransform) {
const auto& transform =
internal::checked_cast<const BoundTransform&>(term).transform();
if (transform->transform_type() == TransformType::kVoid) {
return Literal::Null(GetPrimitiveType(term));
}
preserves_order = transform->PreservesOrder();
}

const auto ref = term.reference();
const auto id = ref->field().field_id();
const auto& bounds = aggregate.op() == Expression::Operation::kMax ? file.upper_bounds
: file.lower_bounds;
// A source endpoint can determine the extremum only if the term preserves order.
if (auto it = bounds.find(id); preserves_order && it != bounds.end()) {
ICEBERG_ASSIGN_OR_RAISE(
auto bound, internal::DeserializeBound(it->second, GetPrimitiveType(*ref)));
if (bound) {
if (term.kind() == Term::Kind::kReference) {
return bound;
}
SingleValueStructLike data(std::move(*bound));
return term.Evaluate(data);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The later of the two timestamp examples in Appendix B maps to bucket 0, while the earlier one maps to bucket 1. Taking the bucket of the upper bound returns the wrong MAX. Require an order-preserving transform before deriving an extremum from one endpoint.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MIN/MAX now use file bounds only for column references and order-preserving transforms. I added a regression test using the two timestamps from Appendix B.

}
}
const auto values = file.value_counts.find(id);
const auto nulls = file.null_value_counts.find(id);
if (values != file.value_counts.end() && values->second > 0 &&
nulls != file.null_value_counts.end() && values->second == nulls->second) {
return Literal::Null(GetPrimitiveType(term));
}
return std::nullopt;
}

Result<Literal> FileExtremumOrNull(const BoundAggregate& aggregate,
const DataFile& file) {
ICEBERG_ASSIGN_OR_RAISE(auto value, EvaluateFileExtremum(aggregate, file));
if (!value) {
return Literal::Null(GetPrimitiveType(*aggregate.term()));
}
return std::move(*value);
}

ICEBERG_ASSIGN_OR_RAISE(auto literal, Literal::Deserialize(*bound, ptype));
SingleValueStructLike data(std::move(literal));
return term.Evaluate(data);
bool HasFileExtremum(const BoundAggregate& aggregate, const DataFile& file) {
auto value = EvaluateFileExtremum(aggregate, file);
return value && value->has_value();
}

class CountAggregator : public BoundAggregate::Aggregator {
Expand Down Expand Up @@ -139,27 +181,27 @@ class MaxAggregator : public BoundAggregate::Aggregator {
if (!valid_) {
return {};
}
if (!aggregate_.HasValue(file)) {
ICEBERG_ASSIGN_OR_RAISE(auto value, EvaluateFileExtremum(aggregate_, file));
if (!value) {
valid_ = false;
return {};
}

ICEBERG_ASSIGN_OR_RAISE(auto value, aggregate_.Evaluate(file));
if (value.IsNull()) {
if (value->IsNull()) {
return {};
}
if (current_.IsNull()) {
current_ = std::move(value);
current_ = std::move(*value);
return {};
}

if (auto ordering = value <=> current_;
if (auto ordering = *value <=> current_;
ordering == std::partial_ordering::unordered) {
valid_ = false;
return InvalidArgument("Cannot compare literal {} with current value {}",
value.ToString(), current_.ToString());
value->ToString(), current_.ToString());
} else if (ordering == std::partial_ordering::greater) {
current_ = std::move(value);
current_ = std::move(*value);
}
return {};
}
Expand Down Expand Up @@ -210,27 +252,27 @@ class MinAggregator : public BoundAggregate::Aggregator {
if (!valid_) {
return {};
}
if (!aggregate_.HasValue(file)) {
ICEBERG_ASSIGN_OR_RAISE(auto value, EvaluateFileExtremum(aggregate_, file));
if (!value) {
valid_ = false;
return {};
}

ICEBERG_ASSIGN_OR_RAISE(auto value, aggregate_.Evaluate(file));
if (value.IsNull()) {
if (value->IsNull()) {
return {};
}
if (current_.IsNull()) {
current_ = std::move(value);
current_ = std::move(*value);
return {};
}

if (auto ordering = value <=> current_;
if (auto ordering = *value <=> current_;
ordering == std::partial_ordering::unordered) {
valid_ = false;
return InvalidArgument("Cannot compare literal {} with current value {}",
value.ToString(), current_.ToString());
value->ToString(), current_.ToString());
} else if (ordering == std::partial_ordering::less) {
current_ = std::move(value);
current_ = std::move(*value);
}
return {};
}
Expand Down Expand Up @@ -406,23 +448,15 @@ Result<Literal> MaxAggregate::Evaluate(const StructLike& data) const {
}

Result<Literal> MaxAggregate::Evaluate(const DataFile& file) const {
auto field_id = GetFieldId(term());
auto upper = GetMapValue(file.upper_bounds, field_id);
return EvaluateBoundTerm(*term(), upper);
return FileExtremumOrNull(*this, file);
}

std::unique_ptr<BoundAggregate::Aggregator> MaxAggregate::NewAggregator() const {
return std::unique_ptr<BoundAggregate::Aggregator>(new MaxAggregator(*this));
}

bool MaxAggregate::HasValue(const DataFile& file) const {
auto field_id = GetFieldId(term());
bool has_bound = file.upper_bounds.contains(field_id);
auto value_count = GetMapValue(file.value_counts, field_id);
auto null_count = GetMapValue(file.null_value_counts, field_id);
bool all_null = value_count.has_value() && *value_count > 0 && null_count.has_value() &&
null_count.value() == value_count.value();
return has_bound || all_null;
return HasFileExtremum(*this, file);
}

MinAggregate::MinAggregate(std::shared_ptr<BoundTerm> term)
Expand All @@ -444,23 +478,15 @@ Result<Literal> MinAggregate::Evaluate(const StructLike& data) const {
}

Result<Literal> MinAggregate::Evaluate(const DataFile& file) const {
auto field_id = GetFieldId(term());
auto lower = GetMapValue(file.lower_bounds, field_id);
return EvaluateBoundTerm(*term(), lower);
return FileExtremumOrNull(*this, file);
}

std::unique_ptr<BoundAggregate::Aggregator> MinAggregate::NewAggregator() const {
return std::unique_ptr<BoundAggregate::Aggregator>(new MinAggregator(*this));
}

bool MinAggregate::HasValue(const DataFile& file) const {
auto field_id = GetFieldId(term());
bool has_bound = file.lower_bounds.contains(field_id);
auto value_count = GetMapValue(file.value_counts, field_id);
auto null_count = GetMapValue(file.null_value_counts, field_id);
bool all_null = value_count.has_value() && *value_count > 0 && null_count.has_value() &&
null_count.value() == value_count.value();
return has_bound || all_null;
return HasFileExtremum(*this, file);
}

// -------------------- Unbound binding --------------------
Expand Down
5 changes: 3 additions & 2 deletions src/iceberg/expression/inclusive_metrics_evaluator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "iceberg/manifest/manifest_entry.h"
#include "iceberg/schema.h"
#include "iceberg/transform.h"
#include "iceberg/util/conversions_internal.h"
#include "iceberg/util/macros.h"
#include "iceberg/util/math_util_internal.h"
#include "iceberg/util/temporal_util.h"
Expand Down Expand Up @@ -481,7 +482,7 @@ class InclusiveMetricsVisitor : public BoundVisitor<bool> {
}
auto primitive_type = internal::checked_pointer_cast<PrimitiveType>(type);
if (data_file_.lower_bounds.contains(id)) {
return Literal::Deserialize(data_file_.lower_bounds.at(id), primitive_type);
return internal::DeserializeBound(data_file_.lower_bounds.at(id), primitive_type);
}

return std::nullopt;
Expand All @@ -495,7 +496,7 @@ class InclusiveMetricsVisitor : public BoundVisitor<bool> {
}
auto primitive_type = internal::checked_pointer_cast<PrimitiveType>(type);
if (data_file_.upper_bounds.contains(id)) {
return Literal::Deserialize(data_file_.upper_bounds.at(id), primitive_type);
return internal::DeserializeBound(data_file_.upper_bounds.at(id), primitive_type);
Comment thread
zhjwpku marked this conversation as resolved.
Resolved
}

return std::nullopt;
Expand Down
Loading
Loading