Skip to content
Closed
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 @@ -97,6 +97,28 @@ public class MetaStoreUtils {

protected static final Logger LOG = LoggerFactory.getLogger("hive.log");

// The following two are public for any external users who wish to use them.
/**
* This character is used to mark a database name as having a catalog name prepended. This
* marker should be placed first in the String to make it easy to determine that this has both
* a catalog and a database name. @ is chosen as it is not used in regular expressions. This
* is only intended for use when making old Thrift calls that do not support catalog names.
*/
public static final char CATALOG_DB_THRIFT_NAME_MARKER = '@';

/**
* This String is used to seaprate the catalog name from the database name. This should only
* be used in Strings that are prepended with {@link #CATALOG_DB_THRIFT_NAME_MARKER}. # is
* chosen because it is not used in regular expressions. this is only intended for use when
* making old Thrift calls that do not support catalog names.
*/
public static final String CATALOG_DB_SEPARATOR = "#";

/**
* Mark a database as being empty (as distinct from null).
*/
public static final String DB_EMPTY_MARKER = "!";

public static final String DEFAULT_DATABASE_NAME = "default";
public static final String DEFAULT_DATABASE_COMMENT = "Default Hive database";
public static final String DEFAULT_SERIALIZATION_FORMAT = "1";
Expand Down Expand Up @@ -1977,4 +1999,67 @@ public static void mergeColStats(ColumnStatistics csNew, ColumnStatistics csOld)
}
csNew.setStatsObj(list);
}

/**
* Prepend the default catalog onto the database name if property "metastore.catalog.default" is configured,
* otherwise return the database name.
* @param dbName database name
* @param conf configuration object, used to determine default catalog
* @return a database name with or without the catalog name prepended.
*/
public static String prependCatalogToDbName(String dbName, HiveConf conf, String currentCatalog) {
StringBuilder buf = new StringBuilder();

if(isCatalogEnabled(conf, currentCatalog)) {
buf.append(CATALOG_DB_THRIFT_NAME_MARKER)
.append(currentCatalog)
.append(CATALOG_DB_SEPARATOR);
}

if (dbName != null) {
if (dbName.isEmpty()) buf.append(DB_EMPTY_MARKER);
else buf.append(dbName);
}
return buf.toString();
}

public static Boolean isCatalogEnabled(HiveConf conf) {
if (conf == null) {
return false;
}

String catName = conf.get(CATALOG_DEFAULT);
if (catName == null || "".equals(catName)) return false;
return true;
}

public static Boolean isCatalogEnabled(HiveConf conf, String currentCatalog) {
if (conf == null) {
return false;
}

String catName = conf.get(CATALOG_DEFAULT);
if ((catName == null || "".equals(catName)) && "".equals(currentCatalog)) return false;
return true;
}

public static String getDefaultCatalog(HiveConf conf) {
// If catalog is disabled, we should return an empty string so we don't mistakenly
// use `Warehouse.DEFAULT_CATALOG_NAME` as catalog name.
if (!isCatalogEnabled(conf)) {
return "";
}

if (conf == null) {
LOG.warn("Configuration is null, so going with default catalog.");
return Warehouse.DEFAULT_CATALOG_NAME;
}

String catName = conf.get(CATALOG_DEFAULT);
if (catName == null || "".equals(catName)) catName = Warehouse.DEFAULT_CATALOG_NAME;
return catName;
}

