SuKi2cn commented on code in PR #335:
URL: https://github.com/apache/iceberg-cpp/pull/335#discussion_r2557004364


##########
src/iceberg/expression/aggregate.cc:
##########
@@ -0,0 +1,256 @@
+/*
+ * 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 <vector>
+
+#include "iceberg/exception.h"
+#include "iceberg/expression/binder.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 {
+
+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
+
+// -------------------- Bound aggregates --------------------
+
+BoundCountAggregate::BoundCountAggregate(Expression::Operation op, Mode mode,
+                                         std::shared_ptr<BoundTerm> term)
+    : BoundAggregate(op, std::move(term)), mode_(mode) {}
+
+std::string BoundCountAggregate::ToString() const {
+  if (mode_ == Mode::kStar) {
+    return "count(*)";
+  }
+  ICEBERG_DCHECK(term() != nullptr, "Bound count aggregate should have term");
+  switch (mode_) {
+    case Mode::kNull:
+      return std::format("count_null({})", term()->reference()->name());
+    case Mode::kNonNull:
+      return std::format("count({})", term()->reference()->name());
+    case Mode::kStar:
+      break;
+  }
+  std::unreachable();
+}
+
+Result<Literal> BoundCountAggregate::Evaluate(const StructLike& data) const {
+  switch (mode_) {
+    case Mode::kStar:
+      return Literal::Long(1);
+    case Mode::kNonNull: {
+      ICEBERG_ASSIGN_OR_RAISE(auto literal, term()->Evaluate(data));
+      return Literal::Long(literal.IsNull() ? 0 : 1);
+    }
+    case Mode::kNull: {
+      ICEBERG_ASSIGN_OR_RAISE(auto literal, term()->Evaluate(data));
+      return Literal::Long(literal.IsNull() ? 1 : 0);
+    }
+  }
+  std::unreachable();
+}
+
+BoundValueAggregate::BoundValueAggregate(Expression::Operation op,
+                                         std::shared_ptr<BoundTerm> term)
+    : BoundAggregate(op, std::move(term)) {}
+
+std::string BoundValueAggregate::ToString() const {
+  ICEBERG_DCHECK(term() != nullptr, "Bound value aggregate should have term");
+  auto prefix = op() == Expression::Operation::kMax ? "max" : "min";
+  return std::format("{}({})", prefix, term()->reference()->name());
+}
+
+Result<Literal> BoundValueAggregate::Evaluate(const StructLike& data) const {
+  ICEBERG_ASSIGN_OR_RAISE(auto literal, term()->Evaluate(data));
+  return literal;
+}
+
+// -------------------- Unbound binding --------------------
+
+template <typename B>
+Result<std::shared_ptr<Expression>> UnboundAggregateImpl<B>::Bind(
+    const Schema& schema, bool case_sensitive) const {
+  std::shared_ptr<B> bound_term;
+  if (this->term()) {
+    ICEBERG_ASSIGN_OR_THROW(bound_term, this->term()->Bind(schema, 
case_sensitive));
+  }
+
+  switch (count_mode_) {
+    case CountMode::kStar:
+    case CountMode::kNull:
+    case CountMode::kNonNull: {
+      auto op = this->op() == Expression::Operation::kCountStar
+                    ? Expression::Operation::kCountStar
+                    : Expression::Operation::kCount;
+      auto mode =
+          count_mode_ == CountMode::kNull
+              ? BoundCountAggregate::Mode::kNull
+              : (count_mode_ == CountMode::kStar ? 
BoundCountAggregate::Mode::kStar
+                                                 : 
BoundCountAggregate::Mode::kNonNull);
+      auto aggregate =
+          std::make_shared<BoundCountAggregate>(op, mode, 
std::move(bound_term));
+      return aggregate;
+    }
+    case CountMode::kNone: {
+      if (this->op() != Expression::Operation::kMax &&
+          this->op() != Expression::Operation::kMin) {
+        return NotSupported("Unsupported aggregate operation");
+      }
+      if (!bound_term) {
+        return InvalidExpression("Aggregate requires a term");
+      }
+      auto aggregate =
+          std::make_shared<BoundValueAggregate>(this->op(), 
std::move(bound_term));
+      return aggregate;
+    }
+  }
+  std::unreachable();
+}
+
+template class UnboundAggregateImpl<BoundReference>;
+
+// -------------------- AggregateEvaluator --------------------
+
+namespace {
+
+class AggregateEvaluatorImpl : public AggregateEvaluator {

Review Comment:
   Thanks, that’s a good point.



-- 
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]

Reply via email to