anuragmantri commented on code in PR #18847: URL: https://github.com/apache/iceberg/pull/18847#discussion_r4590002312
########## 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. + */ Review Comment: +1. It might also be worth adding on `results()` that values use Iceberg's internal representation (for example, dates as days and timestamps as microseconds), so callers know they need to convert them. `update` and `allAggregatorsValid` could use a sentence each too, since this is a new public class. ########## 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 was thinking about float and double group-by fields. Identity partitions are allowed on those types, and `StructLikeMap` treats `-0.0` and `0.0` as different keys. Spark normalizes both of those (and NaN) in grouping keys, so a table with `-0.0` and `0.0` partitions would return two rows when pushed down and one row otherwise. I haven't run a test. Should we just exclude float and double group-by fields in this initial version if this is indeed the case? ########## 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) { + if (!canGroup) { + return; + } + + int[] positions = positionsBySpecId.computeIfAbsent(file.specId(), this::identityPositions); + if (positions == null) { + this.canGroup = false; + return; + } + + StructLike partition = file.partition(); + GenericRecord group = GenericRecord.create(groupType); + for (int i = 0; i < positions.length; i += 1) { + group.set(i, partition.get(positions[i], Object.class)); + } + + evaluatorsByGroup + .computeIfAbsent(group, ignored -> AggregateEvaluator.create(aggregates)) + .update(file); Review Comment: `StructLikeMap` has its own `computeIfAbsent(StructLike, Supplier)` that does one wrapper lookup. I think `Function` falls back to the `Map` default, which does a `get` and then a `put`. Since this runs for every file, maybe we should use this instead: ```suggestion evaluatorsByGroup .computeIfAbsent(group, () -> AggregateEvaluator.create(aggregates)) .update(file); ``` -- 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]
