-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Allow impl for LocationProvider for to be created during runtime #1531
Merged
rdblue
merged 26 commits into
apache:master
from
MickJermsurawong:mickjermsurawong/localized-provider
Oct 6, 2020
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
db7f3e5
new table properties
mickjermsurawong-stripe 4074a94
expose helper method to get class from partition field
mickjermsurawong-stripe 9412875
localized location provider
mickjermsurawong-stripe ad8b9c1
test dataframe writes
mickjermsurawong-stripe 9b5f96f
formatting
mickjermsurawong-stripe 51ced5f
format tests
mickjermsurawong-stripe c0abcf6
dynamically load location provider
mickjermsurawong-stripe bd2d8af
revert refactoring
mickjermsurawong-stripe a5eff74
remove extra line
mickjermsurawong-stripe 706cfcb
rename test
mickjermsurawong-stripe 2c51cd7
address feedback: test location provider
mickjermsurawong-stripe 34192da
revert old test
mickjermsurawong-stripe 77210d3
improve error message
mickjermsurawong-stripe 73fe85d
format test
mickjermsurawong-stripe 9b22a8c
propagate exception
mickjermsurawong-stripe c0c25a2
support no-arg and write more negative tests
mickjermsurawong-stripe 6f60d5f
use assert helper for future test extension
mickjermsurawong-stripe c14078a
order and update python props
mickjermsurawong-stripe a34b5d2
prefix with new prop with WRITE
mickjermsurawong-stripe 299ed00
update docs
mickjermsurawong-stripe 0eee288
add doc stiring
mickjermsurawong-stripe 5906be0
fix formatting
mickjermsurawong-stripe 5f9be8c
simplify constructor
mickjermsurawong-stripe 3aeb9e5
add doc to new property
mickjermsurawong-stripe 2729b79
revert manual html updates
mickjermsurawong-stripe 8c324c5
propagate cause
mickjermsurawong-stripe 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
This file contains 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 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
215 changes: 215 additions & 0 deletions
215
core/src/test/java/org/apache/iceberg/TestLocationProvider.java
This file contains 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,215 @@ | ||
/* | ||
* 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.util.Map; | ||
import org.apache.iceberg.io.LocationProvider; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
|
||
@RunWith(Parameterized.class) | ||
public class TestLocationProvider extends TableTestBase { | ||
@Parameterized.Parameters | ||
public static Object[][] parameters() { | ||
return new Object[][] { | ||
new Object[] { 1 }, | ||
new Object[] { 2 }, | ||
}; | ||
} | ||
|
||
public TestLocationProvider(int formatVersion) { | ||
super(formatVersion); | ||
} | ||
|
||
// publicly visible for testing to be dynamically loaded | ||
public static class TwoArgDynamicallyLoadedLocationProvider implements LocationProvider { | ||
String tableLocation; | ||
Map<String, String> properties; | ||
|
||
public TwoArgDynamicallyLoadedLocationProvider(String tableLocation, Map<String, String> properties) { | ||
this.tableLocation = tableLocation; | ||
this.properties = properties; | ||
} | ||
|
||
@Override | ||
public String newDataLocation(String filename) { | ||
return String.format("%s/test_custom_provider/%s", this.tableLocation, filename); | ||
} | ||
|
||
@Override | ||
public String newDataLocation(PartitionSpec spec, StructLike partitionData, String filename) { | ||
throw new RuntimeException("Test custom provider does not expect any invocation"); | ||
} | ||
} | ||
|
||
// publicly visible for testing to be dynamically loaded | ||
public static class NoArgDynamicallyLoadedLocationProvider implements LocationProvider { | ||
// No-arg public constructor | ||
|
||
@Override | ||
public String newDataLocation(String filename) { | ||
return String.format("test_no_arg_provider/%s", filename); | ||
} | ||
|
||
@Override | ||
public String newDataLocation(PartitionSpec spec, StructLike partitionData, String filename) { | ||
throw new RuntimeException("Test custom provider does not expect any invocation"); | ||
} | ||
} | ||
|
||
// publicly visible for testing to be dynamically loaded | ||
public static class InvalidArgTypesDynamicallyLoadedLocationProvider implements LocationProvider { | ||
|
||
public InvalidArgTypesDynamicallyLoadedLocationProvider(Integer bogusArg1, String bogusArg2) { | ||
} | ||
|
||
@Override | ||
public String newDataLocation(String filename) { | ||
throw new RuntimeException("Invalid provider should have not been instantiated!"); | ||
} | ||
|
||
@Override | ||
public String newDataLocation(PartitionSpec spec, StructLike partitionData, String filename) { | ||
throw new RuntimeException("Invalid provider should have not been instantiated!"); | ||
} | ||
} | ||
|
||
// publicly visible for testing to be dynamically loaded | ||
public static class InvalidNoInterfaceDynamicallyLoadedLocationProvider { | ||
// Default no-arg constructor is present, but does not impelemnt interface LocationProvider | ||
} | ||
|
||
@Test | ||
public void testDefaultLocationProvider() { | ||
this.table.updateProperties() | ||
.commit(); | ||
|
||
this.table.locationProvider().newDataLocation("my_file"); | ||
Assert.assertEquals( | ||
"Default data path should have table location as root", | ||
String.format("%s/data/%s", this.table.location(), "my_file"), | ||
this.table.locationProvider().newDataLocation("my_file") | ||
); | ||
} | ||
|
||
@Test | ||
public void testDefaultLocationProviderWithCustomDataLocation() { | ||
this.table.updateProperties() | ||
.set(TableProperties.WRITE_NEW_DATA_LOCATION, "new_location") | ||
.commit(); | ||
|
||
this.table.locationProvider().newDataLocation("my_file"); | ||
Assert.assertEquals( | ||
"Default location provider should allow custom path location", | ||
"new_location/my_file", | ||
this.table.locationProvider().newDataLocation("my_file") | ||
); | ||
} | ||
|
||
@Test | ||
public void testNoArgDynamicallyLoadedLocationProvider() { | ||
String invalidImpl = String.format("%s$%s", | ||
this.getClass().getCanonicalName(), | ||
NoArgDynamicallyLoadedLocationProvider.class.getSimpleName()); | ||
this.table.updateProperties() | ||
.set(TableProperties.WRITE_LOCATION_PROVIDER_IMPL, invalidImpl) | ||
.commit(); | ||
|
||
Assert.assertEquals( | ||
"Custom provider should take base table location", | ||
"test_no_arg_provider/my_file", | ||
this.table.locationProvider().newDataLocation("my_file") | ||
); | ||
} | ||
|
||
@Test | ||
public void testTwoArgDynamicallyLoadedLocationProvider() { | ||
this.table.updateProperties() | ||
.set(TableProperties.WRITE_LOCATION_PROVIDER_IMPL, | ||
String.format("%s$%s", | ||
this.getClass().getCanonicalName(), | ||
TwoArgDynamicallyLoadedLocationProvider.class.getSimpleName())) | ||
.commit(); | ||
|
||
Assert.assertTrue(String.format("Table should load impl defined in its properties"), | ||
this.table.locationProvider() instanceof TwoArgDynamicallyLoadedLocationProvider | ||
); | ||
|
||
Assert.assertEquals( | ||
"Custom provider should take base table location", | ||
String.format("%s/test_custom_provider/%s", this.table.location(), "my_file"), | ||
this.table.locationProvider().newDataLocation("my_file") | ||
); | ||
} | ||
|
||
@Test | ||
public void testDynamicallyLoadedLocationProviderNotFound() { | ||
String nonExistentImpl = String.format("%s$NonExistent%s", | ||
this.getClass().getCanonicalName(), | ||
TwoArgDynamicallyLoadedLocationProvider.class.getSimpleName()); | ||
this.table.updateProperties() | ||
.set(TableProperties.WRITE_LOCATION_PROVIDER_IMPL, nonExistentImpl) | ||
.commit(); | ||
|
||
AssertHelpers.assertThrows("Non-existent implementation should fail on finding constructor", | ||
IllegalArgumentException.class, | ||
String.format("Unable to find a constructor for implementation %s of %s. ", | ||
nonExistentImpl, LocationProvider.class), | ||
() -> table.locationProvider() | ||
); | ||
} | ||
|
||
@Test | ||
public void testInvalidNoInterfaceDynamicallyLoadedLocationProvider() { | ||
String invalidImpl = String.format("%s$%s", | ||
this.getClass().getCanonicalName(), | ||
InvalidNoInterfaceDynamicallyLoadedLocationProvider.class.getSimpleName()); | ||
this.table.updateProperties() | ||
.set(TableProperties.WRITE_LOCATION_PROVIDER_IMPL, invalidImpl) | ||
.commit(); | ||
|
||
AssertHelpers.assertThrows( | ||
"Class with missing interface implementation should fail on instantiation.", | ||
IllegalArgumentException.class, | ||
String.format("Provided implementation for dynamic instantiation should implement %s", | ||
LocationProvider.class), | ||
() -> table.locationProvider() | ||
); | ||
} | ||
|
||
@Test | ||
public void testInvalidArgTypesDynamicallyLoadedLocationProvider() { | ||
String invalidImpl = String.format("%s$%s", | ||
this.getClass().getCanonicalName(), | ||
InvalidArgTypesDynamicallyLoadedLocationProvider.class.getSimpleName()); | ||
this.table.updateProperties() | ||
.set(TableProperties.WRITE_LOCATION_PROVIDER_IMPL, invalidImpl) | ||
.commit(); | ||
|
||
AssertHelpers.assertThrows("Implementation with invalid arg types should fail on finding constructor", | ||
IllegalArgumentException.class, | ||
String.format("Unable to find a constructor for implementation %s of %s. ", | ||
invalidImpl, LocationProvider.class), | ||
() -> table.locationProvider() | ||
); | ||
} | ||
} |
This file contains 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 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
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.
If provided class has the specified constructor signature, but not implementing
LocationProvider
, it can still be loaded into the dynConstructor. The following block differentiates this missing interface more explicitly.Might be a bit more code churn, but it does give more signals to users.