2727import org .apache .doris .catalog .JdbcResource ;
2828import org .apache .doris .common .Config ;
2929import org .apache .doris .common .DdlException ;
30+ import org .apache .doris .common .ErrorCode ;
31+ import org .apache .doris .common .ErrorReport ;
3032import org .apache .doris .common .UserException ;
3133import org .apache .doris .datasource .ExternalDatabase ;
3234import org .apache .doris .datasource .jdbc .client .JdbcClient ;
4042import com .google .common .base .Preconditions ;
4143import com .google .common .collect .ImmutableSet ;
4244import org .apache .hadoop .hive .conf .HiveConf ;
43- import org .apache .hadoop .hive .metastore .api .FieldSchema ;
4445import org .apache .hadoop .hive .metastore .api .Table ;
4546import org .apache .logging .log4j .LogManager ;
4647import 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 }
0 commit comments