Skip to content

Commit 060d27d

Browse files
wsjzHappenLee
authored andcommitted
[feature](external) process tbl/db exist when create/drop db/tbl (apache#33119)
Issue Number: apache#31442
1 parent c37887e commit 060d27d

8 files changed

Lines changed: 198 additions & 39 deletions

File tree

‎fe/fe-core/src/main/java/org/apache/doris/datasource/hive/HiveMetadataOps.java‎

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import org.apache.doris.catalog.JdbcResource;
2828
import org.apache.doris.common.Config;
2929
import org.apache.doris.common.DdlException;
30+
import org.apache.doris.common.ErrorCode;
31+
import org.apache.doris.common.ErrorReport;
3032
import org.apache.doris.common.UserException;
3133
import org.apache.doris.datasource.ExternalDatabase;
3234
import org.apache.doris.datasource.jdbc.client.JdbcClient;
@@ -40,7 +42,6 @@
4042
import com.google.common.base.Preconditions;
4143
import com.google.common.collect.ImmutableSet;
4244
import org.apache.hadoop.hive.conf.HiveConf;
43-
import org.apache.hadoop.hive.metastore.api.FieldSchema;
4445
import org.apache.hadoop.hive.metastore.api.Table;
4546
import org.apache.logging.log4j.LogManager;
4647
import org.apache.logging.log4j.Logger;
@@ -99,6 +100,14 @@ public void createDb(CreateDbStmt stmt) throws DdlException {
99100
String fullDbName = stmt.getFullDbName();
100101
Map<String, String> properties = stmt.getProperties();
101102
long dbId = Env.getCurrentEnv().getNextId();
103+
if (databaseExist(fullDbName)) {
104+
if (stmt.isSetIfNotExists()) {
105+
LOG.info("create database[{}] which already exists", fullDbName);
106+
return;
107+
} else {
108+
ErrorReport.reportDdlException(ErrorCode.ERR_DB_CREATE_EXISTS, fullDbName);
109+
}
110+
}
102111
try {
103112
HiveDatabaseMetadata catalogDatabase = new HiveDatabaseMetadata();
104113
catalogDatabase.setDbName(fullDbName);
@@ -119,6 +128,14 @@ public void createDb(CreateDbStmt stmt) throws DdlException {
119128
@Override
120129
public void dropDb(DropDbStmt stmt) throws DdlException {
121130
String dbName = stmt.getDbName();
131+
if (!databaseExist(dbName)) {
132+
if (stmt.isSetIfExists()) {
133+
LOG.info("drop database[{}] which does not exist", dbName);
134+
return;
135+
} else {
136+
ErrorReport.reportDdlException(ErrorCode.ERR_DB_DROP_EXISTS, dbName);
137+
}
138+
}
122139
try {
123140
client.dropDatabase(dbName);
124141
catalog.onRefresh(true);
@@ -135,6 +152,14 @@ public void createTable(CreateTableStmt stmt) throws UserException {
135152
if (db == null) {
136153
throw new UserException("Failed to get database: '" + dbName + "' in catalog: " + catalog.getName());
137154
}
155+
if (tableExist(dbName, tblName)) {
156+
if (stmt.isSetIfNotExists()) {
157+
LOG.info("create table[{}] which already exists", tblName);
158+
return;
159+
} else {
160+
ErrorReport.reportDdlException(ErrorCode.ERR_TABLE_EXISTS_ERROR, tblName);
161+
}
162+
}
138163
try {
139164
Map<String, String> props = stmt.getProperties();
140165
String fileFormat = props.getOrDefault(FILE_FORMAT_KEY, Config.hive_default_file_format);
@@ -186,24 +211,21 @@ public void createTable(CreateTableStmt stmt) throws UserException {
186211
}
187212
}
188213

189-
private static List<FieldSchema> parsePartitionKeys(Map<String, String> props) {
190-
List<FieldSchema> parsedKeys = new ArrayList<>();
191-
String pkStr = props.getOrDefault("partition_keys", "");
192-
if (pkStr.isEmpty()) {
193-
return parsedKeys;
194-
} else {
195-
// TODO: parse string to partition keys list
196-
return parsedKeys;
197-
}
198-
}
199-
200214
@Override
201215
public void dropTable(DropTableStmt stmt) throws DdlException {
202216
String dbName = stmt.getDbName();
203217
ExternalDatabase<?> db = catalog.getDbNullable(stmt.getDbName());
204218
if (db == null) {
205219
throw new DdlException("Failed to get database: '" + dbName + "' in catalog: " + catalog.getName());
206220
}
221+
if (!tableExist(dbName, stmt.getTableName())) {
222+
if (stmt.isSetIfExists()) {
223+
LOG.info("drop table[{}] which does not exist", dbName);
224+
return;
225+
} else {
226+
ErrorReport.reportDdlException(ErrorCode.ERR_UNKNOWN_TABLE, stmt.getTableName(), dbName);
227+
}
228+
}
207229
try {
208230
client.dropTable(dbName, stmt.getTableName());
209231
db.setUnInitialized(true);
@@ -222,6 +244,11 @@ public boolean tableExist(String dbName, String tblName) {
222244
return client.tableExists(dbName, tblName);
223245
}
224246

247+
@Override
248+
public boolean databaseExist(String dbName) {
249+
return listDatabaseNames().contains(dbName);
250+
}
251+
225252
public List<String> listDatabaseNames() {
226253
return client.getAllDatabases();
227254
}

‎fe/fe-core/src/main/java/org/apache/doris/datasource/hive/HiveUtil.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.google.common.collect.ImmutableMap;
3030
import org.apache.commons.lang3.StringUtils;
3131
import org.apache.hadoop.hive.common.FileUtils;
32+
import org.apache.hadoop.hive.metastore.api.Database;
3233
import org.apache.hadoop.hive.metastore.api.FieldSchema;
3334
import org.apache.hadoop.hive.metastore.api.Partition;
3435
import org.apache.hadoop.hive.metastore.api.SerDeInfo;
@@ -255,4 +256,15 @@ private static Pair<List<FieldSchema>, List<FieldSchema>> toHiveSchema(List<Colu
255256
}
256257
return Pair.of(hiveCols, hiveParts);
257258
}
259+
260+
public static Database toHiveDatabase(HiveDatabaseMetadata hiveDb) {
261+
Database database = new Database();
262+
database.setName(hiveDb.getDbName());
263+
if (StringUtils.isNotEmpty(hiveDb.getLocationUri())) {
264+
database.setLocationUri(hiveDb.getLocationUri());
265+
}
266+
database.setParameters(hiveDb.getProperties());
267+
database.setDescription(hiveDb.getComment());
268+
return database;
269+
}
258270
}

‎fe/fe-core/src/main/java/org/apache/doris/datasource/hive/ThriftHMSCachedClient.java‎

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import com.google.common.collect.ImmutableList;
3232
import com.google.common.collect.ImmutableMap;
3333
import com.google.common.collect.Lists;
34-
import org.apache.commons.lang3.StringUtils;
3534
import org.apache.hadoop.hive.common.StatsSetupConst;
3635
import org.apache.hadoop.hive.common.ValidReaderWriteIdList;
3736
import org.apache.hadoop.hive.common.ValidTxnList;
@@ -137,7 +136,7 @@ public void createDatabase(DatabaseMetadata db) {
137136
if (db instanceof HiveDatabaseMetadata) {
138137
HiveDatabaseMetadata hiveDb = (HiveDatabaseMetadata) db;
139138
ugiDoAs(() -> {
140-
client.client.createDatabase(toHiveDatabase(hiveDb));
139+
client.client.createDatabase(HiveUtil.toHiveDatabase(hiveDb));
141140
return null;
142141
});
143142
}
@@ -150,17 +149,6 @@ public void createDatabase(DatabaseMetadata db) {
150149
}
151150
}
152151

153-
private static Database toHiveDatabase(HiveDatabaseMetadata hiveDb) {
154-
Database database = new Database();
155-
database.setName(hiveDb.getDbName());
156-
if (StringUtils.isNotEmpty(hiveDb.getLocationUri())) {
157-
database.setLocationUri(hiveDb.getLocationUri());
158-
}
159-
database.setParameters(hiveDb.getProperties());
160-
database.setDescription(hiveDb.getComment());
161-
return database;
162-
}
163-
164152
@Override
165153
public void createTable(TableMetadata tbl, boolean ignoreIfExists) {
166154
if (tableExists(tbl.getDbName(), tbl.getTableName())) {

‎fe/fe-core/src/main/java/org/apache/doris/datasource/iceberg/IcebergMetadataOps.java‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import org.apache.doris.catalog.StructField;
2626
import org.apache.doris.catalog.StructType;
2727
import org.apache.doris.common.DdlException;
28+
import org.apache.doris.common.ErrorCode;
29+
import org.apache.doris.common.ErrorReport;
2830
import org.apache.doris.common.UserException;
2931
import org.apache.doris.datasource.DorisTypeVisitor;
3032
import org.apache.doris.datasource.ExternalCatalog;
@@ -67,6 +69,10 @@ public boolean tableExist(String dbName, String tblName) {
6769
return catalog.tableExists(TableIdentifier.of(dbName, tblName));
6870
}
6971

72+
public boolean databaseExist(String dbName) {
73+
return nsCatalog.namespaceExists(Namespace.of(dbName));
74+
}
75+
7076
public List<String> listDatabaseNames() {
7177
return nsCatalog.listNamespaces().stream()
7278
.map(e -> e.toString())
@@ -84,6 +90,14 @@ public void createDb(CreateDbStmt stmt) throws DdlException {
8490
SupportsNamespaces nsCatalog = (SupportsNamespaces) catalog;
8591
String dbName = stmt.getFullDbName();
8692
Map<String, String> properties = stmt.getProperties();
93+
if (databaseExist(dbName)) {
94+
if (stmt.isSetIfNotExists()) {
95+
LOG.info("create database[{}] which already exists", dbName);
96+
return;
97+
} else {
98+
ErrorReport.reportDdlException(ErrorCode.ERR_DB_CREATE_EXISTS, dbName);
99+
}
100+
}
87101
nsCatalog.createNamespace(Namespace.of(dbName), properties);
88102
dorisCatalog.onRefresh(true);
89103
}
@@ -97,6 +111,14 @@ public void dropDb(DropDbStmt stmt) throws DdlException {
97111
dorisCatalog.getIdToDb().remove(aLong);
98112
dorisCatalog.getDbNameToId().remove(dbName);
99113
}
114+
if (!databaseExist(dbName)) {
115+
if (stmt.isSetIfExists()) {
116+
LOG.info("drop database[{}] which does not exist", dbName);
117+
return;
118+
} else {
119+
ErrorReport.reportDdlException(ErrorCode.ERR_DB_DROP_EXISTS, dbName);
120+
}
121+
}
100122
nsCatalog.dropNamespace(Namespace.of(dbName));
101123
dorisCatalog.onRefresh(true);
102124
}
@@ -109,6 +131,14 @@ public void createTable(CreateTableStmt stmt) throws UserException {
109131
throw new UserException("Failed to get database: '" + dbName + "' in catalog: " + dorisCatalog.getName());
110132
}
111133
String tableName = stmt.getTableName();
134+
if (tableExist(dbName, tableName)) {
135+
if (stmt.isSetIfNotExists()) {
136+
LOG.info("create table[{}] which already exists", tableName);
137+
return;
138+
} else {
139+
ErrorReport.reportDdlException(ErrorCode.ERR_TABLE_EXISTS_ERROR, tableName);
140+
}
141+
}
112142
List<Column> columns = stmt.getColumns();
113143
List<StructField> collect = columns.stream()
114144
.map(col -> new StructField(col.getName(), col.getType(), col.getComment(), col.isAllowNull()))
@@ -132,6 +162,14 @@ public void dropTable(DropTableStmt stmt) throws DdlException {
132162
throw new DdlException("Failed to get database: '" + dbName + "' in catalog: " + dorisCatalog.getName());
133163
}
134164
String tableName = stmt.getTableName();
165+
if (!tableExist(dbName, tableName)) {
166+
if (stmt.isSetIfExists()) {
167+
LOG.info("drop table[{}] which does not exist", dbName);
168+
return;
169+
} else {
170+
ErrorReport.reportDdlException(ErrorCode.ERR_UNKNOWN_TABLE, tableName, dbName);
171+
}
172+
}
135173
catalog.dropTable(TableIdentifier.of(dbName, tableName));
136174
db.setUnInitialized(true);
137175
}

‎fe/fe-core/src/main/java/org/apache/doris/datasource/operations/ExternalMetadataOps.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,6 @@ public interface ExternalMetadataOps {
7979
* @return
8080
*/
8181
boolean tableExist(String dbName, String tblName);
82+
83+
boolean databaseExist(String dbName);
8284
}

0 commit comments

Comments
 (0)