Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] (nereids)implement DropTableCommand in nereids #47594

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ supportedDropStatement
((FROM | IN) database=identifier)? properties=propertyClause #dropFile
| DROP WORKLOAD POLICY (IF EXISTS)? name=identifierOrText #dropWorkloadPolicy
| DROP REPOSITORY name=identifier #dropRepository
| DROP TABLE (IF EXISTS)? name=multipartIdentifier FORCE? #dropTable
| DROP (DATABASE | SCHEMA) (IF EXISTS)? name=multipartIdentifier FORCE? #dropDatabase
| DROP statementScope? FUNCTION (IF EXISTS)?
functionIdentifier LEFT_PAREN functionArguments? RIGHT_PAREN #dropFunction
Expand Down Expand Up @@ -718,8 +719,7 @@ fromRollup
;

unsupportedDropStatement
: DROP TABLE (IF EXISTS)? name=multipartIdentifier FORCE? #dropTable
| DROP VIEW (IF EXISTS)? name=multipartIdentifier #dropView
: DROP VIEW (IF EXISTS)? name=multipartIdentifier #dropView
| DROP RESOURCE (IF EXISTS)? name=identifierOrText #dropResource
| DROP ROW POLICY (IF EXISTS)? policyName=identifier
ON tableName=multipartIdentifier
Expand Down
20 changes: 20 additions & 0 deletions fe/fe-core/src/main/java/org/apache/doris/analysis/TableName.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.doris.common.io.Writable;
import org.apache.doris.datasource.InternalCatalog;
import org.apache.doris.persist.gson.GsonUtils;
import org.apache.doris.qe.ConnectContext;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
Expand Down Expand Up @@ -95,6 +96,25 @@ public void analyze(Analyzer analyzer) throws AnalysisException {
}
}

public void analyze(ConnectContext ctx) {
if (Strings.isNullOrEmpty(ctl)) {
ctl = ctx.getDefaultCatalog();
if (Strings.isNullOrEmpty(ctl)) {
ctl = InternalCatalog.INTERNAL_CATALOG_NAME;
}
}
if (Strings.isNullOrEmpty(db)) {
db = ctx.getDatabase();
if (Strings.isNullOrEmpty(db)) {
throw new org.apache.doris.nereids.exceptions.AnalysisException("No database selected");
}
}

if (Strings.isNullOrEmpty(tbl)) {
throw new org.apache.doris.nereids.exceptions.AnalysisException("Table name is null");
}
}

