-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Static Table Operations #1342
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
Merged
Static Table Operations #1342
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
72 changes: 72 additions & 0 deletions
72
core/src/main/java/org/apache/iceberg/StaticTableOperations.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,72 @@ | ||
| /* | ||
| * 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 org.apache.iceberg.io.FileIO; | ||
| import org.apache.iceberg.io.LocationProvider; | ||
|
|
||
| /** | ||
| * TableOperations implementation that provides access to metadata for a Table at some point in time, using a | ||
| * table metadata location. It will never refer to a different Metadata object than the one it was created with | ||
| * and cannot be used to create or delete files. | ||
| */ | ||
| public class StaticTableOperations implements TableOperations { | ||
| private final TableMetadata staticMetadata; | ||
| private final FileIO io; | ||
|
|
||
| /** | ||
| * Creates a StaticTableOperations tied to a specific static version of the TableMetadata | ||
| */ | ||
| public StaticTableOperations(String metadataFileLocation, FileIO io) { | ||
| this.io = io; | ||
| this.staticMetadata = TableMetadataParser.read(io, metadataFileLocation); | ||
| } | ||
|
|
||
| @Override | ||
| public TableMetadata current() { | ||
| return staticMetadata; | ||
| } | ||
|
|
||
| @Override | ||
| public TableMetadata refresh() { | ||
| return staticMetadata; | ||
| } | ||
|
|
||
| @Override | ||
| public void commit(TableMetadata base, TableMetadata metadata) { | ||
| throw new UnsupportedOperationException("Cannot modify a static table"); | ||
| } | ||
|
|
||
| @Override | ||
| public FileIO io() { | ||
| return this.io; | ||
| } | ||
|
|
||
| @Override | ||
| public String metadataFileLocation(String fileName) { | ||
| throw new UnsupportedOperationException("Cannot modify a static table"); | ||
| } | ||
|
|
||
| @Override | ||
| public LocationProvider locationProvider() { | ||
| throw new UnsupportedOperationException("Cannot modify a static table"); | ||
| } | ||
| } |
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
101 changes: 101 additions & 0 deletions
101
core/src/test/java/org/apache/iceberg/hadoop/TestStaticTable.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,101 @@ | ||
| /* | ||
| * 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.hadoop; | ||
|
|
||
| import org.apache.iceberg.AssertHelpers; | ||
| import org.apache.iceberg.HasTableOperations; | ||
| import org.apache.iceberg.MetadataTableType; | ||
| import org.apache.iceberg.StaticTableOperations; | ||
| import org.apache.iceberg.Table; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Maps; | ||
| import org.junit.Assert; | ||
| import org.junit.Test; | ||
|
|
||
| public class TestStaticTable extends HadoopTableTestBase { | ||
|
|
||
| private Table getStaticTable() { | ||
| return TABLES.load(((HasTableOperations) table).operations().current().metadataFileLocation()); | ||
| } | ||
|
|
||
| private Table getStaticTable(MetadataTableType type) { | ||
| return TABLES.load(((HasTableOperations) table).operations().current().metadataFileLocation() + "#" + type); | ||
| } | ||
|
|
||
| @Test | ||
| public void testLoadFromMetadata() { | ||
| Table staticTable = getStaticTable(); | ||
| Assert.assertTrue("Loading a metadata file based table should return StaticTableOperations", | ||
| ((HasTableOperations) staticTable).operations() instanceof StaticTableOperations); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCannotBeAddedTo() { | ||
| Table staticTable = getStaticTable(); | ||
| AssertHelpers.assertThrows("Cannot modify a static table", UnsupportedOperationException.class, | ||
| () -> staticTable.newOverwrite().addFile(FILE_A).commit()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCannotBeDeletedFrom() { | ||
| table.newAppend().appendFile(FILE_A).commit(); | ||
| Table staticTable = getStaticTable(); | ||
| AssertHelpers.assertThrows("Cannot modify a static table", UnsupportedOperationException.class, | ||
| () -> staticTable.newDelete().deleteFile(FILE_A).commit()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testHasSameProperties() { | ||
| table.newAppend().appendFile(FILE_A).commit(); | ||
| table.newAppend().appendFile(FILE_B).commit(); | ||
| table.newOverwrite().deleteFile(FILE_B).addFile(FILE_C).commit(); | ||
| Table staticTable = getStaticTable(); | ||
| Assert.assertTrue("Same history?", | ||
| table.history().containsAll(staticTable.history())); | ||
| Assert.assertTrue("Same snapshot?", | ||
| table.currentSnapshot().snapshotId() == staticTable.currentSnapshot().snapshotId()); | ||
| Assert.assertTrue("Same properties?", | ||
| Maps.difference(table.properties(), staticTable.properties()).areEqual()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testImmutable() { | ||
| table.newAppend().appendFile(FILE_A).commit(); | ||
| Table staticTable = getStaticTable(); | ||
| long originalSnapshot = table.currentSnapshot().snapshotId(); | ||
|
|
||
| table.newAppend().appendFile(FILE_B).commit(); | ||
| table.newOverwrite().deleteFile(FILE_B).addFile(FILE_C).commit(); | ||
| staticTable.refresh(); | ||
|
|
||
| Assert.assertEquals("Snapshot unchanged after table modified", | ||
| staticTable.currentSnapshot().snapshotId(), originalSnapshot); | ||
RussellSpitzer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @Test | ||
| public void testMetadataTables() { | ||
| for (MetadataTableType type : MetadataTableType.values()) { | ||
| String enumName = type.name().replace("_", "").toLowerCase(); | ||
| Assert.assertTrue("Should be able to get MetadataTable of type : " + type, | ||
| getStaticTable(type).getClass().getName().toLowerCase().contains(enumName)); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| } | ||
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.
An alternative to this implementation that I considered was modifying findTable itself. The problem with this is that to do that implementation we need to extract the loadMetadataTable from below. I think this is a cleaner solution but we could do something where we make a StaticTables.java which findTables uses to either return a basetable or metadataTable
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.
I like this. It is more straightforward than the previous implementation.