aokolnychyi commented on code in PR #6371: URL: https://github.com/apache/iceberg/pull/6371#discussion_r1052493128
########## spark/v3.3/spark/src/main/java/org/apache/iceberg/spark/source/SparkPartitioningAwareScan.java: ########## @@ -0,0 +1,244 @@ +/* + * 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.spark.source; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.util.Collections; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; +import org.apache.iceberg.BaseScanTaskGroup; +import org.apache.iceberg.PartitionField; +import org.apache.iceberg.PartitionScanTask; +import org.apache.iceberg.PartitionSpec; +import org.apache.iceberg.Scan; +import org.apache.iceberg.ScanTask; +import org.apache.iceberg.ScanTaskGroup; +import org.apache.iceberg.Schema; +import org.apache.iceberg.StructLike; +import org.apache.iceberg.Table; +import org.apache.iceberg.exceptions.ValidationException; +import org.apache.iceberg.expressions.Expression; +import org.apache.iceberg.io.CloseableIterable; +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.Sets; +import org.apache.iceberg.spark.Spark3Util; +import org.apache.iceberg.spark.SparkReadConf; +import org.apache.iceberg.types.Types; +import org.apache.iceberg.types.Types.StructType; +import org.apache.iceberg.util.StructLikeSet; +import org.apache.iceberg.util.TableScanUtil; +import org.apache.spark.sql.SparkSession; +import org.apache.spark.sql.connector.expressions.Transform; +import org.apache.spark.sql.connector.read.SupportsReportPartitioning; +import org.apache.spark.sql.connector.read.partitioning.KeyGroupedPartitioning; +import org.apache.spark.sql.connector.read.partitioning.Partitioning; +import org.apache.spark.sql.connector.read.partitioning.UnknownPartitioning; + +abstract class SparkPartitioningAwareScan<T extends PartitionScanTask> extends SparkScan + implements SupportsReportPartitioning { + + private final Scan<?, ? extends ScanTask, ? extends ScanTaskGroup<?>> scan; + private final boolean preserveDataGrouping; + + private Set<PartitionSpec> specs = null; // lazy cache of scanned specs + private List<T> tasks = null; // lazy cache of uncombined tasks + private List<ScanTaskGroup<T>> taskGroups = null; // lazy cache of task groups + private StructType groupingKeyType = null; // lazy cache of the grouping key type + private StructLikeSet groupingKeys = null; // lazy cache of grouping keys + + SparkPartitioningAwareScan( + SparkSession spark, + Table table, + Scan<?, ? extends ScanTask, ? extends ScanTaskGroup<?>> scan, + SparkReadConf readConf, + Schema expectedSchema, + List<Expression> filters) { + + super(spark, table, readConf, expectedSchema, filters); + + this.scan = scan; + this.preserveDataGrouping = readConf.preserveDataGrouping(); + + if (scan == null) { + this.specs = Collections.emptySet(); + this.tasks = Collections.emptyList(); + this.taskGroups = Collections.emptyList(); + } + } + + protected abstract Class<T> taskJavaClass(); + + protected Scan<?, ? extends ScanTask, ? extends ScanTaskGroup<?>> scan() { + return scan; + } + + @Override + public Partitioning outputPartitioning() { + Preconditions.checkState(taskGroups() != null, "Task groups must be planned"); + + if (groupingKeyType().fields().isEmpty()) { + return new UnknownPartitioning(taskGroups().size()); + } else { + return new KeyGroupedPartitioning(groupingKeyTransforms(), taskGroups().size()); + } + } + + @Override + protected StructType groupingKeyType() { + if (groupingKeyType == null) { + if (preserveDataGrouping) { + this.groupingKeyType = + org.apache.iceberg.Partitioning.groupingKeyType(expectedSchema(), specs()); + } else { + this.groupingKeyType = StructType.of(); + } + } + + return groupingKeyType; + } + + private Transform[] groupingKeyTransforms() { + Set<Integer> groupingKeyFieldIds = + groupingKeyType().fields().stream() + .map(Types.NestedField::fieldId) + .collect(Collectors.toSet()); + + List<PartitionField> groupingKeyFields = Lists.newArrayList(); + + for (PartitionSpec spec : specs()) { + for (PartitionField field : spec.fields()) { + if (groupingKeyFieldIds.contains(field.fieldId())) { + groupingKeyFields.add(field); + groupingKeyFieldIds.remove(field.fieldId()); + } + } + } + + return Spark3Util.toTransforms(table().schema(), groupingKeyFields); + } + + protected Set<PartitionSpec> specs() { + if (specs == null) { + Set<PartitionSpec> taskSpecs = Sets.newHashSet(); + for (T task : tasks()) { + taskSpecs.add(task.spec()); + } + this.specs = taskSpecs; + } + + return specs; + } + + @SuppressWarnings("unchecked") + protected synchronized List<T> tasks() { + if (tasks == null) { + try (CloseableIterable<? extends ScanTask> taskIterable = scan.planFiles()) { + List<T> plannedTasks = Lists.newArrayList(); + + for (ScanTask task : taskIterable) { + ValidationException.check( Review Comment: This check is to provide a reasonable exception if there is a task of an unsupported type. Since we may have a combination of different tasks, each task is being validated. I don't think it would be super expensive to perform this validation but it is kind of optional too. -- 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: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org