public String getCtl() {
return ctl;
}
Expand Down
13 changes: 11 additions & 2 deletions fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java
Original file line number Diff line number Diff line change
Expand Up @@ -4232,9 +4232,18 @@ public void replayAlterExternalTableSchema(String dbName, String tableName, List

// Drop table
public void dropTable(DropTableStmt stmt) throws DdlException {
CatalogIf<?> catalogIf = catalogMgr.getCatalogOrException(stmt.getCatalogName(),
if (stmt == null) {
throw new DdlException("DropTableStmt is null");
}
dropTable(stmt.getCatalogName(), stmt.getDbName(), stmt.getTableName(), stmt.isView(),
stmt.isMaterializedView(), stmt.isSetIfExists(), stmt.isForceDrop());
}

public void dropTable(String catalogName, String dbName, String tableName, boolean isView, boolean isMtmv,
boolean ifExists, boolean force) throws DdlException {
CatalogIf<?> catalogIf = catalogMgr.getCatalogOrException(catalogName,
catalog -> new DdlException(("Unknown catalog " + catalog)));
catalogIf.dropTable(stmt);
catalogIf.dropTable(dbName, tableName, isView, isMtmv, ifExists, force);
}

public boolean unprotectDropTable(Database db, Table table, boolean isForceDrop, boolean isReplay,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ default void dropDb(DropDbStmt stmt) throws DdlException {

void dropTable(DropTableStmt stmt) throws DdlException;

void dropTable(String dbName, String tableName, boolean isView, boolean isMtmv, boolean ifExists,
boolean force) throws DdlException;

void truncateTable(TruncateTableStmt truncateTableStmt) throws DdlException;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1021,14 +1021,24 @@ public void replayCreateTable(String dbName, String tblName) {

@Override
public void dropTable(DropTableStmt stmt) throws DdlException {
if (stmt == null) {
throw new DdlException("DropTableStmt is null");
}
dropTable(stmt.getDbName(), stmt.getTableName(), stmt.isView(), stmt.isMaterializedView(), stmt.isSetIfExists(),
stmt.isForceDrop());
}

@Override
public void dropTable(String dbName, String tableName, boolean isView, boolean isMtmv, boolean ifExists,
boolean force) throws DdlException {
makeSureInitialized();
if (metadataOps == null) {
LOG.warn("dropTable not implemented");
return;
}
try {
metadataOps.dropTable(stmt);
DropInfo info = new DropInfo(getName(), stmt.getDbName(), stmt.getTableName());
metadataOps.dropTable(dbName, tableName, ifExists);
DropInfo info = new DropInfo(getName(), dbName, tableName);
Env.getCurrentEnv().getEditLog().logDropTable(info);
} catch (Exception e) {
LOG.warn("Failed to drop a table", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -877,11 +877,18 @@ public void replayRenameDatabase(String dbName, String newDbName) {

// Drop table
public void dropTable(DropTableStmt stmt) throws DdlException {
if (stmt == null) {
throw new DdlException("DropTableStmt is null");
}
dropTable(stmt.getDbName(), stmt.getTableName(), stmt.isView(), stmt.isMaterializedView(),
stmt.isSetIfExists(), stmt.isForceDrop());
}

public void dropTable(String dbName, String tableName, boolean isView, boolean isMtmv,
boolean ifExists, boolean force) throws DdlException {
Map<String, Long> costTimes = new TreeMap<String, Long>();
StopWatch watch = StopWatch.createStarted();
String dbName = stmt.getDbName();
String tableName = stmt.getTableName();
LOG.info("begin to drop table: {} from db: {}, is force: {}", tableName, dbName, stmt.isForceDrop());
LOG.info("begin to drop table: {} from db: {}, is force: {}", tableName, dbName, force);

// check database
Database db = getDbOrDdlException(dbName);
Expand All @@ -895,15 +902,15 @@ public void dropTable(DropTableStmt stmt) throws DdlException {
try {
Table table = db.getTableNullable(tableName);
if (table == null) {
if (stmt.isSetIfExists()) {
if (ifExists) {
LOG.info("drop table[{}] which does not exist", tableName);
return;
} else {
ErrorReport.reportDdlException(ErrorCode.ERR_UNKNOWN_TABLE, tableName, dbName);
}
}
// Check if a view
if (stmt.isView()) {
if (isView) {
if (!(table instanceof View)) {
ErrorReport.reportDdlException(ErrorCode.ERR_WRONG_OBJECT, dbName, tableName, "VIEW",
genDropHint(dbName, table));
Expand All @@ -915,43 +922,43 @@ public void dropTable(DropTableStmt stmt) throws DdlException {
}
}

if (!stmt.isMaterializedView() && table instanceof MTMV) {
if (!isMtmv && table instanceof MTMV) {
ErrorReport.reportDdlException(ErrorCode.ERR_WRONG_OBJECT, dbName, tableName, "TABLE",
genDropHint(dbName, table));
} else if (stmt.isMaterializedView() && !(table instanceof MTMV)) {
} else if (isMtmv && !(table instanceof MTMV)) {
ErrorReport.reportDdlException(ErrorCode.ERR_WRONG_OBJECT, dbName, tableName, "MTMV",
genDropHint(dbName, table));
}

if (!stmt.isForceDrop()) {
if (!force) {
if (Env.getCurrentGlobalTransactionMgr().existCommittedTxns(db.getId(), table.getId(), null)) {
throw new DdlException(
"There are still some transactions in the COMMITTED state waiting to be completed. "
+ "The table [" + tableName
+ "] cannot be dropped. If you want to forcibly drop(cannot be recovered),"
+ " please use \"DROP table FORCE\".");
"There are still some transactions in the COMMITTED state waiting to be completed. "
+ "The table [" + tableName
+ "] cannot be dropped. If you want to forcibly drop(cannot be recovered),"
+ " please use \"DROP table FORCE\".");
}
watch.split();
costTimes.put("2:existCommittedTxns", watch.getSplitTime());
}

if (table instanceof OlapTable && !stmt.isForceDrop()) {
if (table instanceof OlapTable && !force) {
OlapTable olapTable = (OlapTable) table;
if ((olapTable.getState() != OlapTableState.NORMAL)) {
throw new DdlException("The table [" + tableName + "]'s state is " + olapTable.getState()
+ ", cannot be dropped. please cancel the operation on olap table firstly."
+ " If you want to forcibly drop(cannot be recovered),"
+ " please use \"DROP table FORCE\".");
+ ", cannot be dropped. please cancel the operation on olap table firstly."
+ " If you want to forcibly drop(cannot be recovered),"
+ " please use \"DROP table FORCE\".");
}
if (olapTable.isInAtomicRestore()) {
throw new DdlException("The table [" + tableName + "]'s state is in atomic restore"
+ ", cannot be dropped. please cancel the restore operation on olap table"
+ " firstly. If you want to forcibly drop(cannot be recovered),"
+ " please use \"DROP table FORCE\".");
+ ", cannot be dropped. please cancel the restore operation on olap table"
+ " firstly. If you want to forcibly drop(cannot be recovered),"
+ " please use \"DROP table FORCE\".");
}
}

dropTableInternal(db, table, stmt.isView(), stmt.isForceDrop(), watch, costTimes);
dropTableInternal(db, table, isView, force, watch, costTimes);
} catch (UserException e) {
throw new DdlException(e.getMessage(), e.getMysqlErrorCode());
} finally {
Expand All @@ -960,7 +967,7 @@ public void dropTable(DropTableStmt stmt) throws DdlException {
watch.stop();
costTimes.put("6:total", watch.getTime());
LOG.info("finished dropping table: {} from db: {}, is view: {}, is force: {}, cost: {}",
tableName, dbName, stmt.isView(), stmt.isForceDrop(), costTimes);
tableName, dbName, isView, force, costTimes);
}

// drop table without any check.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,19 +287,25 @@ public void afterCreateTable(String dbName, String tblName) {

@Override
public void dropTableImpl(DropTableStmt stmt) throws DdlException {
String dbName = stmt.getDbName();
String tblName = stmt.getTableName();
ExternalDatabase<?> db = catalog.getDbNullable(stmt.getDbName());
if (stmt == null) {
throw new DdlException("DropTableStmt is null");
}
dropTableImpl(stmt.getDbName(), stmt.getTableName(), stmt.isSetIfExists());
}

@Override
public void dropTableImpl(String dbName, String tblName, boolean ifExists) throws DdlException {
ExternalDatabase<?> db = catalog.getDbNullable(dbName);
if (db == null) {
if (stmt.isSetIfExists()) {
if (ifExists) {
LOG.info("database [{}] does not exist when drop table[{}]", dbName, tblName);
return;
} else {
ErrorReport.reportDdlException(ErrorCode.ERR_BAD_DB_ERROR, dbName);
}
}
if (!tableExist(dbName, tblName)) {
if (stmt.isSetIfExists()) {
if (ifExists) {
LOG.info("drop table[{}] which does not exist", dbName);
return;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,21 @@ public void afterCreateTable(String dbName, String tblName) {

@Override
public void dropTableImpl(DropTableStmt stmt) throws DdlException {
if (stmt == null) {
throw new DdlException("DropTableStmt is null");
}
dropTableImpl(stmt.getDbName(), stmt.getTableName(), stmt.isSetIfExists());
}

public void dropTableImpl(String dbName, String tableName, boolean ifExists) throws DdlException {
try {
preExecutionAuthenticator.execute(() -> {
performDropTable(stmt);
performDropTable(dbName, tableName, ifExists);
return null;
});
} catch (Exception e) {
throw new DdlException(
"Failed to drop table: " + stmt.getTableName() + ", error message is:" + e.getMessage(), e);
"Failed to drop table: " + tableName + ", error message is:" + e.getMessage(), e);
}
}

Expand All @@ -270,11 +277,16 @@ public void afterDropTable(String dbName, String tblName) {
}

private void performDropTable(DropTableStmt stmt) throws DdlException {
String dbName = stmt.getDbName();
String tableName = stmt.getTableName();
if (stmt == null) {
throw new DdlException("DropTableStmt is null");
}
performDropTable(stmt.getDbName(), stmt.getTableName(), stmt.isSetIfExists());
}

private void performDropTable(String dbName, String tableName, boolean ifExists) throws DdlException {
ExternalDatabase<?> db = dorisCatalog.getDbNullable(dbName);
if (db == null) {
if (stmt.isSetIfExists()) {
if (ifExists) {
LOG.info("database [{}] does not exist when drop table[{}]", dbName, tableName);
return;
} else {
Expand All @@ -283,7 +295,7 @@ private void performDropTable(DropTableStmt stmt) throws DdlException {
}

if (!tableExist(dbName, tableName)) {
if (stmt.isSetIfExists()) {
if (ifExists) {
LOG.info("drop table[{}] which does not exist", tableName);
return;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,15 @@ default void dropTable(DropTableStmt stmt) throws DdlException {
afterDropTable(stmt.getDbName(), stmt.getTableName());
}

default void dropTable(String dbName, String tableName, boolean ifExists) throws DdlException {
dropTableImpl(dbName, tableName, ifExists);
afterDropTable(dbName, tableName);
}

void dropTableImpl(DropTableStmt stmt) throws DdlException;

void dropTableImpl(String dbName, String tableName, boolean ifExists) throws DdlException;

default void afterDropTable(String dbName, String tblName) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@
import org.apache.doris.nereids.DorisParser.DropRollupClauseContext;
import org.apache.doris.nereids.DorisParser.DropSqlBlockRuleContext;
import org.apache.doris.nereids.DorisParser.DropStoragePolicyContext;
import org.apache.doris.nereids.DorisParser.DropTableContext;
import org.apache.doris.nereids.DorisParser.DropUserContext;
import org.apache.doris.nereids.DorisParser.DropWorkloadGroupContext;
import org.apache.doris.nereids.DorisParser.DropWorkloadPolicyContext;
Expand Down Expand Up @@ -560,6 +561,7 @@
import org.apache.doris.nereids.trees.plans.commands.DropRoleCommand;
import org.apache.doris.nereids.trees.plans.commands.DropSqlBlockRuleCommand;
import org.apache.doris.nereids.trees.plans.commands.DropStoragePolicyCommand;
import org.apache.doris.nereids.trees.plans.commands.DropTableCommand;
import org.apache.doris.nereids.trees.plans.commands.DropUserCommand;
import org.apache.doris.nereids.trees.plans.commands.DropWorkloadGroupCommand;
import org.apache.doris.nereids.trees.plans.commands.DropWorkloadPolicyCommand;
Expand Down Expand Up @@ -5371,6 +5373,31 @@ public LogicalPlan visitDropRole(DropRoleContext ctx) {
return new DropRoleCommand(ctx.name.getText(), ctx.EXISTS() != null);
}

@Override
public LogicalPlan visitDropTable(DropTableContext ctx) {
String ctlName = null;
String dbName = null;
String tableName = null;
List<String> nameParts = visitMultipartIdentifier(ctx.name);
if (nameParts.size() == 1) {
tableName = nameParts.get(0);
} else if (nameParts.size() == 2) {
dbName = nameParts.get(0);
tableName = nameParts.get(1);
} else if (nameParts.size() == 3) {
ctlName = nameParts.get(0);
dbName = nameParts.get(1);
tableName = nameParts.get(2);
} else {
throw new AnalysisException("nameParts in create table should be [ctl.][db.]tbl");
}

boolean ifExists = ctx.EXISTS() != null;
boolean forceDrop = ctx.FORCE() != null;
TableNameInfo tblNameInfo = new TableNameInfo(ctlName, dbName, tableName);
return new DropTableCommand(ifExists, tblNameInfo, forceDrop);
}

@Override
public LogicalPlan visitDropCatalog(DropCatalogContext ctx) {
String catalogName = stripQuotes(ctx.name.getText());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,5 +288,6 @@ public enum PlanType {
SWITCH_COMMAND,
HELP_COMMAND,
USE_COMMAND,
DROP_TABLE_COMMAND,
ALTER_SYSTEM_RENAME_COMPUTE_GROUP
}
Loading