-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Allow table defaults to be configured and/ or enforced at catalog level using catalog properties. #4011
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 table defaults to be configured and/ or enforced at catalog level using catalog properties. #4011
Changes from 3 commits
add90e0
4d26bb3
7eb19c5
46711e0
5c70c4d
f9e1ee4
ed1a3a6
6af5b51
a2fbf59
9fe9215
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ | |
|
|
||
| package org.apache.iceberg; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.Map; | ||
| import org.apache.iceberg.catalog.Catalog; | ||
| import org.apache.iceberg.catalog.TableIdentifier; | ||
|
|
@@ -27,12 +28,19 @@ | |
| import org.apache.iceberg.exceptions.NoSuchTableException; | ||
| import org.apache.iceberg.relocated.com.google.common.base.MoreObjects; | ||
| import org.apache.iceberg.relocated.com.google.common.base.Preconditions; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Maps; | ||
| import org.apache.iceberg.util.PropertyUtil; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public abstract class BaseMetastoreCatalog implements Catalog { | ||
| private static final Logger LOG = LoggerFactory.getLogger(BaseMetastoreCatalog.class); | ||
| private Map<String, String> catalogProps = Collections.emptyMap(); | ||
|
|
||
| @Override | ||
| public void initialize(String name, Map<String, String> properties) { | ||
| catalogProps = properties; | ||
| } | ||
|
|
||
| @Override | ||
| public Table loadTable(TableIdentifier identifier) { | ||
|
|
@@ -106,7 +114,7 @@ public String toString() { | |
| protected class BaseMetastoreCatalogTableBuilder implements TableBuilder { | ||
| private final TableIdentifier identifier; | ||
| private final Schema schema; | ||
| private final ImmutableMap.Builder<String, String> propertiesBuilder = ImmutableMap.builder(); | ||
| private final Map<String, String> propertiesBuilder = Maps.newHashMap(); | ||
|
SinghAsDev marked this conversation as resolved.
Outdated
|
||
| private PartitionSpec spec = PartitionSpec.unpartitioned(); | ||
| private SortOrder sortOrder = SortOrder.unsorted(); | ||
| private String location = null; | ||
|
|
@@ -116,6 +124,7 @@ public BaseMetastoreCatalogTableBuilder(TableIdentifier identifier, Schema schem | |
|
|
||
| this.identifier = identifier; | ||
| this.schema = schema; | ||
| this.propertiesBuilder.putAll(tableDefaultProperties()); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -139,7 +148,7 @@ public TableBuilder withLocation(String newLocation) { | |
| @Override | ||
| public TableBuilder withProperties(Map<String, String> properties) { | ||
| if (properties != null) { | ||
| propertiesBuilder.putAll(properties); | ||
| this.propertiesBuilder.putAll(properties); | ||
| } | ||
| return this; | ||
| } | ||
|
|
@@ -158,8 +167,8 @@ public Table create() { | |
| } | ||
|
|
||
| String baseLocation = location != null ? location : defaultWarehouseLocation(identifier); | ||
| Map<String, String> properties = propertiesBuilder.build(); | ||
| TableMetadata metadata = TableMetadata.newTableMetadata(schema, spec, sortOrder, baseLocation, properties); | ||
| this.propertiesBuilder.putAll(tableOverrideProperties()); | ||
| TableMetadata metadata = TableMetadata.newTableMetadata(schema, spec, sortOrder, baseLocation, propertiesBuilder); | ||
|
|
||
| try { | ||
| ops.commit(null, metadata); | ||
|
|
@@ -178,8 +187,8 @@ public Transaction createTransaction() { | |
| } | ||
|
|
||
| String baseLocation = location != null ? location : defaultWarehouseLocation(identifier); | ||
| Map<String, String> properties = propertiesBuilder.build(); | ||
| TableMetadata metadata = TableMetadata.newTableMetadata(schema, spec, sortOrder, baseLocation, properties); | ||
| this.propertiesBuilder.putAll(tableOverrideProperties()); | ||
| TableMetadata metadata = TableMetadata.newTableMetadata(schema, spec, sortOrder, baseLocation, propertiesBuilder); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it now okay to pass |
||
| return Transactions.createTableTransaction(identifier.toString(), ops, metadata); | ||
| } | ||
|
|
||
|
|
@@ -200,12 +209,13 @@ private Transaction newReplaceTableTransaction(boolean orCreate) { | |
| } | ||
|
|
||
| TableMetadata metadata; | ||
| this.propertiesBuilder.putAll(tableOverrideProperties()); | ||
| if (ops.current() != null) { | ||
| String baseLocation = location != null ? location : ops.current().location(); | ||
| metadata = ops.current().buildReplacement(schema, spec, sortOrder, baseLocation, propertiesBuilder.build()); | ||
| metadata = ops.current().buildReplacement(schema, spec, sortOrder, baseLocation, propertiesBuilder); | ||
| } else { | ||
| String baseLocation = location != null ? location : defaultWarehouseLocation(identifier); | ||
| metadata = TableMetadata.newTableMetadata(schema, spec, sortOrder, baseLocation, propertiesBuilder.build()); | ||
| metadata = TableMetadata.newTableMetadata(schema, spec, sortOrder, baseLocation, propertiesBuilder); | ||
| } | ||
|
|
||
| if (orCreate) { | ||
|
|
@@ -214,6 +224,32 @@ private Transaction newReplaceTableTransaction(boolean orCreate) { | |
| return Transactions.replaceTableTransaction(identifier.toString(), ops, metadata); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get default table properties set at Catalog level through catalog properties. | ||
| * | ||
| * @return default table properties specified in catalog properties | ||
| */ | ||
| private Map<String, String> tableDefaultProperties() { | ||
| if (catalogProps == null || catalogProps.isEmpty()) { | ||
| return Collections.emptyMap(); | ||
| } | ||
|
|
||
| return PropertyUtil.propertiesWithPrefix(catalogProps, CatalogProperties.TABLE_DEFAULT_PREFIX); | ||
| } | ||
|
|
||
| /** | ||
| * Get table properties that are enforced at Catalog level through catalog properties. | ||
| * | ||
| * @return default table properties enforced through catalog properties | ||
| */ | ||
| private Map<String, String> tableOverrideProperties() { | ||
| if (catalogProps == null || catalogProps.isEmpty()) { | ||
| return Collections.emptyMap(); | ||
| } | ||
|
SinghAsDev marked this conversation as resolved.
Outdated
|
||
|
|
||
| return PropertyUtil.propertiesWithPrefix(catalogProps, CatalogProperties.TABLE_OVERRIDE_PREFIX); | ||
| } | ||
|
SinghAsDev marked this conversation as resolved.
|
||
| } | ||
|
|
||
| protected static String fullTableName(String catalogName, TableIdentifier identifier) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,6 +29,8 @@ private CatalogProperties() { | |
| public static final String CATALOG_IMPL = "catalog-impl"; | ||
| public static final String FILE_IO_IMPL = "io-impl"; | ||
| public static final String WAREHOUSE_LOCATION = "warehouse"; | ||
| public static final String TABLE_DEFAULT_PREFIX = "table-default."; | ||
| public static final String TABLE_OVERRIDE_PREFIX = "table-override."; | ||
|
Comment on lines
+32
to
+33
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit / open-question: How do people feel about these names? I'd kind of like them to have
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm good with this. |
||
|
|
||
| /** | ||
| * Controls whether the catalog will cache table entries upon load. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,9 @@ | |
|
|
||
| package org.apache.iceberg.util; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class PropertyUtil { | ||
|
|
||
|
|
@@ -70,4 +72,16 @@ public static String propertyAsString(Map<String, String> properties, | |
| } | ||
| return defaultValue; | ||
| } | ||
|
|
||
| public static Map<String, String> propertiesWithPrefix(Map<String, String> properties, | ||
|
rdblue marked this conversation as resolved.
Outdated
|
||
| String prefix) { | ||
|
SinghAsDev marked this conversation as resolved.
Outdated
|
||
| if (properties == null || properties.isEmpty()) { | ||
| return Collections.emptyMap(); | ||
|
SinghAsDev marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| return properties.entrySet().stream() | ||
| .filter(e -> e.getKey().startsWith(prefix)) | ||
| .collect(Collectors.toMap( | ||
| e -> e.getKey().replace(prefix, ""), Map.Entry::getValue)); | ||
|
SinghAsDev marked this conversation as resolved.
Outdated
|
||
| } | ||
|
Comment on lines
+77
to
+97
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We recently took this method in
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this can be removed. Thanks, @rajarshisarkar! |
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.