Skip to content

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

[#158] Fix(catalog): Fix Hive table properties does work (#159)

mchades2a27199e3e69
diff --git a/catalog-hive/src/main/java/com/datastrato/graviton/catalog/hive/HiveCatalogOperations.java b/catalog-hive/src/main/java/com/datastrato/graviton/catalog/hive/HiveCatalogOperations.java
index a5787be392..abfa1a76e0 100644
--- a/catalog-hive/src/main/java/com/datastrato/graviton/catalog/hive/HiveCatalogOperations.java
+++ b/catalog-hive/src/main/java/com/datastrato/graviton/catalog/hive/HiveCatalogOperations.java
@@ -6,7 +6,6 @@ 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 org.apache.hadoop.hive.metastore.TableType.EXTERNAL_TABLE;
import com.datastrato.graviton.NameIdentifier;
import com.datastrato.graviton.Namespace;
@@ -30,7 +29,6 @@ import com.datastrato.graviton.rel.TableCatalog;
import com.datastrato.graviton.rel.TableChange;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
-import com.google.common.collect.Lists;
import java.io.IOException;
import java.time.Instant;
import java.util.List;
@@ -38,7 +36,6 @@ import java.util.Map;
import java.util.stream.Collectors;
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;
@@ -397,22 +394,30 @@ public class HiveCatalogOperations implements CatalogOperations, SupportsSchemas
}
try {
- List<NameIdentifier> tables = Lists.newArrayList();
-
- for (TableType tableType : SUPPORT_TABLE_TYPES) {
- tables.addAll(
- clientPool.run(
- c ->
- c.getTables(schemaIdent.name(), "*", tableType).stream()
- .map(tb -> NameIdentifier.of(namespace, tb))
- .collect(Collectors.toList())));
- }
-
- return tables.toArray(new NameIdentifier[0]);
+ // When a table is created using the HMS interface without specifying the `tableType`,
+ // although Hive treats it as a `MANAGED_TABLE`, it cannot be queried through the `getTable`
+ // interface in HMS with the specified `tableType`. This is because when creating a table
+ // without specifying the `tableType`, the underlying engine of HMS does not store the
+ // information of `tableType`. However, once the `getTable` interface specifies a
+ // `tableType`, HMS will use it as a filter condition to query its underlying storage and
+ // these types of tables will be filtered out.
+ // Therefore, in order to avoid missing these types of tables, we need to query HMS twice. The
+ // first time is to retrieve all types of table names (including the missing type tables), and
+ // then based on
+ // those names we can obtain metadata for each individual table and get the type we needed.
+ List<String> allTables = clientPool.run(c -> c.getAllTables(schemaIdent.name()));
+ return clientPool.run(
+ c ->
+ c.getTableObjectsByName(schemaIdent.name(), allTables).stream()
+ .filter(tb -> SUPPORT_TABLE_TYPES.contains(tb.getTableType()))
+ .map(tb -> NameIdentifier.of(namespace, tb.getTableName()))
+ .toArray(NameIdentifier[]::new));
+ } catch (UnknownDBException e) {
+ throw new NoSuchSchemaException(
+ "Schema (database) does not exist " + namespace + " in Hive Metastore");
} catch (TException e) {
throw new RuntimeException(
- "Failed to list all tables under under namespace : " + namespace + " in Hive Metastore",
- e);
+ "Failed to list all tables under the namespace : " + namespace + " in Hive Metastore", e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
@@ -768,9 +773,8 @@ public class HiveCatalogOperations implements CatalogOperations, SupportsSchemas
@Override
public boolean purgeTable(NameIdentifier tableIdent) throws UnsupportedOperationException {
HiveTable table = (HiveTable) loadTable(tableIdent);
- if (EXTERNAL_TABLE == table.getTableType()) {
- throw new UnsupportedOperationException("Cannot purge Hive table with type EXTERNAL_TABLE");
- }
+ // TODO(minghuang): HiveTable support specify `tableType`, then reject purge Hive table with
+ // `tableType` EXTERNAL_TABLE
return dropHiveTable(tableIdent, true, true);
}
diff --git a/catalog-hive/src/main/java/com/datastrato/graviton/catalog/hive/HiveTable.java b/catalog-hive/src/main/java/com/datastrato/graviton/catalog/hive/HiveTable.java
index e3768c4d12..e8ffa8c485 100644
--- a/catalog-hive/src/main/java/com/datastrato/graviton/catalog/hive/HiveTable.java
+++ b/catalog-hive/src/main/java/com/datastrato/graviton/catalog/hive/HiveTable.java
@@ -18,7 +18,6 @@ import java.util.Set;
import java.util.stream.Collectors;
import lombok.Getter;
import lombok.ToString;
-import org.apache.hadoop.hive.metastore.TableType;
import org.apache.hadoop.hive.metastore.api.FieldSchema;
import org.apache.hadoop.hive.metastore.api.SerDeInfo;
import org.apache.hadoop.hive.metastore.api.StorageDescriptor;
@@ -30,24 +29,14 @@ import org.apache.hadoop.hive.metastore.api.Table;
public class HiveTable extends BaseTable {
// A set of supported Hive table types.
- public static final Set<TableType> SUPPORT_TABLE_TYPES =
- Sets.newHashSet(MANAGED_TABLE, EXTERNAL_TABLE);
+ 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 inputFormat;
-
- private String outputFormat;
-
- private String serLib;
-
- private TableType tableType;
-
private String location;
- private int createTime;
-
private HiveTable() {}
/**
@@ -62,7 +51,6 @@ public class HiveTable extends BaseTable {
return builder
.withComment(table.getParameters().get(HMS_TABLE_COMMENT))
.withProperties(table.getParameters())
- .withTableType(TableType.valueOf(table.getTableType()))
.withColumns(
table.getSd().getCols().stream()
.map(
@@ -73,11 +61,7 @@ public class HiveTable extends BaseTable {
.withComment(f.getComment())
.build())
.toArray(HiveColumn[]::new))
- .withOutputFormat(table.getSd().getOutputFormat())
- .withInputFormat(table.getSd().getInputFormat())
- .withSerLib(table.getSd().getSerdeInfo().getSerializationLib())
.withLocation(table.getSd().getLocation())
- .withCreateTime(table.getCreateTime())
.build();
}
@@ -91,11 +75,8 @@ public class HiveTable extends BaseTable {
hiveTable.setTableName(name);
hiveTable.setDbName(schemaIdentifier().name());
- hiveTable.setOwner(auditInfo.creator());
hiveTable.setSd(buildStorageDescriptor());
- hiveTable.setTableType(String.valueOf(tableType));
hiveTable.setParameters(properties);
- hiveTable.setCreateTime(createTime);
hiveTable.setPartitionKeys(Lists.newArrayList() /* TODO(Minghuang): Add partition support */);
return hiveTable;
@@ -112,18 +93,16 @@ public class HiveTable extends BaseTable {
c.dataType().accept(ToHiveType.INSTANCE).getQualifiedName(),
c.comment()))
.collect(Collectors.toList()));
- sd.setLocation(location);
- sd.setInputFormat(inputFormat);
- sd.setOutputFormat(outputFormat);
sd.setSerdeInfo(buildSerDeInfo());
+ // `location` must not be null, otherwise it will result in an NPE when calling HMS `alterTable`
+ // interface
+ sd.setLocation(location);
return sd;
}
private SerDeInfo buildSerDeInfo() {
- SerDeInfo serDeInfo = new SerDeInfo();
- serDeInfo.setName(name);
- serDeInfo.setSerializationLib(serLib);
- return serDeInfo;
+ // TODO(minghuang): Build SerDeInfo object based on user specifications.
+ return new SerDeInfo();
}
/**
@@ -137,66 +116,16 @@ public class HiveTable extends BaseTable {
/** A builder class for constructing HiveTable instances. */
public static class Builder extends BaseTableBuilder<Builder, HiveTable> {
- private String inputFormat;
-
- private String outputFormat;
-
- private String serLib;
-
- private TableType tableType;
+ // currently, load from HMS only
+ // TODO(minghuang): Support user specify`location` property
private String location;
- private int createTime;
-
- /**
- * Sets the input format for the HiveTable.
- *
- * @param inputFormat The input format to set.
- * @return The Builder instance.
- */
- public Builder withInputFormat(String inputFormat) {
- this.inputFormat = inputFormat;
- return this;
- }
-
- /**
- * Sets the output format for the HiveTable.
- *
- * @param outputFormat The output format to set.
- * @return The Builder instance.
- */
- public Builder withOutputFormat(String outputFormat) {
- this.outputFormat = outputFormat;
- return this;
- }
-
- public Builder withSerLib(String serLib) {
- this.serLib = serLib;
- return this;
- }
-
- /**
- * Sets the table type for the HiveTable.
- *
- * @param tableType The table type to set.
- * @return The Builder instance.
- */
- public Builder withTableType(TableType tableType) {
- this.tableType = tableType;
- return this;
- }
-
public Builder withLocation(String location) {
this.location = location;
return this;
}
- public Builder withCreateTime(int createTime) {
- this.createTime = createTime;
- return this;
- }
-
/**
* Internal method to build a HiveTable instance using the provided values.
*
@@ -213,12 +142,7 @@ public class HiveTable extends BaseTable {
hiveTable.properties = properties;
hiveTable.auditInfo = auditInfo;
hiveTable.columns = columns;
- hiveTable.inputFormat = inputFormat;
- hiveTable.outputFormat = outputFormat;
- hiveTable.serLib = serLib;
hiveTable.location = location;
- hiveTable.createTime = createTime;
- hiveTable.tableType = tableType == null ? MANAGED_TABLE : tableType;
// HMS put table comment in parameters
hiveTable.properties.put(HMS_TABLE_COMMENT, comment);