rdblue commented on code in PR #18699: URL: https://github.com/apache/iceberg/pull/18699#discussion_r4461527157
########## core/src/main/java/org/apache/iceberg/SnapshotValidator.java: ########## @@ -0,0 +1,578 @@ +/* + * 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; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ExecutorService; +import java.util.stream.Collectors; +import org.apache.iceberg.exceptions.ValidationException; +import org.apache.iceberg.expressions.Expression; +import org.apache.iceberg.expressions.Expressions; +import org.apache.iceberg.expressions.ManifestEvaluator; +import org.apache.iceberg.expressions.Projections; +import org.apache.iceberg.io.CloseableIterable; +import org.apache.iceberg.io.CloseableIterator; +import org.apache.iceberg.io.FileIO; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableSet; +import org.apache.iceberg.relocated.com.google.common.collect.Iterables; +import org.apache.iceberg.relocated.com.google.common.collect.Iterators; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; +import org.apache.iceberg.relocated.com.google.common.collect.Maps; +import org.apache.iceberg.relocated.com.google.common.collect.Sets; +import org.apache.iceberg.util.CharSequenceSet; +import org.apache.iceberg.util.ContentFileUtil; +import org.apache.iceberg.util.Pair; +import org.apache.iceberg.util.PartitionSet; +import org.apache.iceberg.util.Tasks; + +class SnapshotValidator { + // data is only added in "append" and "overwrite" operations + private static final Set<String> VALIDATE_ADDED_FILES_OPERATIONS = + ImmutableSet.of(DataOperations.APPEND, DataOperations.OVERWRITE); + // data files are removed in "overwrite", "replace", and "delete" + private static final Set<String> VALIDATE_DATA_FILES_EXIST_OPERATIONS = + ImmutableSet.of(DataOperations.OVERWRITE, DataOperations.REPLACE, DataOperations.DELETE); + private static final Set<String> VALIDATE_DATA_FILES_EXIST_SKIP_DELETE_OPERATIONS = + ImmutableSet.of(DataOperations.OVERWRITE, DataOperations.REPLACE); + // delete files can be added in "overwrite" or "delete" operations + private static final Set<String> VALIDATE_ADDED_DELETE_FILES_OPERATIONS = + ImmutableSet.of(DataOperations.OVERWRITE, DataOperations.DELETE); + // DVs can be added in "overwrite", "delete", and "replace" operations + private static final Set<String> VALIDATE_ADDED_DVS_OPERATIONS = + ImmutableSet.of(DataOperations.OVERWRITE, DataOperations.DELETE, DataOperations.REPLACE); + + private FileIO io; + private Long startingSnapshotId = null; + private boolean caseSensitive = true; + private Expression conflictDetectionFilter = null; + private ExecutorService workerPool = null; + + private TableMetadata base = null; + private Snapshot parent = null; + + private SnapshotValidator() {} + + static SnapshotValidator newValidator() { + return new SnapshotValidator(); + } + + SnapshotValidator io(FileIO newIo) { + this.io = newIo; + return this; + } + + SnapshotValidator startingSnapshotId(Long newStartingSnapshotId) { + this.startingSnapshotId = newStartingSnapshotId; + return this; + } + + SnapshotValidator caseSensitive(boolean isCaseSensitive) { + this.caseSensitive = isCaseSensitive; + return this; + } + + SnapshotValidator conflictDetectionFilter(Expression filter) { + this.conflictDetectionFilter = filter; + return this; + } + + SnapshotValidator scanWith(ExecutorService newWorkerPool) { + this.workerPool = newWorkerPool; + return this; + } + + /** + * Binds the base metadata and parent snapshot the checks run against. Needs to be invoked before + * running validations. + */ + SnapshotValidator forWindow(TableMetadata newBase, Snapshot newParent) { + this.base = newBase; + this.parent = newParent; + return this; + } + + /** + * Validates that no data files matching the conflict detection filter have been added to the + * table since the starting snapshot. + */ + void validateAddedDataFiles() { Review Comment: I went through all of the validations and looked at what they are doing and where they are used. Here's a brief summary: Logical checks that operating data has not changed: - Data files were added - Data files matching conflict filter (`validateAddedDataFiles`) - Data files are missing - Deletes matching conflict filter (`validateDeletedDataFiles`) - used for read/union/overwrite - Deleted files are missing (`failMissingDeleteFiles`) - also reference integrity - Delete files were added - Delete files matching conflict filter (`validateNoNewDeleteFiles`) - Delete files matching replaced data files (`validateNoNewDeletesForDataFiles`) Physical checks for reference integrity - Data and delete files to remove were not already (`failMissingDeletePaths`) - New/updated DVs reference valid data files (`validateDataFilesExist`) - New/updated DVs are still valid (`validateAddedDVs`) There are quite a few validations for row-level deletes, but I think that they are all distinct. `validateAddedDVs` is needed to check reference integrity, even if `validateNoNewDeleteFiles` is a broader validation because the logical check is only run when requested by the caller. That's why it is always used when there are DVs. I think that all 6 checks are currently needed. To name these checks, I think that using `validate` is a little confusing. The original validation, `validateAddedDataFiles` checks that there are no concurrently added data files conflict (match the filter). But "validate" doesn't tell a reader what is being validated, just some logic for added data files is being run. I think a better name is `assert` because it is clear for a reader that something should be true, just like in tests. For example, I think `assertNoAddedDataFiles` is a better name than `validateAddedDataFiles`. I recommend renaming the validation methods like this: - `validateAddedDataFiles` -> `assertNoAddedDataFiles` (matching the conflict filter is implied by config) - `validateDeletedDataFiles` -> `assertNoDeletedDataFiles` - `validateDataFilesExist(Set<String>)` -> `assertDataFilesExist(Set<String>)` - `validateNoNewDeleteFiles` -> `assertNoNewPositionDeletes` / `assertNoNewRowDeletes` - `validateNoNewDeletesForDataFiles(Iterable<DataFile>)` -> `assertNoNewDeletesFor(Iterable<DataFile>)` - `validateAddedDVs` -> `assertNoNewPositionDeletesFor(String...)` Also, some of these assertions can use or validate one another. For instance, `assertNoNewDeletesFor` can be satisfied by `assertNoNewPositionDeletesFor` in v3 and later because the referenced data file is required. -- 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]
