[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:
xxiao2018
2021-02-07 22:41:55 +08:00
committed by GitHub
parent 462efeaf39
commit f12f32da11
4 changed files with 19 additions and 1 deletions

View File

@ -45,7 +45,7 @@ public class CreateTableLikeStmt extends DdlStmt {
this.existedTableName = existedTableName;
}
public boolean isSetIfNotExists() {
public boolean isIfNotExists() {
return ifNotExists;
}

View File

@ -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;
}

View File

@ -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());

View File

@ -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