SuKi2cn commented on code in PR #335: URL: https://github.com/apache/iceberg-cpp/pull/335#discussion_r2553856043
########## src/iceberg/expression/aggregate.cc: ########## @@ -0,0 +1,289 @@ +/* + * 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 "iceberg/expression/aggregate.h" + +#include <format> +#include <optional> + +#include "iceberg/exception.h" +#include "iceberg/expression/binder.h" +#include "iceberg/expression/expression.h" +#include "iceberg/row/struct_like.h" +#include "iceberg/type.h" +#include "iceberg/util/checked_cast.h" +#include "iceberg/util/macros.h" + +namespace iceberg { + +namespace { + +std::string OperationToPrefix(Expression::Operation op) { + switch (op) { + case Expression::Operation::kMax: + return "max"; + case Expression::Operation::kMin: + return "min"; + case Expression::Operation::kCount: + case Expression::Operation::kCountStar: + return "count"; + default: + break; + } + return "aggregate"; +} + +Result<std::shared_ptr<PrimitiveType>> GetPrimitiveType(const BoundTerm& term) { + auto primitive = std::dynamic_pointer_cast<PrimitiveType>(term.type()); + if (primitive == nullptr) { + return InvalidExpression("Aggregate requires primitive type, got {}", + term.type()->ToString()); + } + return primitive; +} + +} // namespace + +CountAggregate::CountAggregate(Expression::Operation op, Mode mode, + std::shared_ptr<UnboundTerm<BoundReference>> term, + std::shared_ptr<NamedReference> reference) + : UnboundAggregate(op), + mode_(mode), + term_(std::move(term)), + reference_(std::move(reference)) {} + +Result<std::unique_ptr<CountAggregate>> CountAggregate::Count( + std::shared_ptr<UnboundTerm<BoundReference>> term) { + auto ref = term->reference(); + return std::unique_ptr<CountAggregate>(new CountAggregate( + Expression::Operation::kCount, Mode::kNonNull, std::move(term), std::move(ref))); +} + +Result<std::unique_ptr<CountAggregate>> CountAggregate::CountNull( + std::shared_ptr<UnboundTerm<BoundReference>> term) { + auto ref = term->reference(); + return std::unique_ptr<CountAggregate>(new CountAggregate( + Expression::Operation::kCount, Mode::kNull, std::move(term), std::move(ref))); +} + +std::unique_ptr<CountAggregate> CountAggregate::CountStar() { + return std::unique_ptr<CountAggregate>(new CountAggregate( + Expression::Operation::kCountStar, Mode::kStar, nullptr, nullptr)); +} + +std::string CountAggregate::ToString() const { + if (mode_ == Mode::kStar) { + return "count(*)"; + } + ICEBERG_DCHECK(reference_ != nullptr, "Count aggregate should have reference"); + switch (mode_) { + case Mode::kNull: + return std::format("count_null({})", reference_->name()); + case Mode::kNonNull: + return std::format("count({})", reference_->name()); + case Mode::kStar: + break; + } + std::unreachable(); +} + +Result<std::shared_ptr<Expression>> CountAggregate::Bind(const Schema& schema, + bool case_sensitive) const { + std::shared_ptr<BoundTerm> bound_term; + if (term_ != nullptr) { + ICEBERG_ASSIGN_OR_THROW(auto bound, term_->Bind(schema, case_sensitive)); + bound_term = std::move(bound); + } + auto aggregate = + std::make_shared<BoundCountAggregate>(op(), mode_, std::move(bound_term)); + return aggregate; +} + +BoundCountAggregate::BoundCountAggregate(Expression::Operation op, + CountAggregate::Mode mode, + std::shared_ptr<BoundTerm> term) + : BoundAggregate(op, std::move(term)), mode_(mode) {} + +std::string BoundCountAggregate::ToString() const { + if (mode_ == CountAggregate::Mode::kStar) { Review Comment: > Why did you special handling for `kStar`? Good catch — mapping all remaining operations to `"aggregate"` is not really correct. In the Java implementation, `Aggregate.toString()` handles each aggregate operation explicitly and the `default` branch throws an `UnsupportedOperationException("Invalid aggregate: " + op())`, so there is no generic `"aggregate"` fallback. I’ll update the C++ helper to mirror that behavior: only the supported aggregate operations will be handled explicitly, and for anything else we’ll treat it as invalid/unreachable instead of returning `"aggregate"`. This way we don’t silently hide unsupported operations and stay consistent with the Java implementation. -- 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]
