Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#412] feat(core): Generic property system support (#441)

mchadesda9482d0fd28
diff --git a/catalogs/catalog-hive/src/main/java/com/datastrato/graviton/catalog/hive/HiveCatalogOperations.java b/catalogs/catalog-hive/src/main/java/com/datastrato/graviton/catalog/hive/HiveCatalogOperations.java
index de9c5cfff8..9023f2a855 100644
--- a/catalogs/catalog-hive/src/main/java/com/datastrato/graviton/catalog/hive/HiveCatalogOperations.java
+++ b/catalogs/catalog-hive/src/main/java/com/datastrato/graviton/catalog/hive/HiveCatalogOperations.java
@@ -10,6 +10,7 @@ import static com.datastrato.graviton.catalog.hive.HiveTable.SUPPORT_TABLE_TYPES
import com.datastrato.graviton.NameIdentifier;
import com.datastrato.graviton.Namespace;
import com.datastrato.graviton.catalog.CatalogOperations;
+import com.datastrato.graviton.catalog.PropertiesMetadata;
import com.datastrato.graviton.catalog.hive.converter.ToHiveType;
import com.datastrato.graviton.exceptions.NoSuchCatalogException;
import com.datastrato.graviton.exceptions.NoSuchSchemaException;
@@ -63,6 +64,8 @@ public class HiveCatalogOperations implements CatalogOperations, SupportsSchemas
private final CatalogEntity entity;
+ private HiveTablePropertiesMetadata tablePropertiesMetadata;
+
/**
* Constructs a new instance of HiveCatalogOperations.
*
@@ -86,6 +89,8 @@ public class HiveCatalogOperations implements CatalogOperations, SupportsSchemas
// todo(xun): add hive client pool size in config
this.clientPool = new HiveClientPool(1, hiveConf);
+
+ this.tablePropertiesMetadata = new HiveTablePropertiesMetadata();
}
/** Closes the Hive catalog and releases the associated client pool. */
@@ -732,4 +737,9 @@ public class HiveCatalogOperations implements CatalogOperations, SupportsSchemas
return System.getProperty("user.name");
}
}
+
+ @Override
+ public PropertiesMetadata tablePropertiesMetadata() throws UnsupportedOperationException {
+ return tablePropertiesMetadata;
+ }
}
diff --git a/catalogs/catalog-hive/src/main/java/com/datastrato/graviton/catalog/hive/HiveTablePropertiesMetadata.java b/catalogs/catalog-hive/src/main/java/com/datastrato/graviton/catalog/hive/HiveTablePropertiesMetadata.java
new file mode 100644
index 0000000000..175e7cc17d
--- /dev/null
+++ b/catalogs/catalog-hive/src/main/java/com/datastrato/graviton/catalog/hive/HiveTablePropertiesMetadata.java
@@ -0,0 +1,18 @@
+/*
+ * Copyright 2023 Datastrato.
+ * This software is licensed under the Apache License version 2.
+ */
+package com.datastrato.graviton.catalog.hive;
+
+import com.datastrato.graviton.catalog.PropertyEntry;
+import com.datastrato.graviton.catalog.TablePropertiesMetadata;
+import com.google.common.collect.Maps;
+import java.util.Map;
+
+public class HiveTablePropertiesMetadata extends TablePropertiesMetadata {
+ @Override
+ protected Map<String, PropertyEntry<?>> tablePropertyEntries() {
+ // TODO(Minghuang): support Hive table property specs
+ return Maps.newHashMap();
+ }
+}
diff --git a/catalogs/catalog-lakehouse-iceberg/src/main/java/com/datastrato/graviton/catalog/lakehouse/iceberg/IcebergCatalogOperations.java b/catalogs/catalog-lakehouse-iceberg/src/main/java/com/datastrato/graviton/catalog/lakehouse/iceberg/IcebergCatalogOperations.java
index cda5ceed85..1f8bb89b8b 100644
--- a/catalogs/catalog-lakehouse-iceberg/src/main/java/com/datastrato/graviton/catalog/lakehouse/iceberg/IcebergCatalogOperations.java
+++ b/catalogs/catalog-lakehouse-iceberg/src/main/java/com/datastrato/graviton/catalog/lakehouse/iceberg/IcebergCatalogOperations.java
@@ -7,6 +7,7 @@ package com.datastrato.graviton.catalog.lakehouse.iceberg;
import com.datastrato.graviton.NameIdentifier;
import com.datastrato.graviton.Namespace;
import com.datastrato.graviton.catalog.CatalogOperations;
+import com.datastrato.graviton.catalog.PropertiesMetadata;
import com.datastrato.graviton.catalog.lakehouse.iceberg.ops.IcebergTableOps;
import com.datastrato.graviton.catalog.lakehouse.iceberg.ops.IcebergTableOpsHelper;
import com.datastrato.graviton.exceptions.NoSuchCatalogException;
@@ -48,6 +49,8 @@ public class IcebergCatalogOperations implements CatalogOperations, SupportsSche
@VisibleForTesting IcebergTableOps icebergTableOps;
+ private IcebergTablePropertiesMetadata tablePropertiesMetadata;
+
private final CatalogEntity entity;
/**
@@ -69,6 +72,7 @@ public class IcebergCatalogOperations implements CatalogOperations, SupportsSche
public void initialize(Map<String, String> conf) throws RuntimeException {
// Todo initialize iceberg ops
this.icebergTableOps = new IcebergTableOps();
+ this.tablePropertiesMetadata = new IcebergTablePropertiesMetadata();
}
/** Closes the Iceberg catalog and releases the associated client pool. */
@@ -375,4 +379,9 @@ public class IcebergCatalogOperations implements CatalogOperations, SupportsSche
private static String currentUser() {
return System.getProperty("user.name");
}
+
+ @Override
+ public PropertiesMetadata tablePropertiesMetadata() throws UnsupportedOperationException {
+ return tablePropertiesMetadata;
+ }
}
diff --git a/catalogs/catalog-lakehouse-iceberg/src/main/java/com/datastrato/graviton/catalog/lakehouse/iceberg/IcebergTablePropertiesMetadata.java b/catalogs/catalog-lakehouse-iceberg/src/main/java/com/datastrato/graviton/catalog/lakehouse/iceberg/IcebergTablePropertiesMetadata.java
new file mode 100644
index 0000000000..b86eaee6ec
--- /dev/null
+++ b/catalogs/catalog-lakehouse-iceberg/src/main/java/com/datastrato/graviton/catalog/lakehouse/iceberg/IcebergTablePropertiesMetadata.java
@@ -0,0 +1,18 @@
+/*
+ * Copyright 2023 Datastrato.
+ * This software is licensed under the Apache License version 2.
+ */
+package com.datastrato.graviton.catalog.lakehouse.iceberg;
+
+import com.datastrato.graviton.catalog.PropertyEntry;
+import com.datastrato.graviton.catalog.TablePropertiesMetadata;
+import com.google.common.collect.Maps;
+import java.util.Map;
+
+public class IcebergTablePropertiesMetadata extends TablePropertiesMetadata {
+ @Override
+ protected Map<String, PropertyEntry<?>> tablePropertyEntries() {
+ // TODO: support Iceberg table property specs
+ return Maps.newHashMap();
+ }
+}
diff --git a/core/src/main/java/com/datastrato/graviton/catalog/BaseCatalog.java b/core/src/main/java/com/datastrato/graviton/catalog/BaseCatalog.java
index cd4275a72d..024972f1d6 100644
--- a/core/src/main/java/com/datastrato/graviton/catalog/BaseCatalog.java
+++ b/core/src/main/java/com/datastrato/graviton/catalog/BaseCatalog.java
@@ -23,7 +23,8 @@ import java.util.Map;
*
* @param <T> The type of the concrete subclass of BaseCatalog.
*/
-public abstract class BaseCatalog<T extends BaseCatalog> implements Catalog, CatalogProvider {
+public abstract class BaseCatalog<T extends BaseCatalog>
+ implements Catalog, CatalogProvider, HasPropertyMetadata {
private CatalogEntity entity;
@@ -39,6 +40,11 @@ public abstract class BaseCatalog<T extends BaseCatalog> implements Catalog, Cat
*/
protected abstract CatalogOperations newOps(Map<String, String> config);
+ @Override
+ public PropertiesMetadata tablePropertiesMetadata() throws UnsupportedOperationException {
+ return ops().tablePropertiesMetadata();
+ }
+
/**
* Retrieves the CatalogOperations instance associated with this catalog. Lazily initializes the
* instance if not already created.
diff --git a/core/src/main/java/com/datastrato/graviton/catalog/CatalogManager.java b/core/src/main/java/com/datastrato/graviton/catalog/CatalogManager.java
index 8c64475af3..672798bed2 100644
--- a/core/src/main/java/com/datastrato/graviton/catalog/CatalogManager.java
+++ b/core/src/main/java/com/datastrato/graviton/catalog/CatalogManager.java
@@ -87,6 +87,11 @@ public class CatalogManager implements SupportsCatalogs, Closeable {
});
}
+ public <R> R doWithPropertiesMeta(ThrowableFunction<HasPropertyMetadata, R> fn)
+ throws Exception {
+ return classLoader.withClassLoader(cl -> fn.apply(catalog.ops()));
+ }
+
public void close() {
try {
classLoader.withClassLoader(
diff --git a/core/src/main/java/com/datastrato/graviton/catalog/CatalogOperationDispatcher.java b/core/src/main/java/com/datastrato/graviton/catalog/CatalogOperationDispatcher.java
index 4dfe727f9d..ec21cb7c5a 100644
--- a/core/src/main/java/com/datastrato/graviton/catalog/CatalogOperationDispatcher.java
+++ b/core/src/main/java/com/datastrato/graviton/catalog/CatalogOperationDispatcher.java
@@ -38,10 +38,12 @@ import com.datastrato.graviton.rel.transforms.Transform;
import com.datastrato.graviton.storage.IdGenerator;
import com.datastrato.graviton.utils.ThrowableFunction;
import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
import java.time.Instant;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
+import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.slf4j.Logger;
@@ -279,16 +281,18 @@ public class CatalogOperationDispatcher implements TableCatalog, SupportsSchemas
*/
@Override
public Table loadTable(NameIdentifier ident) throws NoSuchTableException {
+ NameIdentifier catalogIdentifier = getCatalogIdentifier(ident);
Table table =
doWithCatalog(
- getCatalogIdentifier(ident),
+ catalogIdentifier,
c -> c.doWithTableOps(t -> t.loadTable(ident)),
NoSuchTableException.class);
StringIdentifier stringId = getStringIdFromProperties(table.properties());
// Case 1: The table is not created by Graviton.
if (stringId == null) {
- return EntityCombinedTable.of(table);
+ return EntityCombinedTable.of(table, null)
+ .withHiddenPropertiesSet(getHiddenPropertyNames(catalogIdentifier, table.properties()));
}
TableEntity tableEntity =
@@ -297,7 +301,24 @@ public class CatalogOperationDispatcher implements TableCatalog, SupportsSchemas
identifier -> store.get(identifier, TABLE, TableEntity.class),
"GET",
stringId.id());
- return EntityCombinedTable.of(table, tableEntity);
+
+ return EntityCombinedTable.of(table, tableEntity)
+ .withHiddenPropertiesSet(getHiddenPropertyNames(catalogIdentifier, table.properties()));
+ }
+
+ private Set<String> getHiddenPropertyNames(
+ NameIdentifier catalogIdent, Map<String, String> properties) {
+ return doWithCatalog(
+ catalogIdent,
+ c ->
+ c.doWithPropertiesMeta(
+ p -> {
+ PropertiesMetadata propertiesMetadata = p.tablePropertiesMetadata();
+ return properties.keySet().stream()
+ .filter(propertiesMetadata::isHiddenProperty)
+ .collect(Collectors.toSet());
+ }),
+ IllegalArgumentException.class);
}
/**
@@ -322,6 +343,15 @@ public class CatalogOperationDispatcher implements TableCatalog, SupportsSchemas
Distribution distribution,
SortOrder[] sortOrders)
throws NoSuchSchemaException, TableAlreadyExistsException {
+ doWithCatalog(
+ getCatalogIdentifier(ident),
+ c ->
+ c.doWithPropertiesMeta(
+ p -> {
+ validateCreateTableProperties(p.tablePropertiesMetadata(), properties);
+ return null;
+ }),
+ IllegalArgumentException.class);
long uid = idGenerator.nextId();
// Add StringIdentifier to the properties, the specific catalog will handle this
// StringIdentifier to make sure only when the operation is successful, the related
@@ -362,10 +392,45 @@ public class CatalogOperationDispatcher implements TableCatalog, SupportsSchemas
store.put(tableEntity, true /* overwrite */);
} catch (Exception e) {
LOG.error(FormattedErrorMessages.STORE_OP_FAILURE, "put", ident, e);
- return EntityCombinedTable.of(table);
+ return EntityCombinedTable.of(table, null)
+ .withHiddenPropertiesSet(
+ getHiddenPropertyNames(getCatalogIdentifier(ident), table.properties()));
}
- return EntityCombinedTable.of(table, tableEntity);
+ return EntityCombinedTable.of(table, tableEntity)
+ .withHiddenPropertiesSet(
+ getHiddenPropertyNames(getCatalogIdentifier(ident), table.properties()));
+ }
+
+ private void validateCreateTableProperties(
+ PropertiesMetadata tablePropertiesMetadata, Map<String, String> properties) {
+ if (properties == null) {
+ return;
+ }
+
+ List<String> reservedProperties =
+ properties.keySet().stream()
+ .filter(tablePropertiesMetadata::isReservedProperty)
+ .collect(Collectors.toList());
+ Preconditions.checkArgument(
+ reservedProperties.isEmpty(),
+ "Properties are reserved and cannot be set: %s",
+ reservedProperties);
+
+ List<String> absentProperties =
+ tablePropertiesMetadata.propertyEntries().keySet().stream()
+ .filter(tablePropertiesMetadata::isRequiredProperty)
+ .filter(k -> !properties.containsKey(k))
+ .collect(Collectors.toList());
+ Preconditions.checkArgument(
+ absentProperties.isEmpty(),
+ "Properties are required and must be set: %s",
+ absentProperties);
+
+ // use decode function to validate the property values
+ properties.keySet().stream()
+ .filter(tablePropertiesMetadata::containsProperty)
+ .forEach(k -> tablePropertiesMetadata.propertyEntries().get(k).decode(properties.get(k)));
}
/**
@@ -381,6 +446,7 @@ public class CatalogOperationDispatcher implements TableCatalog, SupportsSchemas
@Override
public Table alterTable(NameIdentifier ident, TableChange... changes)
throws NoSuchTableException, IllegalArgumentException {
+ validateAlterTableProperties(ident, changes);
Table alteredTable =
doWithCatalog(
getCatalogIdentifier(ident),
@@ -391,7 +457,9 @@ public class CatalogOperationDispatcher implements TableCatalog, SupportsSchemas
StringIdentifier stringId = getStringIdFromProperties(alteredTable.properties());
// Case 1: The table is not created by Graviton.
if (stringId == null) {
- return EntityCombinedTable.of(alteredTable);
+ return EntityCombinedTable.of(alteredTable, null)
+ .withHiddenPropertiesSet(
+ getHiddenPropertyNames(getCatalogIdentifier(ident), alteredTable.properties()));
}
TableEntity updatedTableEntity =
@@ -429,7 +497,42 @@ public class CatalogOperationDispatcher implements TableCatalog, SupportsSchemas
"UPDATE",
stringId.id());
- return EntityCombinedTable.of(alteredTable, updatedTableEntity);
+ return EntityCombinedTable.of(alteredTable, updatedTableEntity)
+ .withHiddenPropertiesSet(
+ getHiddenPropertyNames(getCatalogIdentifier(ident), alteredTable.properties()));
+ }
+
+ private void validateAlterTableProperties(NameIdentifier ident, TableChange... changes) {
+ doWithCatalog(
+ getCatalogIdentifier(ident),
+ c ->
+ c.doWithPropertiesMeta(
+ p -> {
+ PropertiesMetadata tablePropertiesMetadata = p.tablePropertiesMetadata();
+ for (TableChange change : changes) {
+ if (change instanceof TableChange.SetProperty) {
+ String propertyName = ((TableChange.SetProperty) change).getProperty();
+ if (tablePropertiesMetadata.isReservedProperty(propertyName)
+ || tablePropertiesMetadata.isImmutableProperty(propertyName)) {
+ throw new IllegalArgumentException(
+ String.format(
+ "Property %s is reserved or immutable and cannot be set",
+ propertyName));
+ }
+ }
+
+ if (change instanceof TableChange.RemoveProperty) {
+ String propertyName = ((TableChange.RemoveProperty) change).getProperty();
+ if (tablePropertiesMetadata.isReservedProperty(propertyName)
+ || tablePropertiesMetadata.isImmutableProperty(propertyName)) {
+ throw new IllegalArgumentException(
+ String.format("Property %s cannot be removed by user", propertyName));
+ }
+ }
+ }
+ return null;
+ }),
+ IllegalArgumentException.class);
}
/**
diff --git a/core/src/main/java/com/datastrato/graviton/catalog/CatalogOperations.java b/core/src/main/java/com/datastrato/graviton/catalog/CatalogOperations.java
index f07d627cfd..280ba3251d 100644
--- a/core/src/main/java/com/datastrato/graviton/catalog/CatalogOperations.java
+++ b/core/src/main/java/com/datastrato/graviton/catalog/CatalogOperations.java
@@ -14,7 +14,7 @@ import java.util.Map;
* should be mixed with other Catalog interface like {@link SupportsSchemas} to provide schema
* operation, {@link TableCatalog} to support table operations, etc.
*/
-public interface CatalogOperations extends Closeable {
+public interface CatalogOperations extends Closeable, HasPropertyMetadata {
/**
* Initialize the CatalogOperation with specified configuration. This method is called after
diff --git a/core/src/main/java/com/datastrato/graviton/catalog/HasPropertyMetadata.java b/core/src/main/java/com/datastrato/graviton/catalog/HasPropertyMetadata.java
new file mode 100644
index 0000000000..ce9dbd41fd
--- /dev/null
+++ b/core/src/main/java/com/datastrato/graviton/catalog/HasPropertyMetadata.java
@@ -0,0 +1,10 @@
+/*
+ * Copyright 2023 Datastrato.
+ * This software is licensed under the Apache License version 2.
+ */
+package com.datastrato.graviton.catalog;
+
+/** This interface represents entities that have properties metadata. */
+public interface HasPropertyMetadata {
+ PropertiesMetadata tablePropertiesMetadata() throws UnsupportedOperationException;
+}
diff --git a/core/src/main/java/com/datastrato/graviton/catalog/PropertiesMetadata.java b/core/src/main/java/com/datastrato/graviton/catalog/PropertiesMetadata.java
new file mode 100644
index 0000000000..e8ccfdddba
--- /dev/null
+++ b/core/src/main/java/com/datastrato/graviton/catalog/PropertiesMetadata.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2023 Datastrato.
+ * This software is licensed under the Apache License version 2.
+ */
+package com.datastrato.graviton.catalog;
+
+import java.util.Map;
+
+/** The PropertiesMetadata class is responsible for managing property metadata. */
+public interface PropertiesMetadata {
+ Map<String, PropertyEntry<?>> propertyEntries();
+
+ default boolean isReservedProperty(String propertyName) {
+ return propertyEntries().containsKey(propertyName)
+ && propertyEntries().get(propertyName).isReserved();
+ }
+
+ default boolean isRequiredProperty(String propertyName) {
+ return propertyEntries().containsKey(propertyName)
+ && propertyEntries().get(propertyName).isRequired();
+ }
+
+ default boolean isImmutableProperty(String propertyName) {
+ return propertyEntries().containsKey(propertyName)
+ && propertyEntries().get(propertyName).isImmutable();
+ }
+
+ default boolean isHiddenProperty(String propertyName) {
+ return propertyEntries().containsKey(propertyName)
+ && propertyEntries().get(propertyName).isHidden();
+ }
+
+ default boolean containsProperty(String propertyName) {
+ return propertyEntries().containsKey(propertyName);
+ }
+}
diff --git a/core/src/main/java/com/datastrato/graviton/catalog/PropertyEntry.java b/core/src/main/java/com/datastrato/graviton/catalog/PropertyEntry.java
new file mode 100644
index 0000000000..c382fe9198
--- /dev/null
+++ b/core/src/main/java/com/datastrato/graviton/catalog/PropertyEntry.java
@@ -0,0 +1,196 @@
+/*
+ * Copyright 2023 Datastrato.
+ * This software is licensed under the Apache License version 2.
+ */
+package com.datastrato.graviton.catalog;
+
+import com.google.common.base.Preconditions;
+import java.util.function.Function;
+import lombok.Getter;
+import org.apache.logging.log4j.util.Strings;
+
+@Getter
+public final class PropertyEntry<T> {
+ private final String name;
+ private final String description;
+ private final boolean required;
+ private final boolean immutable;
+ private final Class<T> javaType;
+ private final T defaultValue;
+ private final Function<String, T> decoder;
+ private final Function<T, String> encoder;
+ private final boolean hidden;
+ private final boolean reserved;
+
+ /**
+ * @param name The name of the property
+ * @param description Describe the purpose of this property
+ * @param required Whether this property is required. If true, the property must be set when
+ * creating a table
+ * @param immutable Whether this property is immutable. If true, the property cannot be changed by
+ * user after the table is created
+ * @param javaType The java type of the property
+ * @param defaultValue Non-required property can have a default value
+ * @param decoder Decode the string value to the java type
+ * @param encoder Encode the java type to the string value
+ * @param hidden Whether this property is hidden from user, such as password
+ * @param reserved This property is reserved and cannot be set by user
+ */
+ private PropertyEntry(
+ String name,
+ String description,
+ boolean required,
+ boolean immutable,
+ Class<T> javaType,
+ T defaultValue,
+ Function<String, T> decoder,
+ Function<T, String> encoder,
+ boolean hidden,
+ boolean reserved) {
+ Preconditions.checkArgument(Strings.isNotBlank(name), "name cannot be null or empty");
+ Preconditions.checkArgument(
+ Strings.isNotBlank(description), "description cannot be null or empty");
+ Preconditions.checkArgument(javaType != null, "javaType cannot be null");
+ Preconditions.checkArgument(decoder != null, "decoder cannot be null");
+ Preconditions.checkArgument(encoder != null, "encoder cannot be null");
+
+ Preconditions.checkArgument(
+ !required || defaultValue == null, "defaultValue cannot be set for required property");
+ Preconditions.checkArgument(!required || !reserved, "required property cannot be reserved");
+ Preconditions.checkArgument(!reserved || immutable, "reserved property must be immutable");
+
+ this.name = name;
+ this.description = description;
+ this.required = required;
+ this.immutable = immutable;
+ this.javaType = javaType;
+ this.defaultValue = defaultValue;
+ this.decoder = decoder;
+ this.encoder = encoder;
+ this.hidden = hidden;
+ this.reserved = reserved;
+ }
+
+ public static class Builder<T> {
+ private String name;
+ private String description;
+ private boolean required;
+ private boolean immutable;
+ private Class<T> javaType;
+ private T defaultValue;
+ private Function<String, T> decoder;
+ private Function<T, String> encoder;
+ private boolean hidden;
+ private boolean reserved;
+
+ public Builder<T> withName(String name) {
+ this.name = name;
+ return this;
+ }
+
+ public Builder<T> withDescription(String description) {
+ this.description = description;
+ return this;
+ }
+
+ public Builder<T> withRequired(boolean required) {
+ this.required = required;
+ return this;
+ }
+
+ public Builder<T> withImmutable(boolean immutable) {
+ this.immutable = immutable;
+ return this;
+ }
+
+ public Builder<T> withJavaType(Class<T> javaType) {
+ this.javaType = javaType;
+ return this;
+ }
+
+ public Builder<T> withDefaultValue(T defaultValue) {
+ this.defaultValue = defaultValue;
+ return this;
+ }
+
+ public Builder<T> withDecoder(Function<String, T> decoder) {
+ this.decoder = decoder;
+ return this;
+ }
+
+ public Builder<T> withEncoder(Function<T, String> encoder) {
+ this.encoder = encoder;
+ return this;
+ }
+
+ public Builder<T> withHidden(boolean hidden) {
+ this.hidden = hidden;
+ return this;
+ }
+
+ public Builder<T> withReserved(boolean reserved) {
+ this.reserved = reserved;
+ return this;
+ }
+
+ public PropertyEntry<T> build() {
+ return new PropertyEntry<T>(
+ name,
+ description,
+ required,
+ immutable,
+ javaType,
+ defaultValue,
+ decoder,
+ encoder,
+ hidden,
+ reserved);
+ }
+ }
+
+ public T decode(String value) {
+ return decoder.apply(value);
+ }
+
+ public static PropertyEntry<String> stringPropertyEntry(
+ String name,
+ String description,
+ boolean required,
+ boolean immutable,
+ String defaultValue,
+ boolean hidden,
+ boolean reserved) {
+ return new Builder<String>()
+ .withName(name)
+ .withDescription(description)
+ .withRequired(required)
+ .withImmutable(immutable)
+ .withJavaType(String.class)
+ .withDefaultValue(defaultValue)
+ .withDecoder(Function.identity())
+ .withEncoder(Function.identity())
+ .withHidden(hidden)
+ .withReserved(reserved)
+ .build();
+ }
+
+ public static PropertyEntry<String> stringReservedPropertyEntry(
+ String name, String description, boolean hidden) {
+ return stringPropertyEntry(name, description, false, true, null, hidden, true);
+ }
+
+ public static PropertyEntry<String> stringRequiredPropertyEntry(
+ String name, String description, boolean immutable, boolean hidden) {
+ return stringPropertyEntry(name, description, true, immutable, null, hidden, false);
+ }
+
+ public static PropertyEntry<String> stringImmutablePropertyEntry(
+ String name,
+ String description,
+ boolean required,
+ String defaultValue,
+ boolean hidden,
+ boolean reserved) {
+ return stringPropertyEntry(name, description, required, true, defaultValue, hidden, reserved);
+ }
+}
diff --git a/core/src/main/java/com/datastrato/graviton/catalog/TablePropertiesMetadata.java b/core/src/main/java/com/datastrato/graviton/catalog/TablePropertiesMetadata.java
new file mode 100644
index 0000000000..a86af23f59
--- /dev/null
+++ b/core/src/main/java/com/datastrato/graviton/catalog/TablePropertiesMetadata.java
@@ -0,0 +1,59 @@
+/*
+ * Copyright 2023 Datastrato.
+ * This software is licensed under the Apache License version 2.
+ */
+package com.datastrato.graviton.catalog;
+
+import static com.datastrato.graviton.StringIdentifier.ID_KEY;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Maps;
+import java.util.List;
+import java.util.Map;
+
+public abstract class TablePropertiesMetadata implements PropertiesMetadata {
+
+ private static final Map<String, PropertyEntry<?>> BASIC_TABLE_PROPERTY_ENTRIES;
+
+ private Map<String, PropertyEntry<?>> propertyEntries;
+
+ static {
+ List<PropertyEntry<?>> basicTablePropertyEntries =
+ ImmutableList.of(
+ PropertyEntry.stringReservedPropertyEntry(
+ ID_KEY,
+ "To differentiate the entities created directly by the underlying sources",
+ true));
+
+ BASIC_TABLE_PROPERTY_ENTRIES =
+ Maps.uniqueIndex(basicTablePropertyEntries, PropertyEntry::getName);
+ }
+
+ @Override
+ public Map<String, PropertyEntry<?>> propertyEntries() {
+ if (propertyEntries == null) {
+ synchronized (this) {
+ if (propertyEntries == null) {
+ ImmutableMap.Builder<String, PropertyEntry<?>> builder = ImmutableMap.builder();
+ Map<String, PropertyEntry<?>> catalogTableProperty = tablePropertyEntries();
+ builder.putAll(catalogTableProperty);
+
+ BASIC_TABLE_PROPERTY_ENTRIES.forEach(
+ (name, entry) -> {
+ Preconditions.checkArgument(
+ !catalogTableProperty.containsKey(name),
+ "Property metadata already exists: " + name);
+ builder.put(name, entry);
+ });
+
+ propertyEntries = builder.build();
+ }
+ }
+ }
+ return propertyEntries;
+ }
+
+ protected abstract Map<String, PropertyEntry<?>> tablePropertyEntries();
+}
diff --git a/core/src/main/java/com/datastrato/graviton/catalog/rel/EntityCombinedTable.java b/core/src/main/java/com/datastrato/graviton/catalog/rel/EntityCombinedTable.java
index f188d7c1ab..eac74a203b 100644
--- a/core/src/main/java/com/datastrato/graviton/catalog/rel/EntityCombinedTable.java
+++ b/core/src/main/java/com/datastrato/graviton/catalog/rel/EntityCombinedTable.java
@@ -13,6 +13,8 @@ import com.datastrato.graviton.rel.SortOrder;
import com.datastrato.graviton.rel.Table;
import com.datastrato.graviton.rel.transforms.Transform;
import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
/**
* A Table class to represent a table metadata object that combines the metadata both from {@link
@@ -24,6 +26,9 @@ public final class EntityCombinedTable implements Table {
private final TableEntity tableEntity;
+ // Sets of properties that should be hidden from the user.
+ private Set<String> hiddenProperties;
+
private EntityCombinedTable(Table table, TableEntity tableEntity) {
this.table = table;
this.tableEntity = tableEntity;
@@ -33,8 +38,9 @@ public final class EntityCombinedTable implements Table {
return new EntityCombinedTable(table, tableEntity);
}
- public static EntityCombinedTable of(Table table) {
- return of(table, null);
+ public EntityCombinedTable withHiddenPropertiesSet(Set<String> hiddenProperties) {
+ this.hiddenProperties = hiddenProperties;
+ return this;
}
@Override
@@ -54,7 +60,9 @@ public final class EntityCombinedTable implements Table {
@Override
public Map<String, String> properties() {
- return table.properties();
+ return table.properties().entrySet().stream()
+ .filter(p -> !hiddenProperties.contains(p.getKey()))
+ .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
@Override
diff --git a/core/src/test/java/com/datastrato/graviton/TestCatalogOperations.java b/core/src/test/java/com/datastrato/graviton/TestCatalogOperations.java
index de5be13b94..674b508efc 100644
--- a/core/src/test/java/com/datastrato/graviton/TestCatalogOperations.java
+++ b/core/src/test/java/com/datastrato/graviton/TestCatalogOperations.java
@@ -5,6 +5,8 @@
package com.datastrato.graviton;
import com.datastrato.graviton.catalog.CatalogOperations;
+import com.datastrato.graviton.catalog.PropertiesMetadata;
+import com.datastrato.graviton.catalog.TablePropertiesMetadata;
import com.datastrato.graviton.exceptions.NoSuchCatalogException;
import com.datastrato.graviton.exceptions.NoSuchSchemaException;
import com.datastrato.graviton.exceptions.NoSuchTableException;
@@ -25,6 +27,7 @@ import com.datastrato.graviton.rel.transforms.Transform;
import com.google.common.collect.Maps;
import java.io.IOException;
import java.time.Instant;
+import java.util.HashMap;
import java.util.Map;
public class TestCatalogOperations implements CatalogOperations, TableCatalog, SupportsSchemas {
@@ -33,9 +36,12 @@ public class TestCatalogOperations implements CatalogOperations, TableCatalog, S
private final Map<NameIdentifier, TestSchema> schemas;
+ private final TablePropertiesMetadata tablePropertiesMetadata;
+
public TestCatalogOperations() {
tables = Maps.newHashMap();
schemas = Maps.newHashMap();
+ tablePropertiesMetadata = new TestTablePropertiesMetadata();
}
@Override
@@ -77,7 +83,7 @@ public class TestCatalogOperations implements CatalogOperations, TableCatalog, S
new TestTable.Builder()
.withName(ident.name())
.withComment(comment)
- .withProperties(properties)
+ .withProperties(new HashMap<>(properties))
.withAuditInfo(auditInfo)
.withColumns(columns)
.withDistribution(distribution)
@@ -91,7 +97,16 @@ public class TestCatalogOperations implements CatalogOperations, TableCatalog, S
tables.put(ident, table);
}
- return table;
+ return new TestTable.Builder()
+ .withName(ident.name())
+ .withComment(comment)
+ .withProperties(new HashMap<>(properties))
+ .withAuditInfo(auditInfo)
+ .withColumns(columns)
+ .withDistribution(distribution)
+ .withSortOrders(sortOrders)
+ .withPartitions(partitions)
+ .build();
}
@Override
@@ -129,14 +144,21 @@ public class TestCatalogOperations implements CatalogOperations, TableCatalog, S
new TestTable.Builder()
.withName(ident.name())
.withComment(table.comment())
- .withProperties(newProps)
+ .withProperties(new HashMap<>(newProps))
.withAuditInfo(updatedAuditInfo)
.withColumns(table.columns())
.withPartitions(table.partitioning())
.build();
tables.put(ident, updatedTable);
- return updatedTable;
+ return new TestTable.Builder()
+ .withName(ident.name())
+ .withComment(table.comment())
+ .withProperties(new HashMap<>(newProps))
+ .withAuditInfo(updatedAuditInfo)
+ .withColumns(table.columns())
+ .withPartitions(table.partitioning())
+ .build();
}
@Override
@@ -246,4 +268,9 @@ public class TestCatalogOperations implements CatalogOperations, TableCatalog, S
return true;
}
+
+ @Override
+ public PropertiesMetadata tablePropertiesMetadata() throws UnsupportedOperationException {
+ return tablePropertiesMetadata;
+ }
}
diff --git a/core/src/test/java/com/datastrato/graviton/TestTablePropertiesMetadata.java b/core/src/test/java/com/datastrato/graviton/TestTablePropertiesMetadata.java
new file mode 100644
index 0000000000..4be0f42f1a
--- /dev/null
+++ b/core/src/test/java/com/datastrato/graviton/TestTablePropertiesMetadata.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2023 Datastrato.
+ * This software is licensed under the Apache License version 2.
+ */
+package com.datastrato.graviton;
+
+import com.datastrato.graviton.catalog.PropertyEntry;
+import com.datastrato.graviton.catalog.TablePropertiesMetadata;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Maps;
+import java.util.List;
+import java.util.Map;
+
+public class TestTablePropertiesMetadata extends TablePropertiesMetadata {
+
+ public static final String COMMENT_KEY = "comment";
+
+ public static final String TEST_REQUIRED_KEY = "k1";
+
+ public static final String TEST_IMMUTABLE_KEY = "immutableKey";
+
+ private static final Map<String, PropertyEntry<?>> TEST_TABLE_PROPERTY;
+
+ static {
+ List<PropertyEntry<?>> tablePropertyMetadata =
+ ImmutableList.of(
+ PropertyEntry.stringRequiredPropertyEntry(
+ TEST_REQUIRED_KEY, "test required k1 property", false, false),
+ PropertyEntry.stringReservedPropertyEntry(COMMENT_KEY, "table comment", true),
+ PropertyEntry.stringImmutablePropertyEntry(
+ TEST_IMMUTABLE_KEY, "test immutable property", false, null, false, false));
+
+ TEST_TABLE_PROPERTY = Maps.uniqueIndex(tablePropertyMetadata, PropertyEntry::getName);
+ }
+
+ @Override
+ protected Map<String, PropertyEntry<?>> tablePropertyEntries() {
+ return TEST_TABLE_PROPERTY;
+ }
+}
diff --git a/core/src/test/java/com/datastrato/graviton/catalog/TestCatalogOperationDispatcher.java b/core/src/test/java/com/datastrato/graviton/catalog/TestCatalogOperationDispatcher.java
index 9524d9a68a..e281631acd 100644
--- a/core/src/test/java/com/datastrato/graviton/catalog/TestCatalogOperationDispatcher.java
+++ b/core/src/test/java/com/datastrato/graviton/catalog/TestCatalogOperationDispatcher.java
@@ -6,6 +6,8 @@ package com.datastrato.graviton.catalog;
import static com.datastrato.graviton.Entity.EntityType.SCHEMA;
import static com.datastrato.graviton.Entity.EntityType.TABLE;
+import static com.datastrato.graviton.StringIdentifier.ID_KEY;
+import static com.datastrato.graviton.TestTablePropertiesMetadata.COMMENT_KEY;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
@@ -44,6 +46,7 @@ import io.substrait.type.TypeCreator;
import java.io.IOException;
import java.time.Instant;
import java.util.Arrays;
+import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import org.junit.jupiter.api.AfterAll;
@@ -124,7 +127,7 @@ public class TestCatalogOperationDispatcher {
// Check if the created Schema's field values are correct
Assertions.assertEquals("schema1", schema.name());
Assertions.assertEquals("comment", schema.comment());
- testProperties(props, schema.properties());
+ testProperties(props, schema.properties(), true);
// Check if the Schema entity is stored in the EntityStore
SchemaEntity schemaEntity = entityStore.get(schemaIdent, SCHEMA, SchemaEntity.class);
@@ -148,7 +151,7 @@ public class TestCatalogOperationDispatcher {
// Check if the created Schema's field values are correct
Assertions.assertEquals("schema2", schema2.name());
Assertions.assertEquals("comment", schema2.comment());
- testProperties(props, schema2.properties());
+ testProperties(props, schema2.properties(), true);
// Check if the Schema entity is stored in the EntityStore
Assertions.assertFalse(entityStore.exists(schemaIdent2, SCHEMA));
@@ -169,7 +172,7 @@ public class TestCatalogOperationDispatcher {
Schema loadedSchema = dispatcher.loadSchema(schemaIdent);
Assertions.assertEquals(schema.name(), loadedSchema.name());
Assertions.assertEquals(schema.comment(), loadedSchema.comment());
- testProperties(schema.properties(), loadedSchema.properties());
+ testProperties(schema.properties(), loadedSchema.properties(), true);
// Audit info is gotten from entity store
Assertions.assertEquals("graviton", loadedSchema.auditInfo().creator());
@@ -220,7 +223,7 @@ public class TestCatalogOperationDispatcher {
Assertions.assertEquals(schema.name(), alteredSchema.name());
Assertions.assertEquals(schema.comment(), alteredSchema.comment());
Map<String, String> expectedProps = ImmutableMap.of("k2", "v2", "k3", "v3");
- testProperties(expectedProps, alteredSchema.properties());
+ testProperties(expectedProps, alteredSchema.properties(), true);
// Audit info is gotten from graviton entity store.
Assertions.assertEquals("graviton", alteredSchema.auditInfo().creator());
Assertions.assertEquals("graviton", alteredSchema.auditInfo().lastModifier());
@@ -232,7 +235,7 @@ public class TestCatalogOperationDispatcher {
Schema alteredSchema1 = dispatcher.alterSchema(schemaIdent, changes);
Assertions.assertEquals(schema.name(), alteredSchema1.name());
Assertions.assertEquals(schema.comment(), alteredSchema1.comment());
- testProperties(expectedProps, alteredSchema1.properties());
+ testProperties(expectedProps, alteredSchema1.properties(), true);
// Audit info is gotten from catalog, not from the entity store
Assertions.assertEquals("test", alteredSchema1.auditInfo().creator());
Assertions.assertEquals("test", alteredSchema1.auditInfo().lastModifier());
@@ -243,7 +246,7 @@ public class TestCatalogOperationDispatcher {
Schema alteredSchema2 = dispatcher.alterSchema(schemaIdent, changes);
Assertions.assertEquals(schema.name(), alteredSchema2.name());
Assertions.assertEquals(schema.comment(), alteredSchema2.comment());
- testProperties(expectedProps, alteredSchema2.properties());
+ testProperties(expectedProps, alteredSchema2.properties(), true);
// Audit info is gotten from catalog, not from the entity store
Assertions.assertEquals("test", alteredSchema2.auditInfo().creator());
Assertions.assertEquals("test", alteredSchema1.auditInfo().lastModifier());
@@ -265,7 +268,7 @@ public class TestCatalogOperationDispatcher {
Schema alteredSchema3 = dispatcher.alterSchema(schemaIdent, changes);
Assertions.assertEquals(schema.name(), alteredSchema3.name());
Assertions.assertEquals(schema.comment(), alteredSchema3.comment());
- testProperties(expectedProps, alteredSchema3.properties());
+ testProperties(expectedProps, alteredSchema3.properties(), true);
// Audit info is gotten from catalog, not from the entity store
Assertions.assertEquals("test", alteredSchema3.auditInfo().creator());
Assertions.assertEquals("test", alteredSchema1.auditInfo().lastModifier());
@@ -303,17 +306,46 @@ public class TestCatalogOperationDispatcher {
Table table1 = dispatcher.createTable(tableIdent1, columns, "comment", props, new Transform[0]);
Assertions.assertEquals("table1", table1.name());
Assertions.assertEquals("comment", table1.comment());
- testProperties(props, table1.properties());
+ testProperties(props, table1.properties(), false);
Assertions.assertEquals(0, table1.partitioning().length);
Assertions.assertArrayEquals(columns, table1.columns());
+ // Test required table properties exception
+ Map<String, String> illegalTableProperties =
+ new HashMap<String, String>() {
+ {
+ put("k2", "v2");
+ }
+ };
+
+ IllegalArgumentException exception =
+ assertThrows(
+ IllegalArgumentException.class,
+ () ->
+ dispatcher.createTable(
+ tableIdent1, columns, "comment", illegalTableProperties, new Transform[0]));
+ Assertions.assertTrue(
+ exception.getMessage().contains("Properties are required and must be set"));
+
+ // Test reserved table properties exception
+ illegalTableProperties.put(COMMENT_KEY, "table comment");
+ illegalTableProperties.put(ID_KEY, "graviton.v1.uidfdsafdsa");
+ IllegalArgumentException exception1 =
+ assertThrows(
+ IllegalArgumentException.class,
+ () ->
+ dispatcher.createTable(
+ tableIdent1, columns, "comment", illegalTableProperties, new Transform[0]));
+ Assertions.assertEquals(
+ "Properties are reserved and cannot be set: [comment, graviton.identifier]",
+ exception1.getMessage());
+
// Check if the Table entity is stored in the EntityStore
TableEntity tableEntity = entityStore.get(tableIdent1, TABLE, TableEntity.class);
Assertions.assertNotNull(tableEntity);
Assertions.assertEquals("table1", tableEntity.name());
- StringIdentifier stringId = StringIdentifier.fromProperties(table1.properties());
- Assertions.assertEquals(stringId.id(), tableEntity.id());
+ Assertions.assertFalse(table1.properties().containsKey(ID_KEY));
Optional<NameIdentifier> ident1 =
Arrays.stream(dispatcher.listTables(tableNs))
@@ -329,7 +361,7 @@ public class TestCatalogOperationDispatcher {
// Check if the created Schema's field values are correct
Assertions.assertEquals("table2", table2.name());
Assertions.assertEquals("comment", table2.comment());
- testProperties(props, table2.properties());
+ testProperties(props, table2.properties(), false);
// Check if the Table entity is stored in the EntityStore
Assertions.assertFalse(entityStore.exists(tableIdent2, TABLE));
@@ -357,7 +389,7 @@ public class TestCatalogOperationDispatcher {
Table loadedTable1 = dispatcher.loadTable(tableIdent1);
Assertions.assertEquals(table1.name(), loadedTable1.name());
Assertions.assertEquals(table1.comment(), loadedTable1.comment());
- testProperties(table1.properties(), loadedTable1.properties());
+ testProperties(table1.properties(), loadedTable1.properties(), false);
Assertions.assertEquals(0, loadedTable1.partitioning().length);
Assertions.assertArrayEquals(table1.columns(), loadedTable1.columns());
// Audit info is gotten from the entity store
@@ -411,6 +443,16 @@ public class TestCatalogOperationDispatcher {
Table table = dispatcher.createTable(tableIdent, columns, "comment", props, new Transform[0]);
+ // Test immutable table properties
+ TableChange[] illegalChange1 =
+ new TableChange[] {TableChange.setProperty(COMMENT_KEY, "new comment")};
+ IllegalArgumentException exception =
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> dispatcher.alterTable(tableIdent, illegalChange1));
+ Assertions.assertEquals(
+ "Property comment is reserved or immutable and cannot be set", exception.getMessage());
+
TableChange[] changes =
new TableChange[] {TableChange.setProperty("k3", "v3"), TableChange.removeProperty("k1")};
@@ -418,7 +460,7 @@ public class TestCatalogOperationDispatcher {
Assertions.assertEquals(table.name(), alteredTable.name());
Assertions.assertEquals(table.comment(), alteredTable.comment());
Map<String, String> expectedProps = ImmutableMap.of("k2", "v2", "k3", "v3");
- testProperties(expectedProps, alteredTable.properties());
+ testProperties(expectedProps, alteredTable.properties(), false);
// Audit info is gotten from graviton entity store
Assertions.assertEquals("graviton", alteredTable.auditInfo().creator());
Assertions.assertEquals("graviton", alteredTable.auditInfo().lastModifier());
@@ -500,14 +542,20 @@ public class TestCatalogOperationDispatcher {
assertEquals(dispatcher.getCatalogIdentifier(id5), NameIdentifier.of("a", "b"));
}
- private void testProperties(Map<String, String> expectedProps, Map<String, String> testProps) {
+ private void testProperties(
+ Map<String, String> expectedProps, Map<String, String> testProps, boolean validateIDKey) {
expectedProps.forEach(
(k, v) -> {
Assertions.assertEquals(v, testProps.get(k));
});
- Assertions.assertTrue(testProps.containsKey(StringIdentifier.ID_KEY));
- StringIdentifier StringId = StringIdentifier.fromString(testProps.get(StringIdentifier.ID_KEY));
- Assertions.assertEquals(StringId.toString(), testProps.get(StringIdentifier.ID_KEY));
+ if (validateIDKey) {
+ Assertions.assertTrue(testProps.containsKey(StringIdentifier.ID_KEY));
+ StringIdentifier StringId =
+ StringIdentifier.fromString(testProps.get(StringIdentifier.ID_KEY));
+ Assertions.assertEquals(StringId.toString(), testProps.get(StringIdentifier.ID_KEY));
+ } else {
+ Assertions.assertFalse(testProps.containsKey(StringIdentifier.ID_KEY));
+ }
}
}
diff --git a/server/src/test/java/com/datastrato/graviton/server/web/rest/TestCatalog.java b/server/src/test/java/com/datastrato/graviton/server/web/rest/TestCatalog.java
index 3e088f1ad2..4381270a50 100644
--- a/server/src/test/java/com/datastrato/graviton/server/web/rest/TestCatalog.java
+++ b/server/src/test/java/com/datastrato/graviton/server/web/rest/TestCatalog.java
@@ -6,6 +6,7 @@ package com.datastrato.graviton.server.web.rest;
import com.datastrato.graviton.catalog.BaseCatalog;
import com.datastrato.graviton.catalog.CatalogOperations;
+import com.datastrato.graviton.catalog.PropertiesMetadata;
import java.io.IOException;
import java.util.Map;
@@ -18,6 +19,11 @@ public class TestCatalog extends BaseCatalog<TestCatalog> {
@Override
protected CatalogOperations newOps(Map config) {
return new CatalogOperations() {
+ @Override
+ public PropertiesMetadata tablePropertiesMetadata() throws UnsupportedOperationException {
+ throw new UnsupportedOperationException();
+ }
+
@Override
public void initialize(Map<String, String> config) throws RuntimeException {}