RussellSpitzer commented on code in PR #4759: URL: https://github.com/apache/iceberg/pull/4759#discussion_r902809363
########## core/src/main/java/org/apache/iceberg/util/SortStrategyUtil.java: ########## @@ -0,0 +1,223 @@ +/* + * 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.util; + +import java.nio.ByteBuffer; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import org.apache.iceberg.DataFile; +import org.apache.iceberg.FileScanTask; +import org.apache.iceberg.Schema; +import org.apache.iceberg.SortField; +import org.apache.iceberg.SortOrder; +import org.apache.iceberg.expressions.Expression; +import org.apache.iceberg.expressions.InclusiveMetricsEvaluator; +import org.apache.iceberg.expressions.StrictMetricsEvaluator; +import org.apache.iceberg.relocated.com.google.common.collect.FluentIterable; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; +import org.apache.iceberg.types.Conversions; +import org.apache.iceberg.types.Types; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static org.apache.iceberg.expressions.Expressions.equal; + +public class SortStrategyUtil { + private static final Logger LOG = LoggerFactory.getLogger(SortStrategyUtil.class); + + private SortStrategyUtil() { + } + + /** + * Measures the sortedness score for a given list of {@link DataFile}s. + * + * @param files List of {@link DataFile}s + * @param sortOrder The sort order + * @return A score between 0.0 and 1.0 to represent how well those data files are sorted + * on the sort order. + */ + public static double sortednessScore(List<DataFile> files, SortOrder sortOrder) { + if (files.size() == 1) { + return 1.0; + } + + List<Integer> sortColIndices = getSortColumnIndices(sortOrder); + if (sortColIndices.isEmpty()) { + // Cannot obtain indices of sort columns, will consider those files are completely unsorted + return 0.0; + } + + double totalScore = 0; + // Compute and aggregate scores for all lowerBounds + List<List<ByteBuffer>> lowerBounds = getColumnValueBounds(files, sortColIndices, true); + for (List<ByteBuffer> point : lowerBounds) { + totalScore += overlapScore(point, files, sortColIndices, sortOrder.schema()); + } + // Compute and aggregate scores for all upperBounds + List<List<ByteBuffer>> upperBounds = getColumnValueBounds(files, sortColIndices, false); + for (List<ByteBuffer> point : upperBounds) { + totalScore += overlapScore(point, files, sortColIndices, sortOrder.schema()); + } + // Then normalize the result + return totalScore / (2 * files.size()); + } + + /** + * Removes files that are already sorted (having no overlap on sort column values with other files) + * from a collection of file scans. + * + * @param fileScans The original collection of file scans + * @param sortOrder The sort order + * @return A collection of file scans after removing sorted ones from the original collection + */ + public static Iterable<FileScanTask> removeSortedFiles(Iterable<FileScanTask> fileScans, SortOrder sortOrder) { + List<Integer> sortColIndices = getSortColumnIndices(sortOrder); + List<DataFile> dataFiles = FluentIterable.from(fileScans).stream() + .map(FileScanTask::file) + .collect(Collectors.toList()); + return FluentIterable.from(fileScans) + .filter(f -> hasOverLap(f.file(), dataFiles, sortColIndices, sortOrder.schema())); + } + + private static boolean hasOverLap( Review Comment: hasOverLap -> hasOverlap since "overlap" is a single word -- 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]
