Skip to content

Commit a70484b

Browse files
committed
Implement create catalog.database syntax
1 parent 6e614d4 commit a70484b

File tree

10 files changed

+65
-37
lines changed

10 files changed

+65
-37
lines changed

parser/src/java/org/apache/hadoop/hive/ql/parse/HiveParser.g

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,7 +1165,7 @@ createDatabaseStatement
11651165
@after { popMsg(state); }
11661166
: KW_CREATE (KW_DATABASE|KW_SCHEMA)
11671167
ifNotExists?
1168-
name=identifier
1168+
name=databaseName
11691169
databaseComment?
11701170
dbLocation?
11711171
dbManagedLocation?
@@ -1174,7 +1174,7 @@ createDatabaseStatement
11741174
11751175
| KW_CREATE KW_REMOTE (KW_DATABASE|KW_SCHEMA)
11761176
ifNotExists?
1177-
name=identifier
1177+
name=databaseName
11781178
databaseComment?
11791179
dbConnectorName
11801180
(KW_WITH KW_DBPROPERTIES dbprops=dbProperties)?

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020

2121
import java.util.Map;
2222

23+
import org.apache.commons.lang3.tuple.Pair;
2324
import org.apache.hadoop.hive.metastore.api.DataConnector;
2425
import org.apache.hadoop.hive.metastore.api.Database;
2526
import org.apache.hadoop.hive.metastore.api.DatabaseType;
2627
import org.apache.hadoop.hive.metastore.api.PrincipalType;
28+
import org.apache.hadoop.hive.ql.ErrorMsg;
2729
import org.apache.hadoop.hive.ql.QueryState;
2830
import org.apache.hadoop.hive.ql.exec.TaskFactory;
2931
import org.apache.hadoop.hive.ql.ddl.DDLSemanticAnalyzerFactory.DDLType;
@@ -47,7 +49,12 @@ public CreateDatabaseAnalyzer(QueryState queryState) throws SemanticException {
4749

4850
@Override
4951
public void analyzeInternal(ASTNode root) throws SemanticException {
50-
String databaseName = unescapeIdentifier(root.getChild(0).getText());
52+
Pair<String, String> catDbNamePair = getCatDbNamePair((ASTNode) root.getChild(0));
53+
String catalogName = catDbNamePair.getLeft();
54+
if (catalogName != null && getCatalog(catalogName) == null) {
55+
throw new SemanticException(ErrorMsg.CATALOG_NOT_EXISTS, catalogName);
56+
}
57+
String databaseName = catDbNamePair.getRight();
5158

5259
boolean ifNotExists = false;
5360
String comment = null;
@@ -92,14 +99,15 @@ public void analyzeInternal(ASTNode root) throws SemanticException {
9299
}
93100
}
94101

95-
if (ifNotExists && getDatabase(databaseName, false) != null) {
102+
if (ifNotExists && getDatabase(catalogName, databaseName, false) != null) {
96103
return;
97104
}
98105

99106
CreateDatabaseDesc desc = null;
100107
Database database = new Database(databaseName, comment, locationUri, props);
108+
database.setCatalogName(catalogName);
101109
if (type.equalsIgnoreCase(DatabaseType.NATIVE.name())) {
102-
desc = new CreateDatabaseDesc(databaseName, comment, locationUri, managedLocationUri, ifNotExists, props);
110+
desc = new CreateDatabaseDesc(catalogName, databaseName, comment, locationUri, managedLocationUri, ifNotExists, props);
103111
database.setType(DatabaseType.NATIVE);
104112
// database = new Database(databaseName, comment, locationUri, props);
105113
if (managedLocationUri != null) {
@@ -109,7 +117,7 @@ public void analyzeInternal(ASTNode root) throws SemanticException {
109117
String remoteDbName = databaseName;
110118
if (props != null && props.get("connector.remoteDbName") != null) // TODO finalize the property name
111119
remoteDbName = props.get("connector.remoteDbName");
112-
desc = new CreateDatabaseDesc(databaseName, comment, locationUri, null, ifNotExists, props, type,
120+
desc = new CreateDatabaseDesc(catalogName, databaseName, comment, locationUri, null, ifNotExists, props, type,
113121
connectorName, remoteDbName);
114122
database.setConnector_name(connectorName);
115123
database.setType(DatabaseType.REMOTE);

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
public class CreateDatabaseDesc implements DDLDesc, Serializable {
3636
private static final long serialVersionUID = 1L;
3737

38+
private final String catalogName;
3839
private final String databaseName;
3940
private final String comment;
4041
private final String locationUri;
@@ -45,13 +46,14 @@ public class CreateDatabaseDesc implements DDLDesc, Serializable {
4546
private final String remoteDbName;
4647
private final Map<String, String> dbProperties;
4748

48-
public CreateDatabaseDesc(String databaseName, String comment, String locationUri, String managedLocationUri,
49+
public CreateDatabaseDesc(String catalogName, String databaseName, String comment, String locationUri, String managedLocationUri,
4950
boolean ifNotExists, Map<String, String> dbProperties) {
50-
this(databaseName, comment, locationUri, managedLocationUri, ifNotExists, dbProperties, "NATIVE", null, null);
51+
this(catalogName, databaseName, comment, locationUri, managedLocationUri, ifNotExists, dbProperties, "NATIVE", null, null);
5152
}
5253

53-
public CreateDatabaseDesc(String databaseName, String comment, String locationUri, String managedLocationUri,
54+
public CreateDatabaseDesc(String catalogName, String databaseName, String comment, String locationUri, String managedLocationUri,
5455
boolean ifNotExists, Map<String, String> dbProperties, String dbtype, String connectorName, String remoteDbName) {
56+
this.catalogName = catalogName;
5557
this.databaseName = databaseName;
5658
this.comment = comment;
5759
if (dbtype != null && dbtype.equalsIgnoreCase("REMOTE")) {
@@ -80,6 +82,11 @@ public Map<String, String> getDatabaseProperties() {
8082
return dbProperties;
8183
}
8284

85+
@Explain(displayName="catalogName", explainLevels = { Level.USER, Level.DEFAULT, Level.EXTENDED })
86+
public String getCatalogName() {
87+
return catalogName;
88+
}
89+
8390
@Explain(displayName="name", explainLevels = { Level.USER, Level.DEFAULT, Level.EXTENDED })
8491
public String getName() {
8592
return databaseName;

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public CreateDatabaseOperation(DDLOperationContext context, CreateDatabaseDesc d
4747
public int execute() throws HiveException {
4848
Database database = new Database(desc.getName(), desc.getComment(), desc.getLocationUri(),
4949
desc.getDatabaseProperties());
50+
database.setCatalogName(desc.getCatalogName());
5051
database.setOwnerName(SessionState.getUserFromAuthenticator());
5152
database.setOwnerType(PrincipalType.USER);
5253
database.setType(desc.getDatabaseType());
@@ -55,12 +56,12 @@ public int execute() throws HiveException {
5556
if (desc.getManagedLocationUri() != null) {
5657
database.setManagedLocationUri(desc.getManagedLocationUri());
5758
}
58-
makeLocationQualified(database); // TODO add catalog prefix for db location
59+
makeLocationQualified(database); // TODO catalog. Add catalog prefix for location
5960
if (database.getLocationUri().equalsIgnoreCase(database.getManagedLocationUri())) {
6061
throw new HiveException("Managed and external locations for database cannot be the same");
6162
}
6263
} else if (desc.getDatabaseType() == DatabaseType.REMOTE) {
63-
makeLocationQualified(database); // TODO add catalog prefix for db location
64+
makeLocationQualified(database); // TODO catalog. Add catalog prefix for location
6465
database.setConnector_name(desc.getConnectorName());
6566
database.setRemote_dbname(desc.getRemoteDbName());
6667
} else { // should never be here

ql/src/java/org/apache/hadoop/hive/ql/ddl/database/drop/DropDatabaseAnalyzer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void analyzeInternal(ASTNode root) throws SemanticException {
5959
boolean isSoftDelete = HiveConf.getBoolVar(conf, HiveConf.ConfVars.HIVE_ACID_LOCKLESS_READS_ENABLED);
6060

6161
String catalogName = catDbNamePair.getLeft();
62-
if (getCatalog(catalogName) == null) {
62+
if (catalogName != null && getCatalog(catalogName) == null) {
6363
throw new SemanticException(ErrorMsg.CATALOG_NOT_EXISTS, catalogName);
6464
}
6565
String databaseName = catDbNamePair.getRight();

ql/src/java/org/apache/hadoop/hive/ql/exec/repl/bootstrap/load/LoadDatabase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ private boolean isDbEmpty(String dbName) throws HiveException {
150150

151151
private Task<?> createDbTask(Database dbObj) throws MetaException {
152152
// note that we do not set location - for repl load, we want that auto-created.
153-
CreateDatabaseDesc createDbDesc = new CreateDatabaseDesc(dbObj.getName(), dbObj.getDescription(),
153+
CreateDatabaseDesc createDbDesc = new CreateDatabaseDesc(dbObj.getCatalogName(), dbObj.getName(), dbObj.getDescription(),
154154
getDbLocation(dbObj), getDbManagedLocation(dbObj), false, updateDbProps(dbObj, context.dumpDirectory));
155155
// If it exists, we want this to be an error condition. Repl Load is not intended to replace a
156156
// db.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ public void dropCatalog(String catName, boolean ignoreUnknownCat)
668668
public void createDatabase(Database db, boolean ifNotExist)
669669
throws AlreadyExistsException, HiveException {
670670
try {
671-
db.setCatalogName(SessionState.get().getCurrentCatalog());
671+
db.setCatalogName(Objects.requireNonNullElse(db.getCatalogName(), SessionState.get().getCurrentCatalog()));
672672
getMSC().createDatabase(db);
673673
} catch (AlreadyExistsException e) {
674674
if (!ifNotExist) {
@@ -725,7 +725,7 @@ public void dropDatabase(String name, boolean deleteData, boolean ignoreUnknownD
725725
*/
726726
public void dropDatabase(String name, boolean deleteData, boolean ignoreUnknownDb, boolean cascade)
727727
throws HiveException, NoSuchObjectException {
728-
dropDatabase(new DropDatabaseDesc(getDefaultCatalog(conf) ,name, ignoreUnknownDb, cascade, deleteData)); //TODO check the actual catalog
728+
dropDatabase(new DropDatabaseDesc(getDefaultCatalog(conf) ,name, ignoreUnknownDb, cascade, deleteData)); // TODO catalog. check the actual catalog
729729
}
730730

731731
public void dropDatabase(DropDatabaseDesc desc)

ql/src/java/org/apache/hadoop/hive/ql/parse/repl/load/message/CreateDatabaseHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ public List<Task<?>> handle(Context context)
5656
Database db = metaData.getDatabase();
5757
String destinationDBName =
5858
context.dbName == null ? db.getName() : context.dbName;
59+
String destinationCatalogName = db.getCatalogName(); // TODO catalog. Need to double check the catalog here.
5960

6061
CreateDatabaseDesc createDatabaseDesc =
61-
new CreateDatabaseDesc(destinationDBName, db.getDescription(), null, null, true, db.getParameters());
62+
new CreateDatabaseDesc(destinationCatalogName, destinationDBName, db.getDescription(), null, null, true, db.getParameters());
6263
Task<DDLWork> createDBTask = TaskFactory.get(
6364
new DDLWork(new HashSet<>(), new HashSet<>(), createDatabaseDesc, true,
6465
context.getDumpDirectory(), context.getMetricCollector()), context.hiveConf);

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,25 @@ CREATE CATALOG testcat LOCATION '/tmp/testcat' COMMENT 'Hive test catalog';
1111
-- Check catalogs list
1212
SHOW CATALOGS;
1313

14+
-- CREATE DATABASE in new catalog testcat by catalog.db pattern
15+
CREATE DATABASE testcat.testdb_1;
16+
1417
-- Switch the catalog from hive to 'testcat'
1518
SET CATALOG testcat;
1619

17-
-- CREATE DATABASE in default catalog 'hive'
18-
CREATE DATABASE testdb_new;
20+
-- CREATE DATABASE in new catalog testcat
21+
CREATE DATABASE testdb_2;
1922

2023
-- Check databases in catalog 'testcat',
21-
-- The list of databases in the catalog 'hive' should only contain the default and the testdb_new.
24+
-- The list of databases in the catalog 'hive' should contain default and testdb_1 and testdb_2.
2225
SHOW DATABASES;
2326

2427
-- Switch database by catalog.db pattern
25-
USE testcat.testdb_new;
28+
USE testcat.testdb_1;
2629

2730
-- Drop database by catalog.db pattern
28-
DROP DATABASE testcat.testdb_new;
31+
DROP DATABASE testcat.testdb_1;
2932

3033
-- Check databases in catalog 'testcat',
31-
-- The list of databases in the catalog 'hive' should only contain the default.
34+
-- The list of databases in the catalog 'hive' should contain default and testdb_2.
3235
SHOW DATABASES;

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

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,40 +24,48 @@ POSTHOOK: query: SHOW CATALOGS
2424
POSTHOOK: type: SHOWCATALOGS
2525
hive
2626
testcat
27+
PREHOOK: query: CREATE DATABASE testcat.testdb_1
28+
PREHOOK: type: CREATEDATABASE
29+
PREHOOK: Output: database:testdb_1
30+
POSTHOOK: query: CREATE DATABASE testcat.testdb_1
31+
POSTHOOK: type: CREATEDATABASE
32+
POSTHOOK: Output: database:testdb_1
2733
PREHOOK: query: SET CATALOG testcat
2834
PREHOOK: type: SWITCHCATALOG
2935
PREHOOK: Input: catalog:testcat
3036
POSTHOOK: query: SET CATALOG testcat
3137
POSTHOOK: type: SWITCHCATALOG
3238
POSTHOOK: Input: catalog:testcat
33-
PREHOOK: query: CREATE DATABASE testdb_new
39+
PREHOOK: query: CREATE DATABASE testdb_2
3440
PREHOOK: type: CREATEDATABASE
35-
PREHOOK: Output: database:testdb_new
36-
POSTHOOK: query: CREATE DATABASE testdb_new
41+
PREHOOK: Output: database:testdb_2
42+
POSTHOOK: query: CREATE DATABASE testdb_2
3743
POSTHOOK: type: CREATEDATABASE
38-
POSTHOOK: Output: database:testdb_new
44+
POSTHOOK: Output: database:testdb_2
3945
PREHOOK: query: SHOW DATABASES
4046
PREHOOK: type: SHOWDATABASES
4147
POSTHOOK: query: SHOW DATABASES
4248
POSTHOOK: type: SHOWDATABASES
4349
default
44-
testdb_new
45-
PREHOOK: query: USE testcat.testdb_new
50+
testdb_1
51+
testdb_2
52+
PREHOOK: query: USE testcat.testdb_1
4653
PREHOOK: type: SWITCHDATABASE
47-
PREHOOK: Input: database:testdb_new
48-
POSTHOOK: query: USE testcat.testdb_new
54+
PREHOOK: Input: database:testdb_1
55+
POSTHOOK: query: USE testcat.testdb_1
4956
POSTHOOK: type: SWITCHDATABASE
50-
POSTHOOK: Input: database:testdb_new
51-
PREHOOK: query: DROP DATABASE testcat.testdb_new
57+
POSTHOOK: Input: database:testdb_1
58+
PREHOOK: query: DROP DATABASE testcat.testdb_1
5259
PREHOOK: type: DROPDATABASE
53-
PREHOOK: Input: database:testdb_new
54-
PREHOOK: Output: database:testdb_new
55-
POSTHOOK: query: DROP DATABASE testcat.testdb_new
60+
PREHOOK: Input: database:testdb_1
61+
PREHOOK: Output: database:testdb_1
62+
POSTHOOK: query: DROP DATABASE testcat.testdb_1
5663
POSTHOOK: type: DROPDATABASE
57-
POSTHOOK: Input: database:testdb_new
58-
POSTHOOK: Output: database:testdb_new
64+
POSTHOOK: Input: database:testdb_1
65+
POSTHOOK: Output: database:testdb_1
5966
PREHOOK: query: SHOW DATABASES
6067
PREHOOK: type: SHOWDATABASES
6168
POSTHOOK: query: SHOW DATABASES
6269
POSTHOOK: type: SHOWDATABASES
6370
default
71+
testdb_2

0 commit comments

Comments
 (0)