rdblue commented on a change in pull request #1301: URL: https://github.com/apache/iceberg/pull/1301#discussion_r466667716
########## File path: core/src/main/java/org/apache/iceberg/deletes/Deletes.java ########## @@ -0,0 +1,159 @@ +/* + * 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.deletes; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.util.Comparator; +import java.util.List; +import java.util.function.Function; +import org.apache.iceberg.Accessor; +import org.apache.iceberg.Schema; +import org.apache.iceberg.StructLike; +import org.apache.iceberg.io.CloseableGroup; +import org.apache.iceberg.io.CloseableIterable; +import org.apache.iceberg.io.CloseableIterator; +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.Comparators; +import org.apache.iceberg.types.Types; +import org.apache.iceberg.util.Filter; +import org.apache.iceberg.util.FilterIterator; +import org.apache.iceberg.util.SortedMerge; + +public class Deletes { + private static final Schema POSITION_DELETE_SCHEMA = new Schema( + Types.NestedField.required(1, "filename", Types.StringType.get(), "Data file location of the deleted row"), + Types.NestedField.required(2, "pos", Types.LongType.get(), "Row position in the data file of the deleted row") + ); + + private static final Accessor<StructLike> FILENAME_ACCESSOR = POSITION_DELETE_SCHEMA.accessorForField(1); + private static final Accessor<StructLike> POSITION_ACCESSOR = POSITION_DELETE_SCHEMA.accessorForField(2); + + private Deletes() { + } + + public static <T> CloseableIterable<T> positionFilter(CloseableIterable<T> rows, Function<T, Long> rowToPosition, + CloseableIterable<Long> posDeletes) { + return new PositionDeleteFilter<>(rows, rowToPosition, posDeletes); + } + + public static CloseableIterable<Long> deletePositions(String dataLocation, CloseableIterable<StructLike> posDeletes) { + return deletePositions(dataLocation, ImmutableList.of(posDeletes)); + } + + public static CloseableIterable<Long> deletePositions(String dataLocation, + List<CloseableIterable<StructLike>> deleteFiles) { + DataFileFilter locationFilter = new DataFileFilter(dataLocation); + List<CloseableIterable<Long>> positions = Lists.transform(deleteFiles, deletes -> + CloseableIterable.transform(locationFilter.filter(deletes), row -> (Long) POSITION_ACCESSOR.get(row))); + + return new SortedMerge<>(Long::compare, positions); + } + + private static class PositionDeleteFilter<T> extends CloseableGroup implements CloseableIterable<T> { Review comment: This filter doesn't require us to answer most of those questions right now. All it requires is some object and a way to get the position of that object. That should be flexible enough that we can handle position as a column or using a class that directly supports it. For the initial integration with Spark, I was thinking that we would add the _pos metadata column at the end of the requested projection. That way, we can return the rows without copying the data. At most, we would need to tell the row to report that it is one column shorter. The main idea here is to make it easy for readers to create filters for tasks and apply them. The reader just needs to open the delete files, then pass them to these methods to merge deletes together and use them as a row filter. For vectorization, we will probably want a different implementation, but we could reuse some of these classes (like the merging iterator). ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
