drexler-sky commented on code in PR #18847:
URL: https://github.com/apache/iceberg/pull/18847#discussion_r4634348763


##########
core/src/main/java/org/apache/iceberg/expressions/GroupedAggregateEvaluator.java:
##########
@@ -0,0 +1,173 @@
+/*
+ * 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.
+ */
+package org.apache.iceberg.expressions;
+
+import java.util.List;
+import java.util.Map;
+import org.apache.iceberg.DataFile;
+import org.apache.iceberg.PartitionField;
+import org.apache.iceberg.PartitionSpec;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.StructLike;
+import org.apache.iceberg.data.GenericRecord;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.apache.iceberg.types.Types;
+import org.apache.iceberg.util.StructLikeMap;
+
+/**
+ * Evaluates aggregates for each group of identity partition values, using 
file metadata.
+ *
+ * <p>Each group-by field must be the source of an identity partition field in 
the spec of every
+ * file passed to {@link #update(DataFile)}. Otherwise, the grouped result 
cannot be computed from
+ * metadata and {@link #allAggregatorsValid()} returns false.
+ */
+public class GroupedAggregateEvaluator {
+
+  public static GroupedAggregateEvaluator create(
+      Schema schema,
+      Map<Integer, PartitionSpec> specs,
+      List<Integer> groupByFieldIds,
+      List<BoundAggregate<?, ?>> aggregates) {
+    return new GroupedAggregateEvaluator(schema, specs, groupByFieldIds, 
aggregates);
+  }
+
+  private final Map<Integer, PartitionSpec> specs;
+  private final List<Integer> groupByFieldIds;
+  private final List<BoundAggregate<?, ?>> aggregates;
+  private final Types.StructType groupType;
+  private final Types.StructType resultType;
+  private final Map<Integer, int[]> positionsBySpecId = Maps.newHashMap();
+  private final StructLikeMap<AggregateEvaluator> evaluatorsByGroup;
+  private boolean canGroup = true;
+
+  private GroupedAggregateEvaluator(
+      Schema schema,
+      Map<Integer, PartitionSpec> specs,
+      List<Integer> groupByFieldIds,
+      List<BoundAggregate<?, ?>> aggregates) {
+    Preconditions.checkArgument(!groupByFieldIds.isEmpty(), "Invalid group by 
fields: empty");
+
+    List<Types.NestedField> groupFields = Lists.newArrayList();
+    for (int pos = 0; pos < groupByFieldIds.size(); pos += 1) {
+      int fieldId = groupByFieldIds.get(pos);
+      Types.NestedField field = schema.findField(fieldId);
+      Preconditions.checkArgument(field != null, "Cannot find group by field 
with ID %s", fieldId);
+      groupFields.add(
+          Types.NestedField.optional(pos, schema.findColumnName(fieldId), 
field.type()));
+    }
+
+    List<Types.NestedField> resultFields = Lists.newArrayList(groupFields);
+    for (BoundAggregate<?, ?> aggregate : aggregates) {
+      resultFields.add(
+          Types.NestedField.optional(resultFields.size(), 
aggregate.describe(), aggregate.type()));
+    }
+
+    this.specs = specs;
+    this.groupByFieldIds = groupByFieldIds;
+    this.aggregates = aggregates;
+    this.groupType = Types.StructType.of(groupFields);
+    this.resultType = Types.StructType.of(resultFields);
+    this.evaluatorsByGroup = StructLikeMap.create(groupType);
+  }
+
+  public void update(DataFile file) {

Review Comment:
   I excluded float and double for now.



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