wgtmac commented on code in PR #1157:
URL: https://github.com/apache/iceberg-cpp/pull/1157#discussion_r4689377379
##########
src/iceberg/expression/inclusive_metrics_evaluator.cc:
##########
@@ -486,7 +487,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);
Review Comment:
A date upper bound of 89,478,486 days fits in timestamp micros, but its hour
count wraps to a negative int32. This makes `hour(ts) >= 0` prune a file
containing epoch. Skip hour-based pruning when the transformed range can wrap.
##########
src/iceberg/test/aggregate_test.cc:
##########
@@ -327,6 +328,44 @@ TEST(AggregateTest, AggregatesFromDataFileWithTransform) {
EXPECT_TRUE(evaluator->AllAggregatorsValid());
}
+TEST(AggregateTest, MaxDayUsesTimestampUpperBound) {
+ Schema schema({SchemaField::MakeRequired(1, "ts", timestamp())});
+ auto max_day = BindAggregate(schema,
Expressions::Max(Expressions::Day("ts")));
+ ICEBERG_UNWRAP_OR_FAIL(auto upper,
+ Literal::Timestamp(2 * internal::kMicrosPerDay +
1).Serialize());
+ DataFile file{.record_count = 1, .upper_bounds = {{1, upper}}};
+ EXPECT_TRUE(max_day->HasValue(file));
Review Comment:
A file containing only day 1 may have an upper bound on day 2. What
guarantees this bound is exact? Please cover that case and fall back when the
metrics cannot establish the MAX.
##########
src/iceberg/util/conversions.cc:
##########
@@ -158,20 +190,27 @@ Result<Literal::Value> Conversions::FromBytes(const
PrimitiveType& type,
return Literal::Value{value};
}
case TypeId::kLong:
- case TypeId::kTime:
case TypeId::kTimestamp:
- case TypeId::kTimestampTz:
case TypeId::kTimestampNs:
- case TypeId::kTimestampTzNs: {
- int64_t value;
- if (data.size() < 8) {
- // Type was promoted from int to long
- ICEBERG_ASSIGN_OR_RAISE(auto int_value,
ReadLittleEndian<int32_t>(data));
- value = static_cast<int64_t>(int_value);
- } else {
- ICEBERG_ASSIGN_OR_RAISE(auto long_value,
ReadLittleEndian<int64_t>(data));
- value = long_value;
+ if (data.size() == sizeof(int32_t)) {
+ ICEBERG_ASSIGN_OR_RAISE(auto value, ReadLittleEndian<int32_t>(data));
+ if (type_id == TypeId::kLong) {
+ return Literal::Value{static_cast<int64_t>(value)};
+ }
+ // Only timestamps without a zone can be promoted from a four-byte
date.
+ // https://iceberg.apache.org/spec/#schema-evolution
+ auto timestamp = PromoteDate(value, type_id);
+ if (!timestamp) {
+ return InvalidArgument("Date {} is outside the range of {}", value,
Review Comment:
ManifestEvaluator still reaches this error for partition summaries. An old
identity-date manifest may contain day 1 with a loose upper bound of day
106752. After promotion to timestamp_ns, keep that manifest for `ts > epoch`
and distinguish an unrepresentable bound from an all-null summary.
##########
src/iceberg/expression/aggregate.cc:
##########
@@ -56,17 +57,46 @@ 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();
+ 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;
+ if (auto it = bounds.find(id); 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);
Review Comment:
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.
--
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]