Skip to content
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

[#318] refactor(core, catalog-*): Refactor the catalog operations to guarantee SSOT #403

Merged
merged 9 commits into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
*/
package com.datastrato.graviton.catalog.hive;

import com.datastrato.graviton.meta.rel.BaseColumn;
import com.datastrato.graviton.catalog.rel.BaseColumn;
import lombok.EqualsAndHashCode;

/** Represents a column in the Hive Metastore catalog. */
@EqualsAndHashCode(callSuper = true)
public class HiveColumn extends BaseColumn {

private HiveColumn() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*/
package com.datastrato.graviton.catalog.hive;

import com.datastrato.graviton.catalog.rel.BaseSchema;
import com.datastrato.graviton.meta.AuditInfo;
import com.datastrato.graviton.meta.rel.BaseSchema;
import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
import java.util.Collections;
Expand All @@ -23,8 +23,6 @@ public class HiveSchema extends BaseSchema {
private static final String HMS_DB_OWNER = "hive.metastore.database.owner";
private static final String HMS_DB_OWNER_TYPE = "hive.metastore.database.owner-type";

private Database innerDb;

private Configuration conf;

private HiveSchema() {}
Expand All @@ -33,22 +31,23 @@ private HiveSchema() {}
* Creates a new HiveSchema instance from a Database and a Builder.
*
* @param db The Database representing the HiveSchema.
* @param builder The Builder used to construct the HiveSchema.
* @param hiveConf The HiveConf used to construct the HiveSchema.
* @return A new HiveSchema instance.
*/
public static HiveSchema fromInnerDB(Database db, Builder builder) {
public static HiveSchema fromHiveDB(Database db, Configuration hiveConf) {
Map<String, String> properties = convertToMetadata(db);

// Get audit info from Hive's Database object. Because Hive's database doesn't store create
// time, last modifier and last modified time, we only get creator from Hive's database.
AuditInfo.Builder auditInfoBuilder = new AuditInfo.Builder();
Optional.ofNullable(db.getOwnerName()).ifPresent(auditInfoBuilder::withCreator);

return builder
return new HiveSchema.Builder()
.withName(db.getName())
.withComment(db.getDescription())
.withProperties(properties)
.withAuditInfo(auditInfoBuilder.build())
.withConf(hiveConf)
.build();
}

Expand Down Expand Up @@ -81,12 +80,9 @@ public static Map<String, String> convertToMetadata(Database database) {
*
* @return The converted Database object.
*/
public Database toInnerDB() {
if (innerDb != null) {
return innerDb;
}
public Database toHiveDB() {
Database innerDb = new Database();

innerDb = new Database();
Map<String, String> parameter = Maps.newHashMap();
innerDb.setName(name());
innerDb.setLocationUri(databaseLocation(name()));
Expand Down Expand Up @@ -164,9 +160,7 @@ public Builder withConf(Configuration conf) {
@Override
protected HiveSchema internalBuild() {
HiveSchema hiveSchema = new HiveSchema();
hiveSchema.id = id;
hiveSchema.name = name;
hiveSchema.namespace = namespace;
hiveSchema.comment = comment;
hiveSchema.properties = properties;
hiveSchema.auditInfo = auditInfo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
import static org.apache.hadoop.hive.metastore.TableType.EXTERNAL_TABLE;
import static org.apache.hadoop.hive.metastore.TableType.MANAGED_TABLE;

import com.datastrato.graviton.NameIdentifier;
import com.datastrato.graviton.catalog.hive.converter.FromHiveType;
import com.datastrato.graviton.catalog.hive.converter.ToHiveType;
import com.datastrato.graviton.catalog.rel.BaseTable;
import com.datastrato.graviton.meta.AuditInfo;
import com.datastrato.graviton.meta.rel.BaseTable;
import com.datastrato.graviton.rel.Column;
import com.datastrato.graviton.rel.Distribution;
import com.datastrato.graviton.rel.SortOrder;
Expand All @@ -29,7 +28,6 @@
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.Getter;
import lombok.ToString;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
Expand All @@ -41,7 +39,6 @@

/** Represents a Hive Table entity in the Hive Metastore catalog. */
@ToString
@Getter
public class HiveTable extends BaseTable {

// A set of supported Hive table types.
Expand All @@ -53,16 +50,17 @@ public class HiveTable extends BaseTable {

private String location;

private String schemaName;

private HiveTable() {}

/**
* Creates a new HiveTable instance from a Table and a Builder.
*
* @param table The inner Table representing the HiveTable.
* @param builder The Builder used to construct the HiveTable.
* @return A new HiveTable instance.
*/
public static HiveTable fromInnerTable(Table table, Builder builder) {
public static HiveTable fromHiveTable(Table table) {
// Get audit info from Hive's Table object. Because Hive's table doesn't store last modifier
// and last modified time, we only get creator and create time from Hive's table.
AuditInfo.Builder auditInfoBuilder = new AuditInfo.Builder();
Expand Down Expand Up @@ -96,7 +94,8 @@ public static HiveTable fromInnerTable(Table table, Builder builder) {
.toArray(SortOrder[]::new);
}

return builder
return new HiveTable.Builder()
.withName(table.getTableName())
.withComment(table.getParameters().get(HMS_TABLE_COMMENT))
.withProperties(table.getParameters())
.withColumns(
Expand All @@ -117,6 +116,7 @@ public static HiveTable fromInnerTable(Table table, Builder builder) {
table.getPartitionKeys().stream()
.map(p -> identity(new String[] {p.getName()}))
.toArray(Transforms.NamedReference[]::new))
.withSchemaName(table.getDbName())
.build();
}

Expand All @@ -125,11 +125,11 @@ public static HiveTable fromInnerTable(Table table, Builder builder) {
*
* @return The converted Table.
*/
public Table toInnerTable() {
public Table toHiveTable() {
Table hiveTable = new Table();

hiveTable.setTableName(name);
hiveTable.setDbName(schemaIdentifier().name());
hiveTable.setDbName(schemaName);
hiveTable.setSd(buildStorageDescriptor());
hiveTable.setParameters(properties);
hiveTable.setPartitionKeys(buildPartitionKeys());
Expand Down Expand Up @@ -208,27 +208,37 @@ private SerDeInfo buildSerDeInfo() {
return serDeInfo;
}

/**
* Gets the schema identifier for this HiveTable.
*
* @return The schema identifier.
*/
public NameIdentifier schemaIdentifier() {
return NameIdentifier.of(nameIdentifier().namespace().levels());
}

/** 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.
*
* @param schemaName The string schema name of the HiveTable.
* @return This Builder instance.
*/
public Builder withSchemaName(String schemaName) {
this.schemaName = schemaName;
return this;
}

/**
* Internal method to build a HiveTable instance using the provided values.
*
Expand All @@ -237,8 +247,6 @@ public Builder withLocation(String location) {
@Override
protected HiveTable internalBuild() {
HiveTable hiveTable = new HiveTable();
hiveTable.id = id;
hiveTable.namespace = namespace;
hiveTable.name = name;
hiveTable.comment = comment;
hiveTable.properties = properties != null ? Maps.newHashMap(properties) : Maps.newHashMap();
Expand All @@ -248,6 +256,7 @@ protected HiveTable internalBuild() {
hiveTable.distribution = distribution;
hiveTable.sortOrders = sortOrders;
hiveTable.partitions = partitions;
hiveTable.schemaName = schemaName;

// HMS put table comment in parameters
if (comment != null) {
Expand Down
Loading
Loading