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


##########
src/iceberg/expression/aggregate.cc:
##########
@@ -0,0 +1,398 @@
+/*
+ * 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) 
{
+  if (!term.type()->is_primitive()) {
+    return InvalidExpression("Aggregate requires primitive type, got {}",
+                             term.type()->ToString());
+  }
+  return internal::checked_pointer_cast<PrimitiveType>(term.type());
+}
+
+class CountNonNullAggregator : public BoundAggregate::Aggregator {
+ public:
+  explicit CountNonNullAggregator(std::shared_ptr<BoundTerm> term)
+      : term_(std::move(term)) {}
+
+  Status Update(const StructLike& row) override {
+    ICEBERG_ASSIGN_OR_RAISE(auto literal, term_->Evaluate(row));
+    if (!literal.IsNull()) {
+      ++count_;
+    }
+    return {};
+  }
+
+  Result<Literal> ResultLiteral() const override { return 
Literal::Long(count_); }
+
+ private:
+  std::shared_ptr<BoundTerm> term_;
+  int64_t count_ = 0;
+};
+
+class CountNullAggregator : public BoundAggregate::Aggregator {
+ public:
+  explicit CountNullAggregator(std::shared_ptr<BoundTerm> term)
+      : term_(std::move(term)) {}
+
+  Status Update(const StructLike& row) override {
+    ICEBERG_ASSIGN_OR_RAISE(auto literal, term_->Evaluate(row));
+    if (literal.IsNull()) {
+      ++count_;
+    }
+    return {};
+  }
+
+  Result<Literal> ResultLiteral() const override { return 
Literal::Long(count_); }
+
+ private:
+  std::shared_ptr<BoundTerm> term_;
+  int64_t count_ = 0;
+};
+
+class CountStarAggregator : public BoundAggregate::Aggregator {
+ public:
+  Status Update(const StructLike& /*row*/) override {
+    ++count_;
+    return {};
+  }
+
+  Result<Literal> ResultLiteral() const override { return 
Literal::Long(count_); }
+
+ private:
+  int64_t count_ = 0;
+};
+
+class ValueAggregatorImpl : public BoundAggregate::Aggregator {
+ public:
+  ValueAggregatorImpl(bool is_max, std::shared_ptr<BoundTerm> term)
+      : is_max_(is_max), term_(std::move(term)) {}
+
+  Status Update(const StructLike& row) override {
+    ICEBERG_ASSIGN_OR_RAISE(auto val_literal, term_->Evaluate(row));
+    if (val_literal.IsNull()) {
+      return {};
+    }
+    if (!current_) {
+      current_ = std::move(val_literal);
+      return {};
+    }
+
+    auto ordering = val_literal <=> *current_;
+    if (ordering == std::partial_ordering::unordered) {
+      return InvalidExpression("Cannot compare literals of type {}",
+                               val_literal.type()->ToString());
+    }
+
+    if (is_max_) {
+      if (ordering == std::partial_ordering::greater) {
+        current_ = std::move(val_literal);
+      }
+    } else {
+      if (ordering == std::partial_ordering::less) {
+        current_ = std::move(val_literal);
+      }
+    }
+    return {};
+  }
+
+  Result<Literal> ResultLiteral() const override {
+    if (current_) {
+      return *current_;
+    }
+    ICEBERG_ASSIGN_OR_RAISE(auto type, GetPrimitiveType(*term_));
+    return Literal::Null(type);
+  }
+
+ private:
+  bool is_max_;
+  std::shared_ptr<BoundTerm> term_;
+  std::optional<Literal> current_;
+};
+
+}  // namespace
+
+// -------------------- Bound aggregates --------------------
+
+CountNonNullAggregate::CountNonNullAggregate(std::shared_ptr<BoundTerm> term)
+    : CountAggregate(Expression::Operation::kCount, std::move(term)) {}
+
+Result<std::shared_ptr<CountNonNullAggregate>> CountNonNullAggregate::Make(

Review Comment:
   > 
   
   
   
   > Same for other Make functions.
   
   Applied this suggestion and changed all `Make` functions to return 
`std::unique_ptr`, converting to `std::shared_ptr` at the call sites where 
shared ownership is actually required.



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