SuKi2cn commented on code in PR #373:
URL: https://github.com/apache/iceberg-cpp/pull/373#discussion_r2583355826
##########
src/iceberg/expression/aggregate.cc:
##########
@@ -38,6 +40,58 @@ std::shared_ptr<PrimitiveType> GetPrimitiveType(const
BoundTerm& term) {
return internal::checked_pointer_cast<PrimitiveType>(term.type());
}
+Result<Scalar> LiteralToScalar(const Literal& literal) {
+ if (literal.IsNull()) {
+ return Scalar{std::monostate{}};
+ }
+
+ switch (literal.type()->type_id()) {
+ case TypeId::kBoolean:
+ return Scalar{std::get<bool>(literal.value())};
+ case TypeId::kInt:
+ case TypeId::kDate:
+ return Scalar{std::get<int32_t>(literal.value())};
+ case TypeId::kLong:
+ case TypeId::kTime:
+ case TypeId::kTimestamp:
+ case TypeId::kTimestampTz:
+ return Scalar{std::get<int64_t>(literal.value())};
+ case TypeId::kFloat:
+ return Scalar{std::get<float>(literal.value())};
+ case TypeId::kDouble:
+ return Scalar{std::get<double>(literal.value())};
+ case TypeId::kString: {
+ const auto& str = std::get<std::string>(literal.value());
+ return Scalar{std::string_view(str)};
+ }
+ case TypeId::kBinary:
+ case TypeId::kFixed: {
+ const auto& bytes = std::get<std::vector<uint8_t>>(literal.value());
+ return Scalar{
+ std::string_view(reinterpret_cast<const char*>(bytes.data()),
bytes.size())};
+ }
+ case TypeId::kDecimal:
+ return Scalar{std::get<Decimal>(literal.value())};
+ default:
+ return NotSupported("Cannot convert literal of type {} to Scalar",
+ literal.type()->ToString());
+ }
+}
+
+class SingleValueStructLike : public StructLike {
+ public:
+ explicit SingleValueStructLike(Literal literal) :
literal_(std::move(literal)) {}
+
+ Result<Scalar> GetField(size_t /*pos*/) const override {
Review Comment:
> Should we return error if pos is not 0?
I moved `LiteralToScalar`/`SingleValueStructLike` into `row/struct_like` as
the small adapter we use when evaluating aggregates from file metrics. I
originally tried to return an error when `pos != 0`, but that breaks the
metrics aggregation path: bound terms carry the original field position (often
1,2,…) so `Count`/`Max`/`Min` on file metrics all fail (ctest reproduces this).
The Java equivalent `ValueAggregate`
([ValueAggregate](https://github.com/apache/iceberg/blob/main/api/src/main/java/org/apache/iceberg/expressions/ValueAggregate.java#L60-L68))
also ignores the index for the same reason. It isn’t a reusable general
StructLike; it’s a narrow, internal adapter, so I think we need to ignore the
incoming index here for correctness. If anything in my understanding is off,
please let me know.
--
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]