yyanyy commented on a change in pull request #1858: URL: https://github.com/apache/iceberg/pull/1858#discussion_r535758828
########## File path: data/src/test/java/org/apache/iceberg/io/TestGenericSortedPosDeleteWriter.java ########## @@ -0,0 +1,315 @@ +/* + * 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.io; + +import java.io.File; +import java.io.IOException; +import java.util.List; +import java.util.Locale; +import org.apache.iceberg.DataFile; +import org.apache.iceberg.DeleteFile; +import org.apache.iceberg.FileFormat; +import org.apache.iceberg.Files; +import org.apache.iceberg.PartitionSpec; +import org.apache.iceberg.RowDelta; +import org.apache.iceberg.Schema; +import org.apache.iceberg.TableTestBase; +import org.apache.iceberg.avro.Avro; +import org.apache.iceberg.data.GenericAppenderFactory; +import org.apache.iceberg.data.GenericRecord; +import org.apache.iceberg.data.IcebergGenerics; +import org.apache.iceberg.data.Record; +import org.apache.iceberg.data.avro.DataReader; +import org.apache.iceberg.data.parquet.GenericParquetReaders; +import org.apache.iceberg.encryption.EncryptedOutputFile; +import org.apache.iceberg.parquet.Parquet; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; +import org.apache.iceberg.util.StructLikeSet; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +@RunWith(Parameterized.class) +public class TestGenericSortedPosDeleteWriter extends TableTestBase { + private static final int FORMAT_V2 = 2; + + private final FileFormat format; + + private OutputFileFactory fileFactory; + private Record gRecord; + + @Parameterized.Parameters(name = "FileFormat={0}") + public static Object[] parameters() { + return new Object[][] { + new Object[] {"avro"}, + new Object[] {"parquet"}, + }; + } + + public TestGenericSortedPosDeleteWriter(String fileFormat) { + super(FORMAT_V2); + this.format = FileFormat.valueOf(fileFormat.toUpperCase(Locale.ENGLISH)); + } + + @Before + public void setupTable() throws IOException { + this.tableDir = temp.newFolder(); + Assert.assertTrue(tableDir.delete()); + + this.metadataDir = new File(tableDir, "metadata"); + this.table = create(SCHEMA, PartitionSpec.unpartitioned()); + this.gRecord = GenericRecord.create(SCHEMA); + + this.fileFactory = new OutputFileFactory(table.spec(), format, table.locationProvider(), table.io(), + table.encryption(), 1, 1); + + table.updateProperties() + .defaultFormat(format) + .commit(); + } + + private EncryptedOutputFile createEncryptedOutputFile() { + return fileFactory.newOutputFile(); + } + + private DataFile prepareDataFile(FileAppenderFactory<Record> appenderFactory, List<Record> rowSet) + throws IOException { + DataWriter<Record> writer = appenderFactory.newDataWriter(createEncryptedOutputFile(), format, null); + try (DataWriter<Record> closeableWriter = writer) { + for (Record record : rowSet) { + closeableWriter.add(record); + } + } + + return writer.toDataFile(); + } + + private Record createRow(Integer id, String data) { + Record row = gRecord.copy(); + row.setField("id", id); + row.setField("data", data); + return row; + } + + private StructLikeSet expectedRowSet(Iterable<Record> records) { + StructLikeSet set = StructLikeSet.create(table.schema().asStruct()); + records.forEach(set::add); + return set; + } + + private StructLikeSet actualRowSet(String... columns) throws IOException { + StructLikeSet set = StructLikeSet.create(table.schema().asStruct()); + try (CloseableIterable<Record> reader = IcebergGenerics.read(table).select(columns).build()) { + reader.forEach(set::add); + } + return set; + } + + @Test + public void testSortedPosDelete() throws IOException { + List<Record> rowSet = Lists.newArrayList( + createRow(0, "aaa"), + createRow(1, "bbb"), + createRow(2, "ccc"), + createRow(3, "ddd"), + createRow(4, "eee") + ); + + FileAppenderFactory<Record> appenderFactory = new GenericAppenderFactory(table.schema(), table.spec(), + null, null, null); + DataFile dataFile = prepareDataFile(appenderFactory, rowSet); + + SortedPosDeleteWriter<Record> writer = new SortedPosDeleteWriter<>(appenderFactory, fileFactory, format, null, 100); + try (SortedPosDeleteWriter<Record> closeableWriter = writer) { + for (int index = 0; index < rowSet.size(); index += 2) { Review comment: I think if we delete them in natural order, sorting them or not in delete writer will result in the correct order. Do we want to initialize the index as 4 and decrement the counter to test the sorting logic? ########## File path: core/src/main/java/org/apache/iceberg/io/SortedPosDeleteWriter.java ########## @@ -0,0 +1,190 @@ +/* + * 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.io; + +import java.io.Closeable; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.apache.iceberg.DeleteFile; +import org.apache.iceberg.FileFormat; +import org.apache.iceberg.PartitionKey; +import org.apache.iceberg.deletes.PositionDeleteWriter; +import org.apache.iceberg.encryption.EncryptedOutputFile; +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.Comparators; +import org.apache.iceberg.util.CharSequenceSet; +import org.apache.iceberg.util.CharSequenceWrapper; + +class SortedPosDeleteWriter<T> implements Closeable { + private static final long DEFAULT_RECORDS_NUM_THRESHOLD = 100_000L; + + private final Map<CharSequenceWrapper, List<PosValue<T>>> posDeletes = Maps.newHashMap(); + private final List<DeleteFile> completedFiles = Lists.newArrayList(); + private final Set<CharSequence> referencedDataFiles = CharSequenceSet.empty(); + private final PosValueComparator<T> posValueComparator = new PosValueComparator<>(); + private final CharSequenceWrapper wrapper = CharSequenceWrapper.wrap(null); + + private final FileAppenderFactory<T> appenderFactory; + private final OutputFileFactory fileFactory; + private final FileFormat format; + private final PartitionKey partition; + private final long recordsNumThreshold; + + private int records = 0; + + SortedPosDeleteWriter(FileAppenderFactory<T> appenderFactory, + OutputFileFactory fileFactory, + FileFormat format, + PartitionKey partition, + long recordsNumThreshold) { + this.appenderFactory = appenderFactory; + this.fileFactory = fileFactory; + this.format = format; + this.partition = partition; + this.recordsNumThreshold = recordsNumThreshold; + } + + SortedPosDeleteWriter(FileAppenderFactory<T> appenderFactory, + OutputFileFactory fileFactory, + FileFormat format, + PartitionKey partition) { + this(appenderFactory, fileFactory, format, partition, DEFAULT_RECORDS_NUM_THRESHOLD); + } + + public void delete(CharSequence path, long pos) { + delete(path, pos, null); + } + + public void delete(CharSequence path, long pos, T row) { + posDeletes.compute(CharSequenceWrapper.wrap(path), (k, v) -> { + if (v == null) { + return Lists.newArrayList(PosValue.of(pos, row)); + } else { + v.add(PosValue.of(pos, row)); + return v; + } + }); + + records += 1; + + // TODO Flush buffer based on the policy that checking whether whole heap memory size exceed the threshold. + if (records >= recordsNumThreshold) { + flushBuffer(); + } + } + + public List<DeleteFile> complete() throws IOException { + close(); + + return completedFiles; + } + + public Set<CharSequence> referencedDataFiles() { + return referencedDataFiles; + } + + @Override + public void close() throws IOException { + flushBuffer(); + } + + private void flushBuffer() { + if (posDeletes.isEmpty()) { + return; + } + + // Create a new output file. + EncryptedOutputFile outputFile; + if (partition == null) { + outputFile = fileFactory.newOutputFile(); + } else { + outputFile = fileFactory.newOutputFile(partition); + } + + PositionDeleteWriter<T> writer = appenderFactory.newPosDeleteWriter(outputFile, format, partition); + try (PositionDeleteWriter<T> closeableWriter = writer) { + // Sort all the paths. + CharSequence[] paths = new CharSequence[posDeletes.size()]; + int index = 0; + for (CharSequenceWrapper charSequenceWrapper : posDeletes.keySet()) { + paths[index] = charSequenceWrapper.get(); + index += 1; + } + Arrays.sort(paths, Comparators.charSequences()); + + // Write all the sorted <path, pos, row> triples. + for (CharSequence path : paths) { + List<PosValue<T>> positions = posDeletes.get(wrapper.set(path)); + positions.sort(posValueComparator); Review comment: Nit: could probably be `positions.sort(Comparator.comparingLong(PosValue::pos))` ########## File path: core/src/main/java/org/apache/iceberg/io/SortedPosDeleteWriter.java ########## @@ -0,0 +1,190 @@ +/* + * 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.io; + +import java.io.Closeable; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.apache.iceberg.DeleteFile; +import org.apache.iceberg.FileFormat; +import org.apache.iceberg.PartitionKey; +import org.apache.iceberg.deletes.PositionDeleteWriter; +import org.apache.iceberg.encryption.EncryptedOutputFile; +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.Comparators; +import org.apache.iceberg.util.CharSequenceSet; +import org.apache.iceberg.util.CharSequenceWrapper; + +class SortedPosDeleteWriter<T> implements Closeable { + private static final long DEFAULT_RECORDS_NUM_THRESHOLD = 100_000L; + + private final Map<CharSequenceWrapper, List<PosValue<T>>> posDeletes = Maps.newHashMap(); + private final List<DeleteFile> completedFiles = Lists.newArrayList(); + private final Set<CharSequence> referencedDataFiles = CharSequenceSet.empty(); + private final PosValueComparator<T> posValueComparator = new PosValueComparator<>(); + private final CharSequenceWrapper wrapper = CharSequenceWrapper.wrap(null); + + private final FileAppenderFactory<T> appenderFactory; + private final OutputFileFactory fileFactory; + private final FileFormat format; + private final PartitionKey partition; + private final long recordsNumThreshold; + + private int records = 0; + + SortedPosDeleteWriter(FileAppenderFactory<T> appenderFactory, + OutputFileFactory fileFactory, + FileFormat format, + PartitionKey partition, + long recordsNumThreshold) { + this.appenderFactory = appenderFactory; + this.fileFactory = fileFactory; + this.format = format; + this.partition = partition; + this.recordsNumThreshold = recordsNumThreshold; + } + + SortedPosDeleteWriter(FileAppenderFactory<T> appenderFactory, + OutputFileFactory fileFactory, + FileFormat format, + PartitionKey partition) { + this(appenderFactory, fileFactory, format, partition, DEFAULT_RECORDS_NUM_THRESHOLD); + } + + public void delete(CharSequence path, long pos) { + delete(path, pos, null); + } + + public void delete(CharSequence path, long pos, T row) { + posDeletes.compute(CharSequenceWrapper.wrap(path), (k, v) -> { Review comment: Nit: `wrapper.set`? ---------------------------------------------------------------- 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]
