aokolnychyi commented on a change in pull request #1301: URL: https://github.com/apache/iceberg/pull/1301#discussion_r466657607
########## 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> { + private final CloseableIterable<T> rows; + private final Function<T, Long> extractPos; + private final CloseableIterable<Long> deletePositions; + + private PositionDeleteFilter(CloseableIterable<T> rows, Function<T, Long> extractPos, + CloseableIterable<Long> deletePositions) { + this.rows = rows; + this.extractPos = extractPos; + this.deletePositions = deletePositions; + } + + @Override + public CloseableIterator<T> iterator() { + CloseableIterator<Long> deletePosIterator = deletePositions.iterator(); + + CloseableIterator<T> iter; + if (deletePosIterator.hasNext()) { + iter = new PositionFilterIterator(rows.iterator(), deletePosIterator); + } else { + iter = rows.iterator(); + try { + deletePosIterator.close(); + } catch (IOException e) { + throw new UncheckedIOException("Failed to close delete positions iterator", e); + } + } + + addCloseable(iter); + + return iter; + } + + private class PositionFilterIterator extends FilterIterator<T> { + private final CloseableIterator<Long> deletePosIterator; + private long nextDeletePos; + + protected PositionFilterIterator(CloseableIterator<T> items, CloseableIterator<Long> deletePositions) { + super(items); + this.deletePosIterator = deletePositions; + this.nextDeletePos = deletePosIterator.next(); + } + + @Override + protected boolean shouldKeep(T row) { + long currentPos = extractPos.apply(row); Review comment: Does this assume rows are ordered by position? ---------------------------------------------------------------- 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]
