Repository navigation
Spark: Add flag to handle missing files for importSparkTable
#12212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
RussellSpitzer
merged 5 commits into
apache:main
from
jshmchenxi:bugfix/spark-util-partition-location-nonexist
May 5, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
eedecdd
Data: Handle case where partition location is missing for `TableMigra…
jshmchenxi 66abe94
Add end-to-end test to `TestSnapshotTableAction`
jshmchenxi ce991f6
Add log when skipping partition with missing location
jshmchenxi 909f0c2
Handle FileNotFoundException from caller
jshmchenxi c63beab
Address comments
jshmchenxi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
data/src/test/java/org/apache/iceberg/data/TestTableMigrationUtil.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| /* | ||
| * 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.data; | ||
|
|
||
| import static org.apache.iceberg.types.Types.NestedField.optional; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| import java.io.File; | ||
| import java.io.FileNotFoundException; | ||
| import java.io.IOException; | ||
| import java.nio.file.Path; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import org.apache.avro.generic.GenericData; | ||
| import org.apache.hadoop.conf.Configuration; | ||
| import org.apache.iceberg.DataFile; | ||
| import org.apache.iceberg.Files; | ||
| import org.apache.iceberg.MetricsConfig; | ||
| import org.apache.iceberg.PartitionSpec; | ||
| import org.apache.iceberg.Schema; | ||
| import org.apache.iceberg.avro.RandomAvroData; | ||
| import org.apache.iceberg.io.FileAppender; | ||
| import org.apache.iceberg.parquet.Parquet; | ||
| import org.apache.iceberg.parquet.ParquetAvroWriter; | ||
| import org.apache.iceberg.types.Types; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.io.TempDir; | ||
|
|
||
| class TestTableMigrationUtil { | ||
| private static final Schema SCHEMA = | ||
| new Schema( | ||
| optional(1, "id", Types.IntegerType.get()), optional(2, "data", Types.StringType.get())); | ||
| private static final PartitionSpec SPEC = PartitionSpec.builderFor(SCHEMA).identity("id").build(); | ||
| private static final Map<String, String> PARTITION = Map.of("id", "1"); | ||
| private static final String FORMAT = "parquet"; | ||
| private static final Configuration CONF = new Configuration(); | ||
|
|
||
| @TempDir protected Path tempTableLocation; | ||
|
|
||
| @Test | ||
| void testListPartition() throws IOException { | ||
| Path partitionPath = tempTableLocation.resolve("id=1"); | ||
| String partitionUri = partitionPath.toUri().toString(); | ||
| java.nio.file.Files.createDirectories(partitionPath); | ||
| writePartitionFile(partitionPath.toFile()); | ||
|
|
||
| List<DataFile> dataFiles = | ||
| TableMigrationUtil.listPartition( | ||
| PARTITION, partitionUri, FORMAT, SPEC, CONF, MetricsConfig.getDefault(), null); | ||
| assertThat(dataFiles) | ||
| .as("List partition with 1 Parquet file should return 1 DataFile") | ||
| .hasSize(1); | ||
| } | ||
|
|
||
| @Test | ||
| void testListEmptyPartition() throws IOException { | ||
| Path partitionPath = tempTableLocation.resolve("id=1"); | ||
| String partitionUri = partitionPath.toUri().toString(); | ||
| java.nio.file.Files.createDirectories(partitionPath); | ||
|
|
||
| List<DataFile> dataFiles = | ||
| TableMigrationUtil.listPartition( | ||
| PARTITION, partitionUri, FORMAT, SPEC, CONF, MetricsConfig.getDefault(), null); | ||
| assertThat(dataFiles).as("List partition with 0 file should return 0 DataFile").isEmpty(); | ||
| } | ||
|
|
||
| @Test | ||
| void testListPartitionMissingFilesFailure() { | ||
| String partitionUri = tempTableLocation.resolve("id=1").toUri().toString(); | ||
|
|
||
| assertThatThrownBy( | ||
| () -> | ||
| TableMigrationUtil.listPartition( | ||
| PARTITION, partitionUri, FORMAT, SPEC, CONF, MetricsConfig.getDefault(), null)) | ||
| .hasMessageContaining("Unable to list files in partition: " + partitionUri) | ||
| .isInstanceOf(RuntimeException.class) | ||
| .hasRootCauseInstanceOf(FileNotFoundException.class); | ||
| } | ||
|
|
||
| private static void writePartitionFile(File outputDir) throws IOException { | ||
| Iterable<GenericData.Record> records = RandomAvroData.generate(SCHEMA, 1000, 54310); | ||
| File testFile = File.createTempFile("junit", ".parquet", outputDir); | ||
| assertThat(testFile.delete()).as("Delete should succeed").isTrue(); | ||
|
|
||
| try (FileAppender<GenericData.Record> writer = | ||
| Parquet.write(Files.localOutput(testFile)) | ||
| .schema(SCHEMA) | ||
| .createWriterFunc(ParquetAvroWriter::buildWriter) | ||
| .build()) { | ||
| writer.addAll(records); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a big deal since this change set is small, but usually we only do a change in one Spark version at a time, review and then backport.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, I’ll follow this approach in the next PRs.