Skip to content

Commit 0a57566

Browse files
committed
HIVE-29241: Distinguish the default location of the database by catalog
1 parent e299310 commit 0a57566

14 files changed

Lines changed: 51 additions & 56 deletions

File tree

ql/src/java/org/apache/hadoop/hive/ql/ddl/catalog/create/CreateCatalogAnalyzer.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void analyzeInternal(ASTNode root) throws SemanticException {
5050
String locationUrl = null;
5151
boolean ifNotExists = false;
5252
String comment = null;
53-
Map<String, String> props = null;
53+
Map<String, String> props = new HashMap<>();
5454

5555
for (int i = 1; i < root.getChildCount(); i++) {
5656
ASTNode childNode = (ASTNode) root.getChild(i);
@@ -73,10 +73,7 @@ public void analyzeInternal(ASTNode root) throws SemanticException {
7373
}
7474
}
7575

76-
// Initialize props if null, and set default type to native if not specified
77-
if (props == null) {
78-
props = new HashMap<>();
79-
}
76+
// Set default type to hive if not specified
8077
checkCatalogType(props);
8178

8279
CreateCatalogDesc desc = new CreateCatalogDesc(catalogName, comment, locationUrl, ifNotExists, props);
@@ -87,15 +84,10 @@ public void analyzeInternal(ASTNode root) throws SemanticException {
8784
}
8885

8986
private static void checkCatalogType(Map<String, String> props) throws SemanticException {
90-
String catalogType = props.get("type");
91-
// Set default type to native if not specified
92-
if (Strings.isNullOrEmpty(catalogType)) {
93-
props.put("type", CatalogUtil.NATIVE);
94-
return;
95-
}
87+
String catalogType = props.get(CatalogUtil.TYPE);
9688
// Normalize and validate catalog type (fail fast on invalid input)
9789
try {
98-
props.put("type", CatalogUtil.normalizeCatalogType(catalogType));
90+
props.put(CatalogUtil.TYPE, CatalogUtil.normalizeCatalogType(catalogType));
9991
} catch (IllegalArgumentException e) {
10092
throw new SemanticException(String.format("type '%s' is not valid", catalogType));
10193
}

ql/src/java/org/apache/hadoop/hive/ql/ddl/database/create/CreateDatabaseOperation.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.apache.hadoop.hive.ql.ddl.DDLOperationContext;
3232
import org.apache.hadoop.hive.ql.exec.Utilities;
3333
import org.apache.hadoop.hive.ql.metadata.HiveException;
34+
import org.apache.hadoop.hive.ql.metadata.HiveUtils;
3435
import org.apache.hadoop.hive.ql.session.SessionState;
3536

3637
/**
@@ -83,7 +84,7 @@ public int execute() throws HiveException {
8384
private void makeLocationQualified(Database database) throws HiveException {
8485
String catalogName = database.getCatalogName().toLowerCase();
8586
String dbName = database.getName().toLowerCase();
86-
boolean isDefaultCatalog = Warehouse.DEFAULT_CATALOG_NAME.equalsIgnoreCase(catalogName);
87+
boolean isDefaultCatalog = HiveUtils.isDefaultCatalog(catalogName, context.getConf());
8788

8889
// -------- External location --------
8990
if (database.isSetLocationUri()) {

ql/src/java/org/apache/hadoop/hive/ql/metadata/HiveUtils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,4 +553,8 @@ public static String getCurrentCatalogOrDefault(Configuration conf) {
553553
.map(SessionState::getCurrentCatalog)
554554
.orElseGet(() -> getDefaultCatalog(conf));
555555
}
556+
557+
public static boolean isDefaultCatalog(String catName, Configuration conf) {
558+
return catName == null || catName.isEmpty() || getDefaultCatalog(conf).equals(catName);
559+
}
556560
}

ql/src/java/org/apache/hadoop/hive/ql/metadata/SessionHiveMetaStoreClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ private Warehouse getWh() throws MetaException {
181181
}
182182

183183
private boolean isDefaultCatalog(String catName) {
184-
return catName == null || catName.isEmpty() || getDefaultCatalog(conf).equals(catName);
184+
return HiveUtils.isDefaultCatalog(catName, conf);
185185
}
186186

187187
/**

ql/src/java/org/apache/hadoop/hive/ql/parse/EximUtil.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.apache.hadoop.hive.ql.hooks.WriteEntity;
4141
import org.apache.hadoop.hive.ql.metadata.Hive;
4242
import org.apache.hadoop.hive.ql.metadata.HiveException;
43+
import org.apache.hadoop.hive.ql.metadata.HiveUtils;
4344
import org.apache.hadoop.hive.ql.metadata.Partition;
4445
import org.apache.hadoop.hive.ql.metadata.Table;
4546
import org.apache.hadoop.hive.ql.parse.repl.DumpType;
@@ -391,7 +392,7 @@ private static void updateIfCustomDbLocations(Database database, Configuration c
391392
try {
392393
String catName = database.getCatalogName();
393394
String dbName = database.getName().toLowerCase();
394-
boolean isDefaultCatalog = Warehouse.DEFAULT_CATALOG_NAME.equals(catName);
395+
boolean isDefaultCatalog = HiveUtils.isDefaultCatalog(catName, conf);
395396

396397
// external warehouse root
397398
String whLocation = MetastoreConf.getVar(conf,

ql/src/java/org/apache/hadoop/hive/ql/queryhistory/repository/AbstractRepository.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,12 @@ protected Database initDatabase(Hive hive) {
8585
db = hive.getDatabase(QUERY_HISTORY_DB_NAME);
8686
if (db == null) {
8787
LOG.warn("Database ({}) for query history table hasn't been found, auto-creating one", QUERY_HISTORY_DB_NAME);
88-
db = new Database();
89-
// TODO catalog. The Hive Query History functionality is currently limited to the default catalog.
88+
// TODO catalog. The Hive Query History functionality is currently limited to the default catalog. Depend on HIVE-29561
89+
db = new Database(QUERY_HISTORY_DB_NAME, QUERY_HISTORY_DB_COMMENT,
90+
null, null);
9091
db.setCatalogName(getDefaultCatalog(conf));
91-
db.setName(QUERY_HISTORY_DB_NAME);
9292
String location = getDatabaseLocation(db);
93-
db = new Database(QUERY_HISTORY_DB_NAME, QUERY_HISTORY_DB_COMMENT,
94-
location, null);
93+
db.setLocationUri(location);
9594
hive.createDatabase(db, false);
9695
}
9796
return db;

ql/src/java/org/apache/hadoop/hive/ql/security/authorization/StorageBasedAuthorizationProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public void authorizeDbLevelOperations(Privilege[] readRequiredPriv, Privilege[]
133133
try {
134134
initWh();
135135
// TODO catalog. Need to determine auth root path based on catalog name.
136-
// Not sure how to get catalog name yet. Use getWhRoot(String) instead.
136+
// If the catalog name is available, use `wh.getWhRoot(catName)` to obtain the auth path. Depend on HIVE-29562
137137
root = wh.getWhRoot();
138138
// When we have some path in outputs, we should check access on that path, usually happens when
139139
// we have HiveOperation.CREATEDATABASE query with some location

ql/src/test/queries/clientnegative/lockneg_try_lock_cat_db_in_use.q

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
set hive.lock.numretries=0;
22
set hive.support.concurrency=true;
33

4-
CREATE CATALOG testcat LOCATION '/tmp/testcat' COMMENT 'Hive test catalog' PROPERTIES('type'='native');
4+
CREATE CATALOG testcat LOCATION '/tmp/testcat' COMMENT 'Hive test catalog';
55
create database testcat.lockneg9;
66

77
lock database testcat.lockneg9 shared;

ql/src/test/queries/clientpositive/catalog.q

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@ dfs -mkdir -p hdfs:///tmp/test_cat;
88
SHOW CATALOGS;
99

1010
-- CREATE with comment and default location
11-
CREATE CATALOG test_cat COMMENT 'Hive test catalog' PROPERTIES('type'='NATIVE');
11+
CREATE CATALOG test_cat COMMENT 'Hive test catalog';
1212

1313
-- DESCRIBE
1414
DESC CATALOG test_cat;
1515

1616
-- CREATE INE already exists
17-
CREATE CATALOG IF NOT EXISTS test_cat LOCATION 'hdfs:///tmp/test_cat' PROPERTIES('type'='native');
17+
CREATE CATALOG IF NOT EXISTS test_cat LOCATION 'hdfs:///tmp/test_cat';
1818
SHOW CATALOGS;
1919

2020
-- DROP
2121
DROP CATALOG test_cat;
2222
SHOW CATALOGS;
2323

2424
-- CREATE INE doesn't exist
25-
CREATE CATALOG IF NOT EXISTS test_cat LOCATION 'hdfs:///tmp/test_cat' COMMENT 'Hive test catalog' PROPERTIES('type'='native');
25+
CREATE CATALOG IF NOT EXISTS test_cat LOCATION 'hdfs:///tmp/test_cat' COMMENT 'Hive test catalog' PROPERTIES('type'='hive');
2626
SHOW CATALOGS;
2727

2828
-- DROP IE exists
@@ -33,7 +33,7 @@ SHOW CATALOGS;
3333
DROP CATALOG IF EXISTS test_cat;
3434

3535
-- SHOW
36-
CREATE CATALOG test_cat LOCATION 'hdfs:///tmp/test_cat' COMMENT 'Hive test catalog' PROPERTIES('type'='native');
36+
CREATE CATALOG test_cat LOCATION 'hdfs:///tmp/test_cat' COMMENT 'Hive test catalog' PROPERTIES('type'='hive');
3737
SHOW CATALOGS;
3838

3939
-- SHOW pattern

ql/src/test/results/clientpositive/llap/catalog.q.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ PREHOOK: type: SHOWCATALOGS
33
POSTHOOK: query: SHOW CATALOGS
44
POSTHOOK: type: SHOWCATALOGS
55
hive
6-
PREHOOK: query: CREATE CATALOG test_cat COMMENT 'Hive test catalog' PROPERTIES('type'='NATIVE')
6+
PREHOOK: query: CREATE CATALOG test_cat COMMENT 'Hive test catalog'
77
PREHOOK: type: CREATECATALOG
88
PREHOOK: Output: catalog:test_cat
9-
POSTHOOK: query: CREATE CATALOG test_cat COMMENT 'Hive test catalog' PROPERTIES('type'='NATIVE')
9+
POSTHOOK: query: CREATE CATALOG test_cat COMMENT 'Hive test catalog'
1010
POSTHOOK: type: CREATECATALOG
1111
POSTHOOK: Output: catalog:test_cat
1212
PREHOOK: query: DESC CATALOG test_cat

0 commit comments

Comments
 (0)