Skip to content

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

[#443] feat(hive): support hive table properties (#472)

mchades119c8e087a4e
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 3c0c3724ca..2d32f07d8b 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
@@ -4,8 +4,9 @@
*/
package com.datastrato.graviton.catalog.hive;
-import static com.datastrato.graviton.catalog.hive.HiveTable.HMS_TABLE_COMMENT;
import static com.datastrato.graviton.catalog.hive.HiveTable.SUPPORT_TABLE_TYPES;
+import static com.datastrato.graviton.catalog.hive.HiveTablePropertiesMetadata.COMMENT;
+import static com.datastrato.graviton.catalog.hive.HiveTablePropertiesMetadata.TABLE_TYPE;
import com.datastrato.graviton.NameIdentifier;
import com.datastrato.graviton.Namespace;
@@ -42,6 +43,7 @@ import java.util.stream.Collectors;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.metastore.TableType;
import org.apache.hadoop.hive.metastore.api.AlreadyExistsException;
import org.apache.hadoop.hive.metastore.api.Database;
import org.apache.hadoop.hive.metastore.api.FieldSchema;
@@ -434,6 +436,11 @@ public class HiveCatalogOperations implements CatalogOperations, SupportsSchemas
"Hive partition only supports identity transform");
validateDistributionAndSort(distribution, sortOrders);
+ TableType tableType = (TableType) tablePropertiesMetadata.getOrDefault(properties, TABLE_TYPE);
+ Preconditions.checkArgument(
+ SUPPORT_TABLE_TYPES.contains(tableType.name()),
+ "Unsupported table type: " + tableType.name());
+
try {
if (!schemaExists(schemaIdent)) {
LOG.warn("Hive schema (database) does not exist: {}", schemaIdent);
@@ -458,7 +465,7 @@ public class HiveCatalogOperations implements CatalogOperations, SupportsSchemas
.build();
clientPool.run(
c -> {
- c.createTable(hiveTable.toHiveTable());
+ c.createTable(hiveTable.toHiveTable(tablePropertiesMetadata));
return null;
});
@@ -492,7 +499,8 @@ public class HiveCatalogOperations implements CatalogOperations, SupportsSchemas
try {
// TODO(@Minghuang): require a table lock to avoid race condition
HiveTable table = (HiveTable) loadTable(tableIdent);
- org.apache.hadoop.hive.metastore.api.Table alteredHiveTable = table.toHiveTable();
+ org.apache.hadoop.hive.metastore.api.Table alteredHiveTable =
+ table.toHiveTable(tablePropertiesMetadata);
for (TableChange change : changes) {
// Table change
@@ -595,7 +603,7 @@ public class HiveCatalogOperations implements CatalogOperations, SupportsSchemas
private void doUpdateComment(
org.apache.hadoop.hive.metastore.api.Table hiveTable, TableChange.UpdateComment change) {
Map<String, String> parameters = hiveTable.getParameters();
- parameters.put(HMS_TABLE_COMMENT, change.getNewComment());
+ parameters.put(COMMENT, change.getNewComment());
}
private void doSetProperty(
diff --git a/catalogs/catalog-hive/src/main/java/com/datastrato/graviton/catalog/hive/HiveTable.java b/catalogs/catalog-hive/src/main/java/com/datastrato/graviton/catalog/hive/HiveTable.java
index 2391762029..fca92b3657 100644
--- a/catalogs/catalog-hive/src/main/java/com/datastrato/graviton/catalog/hive/HiveTable.java
+++ b/catalogs/catalog-hive/src/main/java/com/datastrato/graviton/catalog/hive/HiveTable.java
@@ -4,6 +4,16 @@
*/
package com.datastrato.graviton.catalog.hive;
+import static com.datastrato.graviton.catalog.hive.HiveTablePropertiesMetadata.COMMENT;
+import static com.datastrato.graviton.catalog.hive.HiveTablePropertiesMetadata.EXTERNAL;
+import static com.datastrato.graviton.catalog.hive.HiveTablePropertiesMetadata.FORMAT;
+import static com.datastrato.graviton.catalog.hive.HiveTablePropertiesMetadata.INPUT_FORMAT;
+import static com.datastrato.graviton.catalog.hive.HiveTablePropertiesMetadata.LOCATION;
+import static com.datastrato.graviton.catalog.hive.HiveTablePropertiesMetadata.OUTPUT_FORMAT;
+import static com.datastrato.graviton.catalog.hive.HiveTablePropertiesMetadata.SERDE_LIB;
+import static com.datastrato.graviton.catalog.hive.HiveTablePropertiesMetadata.SERDE_NAME;
+import static com.datastrato.graviton.catalog.hive.HiveTablePropertiesMetadata.SERDE_PARAMETER_PREFIX;
+import static com.datastrato.graviton.catalog.hive.HiveTablePropertiesMetadata.TABLE_TYPE;
import static com.datastrato.graviton.rel.transforms.Transforms.identity;
import static org.apache.hadoop.hive.metastore.TableType.EXTERNAL_TABLE;
import static org.apache.hadoop.hive.metastore.TableType.MANAGED_TABLE;
@@ -25,12 +35,14 @@ import com.google.common.collect.Sets;
import java.time.Instant;
import java.util.Arrays;
import java.util.List;
+import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.ToString;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
+import org.apache.hadoop.hive.metastore.TableType;
import org.apache.hadoop.hive.metastore.api.FieldSchema;
import org.apache.hadoop.hive.metastore.api.Order;
import org.apache.hadoop.hive.metastore.api.SerDeInfo;
@@ -44,12 +56,6 @@ public class HiveTable extends BaseTable {
// A set of supported Hive table types.
public static final Set<String> SUPPORT_TABLE_TYPES =
Sets.newHashSet(MANAGED_TABLE.name(), EXTERNAL_TABLE.name());
-
- // The key to store the Hive table comment in the parameters map.
- public static final String HMS_TABLE_COMMENT = "comment";
-
- private String location;
-
private String schemaName;
private HiveTable() {}
@@ -78,6 +84,8 @@ public class HiveTable extends BaseTable {
.map(f -> new NamedReference(new String[] {f}))
.toArray(Transform[]::new))
.withNumber(table.getSd().getNumBuckets())
+ // Hive table use hash strategy as bucketing strategy
+ .withStrategy(Distribution.Strategy.HASH)
.build();
}
@@ -96,8 +104,8 @@ public class HiveTable extends BaseTable {
return new HiveTable.Builder()
.withName(table.getTableName())
- .withComment(table.getParameters().get(HMS_TABLE_COMMENT))
- .withProperties(table.getParameters())
+ .withComment(table.getParameters().get(COMMENT))
+ .withProperties(buildTableProperties(table))
.withColumns(
table.getSd().getCols().stream()
.map(
@@ -108,7 +116,6 @@ public class HiveTable extends BaseTable {
.withComment(f.getComment())
.build())
.toArray(HiveColumn[]::new))
- .withLocation(table.getSd().getLocation())
.withDistribution(distribution)
.withSortOrders(sortOrders)
.withAuditInfo(auditInfoBuilder.build())
@@ -120,18 +127,40 @@ public class HiveTable extends BaseTable {
.build();
}
+ private static Map<String, String> buildTableProperties(Table table) {
+ Map<String, String> properties = Maps.newHashMap(table.getParameters());
+
+ Optional.ofNullable(table.getTableType()).ifPresent(t -> properties.put(TABLE_TYPE, t));
+
+ StorageDescriptor sd = table.getSd();
+ properties.put(LOCATION, sd.getLocation());
+ properties.put(INPUT_FORMAT, sd.getInputFormat());
+ properties.put(OUTPUT_FORMAT, sd.getOutputFormat());
+
+ SerDeInfo serdeInfo = sd.getSerdeInfo();
+ Optional.ofNullable(serdeInfo.getName()).ifPresent(name -> properties.put(SERDE_NAME, name));
+ Optional.ofNullable(serdeInfo.getSerializationLib())
+ .ifPresent(lib -> properties.put(SERDE_LIB, lib));
+ Optional.ofNullable(serdeInfo.getParameters())
+ .ifPresent(p -> p.forEach((k, v) -> properties.put(SERDE_PARAMETER_PREFIX + k, v)));
+
+ return properties;
+ }
+
/**
* Converts this HiveTable to its corresponding Table in the Hive Metastore.
*
* @return The converted Table.
*/
- public Table toHiveTable() {
+ public Table toHiveTable(HiveTablePropertiesMetadata tablePropertiesMetadata) {
Table hiveTable = new Table();
hiveTable.setTableName(name);
hiveTable.setDbName(schemaName);
- hiveTable.setSd(buildStorageDescriptor());
- hiveTable.setParameters(properties);
+ hiveTable.setTableType(
+ ((TableType) tablePropertiesMetadata.getOrDefault(properties(), TABLE_TYPE)).name());
+ hiveTable.setSd(buildStorageDescriptor(tablePropertiesMetadata));
+ hiveTable.setParameters(buildTableParameters());
hiveTable.setPartitionKeys(buildPartitionKeys());
// Set AuditInfo to Hive's Table object. Hive's Table doesn't support setting last modifier
@@ -142,6 +171,25 @@ public class HiveTable extends BaseTable {
return hiveTable;
}
+ private Map<String, String> buildTableParameters() {
+ Map<String, String> parameters = Maps.newHashMap(properties());
+ parameters.put(COMMENT, comment);
+ String ignore =
+ EXTERNAL_TABLE.name().equalsIgnoreCase(properties().get(TABLE_TYPE))
+ ? parameters.put(EXTERNAL, "TRUE")
+ : parameters.put(EXTERNAL, "FALSE");
+
+ parameters.remove(LOCATION);
+ parameters.remove(TABLE_TYPE);
+ parameters.remove(INPUT_FORMAT);
+ parameters.remove(OUTPUT_FORMAT);
+ parameters.remove(SERDE_NAME);
+ parameters.remove(SERDE_LIB);
+ parameters.remove(FORMAT);
+ parameters.keySet().removeIf(k -> k.startsWith(SERDE_PARAMETER_PREFIX));
+ return parameters;
+ }
+
private List<FieldSchema> buildPartitionKeys() {
return Arrays.stream(partitions)
.map(p -> getPartitionKey(((Transforms.NamedReference) p).value()))
@@ -163,7 +211,8 @@ public class HiveTable extends BaseTable {
partitionColumns.get(0).comment());
}
- private StorageDescriptor buildStorageDescriptor() {
+ private StorageDescriptor buildStorageDescriptor(
+ HiveTablePropertiesMetadata tablePropertiesMetadata) {
StorageDescriptor sd = new StorageDescriptor();
sd.setCols(
Arrays.stream(columns)
@@ -174,14 +223,22 @@ public class HiveTable extends BaseTable {
c.dataType().accept(ToHiveType.INSTANCE).getQualifiedName(),
c.comment()))
.collect(Collectors.toList()));
- sd.setSerdeInfo(buildSerDeInfo());
+
// `location` must not be null, otherwise it will result in an NPE when calling HMS `alterTable`
// interface
- sd.setLocation(location);
- // TODO(minghuang): Remove hard coding below after supporting the specified Hive table
- // properties
- sd.setInputFormat("org.apache.hadoop.mapred.TextInputFormat");
- sd.setOutputFormat("org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat");
+ Optional.ofNullable(properties().get(LOCATION))
+ .ifPresent(l -> sd.setLocation(properties().get(LOCATION)));
+
+ sd.setSerdeInfo(buildSerDeInfo(tablePropertiesMetadata));
+ HiveTablePropertiesMetadata.StorageFormat storageFormat =
+ (HiveTablePropertiesMetadata.StorageFormat)
+ tablePropertiesMetadata.getOrDefault(properties(), FORMAT);
+ sd.setInputFormat(storageFormat.getInputFormat());
+ sd.setOutputFormat(storageFormat.getOutputFormat());
+ // Individually specified INPUT_FORMAT and OUTPUT_FORMAT can override the inputFormat and
+ // outputFormat of FORMAT
+ Optional.ofNullable(properties().get(INPUT_FORMAT)).ifPresent(sd::setInputFormat);
+ Optional.ofNullable(properties().get(OUTPUT_FORMAT)).ifPresent(sd::setOutputFormat);
if (ArrayUtils.isNotEmpty(sortOrders)) {
for (SortOrder sortOrder : sortOrders) {
@@ -201,33 +258,31 @@ public class HiveTable extends BaseTable {
return sd;
}
- private SerDeInfo buildSerDeInfo() {
- // TODO(minghuang): Build SerDeInfo object based on user specifications.
+ private SerDeInfo buildSerDeInfo(HiveTablePropertiesMetadata tablePropertiesMetadata) {
SerDeInfo serDeInfo = new SerDeInfo();
- serDeInfo.setSerializationLib("org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe");
+ serDeInfo.setName(properties().getOrDefault(SERDE_NAME, name()));
+
+ HiveTablePropertiesMetadata.StorageFormat storageFormat =
+ (HiveTablePropertiesMetadata.StorageFormat)
+ tablePropertiesMetadata.getOrDefault(properties(), FORMAT);
+ serDeInfo.setSerializationLib(storageFormat.getSerde());
+ // Individually specified SERDE_LIB can override the serdeLib of FORMAT
+ Optional.ofNullable(properties().get(SERDE_LIB)).ifPresent(serDeInfo::setSerializationLib);
+
+ properties().entrySet().stream()
+ .filter(e -> e.getKey().startsWith(SERDE_PARAMETER_PREFIX))
+ .forEach(
+ e ->
+ serDeInfo.putToParameters(
+ e.getKey().substring(SERDE_PARAMETER_PREFIX.length()), e.getValue()));
return serDeInfo;
}
/** A builder class for constructing HiveTable instances. */
public static class Builder extends BaseTableBuilder<Builder, HiveTable> {
- // currently, load from HMS only
- // TODO(minghuang): Support user specify`location` property
- private String location;
-
private String schemaName;
- /**
- * Sets the location to be used for building the HiveTable.
- *
- * @param location The string location of the HiveTable.
- * @return This Builder instance.
- */
- public Builder withLocation(String location) {
- this.location = location;
- return this;
- }
-
/**
* Sets the Hive schema (database) name to be used for building the HiveTable.
*
@@ -252,7 +307,6 @@ public class HiveTable extends BaseTable {
hiveTable.properties = properties != null ? Maps.newHashMap(properties) : Maps.newHashMap();
hiveTable.auditInfo = auditInfo;
hiveTable.columns = columns;
- hiveTable.location = location;
hiveTable.distribution = distribution;
hiveTable.sortOrders = sortOrders;
hiveTable.partitions = partitions;
@@ -260,7 +314,7 @@ public class HiveTable extends BaseTable {
// HMS put table comment in parameters
if (comment != null) {
- hiveTable.properties.put(HMS_TABLE_COMMENT, comment);
+ hiveTable.properties.put(COMMENT, comment);
}
return hiveTable;
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
index 9bba53bc3f..b37c6c368f 100644
--- 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
@@ -4,15 +4,180 @@
*/
package com.datastrato.graviton.catalog.hive;
+import static com.datastrato.graviton.catalog.PropertyEntry.booleanReservedPropertyEntry;
+import static com.datastrato.graviton.catalog.PropertyEntry.enumImmutablePropertyEntry;
+import static com.datastrato.graviton.catalog.PropertyEntry.stringImmutablePropertyEntry;
+import static com.datastrato.graviton.catalog.PropertyEntry.stringReservedPropertyEntry;
+import static com.datastrato.graviton.catalog.hive.HiveTablePropertiesMetadata.StorageFormat.TEXTFILE;
+import static org.apache.hadoop.hive.metastore.TableType.MANAGED_TABLE;
+
import com.datastrato.graviton.catalog.BasePropertiesMetadata;
import com.datastrato.graviton.catalog.PropertyEntry;
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps;
+import java.util.List;
import java.util.Map;
+import org.apache.hadoop.hive.metastore.TableType;
public class HiveTablePropertiesMetadata extends BasePropertiesMetadata {
+ public static final String COMMENT = "comment";
+ public static final String NUM_FILES = "numFiles";
+ public static final String TOTAL_SIZE = "totalSize";
+ public static final String EXTERNAL = "EXTERNAL";
+ public static final String LOCATION = "location";
+ public static final String FORMAT = "format";
+ public static final String TABLE_TYPE = "tableType";
+ public static final String INPUT_FORMAT = "inputFormat";
+ public static final String OUTPUT_FORMAT = "outputFormat";
+ public static final String SERDE_NAME = "serdeName";
+ public static final String SERDE_LIB = "serdeLib";
+ public static final String SERDE_PARAMETER_PREFIX = "serde.parameter.";
+ public static final String TRANSIENT_LAST_DDL_TIME = "transient_lastDdlTime";
+
+ public static final String TEXT_INPUT_FORMAT_CLASS = "org.apache.hadoop.mapred.TextInputFormat";
+ public static final String IGNORE_KEY_OUTPUT_FORMAT_CLASS =
+ "org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat";
+ public static final String LAZY_SIMPLE_SERDE_CLASS =
+ "org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe";
+
+ private static final String SEQUENCEFILE_INPUT_FORMAT_CLASS =
+ "org.apache.hadoop.mapred.SequenceFileInputFormat";
+ private static final String SEQUENCEFILE_OUTPUT_FORMAT_CLASS =
+ "org.apache.hadoop.hive.ql.io.HiveSequenceFileOutputFormat";
+
+ @VisibleForTesting
+ public static final String ORC_INPUT_FORMAT_CLASS =
+ "org.apache.hadoop.hive.ql.io.orc.OrcInputFormat";
+
+ @VisibleForTesting
+ public static final String ORC_OUTPUT_FORMAT_CLASS =
+ "org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat";
+
+ @VisibleForTesting
+ public static final String ORC_SERDE_CLASS = "org.apache.hadoop.hive.ql.io.orc.OrcSerde";
+
+ private static final String PARQUET_INPUT_FORMAT_CLASS =
+ "org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat";
+ private static final String PARQUET_OUTPUT_FORMAT_CLASS =
+ "org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat";
+ private static final String PARQUET_SERDE_CLASS =
+ "org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe";
+ private static final String COLUMNAR_SERDE_CLASS =
+ "org.apache.hadoop.hive.serde2.columnar.ColumnarSerDe";
+ private static final String RCFILE_INPUT_FORMAT_CLASS =
+ "org.apache.hadoop.hive.ql.io.RCFileInputFormat";
+ private static final String RCFILE_OUTPUT_FORMAT_CLASS =
+ "org.apache.hadoop.hive.ql.io.RCFileOutputFormat";
+ private static final String AVRO_INPUT_FORMAT_CLASS =
+ "org.apache.hadoop.hive.ql.io.avro.AvroContainerInputFormat";
+ private static final String AVRO_OUTPUT_FORMAT_CLASS =
+ "org.apache.hadoop.hive.ql.io.avro.AvroContainerOutputFormat";
+ private static final String AVRO_SERDE_CLASS = "org.apache.hadoop.hive.serde2.avro.AvroSerDe";
+ private static final String JSON_SERDE_CLASS = "org.apache.hive.hcatalog.data.JsonSerDe";
+
+ @VisibleForTesting
+ public static final String OPENCSV_SERDE_CLASS = "org.apache.hadoop.hive.serde2.OpenCSVSerde";
+
+ private static final String REGEX_SERDE_CLASS = "org.apache.hadoop.hive.serde2.RegexSerDe";
+
+ enum StorageFormat {
+ SEQUENCEFILE(
+ SEQUENCEFILE_INPUT_FORMAT_CLASS, SEQUENCEFILE_OUTPUT_FORMAT_CLASS, LAZY_SIMPLE_SERDE_CLASS),
+ TEXTFILE(TEXT_INPUT_FORMAT_CLASS, IGNORE_KEY_OUTPUT_FORMAT_CLASS, LAZY_SIMPLE_SERDE_CLASS),
+ RCFILE(RCFILE_INPUT_FORMAT_CLASS, RCFILE_OUTPUT_FORMAT_CLASS, COLUMNAR_SERDE_CLASS),
+ ORC(ORC_INPUT_FORMAT_CLASS, ORC_OUTPUT_FORMAT_CLASS, ORC_SERDE_CLASS),
+ PARQUET(PARQUET_INPUT_FORMAT_CLASS, PARQUET_OUTPUT_FORMAT_CLASS, PARQUET_SERDE_CLASS),
+ AVRO(AVRO_INPUT_FORMAT_CLASS, AVRO_OUTPUT_FORMAT_CLASS, AVRO_SERDE_CLASS),
+ JSON(TEXT_INPUT_FORMAT_CLASS, IGNORE_KEY_OUTPUT_FORMAT_CLASS, JSON_SERDE_CLASS),
+ CSV(TEXT_INPUT_FORMAT_CLASS, IGNORE_KEY_OUTPUT_FORMAT_CLASS, OPENCSV_SERDE_CLASS),
+ REGEX(TEXT_INPUT_FORMAT_CLASS, IGNORE_KEY_OUTPUT_FORMAT_CLASS, REGEX_SERDE_CLASS);
+
+ private final String inputFormat;
+ private final String outputFormat;
+ private final String serde;
+
+ StorageFormat(String inputFormat, String outputFormat, String serde) {
+ Preconditions.checkArgument(inputFormat != null, "inputFormat must not be null");
+ Preconditions.checkArgument(outputFormat != null, "outputFormat must not be null");
+ Preconditions.checkArgument(serde != null, "serde must not be null");
+ this.inputFormat = inputFormat;
+ this.outputFormat = outputFormat;
+ this.serde = serde;
+ }
+
+ public String getInputFormat() {
+ return inputFormat;
+ }
+
+ public String getOutputFormat() {
+ return outputFormat;
+ }
+
+ public String getSerde() {
+ return serde;
+ }
+ }
+
+ private static final Map<String, PropertyEntry<?>> propertiesMetadata;
+
+ static {
+ List<PropertyEntry<?>> propertyEntries =
+ ImmutableList.of(
+ stringReservedPropertyEntry(COMMENT, "table comment", true),
+ stringReservedPropertyEntry(NUM_FILES, "number of files", false),
+ stringReservedPropertyEntry(TOTAL_SIZE, "total size of the table", false),
+ booleanReservedPropertyEntry(
+ EXTERNAL, "Indicate whether it is an external table", false, true),
+ stringImmutablePropertyEntry(
+ LOCATION, "HDFS location for table storage", false, null, false, false),
+ enumImmutablePropertyEntry(
+ TABLE_TYPE,
+ "The type of Hive table",
+ false,
+ TableType.class,
+ MANAGED_TABLE,
+ false,
+ false),
+ enumImmutablePropertyEntry(
+ FORMAT,
+ "The table storage format",
+ false,
+ StorageFormat.class,
+ TEXTFILE,
+ false,
+ false),
+ stringImmutablePropertyEntry(
+ INPUT_FORMAT,
+ "The input format class for the table",
+ false,
+ TEXT_INPUT_FORMAT_CLASS,
+ false,
+ false),
+ stringImmutablePropertyEntry(
+ OUTPUT_FORMAT,
+ "The output format class for the table",
+ false,
+ IGNORE_KEY_OUTPUT_FORMAT_CLASS,
+ false,
+ false),
+ stringReservedPropertyEntry(TRANSIENT_LAST_DDL_TIME, "Last DDL time", false),
+ stringImmutablePropertyEntry(
+ SERDE_NAME, "Name of the serde, table name by default", false, null, false, false),
+ stringImmutablePropertyEntry(
+ SERDE_LIB,
+ "The serde library class for the table",
+ false,
+ LAZY_SIMPLE_SERDE_CLASS,
+ false,
+ false));
+
+ propertiesMetadata = Maps.uniqueIndex(propertyEntries, PropertyEntry::getName);
+ }
+
@Override
protected Map<String, PropertyEntry<?>> specificPropertyEntries() {
- // TODO(Minghuang): support Hive table property specs
- return Maps.newHashMap();
+ return propertiesMetadata;
}
}
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 5db11be0e0..c2bf7b6642 100644
--- a/core/src/main/java/com/datastrato/graviton/catalog/CatalogOperationDispatcher.java
+++ b/core/src/main/java/com/datastrato/graviton/catalog/CatalogOperationDispatcher.java
@@ -362,22 +362,28 @@ public class CatalogOperationDispatcher implements TableCatalog, SupportsSchemas
StringIdentifier stringId = StringIdentifier.fromId(uid);
Map<String, String> updatedProperties = StringIdentifier.addToProperties(stringId, properties);
+ doWithCatalog(
+ getCatalogIdentifier(ident),
+ c ->
+ c.doWithTableOps(
+ t ->
+ t.createTable(
+ ident,
+ columns,
+ comment,
+ updatedProperties,
+ partitions == null ? new Transform[0] : partitions,
+ distribution == null ? Distribution.NONE : distribution,
+ sortOrders == null ? new SortOrder[0] : sortOrders)),
+ NoSuchSchemaException.class,
+ TableAlreadyExistsException.class);
+
+ // Retrieve the Table again to obtain some values generated by underlying catalog
Table table =
doWithCatalog(
getCatalogIdentifier(ident),
- c ->
- c.doWithTableOps(
- t ->
- t.createTable(
- ident,
- columns,
- comment,
- updatedProperties,
- partitions == null ? new Transform[0] : partitions,
- distribution == null ? Distribution.NONE : distribution,
- sortOrders == null ? new SortOrder[0] : sortOrders)),
- NoSuchSchemaException.class,
- TableAlreadyExistsException.class);
+ c -> c.doWithTableOps(t -> t.loadTable(ident)),
+ NoSuchTableException.class);
TableEntity tableEntity =
new TableEntity.Builder()
@@ -419,13 +425,25 @@ public class CatalogOperationDispatcher implements TableCatalog, SupportsSchemas
public Table alterTable(NameIdentifier ident, TableChange... changes)
throws NoSuchTableException, IllegalArgumentException {
validateAlterTableProperties(ident, changes);
- Table alteredTable =
+
+ NameIdentifier catalogIdent = getCatalogIdentifier(ident);
+ Table tempAlteredTable =
doWithCatalog(
- getCatalogIdentifier(ident),
+ catalogIdent,
c -> c.doWithTableOps(t -> t.alterTable(ident, changes)),
NoSuchTableException.class,
IllegalArgumentException.class);
+ // Retrieve the Table again to obtain some values generated by underlying catalog
+ Table alteredTable =
+ doWithCatalog(
+ catalogIdent,
+ c ->
+ c.doWithTableOps(
+ t ->
+ t.loadTable(NameIdentifier.of(ident.namespace(), tempAlteredTable.name()))),
+ NoSuchTableException.class);
+
StringIdentifier stringId = getStringIdFromProperties(alteredTable.properties());
// Case 1: The table is not created by Graviton.
if (stringId == null) {
diff --git a/core/src/main/java/com/datastrato/graviton/catalog/PropertiesMetadata.java b/core/src/main/java/com/datastrato/graviton/catalog/PropertiesMetadata.java
index be9ae9261d..622811c2f2 100644
--- a/core/src/main/java/com/datastrato/graviton/catalog/PropertiesMetadata.java
+++ b/core/src/main/java/com/datastrato/graviton/catalog/PropertiesMetadata.java
@@ -106,4 +106,23 @@ public interface PropertiesMetadata {
}
}
}
+
+ default Object getOrDefault(Map<String, String> properties, String propertyName) {
+ if (!containsProperty(propertyName)) {
+ throw new IllegalArgumentException("Property is not defined: " + propertyName);
+ }
+
+ if (properties.containsKey(propertyName)) {
+ return propertyEntries().get(propertyName).decode(properties.get(propertyName));
+ }
+ return propertyEntries().get(propertyName).getDefaultValue();
+ }
+
+ default Object getDefaultValue(String propertyName) {
+ if (!containsProperty(propertyName)) {
+ throw new IllegalArgumentException("Property is not defined: " + propertyName);
+ }
+
+ return propertyEntries().get(propertyName).getDefaultValue();
+ }
}
diff --git a/core/src/main/java/com/datastrato/graviton/catalog/PropertyEntry.java b/core/src/main/java/com/datastrato/graviton/catalog/PropertyEntry.java
index c382fe9198..c7c78c43dc 100644
--- a/core/src/main/java/com/datastrato/graviton/catalog/PropertyEntry.java
+++ b/core/src/main/java/com/datastrato/graviton/catalog/PropertyEntry.java
@@ -4,7 +4,11 @@
*/
package com.datastrato.graviton.catalog;
+import static java.lang.String.format;
+import static java.util.stream.Collectors.joining;
+
import com.google.common.base.Preconditions;
+import java.util.EnumSet;
import java.util.function.Function;
import lombok.Getter;
import org.apache.logging.log4j.util.Strings;
@@ -179,6 +183,33 @@ public final class PropertyEntry<T> {
return stringPropertyEntry(name, description, false, true, null, hidden, true);
}
+ public static PropertyEntry<Boolean> booleanReservedPropertyEntry(
+ String name, String description, boolean defaultValue, boolean hidden) {
+ return booleanPropertyEntry(name, description, false, true, defaultValue, hidden, true);
+ }
+
+ public static PropertyEntry<Boolean> booleanPropertyEntry(
+ String name,
+ String description,
+ boolean required,
+ boolean immutable,
+ Boolean defaultValue,
+ boolean hidden,
+ boolean reserved) {
+ return new Builder<Boolean>()
+ .withName(name)
+ .withDescription(description)
+ .withRequired(required)
+ .withImmutable(immutable)
+ .withJavaType(Boolean.class)
+ .withDefaultValue(defaultValue)
+ .withDecoder(Boolean::valueOf)
+ .withEncoder(b -> Boolean.toString(b).toUpperCase())
+ .withHidden(hidden)
+ .withReserved(reserved)
+ .build();
+ }
+
public static PropertyEntry<String> stringRequiredPropertyEntry(
String name, String description, boolean immutable, boolean hidden) {
return stringPropertyEntry(name, description, true, immutable, null, hidden, false);
@@ -193,4 +224,52 @@ public final class PropertyEntry<T> {
boolean reserved) {
return stringPropertyEntry(name, description, required, true, defaultValue, hidden, reserved);
}
+
+ public static <T extends Enum<T>> PropertyEntry<T> enumPropertyEntry(
+ String name,
+ String description,
+ boolean required,
+ boolean immutable,
+ Class<T> javaType,
+ T defaultValue,
+ boolean hidden,
+ boolean reserved) {
+ String validValues =
+ EnumSet.allOf(javaType).stream()
+ .map(Enum::name)
+ .map(String::toLowerCase)
+ .collect(joining(", ", "[", "]"));
+ return new Builder<T>()
+ .withName(name)
+ .withDescription(description)
+ .withRequired(required)
+ .withImmutable(immutable)
+ .withJavaType(javaType)
+ .withDefaultValue(defaultValue)
+ .withDecoder(
+ value -> {
+ try {
+ return Enum.valueOf(javaType, value.toUpperCase());
+ } catch (IllegalArgumentException | NullPointerException e) {
+ throw new IllegalArgumentException(
+ format("Invalid value [%s]. Valid values: %s", value, validValues), e);
+ }
+ })
+ .withEncoder(e -> e.name().toLowerCase())
+ .withHidden(hidden)
+ .withReserved(reserved)
+ .build();
+ }
+
+ public static <T extends Enum<T>> PropertyEntry<T> enumImmutablePropertyEntry(
+ String name,
+ String description,
+ boolean required,
+ Class<T> javaType,
+ T defaultValue,
+ boolean hidden,
+ boolean reserved) {
+ return enumPropertyEntry(
+ name, description, required, true, javaType, defaultValue, hidden, reserved);
+ }
}
diff --git a/integration-test/src/test/java/com/datastrato/graviton/integration/test/catalog/hive/CatalogHiveIT.java b/integration-test/src/test/java/com/datastrato/graviton/integration/test/catalog/hive/CatalogHiveIT.java
index a744091dda..f30792c6d9 100644
--- a/integration-test/src/test/java/com/datastrato/graviton/integration/test/catalog/hive/CatalogHiveIT.java
+++ b/integration-test/src/test/java/com/datastrato/graviton/integration/test/catalog/hive/CatalogHiveIT.java
@@ -4,12 +4,28 @@
*/
package com.datastrato.graviton.integration.test.catalog.hive;
+import static com.datastrato.graviton.catalog.hive.HiveTablePropertiesMetadata.COMMENT;
+import static com.datastrato.graviton.catalog.hive.HiveTablePropertiesMetadata.EXTERNAL;
+import static com.datastrato.graviton.catalog.hive.HiveTablePropertiesMetadata.FORMAT;
+import static com.datastrato.graviton.catalog.hive.HiveTablePropertiesMetadata.IGNORE_KEY_OUTPUT_FORMAT_CLASS;
+import static com.datastrato.graviton.catalog.hive.HiveTablePropertiesMetadata.INPUT_FORMAT;
+import static com.datastrato.graviton.catalog.hive.HiveTablePropertiesMetadata.LOCATION;
+import static com.datastrato.graviton.catalog.hive.HiveTablePropertiesMetadata.NUM_FILES;
+import static com.datastrato.graviton.catalog.hive.HiveTablePropertiesMetadata.OPENCSV_SERDE_CLASS;
+import static com.datastrato.graviton.catalog.hive.HiveTablePropertiesMetadata.OUTPUT_FORMAT;
+import static com.datastrato.graviton.catalog.hive.HiveTablePropertiesMetadata.SERDE_LIB;
+import static com.datastrato.graviton.catalog.hive.HiveTablePropertiesMetadata.TABLE_TYPE;
+import static com.datastrato.graviton.catalog.hive.HiveTablePropertiesMetadata.TEXT_INPUT_FORMAT_CLASS;
+import static com.datastrato.graviton.catalog.hive.HiveTablePropertiesMetadata.TOTAL_SIZE;
+import static com.datastrato.graviton.catalog.hive.HiveTablePropertiesMetadata.TRANSIENT_LAST_DDL_TIME;
import static com.datastrato.graviton.rel.transforms.Transforms.identity;
+import static org.apache.hadoop.hive.metastore.TableType.EXTERNAL_TABLE;
import static org.junit.jupiter.api.Assertions.assertThrows;
import com.datastrato.graviton.Catalog;
import com.datastrato.graviton.NameIdentifier;
import com.datastrato.graviton.catalog.hive.HiveClientPool;
+import com.datastrato.graviton.catalog.hive.HiveTablePropertiesMetadata;
import com.datastrato.graviton.client.GravitonMetaLake;
import com.datastrato.graviton.dto.rel.ColumnDTO;
import com.datastrato.graviton.integration.test.util.AbstractIT;
@@ -26,6 +42,7 @@ import com.datastrato.graviton.rel.TableChange;
import com.datastrato.graviton.rel.transforms.Transform;
import com.datastrato.graviton.rel.transforms.Transforms;
import com.datastrato.graviton.rel.transforms.Transforms.NamedReference;
+import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import io.substrait.type.TypeCreator;
import java.util.Arrays;
@@ -34,6 +51,7 @@ import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.metastore.TableType;
import org.apache.hadoop.hive.metastore.api.Database;
import org.apache.hadoop.hive.metastore.api.FieldSchema;
import org.apache.hadoop.hive.metastore.api.NoSuchObjectException;
@@ -326,6 +344,73 @@ public class CatalogHiveIT extends AbstractIT {
assertTableEquals(createdTable1, hiveTable1);
}
+ @Test
+ public void testHiveTableProperties() throws TException, InterruptedException {
+ ColumnDTO[] columns = createColumns();
+ NameIdentifier nameIdentifier =
+ NameIdentifier.of(metalakeName, catalogName, schemaName, tableName);
+ // test default properties
+ Table createdTable =
+ catalog
+ .asTableCatalog()
+ .createTable(
+ nameIdentifier, columns, table_comment, ImmutableMap.of(), new Transform[0]);
+ HiveTablePropertiesMetadata tablePropertiesMetadata = new HiveTablePropertiesMetadata();
+ org.apache.hadoop.hive.metastore.api.Table actualTable =
+ hiveClientPool.run(client -> client.getTable(schemaName, tableName));
+ assertDefaultTableProperties(createdTable, actualTable);
+
+ // test set properties
+ String table2 = GravitonITUtils.genRandomName("CatalogHiveIT_table");
+ Table createdTable2 =
+ catalog
+ .asTableCatalog()
+ .createTable(
+ NameIdentifier.of(metalakeName, catalogName, schemaName, table2),
+ columns,
+ table_comment,
+ ImmutableMap.of(
+ TABLE_TYPE,
+ "external_table",
+ LOCATION,
+ "/tmp",
+ FORMAT,
+ "textfile",
+ SERDE_LIB,
+ OPENCSV_SERDE_CLASS),
+ new Transform[0]);
+ org.apache.hadoop.hive.metastore.api.Table actualTable2 =
+ hiveClientPool.run(client -> client.getTable(schemaName, table2));
+
+ Assertions.assertEquals(
+ OPENCSV_SERDE_CLASS, actualTable2.getSd().getSerdeInfo().getSerializationLib());
+ Assertions.assertEquals(TEXT_INPUT_FORMAT_CLASS, actualTable2.getSd().getInputFormat());
+ Assertions.assertEquals(IGNORE_KEY_OUTPUT_FORMAT_CLASS, actualTable2.getSd().getOutputFormat());
+ Assertions.assertEquals(EXTERNAL_TABLE.name(), actualTable2.getTableType());
+ Assertions.assertEquals(table2, actualTable2.getSd().getSerdeInfo().getName());
+ Assertions.assertEquals(table_comment, actualTable2.getParameters().get(COMMENT));
+ Assertions.assertEquals(
+ ((Boolean) tablePropertiesMetadata.getDefaultValue(EXTERNAL)).toString().toUpperCase(),
+ actualTable.getParameters().get(EXTERNAL));
+ Assertions.assertTrue(actualTable2.getSd().getLocation().endsWith("/tmp"));
+ Assertions.assertNotNull(createdTable2.properties().get(TRANSIENT_LAST_DDL_TIME));
+ Assertions.assertNotNull(createdTable2.properties().get(NUM_FILES));
+ Assertions.assertNotNull(createdTable2.properties().get(TOTAL_SIZE));
+
+ // test alter properties exception
+ IllegalArgumentException exception =
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> {
+ catalog
+ .asTableCatalog()
+ .alterTable(
+ NameIdentifier.of(metalakeName, catalogName, schemaName, tableName),
+ TableChange.setProperty(TRANSIENT_LAST_DDL_TIME, "1234"));
+ });
+ Assertions.assertTrue(exception.getMessage().contains("cannot be set"));
+ }
+
@Test
public void testCreatePartitionedHiveTable() throws TException, InterruptedException {
// Create table from Graviton API
@@ -416,18 +501,20 @@ public class CatalogHiveIT extends AbstractIT {
table_comment,
createProperties(),
new Transform[] {identity(columns[0])});
- catalog
- .asTableCatalog()
- .alterTable(
- NameIdentifier.of(metalakeName, catalogName, schemaName, tableName),
- TableChange.rename(alertTableName),
- TableChange.updateComment(table_comment + "_new"),
- TableChange.removeProperty("key1"),
- TableChange.setProperty("key2", "val2_new"),
- TableChange.addColumn(new String[] {"col_4"}, TypeCreator.NULLABLE.STRING),
- TableChange.renameColumn(new String[] {HIVE_COL_NAME2}, "col_2_new"),
- TableChange.updateColumnComment(new String[] {HIVE_COL_NAME1}, "comment_new"),
- TableChange.updateColumnType(new String[] {HIVE_COL_NAME1}, TypeCreator.NULLABLE.I32));
+ Table alteredTable =
+ catalog
+ .asTableCatalog()
+ .alterTable(
+ NameIdentifier.of(metalakeName, catalogName, schemaName, tableName),
+ TableChange.rename(alertTableName),
+ TableChange.updateComment(table_comment + "_new"),
+ TableChange.removeProperty("key1"),
+ TableChange.setProperty("key2", "val2_new"),
+ TableChange.addColumn(new String[] {"col_4"}, TypeCreator.NULLABLE.STRING),
+ TableChange.renameColumn(new String[] {HIVE_COL_NAME2}, "col_2_new"),
+ TableChange.updateColumnComment(new String[] {HIVE_COL_NAME1}, "comment_new"),
+ TableChange.updateColumnType(
+ new String[] {HIVE_COL_NAME1}, TypeCreator.NULLABLE.I32));
// Direct get table from hive metastore to check if the table is altered successfully.
org.apache.hadoop.hive.metastore.api.Table hiveTab =
@@ -454,6 +541,31 @@ public class CatalogHiveIT extends AbstractIT {
Assertions.assertEquals(1, hiveTab.getPartitionKeys().size());
Assertions.assertEquals(columns[0].name(), hiveTab.getPartitionKeys().get(0).getName());
+ assertDefaultTableProperties(alteredTable, hiveTab);
+ }
+
+ private void assertDefaultTableProperties(
+ Table gravitonReturnTable, org.apache.hadoop.hive.metastore.api.Table actualTable) {
+ HiveTablePropertiesMetadata tablePropertiesMetadata = new HiveTablePropertiesMetadata();
+ Assertions.assertEquals(
+ tablePropertiesMetadata.getDefaultValue(SERDE_LIB),
+ actualTable.getSd().getSerdeInfo().getSerializationLib());
+ Assertions.assertEquals(
+ tablePropertiesMetadata.getDefaultValue(INPUT_FORMAT),
+ actualTable.getSd().getInputFormat());
+ Assertions.assertEquals(
+ tablePropertiesMetadata.getDefaultValue(OUTPUT_FORMAT),
+ actualTable.getSd().getOutputFormat());
+ Assertions.assertEquals(
+ ((TableType) tablePropertiesMetadata.getDefaultValue(TABLE_TYPE)).name(),
+ actualTable.getTableType());
+ Assertions.assertEquals(tableName, actualTable.getSd().getSerdeInfo().getName());
+ Assertions.assertEquals(
+ ((Boolean) tablePropertiesMetadata.getDefaultValue(EXTERNAL)).toString().toUpperCase(),
+ actualTable.getParameters().get(EXTERNAL));
+ Assertions.assertNotNull(actualTable.getParameters().get(COMMENT));
+ Assertions.assertNotNull(actualTable.getSd().getLocation());
+ Assertions.assertNotNull(gravitonReturnTable.properties().get(TRANSIENT_LAST_DDL_TIME));
}
@Test