[Bug] Support if not exists in create table like stmt (#5368)
Currently we support syntax `create table xx if not exists like xxx`, but `if not exists` does not work well.
This commit is contained in:
@ -45,7 +45,7 @@ public class CreateTableLikeStmt extends DdlStmt {
|
||||
this.existedTableName = existedTableName;
|
||||
}
|
||||
|
||||
public boolean isSetIfNotExists() {
|
||||
public boolean isIfNotExists() {
|
||||
return ifNotExists;
|
||||
}
|
||||
|
||||
|
||||
@ -169,6 +169,8 @@ public class CreateTableStmt extends DdlStmt {
|
||||
|
||||
public void addColumnDef(ColumnDef columnDef) { columnDefs.add(columnDef); }
|
||||
|
||||
public void setIfNotExists(boolean ifNotExists) { this.ifNotExists = ifNotExists; }
|
||||
|
||||
public boolean isSetIfNotExists() {
|
||||
return ifNotExists;
|
||||
}
|
||||
|
||||
@ -3018,6 +3018,7 @@ public class Catalog {
|
||||
}
|
||||
CreateTableStmt parsedCreateTableStmt = (CreateTableStmt) SqlParserUtils.parseAndAnalyzeStmt(createTableStmt.get(0), ConnectContext.get());
|
||||
parsedCreateTableStmt.setTableName(stmt.getTableName());
|
||||
parsedCreateTableStmt.setIfNotExists(stmt.isIfNotExists());
|
||||
createTable(parsedCreateTableStmt);
|
||||
} catch (UserException e) {
|
||||
throw new DdlException("Failed to execute CREATE TABLE LIKE " + stmt.getExistedTableName() + ". Reason: " + e.getMessage());
|
||||
|
||||
@ -218,6 +218,21 @@ public class CreateTableLikeTest {
|
||||
String newTblName8 = "testMysqlTbl_like";
|
||||
String existedTblName8 = "testMysqlTbl";
|
||||
checkCreateMysqlTableLike(createNonOlapTableSql, createTableLikeSql8, newDbName8, existedDbName8, newTblName8, existedTblName8);
|
||||
|
||||
// 9. test if not exist
|
||||
String createTableLikeSql9 = "create table test.testMysqlTbl_like like test.testMysqlTbl";
|
||||
try {
|
||||
createTableLike(createTableLikeSql9);
|
||||
Assert.fail();
|
||||
} catch (Exception e) {
|
||||
Assert.assertTrue(e.getMessage().contains("already exists"));
|
||||
}
|
||||
createTableLikeSql9 = "create table if not exists test.testMysqlTbl_like like test.testMysqlTbl";
|
||||
try {
|
||||
createTableLike(createTableLikeSql9);
|
||||
} catch (Exception e) {
|
||||
Assert.fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user