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


##########
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) {

Review Comment:
   > Why did you special handling for `kStar`?
   
   The special handling for `kStar` is intentional and mirrors the Java 
implementation.
   
   In the Java version, `Aggregate.toString()` has a dedicated branch for 
`Operation.COUNT_STAR` that renders: `count(*)`
   
   In the C++ design, the different COUNT variants are represented via 
`BoundCountAggregate::Mode`
   (`kNonNull`, `kNull`, `kStar`). Therefore, this `kStar` special case is the 
equivalent of the Java
   `COUNT_STAR` branch, ensuring we correctly print `count(*)` for `COUNT(*)` 
semantics.
   
   In other words:
   - `Mode::kStar`   → `COUNT(*)`
   - `Mode::kNonNull` → `COUNT(column)`
   - `Mode::kNull` → `COUNT_NULL(column)`
   
   This preserves consistency with the Java behavior while adapting to the C++ 
expression model.
   



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