//"The default catalog to use when a catalog is not specified. Default is 'hive' (the default catalog).
public static final String CATALOG_DEFAULT = "metastore.catalog.default";
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
* This class represents a warehouse where data of Hive tables is stored
*/
public class Warehouse {
public static final String DEFAULT_CATALOG_NAME = "hive";

private Path whRoot;
private final Configuration conf;
private final String whRootString;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
*/
public class CompositePartitionSpecProxy extends PartitionSpecProxy {

private String catName;
private String dbName;
private String tableName;
private List<PartitionSpec> partitionSpecs;
Expand All @@ -42,10 +43,12 @@ public class CompositePartitionSpecProxy extends PartitionSpecProxy {
protected CompositePartitionSpecProxy(List<PartitionSpec> partitionSpecs) {
this.partitionSpecs = partitionSpecs;
if (partitionSpecs.isEmpty()) {
catName = null;
dbName = null;
tableName = null;
}
else {
catName = partitionSpecs.get(0).getCatName();
dbName = partitionSpecs.get(0).getDbName();
tableName = partitionSpecs.get(0).getTableName();
this.partitionSpecProxies = new ArrayList<PartitionSpecProxy>(partitionSpecs.size());
Expand All @@ -60,6 +63,7 @@ protected CompositePartitionSpecProxy(List<PartitionSpec> partitionSpecs) {
}

protected CompositePartitionSpecProxy(String dbName, String tableName, List<PartitionSpec> partitionSpecs) {
this.catName = null;
this.dbName = dbName;
this.tableName = tableName;
this.partitionSpecs = partitionSpecs;
Expand Down Expand Up @@ -147,6 +151,11 @@ public Partition getCurrent() {
return iterator.getCurrent();
}

@Override
public String getCatName() {
return composite.getCatName();
}

@Override
public String getDbName() {
return composite.dbName;
Expand Down Expand Up @@ -183,6 +192,14 @@ public void setCreateTime(long time) {
}
}

@Override
public void setCatName(String catName) {
this.catName = catName;
for (PartitionSpecProxy partSpecProxy : partitionSpecProxies) {
partSpecProxy.setCatName(catName);
}
}

@Override
public void setDbName(String dbName) {
this.dbName = dbName;
Expand All @@ -199,6 +216,11 @@ public void setTableName(String tableName) {
}
}

@Override
public String getCatName() {
return catName;
}

@Override
public String getDbName() {
return dbName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ protected PartitionListComposingSpecProxy(PartitionSpec partitionSpec) {
this.partitionSpec = partitionSpec;
}

@Override
public String getCatName() {
return partitionSpec.getCatName();
}

@Override
public String getDbName() {
return partitionSpec.getDbName();
Expand All @@ -64,6 +69,14 @@ public int size() {
return partitionSpec.getPartitionList().getPartitionsSize();
}

@Override
public void setCatName(String catName) {
partitionSpec.setCatName(catName);
for (Partition partition : partitionSpec.getPartitionList().getPartitions()) {
partition.setCatName(catName);
}
}

@Override
public void setDbName(String dbName) {
partitionSpec.setDbName(dbName);
Expand Down Expand Up @@ -117,6 +130,11 @@ public Partition getCurrent() {
return partitionList.get(index);
}

@Override
public String getCatName() {
return partitionSpecProxy.getCatName();
}

@Override
public String getDbName() {
return partitionSpecProxy.getDbName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public abstract class PartitionSpecProxy {
*/
public abstract int size();

/**
* Set catalog name.
* @param catName catalog name.
*/
public abstract void setCatName(String catName);

/**
* Setter for name of the DB.
* @param dbName The name of the DB.
Expand All @@ -48,6 +54,12 @@ public abstract class PartitionSpecProxy {
*/
public abstract void setTableName(String tableName);

/**
* Get catalog name.
* @return catalog name.
*/
public abstract String getCatName();

/**
* Getter for name of the DB.
* @return The name of the DB.
Expand Down Expand Up @@ -130,6 +142,12 @@ public static interface PartitionIterator extends java.util.Iterator<Partition>
*/
public Partition getCurrent();

/**
* Get the catalog name.
* @return catalog name.
*/
String getCatName();

/**
* Getter for the name of the DB.
* @return Name of the DB.
Expand Down Expand Up @@ -184,6 +202,7 @@ public static class SimplePartitionWrapperIterator implements PartitionIterator
public SimplePartitionWrapperIterator(Partition partition) {this.partition = partition;}

@Override public Partition getCurrent() { return partition; }
@Override public String getCatName() { return partition.getCatName(); }
@Override public String getDbName() { return partition.getDbName(); }
@Override public String getTableName() { return partition.getTableName(); }
@Override public Map<String, String> getParameters() { return partition.getParameters(); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public int size() {
return partitionSpec.getSharedSDPartitionSpec().getPartitionsSize();
}

@Override
public void setCatName(String catName) {
partitionSpec.setCatName(catName);
}

@Override
public void setDbName(String dbName) {
partitionSpec.setDbName(dbName);
Expand All @@ -58,6 +63,11 @@ public void setTableName(String tableName) {
partitionSpec.setTableName(tableName);
}

@Override
public String getCatName() {
return partitionSpec.getCatName();
}

@Override
public String getDbName() {
return partitionSpec.getDbName();
Expand Down Expand Up @@ -121,7 +131,7 @@ public Partition getCurrent() {
StorageDescriptor partSD = new StorageDescriptor(pSpec.getSd());
partSD.setLocation(partSD.getLocation() + partWithoutSD.getRelativePath());

return new Partition(
Partition p = new Partition(
partWithoutSD.getValues(),
partitionSpecWithSharedSDProxy.partitionSpec.getDbName(),
partitionSpecWithSharedSDProxy.partitionSpec.getTableName(),
Expand All @@ -130,6 +140,13 @@ public Partition getCurrent() {
partSD,
partWithoutSD.getParameters()
);
p.setCatName(partitionSpecWithSharedSDProxy.partitionSpec.getCatName());
return p;
}

@Override
public String getCatName() {
return partitionSpecWithSharedSDProxy.partitionSpec.getCatName();
}

@Override
Expand Down