This is an automated email from the ASF dual-hosted git repository.
dataroaring pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/master by this push:
new 52879bdeb08 [opt](temp_table) add drop temp table and let AS be
optional in ctas (#55657)
52879bdeb08 is described below
commit 52879bdeb08fd345e275e8d7239fa359084eba60
Author: morrySnow <[email protected]>
AuthorDate: Thu Sep 4 19:23:18 2025 +0800
[opt](temp_table) add drop temp table and let AS be optional in ctas
(#55657)
### What problem does this PR solve?
MySQL docs:
drop table: https://dev.mysql.com/doc/refman/8.4/en/drop-table.html
create table: https://dev.mysql.com/doc/refman/8.4/en/create-table.html
---
.../antlr4/org/apache/doris/nereids/DorisParser.g4 | 8 ++--
.../main/java/org/apache/doris/catalog/Env.java | 8 ++--
.../doris/catalog/InternalSchemaInitializer.java | 2 +-
.../org/apache/doris/datasource/CatalogIf.java | 2 +-
.../apache/doris/datasource/ExternalCatalog.java | 2 +-
.../apache/doris/datasource/InternalCatalog.java | 5 ++-
.../iceberg/IcebergDLFExternalCatalog.java | 2 +-
.../doris/nereids/parser/LogicalPlanBuilder.java | 6 +--
.../trees/plans/commands/CreateTableCommand.java | 1 +
.../trees/plans/commands/DropTableCommand.java | 16 +++++--
.../doris/catalog/DropMaterializedViewTest.java | 1 +
.../dlf/client/IcebergDLFExternalCatalogTest.java | 2 +-
.../doris/nereids/parser/NereidsParserTest.java | 50 ++++++++++++++++++++++
regression-test/ctas_p0/ctas_aggregate_t1.groovy | 2 +-
.../suites/temp_table_p0/test_temp_table.groovy | 1 +
15 files changed, 87 insertions(+), 21 deletions(-)
diff --git a/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4
b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4
index e7896cd73b1..d8f5f810eb0 100644
--- a/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4
+++ b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4
@@ -85,7 +85,7 @@ materializedViewStatement
(DISTRIBUTED BY (HASH hashKeys=identifierList | RANDOM)
(BUCKETS (INTEGER_VALUE | AUTO))?)?
propertyClause?
- AS query
#createMTMV
+ AS? query
#createMTMV
| REFRESH MATERIALIZED VIEW mvName=multipartIdentifier (partitionSpec |
COMPLETE | AUTO) #refreshMTMV
| ALTER MATERIALIZED VIEW mvName=multipartIdentifier ((RENAME
newName=identifier)
| (REFRESH (refreshMethod | refreshTrigger | refreshMethod
refreshTrigger))
@@ -173,10 +173,10 @@ supportedCreateStatement
(ROLLUP LEFT_PAREN rollupDefs RIGHT_PAREN)?
properties=propertyClause?
(BROKER extProperties=propertyClause)?
- (AS query)?
#createTable
+ (AS? query)?
#createTable
| CREATE (OR REPLACE)? VIEW (IF NOT EXISTS)? name=multipartIdentifier
(LEFT_PAREN cols=simpleColumnDefs RIGHT_PAREN)?
- (COMMENT STRING_LITERAL)? AS query
#createView
+ (COMMENT STRING_LITERAL)? AS? query
#createView
| CREATE FILE name=STRING_LITERAL
((FROM | IN) database=identifier)? properties=propertyClause
#createFile
| CREATE (EXTERNAL | TEMPORARY)? TABLE (IF NOT EXISTS)?
name=multipartIdentifier
@@ -305,7 +305,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 TEMPORARY? 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
diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java
b/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java
index 6290219e0c6..49e072cf5c6 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java
@@ -4779,20 +4779,20 @@ public class Env {
}
DropMTMVInfo dropMTMVInfo = command.getDropMTMVInfo();
dropTable(dropMTMVInfo.getCatalogName(), dropMTMVInfo.getDbName(),
dropMTMVInfo.getTableName(), false,
- true, dropMTMVInfo.isIfExists(), true);
+ true, dropMTMVInfo.isIfExists(), false, true);
}
public void dropTable(String catalogName, String dbName, String tableName,
boolean isView, boolean isMtmv,
- boolean ifExists, boolean force) throws DdlException
{
+ boolean ifExists, boolean mustTemporary, boolean
force) throws DdlException {
CatalogIf<?> catalogIf = catalogMgr.getCatalogOrException(catalogName,
catalog -> new DdlException(("Unknown catalog " + catalog)));
- catalogIf.dropTable(dbName, tableName, isView, isMtmv, ifExists,
force);
+ catalogIf.dropTable(dbName, tableName, isView, isMtmv, ifExists,
mustTemporary, force);
}
public void dropView(String catalogName, String dbName, String tableName,
boolean ifExists) throws DdlException {
CatalogIf<?> catalogIf = catalogMgr.getCatalogOrException(catalogName,
catalog -> new DdlException(("Unknown catalog " + catalog)));
- catalogIf.dropTable(dbName, tableName, true, false, ifExists, false);
+ catalogIf.dropTable(dbName, tableName, true, false, ifExists, false,
false);
}
public boolean unprotectDropTable(Database db, Table table, boolean
isForceDrop, boolean isReplay,
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/catalog/InternalSchemaInitializer.java
b/fe/fe-core/src/main/java/org/apache/doris/catalog/InternalSchemaInitializer.java
index f5a3f53653e..d9c7db41758 100644
---
a/fe/fe-core/src/main/java/org/apache/doris/catalog/InternalSchemaInitializer.java
+++
b/fe/fe-core/src/main/java/org/apache/doris/catalog/InternalSchemaInitializer.java
@@ -555,7 +555,7 @@ public class InternalSchemaInitializer extends Thread {
try {
Env.getCurrentEnv().getInternalCatalog()
.dropTable(StatisticConstants.DB_NAME,
StatisticConstants.TABLE_STATISTIC_TBL_NAME,
- false, false, true, true);
+ false, false, true, false, true);
} catch (Exception e) {
LOG.warn("Failed to drop outdated table", e);
}
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/datasource/CatalogIf.java
b/fe/fe-core/src/main/java/org/apache/doris/datasource/CatalogIf.java
index cef13f35cf6..f394b86660f 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/datasource/CatalogIf.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/datasource/CatalogIf.java
@@ -205,7 +205,7 @@ public interface CatalogIf<T extends DatabaseIf> {
boolean createTable(CreateTableStmt stmt) throws UserException;
void dropTable(String dbName, String tableName, boolean isView, boolean
isMtmv, boolean ifExists,
- boolean force) throws DdlException;
+ boolean mustTemporary, boolean force) throws DdlException;
default void renameTable(String dbName, String oldTableName, String
newTableName) throws DdlException {
throw new UnsupportedOperationException("Not support rename table
operation");
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/datasource/ExternalCatalog.java
b/fe/fe-core/src/main/java/org/apache/doris/datasource/ExternalCatalog.java
index c7671f9ac55..5ca6075271a 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/datasource/ExternalCatalog.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/datasource/ExternalCatalog.java
@@ -1168,7 +1168,7 @@ public abstract class ExternalCatalog
@Override
public void dropTable(String dbName, String tableName, boolean isView,
boolean isMtmv, boolean ifExists,
- boolean force) throws DdlException {
+ boolean mustTemporary, boolean force) throws DdlException {
makeSureInitialized();
if (metadataOps == null) {
throw new DdlException("Drop table is not supported for catalog: "
+ getName());
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/datasource/InternalCatalog.java
b/fe/fe-core/src/main/java/org/apache/doris/datasource/InternalCatalog.java
index dba12b7b4e9..d5895948669 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/datasource/InternalCatalog.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/datasource/InternalCatalog.java
@@ -856,7 +856,7 @@ public class InternalCatalog implements CatalogIf<Database>
{
@Override
public void dropTable(String dbName, String tableName, boolean isView,
boolean isMtmv,
- boolean ifExists, boolean force) throws DdlException
{
+ boolean ifExists, boolean mustTemporary, boolean force) throws
DdlException {
Map<String, Long> costTimes = new TreeMap<String, Long>();
StopWatch watch = StopWatch.createStarted();
LOG.info("begin to drop table: {} from db: {}, is force: {}",
tableName, dbName, force);
@@ -932,6 +932,9 @@ public class InternalCatalog implements CatalogIf<Database>
{
if (table.isTemporary()) {
dropTableInternal(db, table, false, true, watch, costTimes);
} else {
+ if (mustTemporary) {
+
ErrorReport.reportDdlException(ErrorCode.ERR_UNKNOWN_TABLE, tableName, dbName);
+ }
dropTableInternal(db, table, isView, force, watch, costTimes);
}
} catch (UserException e) {
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/datasource/iceberg/IcebergDLFExternalCatalog.java
b/fe/fe-core/src/main/java/org/apache/doris/datasource/iceberg/IcebergDLFExternalCatalog.java
index a9a4bf6868d..9eeaf81ba7c 100644
---
a/fe/fe-core/src/main/java/org/apache/doris/datasource/iceberg/IcebergDLFExternalCatalog.java
+++
b/fe/fe-core/src/main/java/org/apache/doris/datasource/iceberg/IcebergDLFExternalCatalog.java
@@ -59,7 +59,7 @@ public class IcebergDLFExternalCatalog extends
IcebergExternalCatalog {
@Override
public void dropTable(String dbName, String tableName, boolean isView,
boolean isMtmv, boolean ifExists,
- boolean force) throws DdlException {
+ boolean mustTemporary, boolean force) throws DdlException {
throw new NotSupportedException("iceberg catalog with dlf type not
supports 'drop table'");
}
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java
b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java
index 4e3381fb75e..abb468a19cc 100644
---
a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java
+++
b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java
@@ -3621,7 +3621,7 @@ public class LogicalPlanBuilder extends
DorisParserBaseVisitor<Object> {
}
if (ctx.columnDefs() != null) {
- if (ctx.AS() != null) {
+ if (ctx.query() != null) {
throw new AnalysisException("Should not define the entire
column in CTAS");
}
return new CreateTableCommand(Optional.empty(), new
CreateTableInfo(
@@ -3643,7 +3643,7 @@ public class LogicalPlanBuilder extends
DorisParserBaseVisitor<Object> {
properties,
extProperties,
ctx.clusterKeys != null ?
visitIdentifierList(ctx.clusterKeys) : ImmutableList.of()));
- } else if (ctx.AS() != null) {
+ } else if (ctx.query() != null) {
return new
CreateTableCommand(Optional.of(visitQuery(ctx.query())), new CreateTableInfo(
ctx.EXISTS() != null,
ctx.EXTERNAL() != null,
@@ -6570,7 +6570,7 @@ public class LogicalPlanBuilder extends
DorisParserBaseVisitor<Object> {
boolean ifExists = ctx.EXISTS() != null;
boolean forceDrop = ctx.FORCE() != null;
TableNameInfo tblNameInfo = new TableNameInfo(ctlName, dbName,
tableName);
- return new DropTableCommand(ifExists, tblNameInfo, forceDrop);
+ return new DropTableCommand(ifExists, ctx.TEMPORARY() != null,
tblNameInfo, forceDrop);
}
@Override
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/CreateTableCommand.java
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/CreateTableCommand.java
index 435b6160fc2..295e2a62082 100644
---
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/CreateTableCommand.java
+++
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/CreateTableCommand.java
@@ -198,6 +198,7 @@ public class CreateTableCommand extends Command implements
NeedAuditEncryption,
false,
false,
false,
+ false,
true
);
} catch (Exception e) {
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/DropTableCommand.java
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/DropTableCommand.java
index 8b3c4708471..f26e28c97b6 100644
---
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/DropTableCommand.java
+++
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/DropTableCommand.java
@@ -37,22 +37,32 @@ import com.google.common.base.Strings;
public class DropTableCommand extends Command implements ForwardWithSync {
private boolean ifExists;
+ private boolean mustTemporary;
private final TableNameInfo tableName;
private final boolean isView;
private boolean forceDrop;
private boolean isMaterializedView;
- public DropTableCommand(boolean ifExists, TableNameInfo tableName, boolean
forceDrop) {
+ /**
+ * constructor.
+ */
+ public DropTableCommand(boolean ifExists, boolean mustTemporary,
TableNameInfo tableName, boolean forceDrop) {
super(PlanType.DROP_TABLE_COMMAND);
this.ifExists = ifExists;
+ this.mustTemporary = mustTemporary;
this.tableName = tableName;
this.isView = false;
this.forceDrop = forceDrop;
}
- public DropTableCommand(boolean ifExists, TableNameInfo tableName, boolean
isView, boolean forceDrop) {
+ /**
+ * constructor.
+ */
+ public DropTableCommand(boolean ifExists, boolean mustTemporary,
+ TableNameInfo tableName, boolean isView, boolean forceDrop) {
super(PlanType.DROP_TABLE_COMMAND);
this.ifExists = ifExists;
+ this.mustTemporary = mustTemporary;
this.tableName = tableName;
this.isView = isView;
this.forceDrop = forceDrop;
@@ -77,7 +87,7 @@ public class DropTableCommand extends Command implements
ForwardWithSync {
ErrorReport.reportAnalysisException(ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR,
"DROP");
}
Env.getCurrentEnv().dropTable(tableName.getCtl(), tableName.getDb(),
tableName.getTbl(), isView,
- isMaterializedView, ifExists, forceDrop);
+ isMaterializedView, ifExists, mustTemporary, forceDrop);
}
public void setMaterializedView(boolean materializedView) {
diff --git
a/fe/fe-core/src/test/java/org/apache/doris/catalog/DropMaterializedViewTest.java
b/fe/fe-core/src/test/java/org/apache/doris/catalog/DropMaterializedViewTest.java
index d4ef4cf43f7..27909122be9 100644
---
a/fe/fe-core/src/test/java/org/apache/doris/catalog/DropMaterializedViewTest.java
+++
b/fe/fe-core/src/test/java/org/apache/doris/catalog/DropMaterializedViewTest.java
@@ -121,6 +121,7 @@ public class DropMaterializedViewTest {
false,
isMaterializedView,
false,
+ false,
false);
}
diff --git
a/fe/fe-core/src/test/java/org/apache/doris/datasource/iceberg/dlf/client/IcebergDLFExternalCatalogTest.java
b/fe/fe-core/src/test/java/org/apache/doris/datasource/iceberg/dlf/client/IcebergDLFExternalCatalogTest.java
index c797411fc9f..0af08cd6fc7 100644
---
a/fe/fe-core/src/test/java/org/apache/doris/datasource/iceberg/dlf/client/IcebergDLFExternalCatalogTest.java
+++
b/fe/fe-core/src/test/java/org/apache/doris/datasource/iceberg/dlf/client/IcebergDLFExternalCatalogTest.java
@@ -48,7 +48,7 @@ public class IcebergDLFExternalCatalogTest {
IcebergDLFExternalCatalog catalog = new IcebergDLFExternalCatalog(1,
"test", "test", props, "test");
Assert.assertThrows(NotSupportedException.class, () ->
catalog.createDb("db1", true, Maps.newHashMap()));
Assert.assertThrows(NotSupportedException.class, () ->
catalog.dropDb("", true, true));
- Assert.assertThrows(NotSupportedException.class, () ->
catalog.dropTable("", "", true, true, true, true));
+ Assert.assertThrows(NotSupportedException.class, () ->
catalog.dropTable("", "", true, true, true, false, true));
Assert.assertThrows(NotSupportedException.class, () ->
catalog.truncateTable("", "", null, true, ""));
}
}
diff --git
a/fe/fe-core/src/test/java/org/apache/doris/nereids/parser/NereidsParserTest.java
b/fe/fe-core/src/test/java/org/apache/doris/nereids/parser/NereidsParserTest.java
index 0db2327c5df..7a2447b82c1 100644
---
a/fe/fe-core/src/test/java/org/apache/doris/nereids/parser/NereidsParserTest.java
+++
b/fe/fe-core/src/test/java/org/apache/doris/nereids/parser/NereidsParserTest.java
@@ -36,6 +36,10 @@ import org.apache.doris.nereids.trees.plans.DistributeType;
import org.apache.doris.nereids.trees.plans.JoinType;
import org.apache.doris.nereids.trees.plans.Plan;
import org.apache.doris.nereids.trees.plans.PlanType;
+import
org.apache.doris.nereids.trees.plans.commands.CreateMaterializedViewCommand;
+import org.apache.doris.nereids.trees.plans.commands.CreateTableCommand;
+import org.apache.doris.nereids.trees.plans.commands.CreateViewCommand;
+import org.apache.doris.nereids.trees.plans.commands.DropTableCommand;
import org.apache.doris.nereids.trees.plans.commands.ExplainCommand;
import
org.apache.doris.nereids.trees.plans.commands.ExplainCommand.ExplainLevel;
import org.apache.doris.nereids.trees.plans.commands.ReplayCommand;
@@ -57,14 +61,17 @@ import org.apache.doris.qe.GlobalVariable;
import org.apache.doris.qe.SqlModeHelper;
import org.apache.doris.qe.StmtExecutor;
+import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
+import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
+import java.util.Map;
import java.util.Optional;
import java.util.Set;
@@ -1071,4 +1078,47 @@ public class NereidsParserTest extends ParserTestBase {
.assertThrowsExactly(ParseException.class)
.assertMessageContains("mismatched input '->' expecting
{<EOF>, ';'}");
}
+
+ @Test
+ public void testCtasWithoutAs() {
+ NereidsParser parser = new NereidsParser();
+ String sql = "CREATE TABLE t1 SELECT * FROM t2";
+ LogicalPlan logicalPlan = parser.parseSingle(sql);
+ Assertions.assertInstanceOf(CreateTableCommand.class, logicalPlan);
+ CreateTableCommand createTableCommand = (CreateTableCommand)
logicalPlan;
+ Assertions.assertTrue(createTableCommand.getCtasQuery().isPresent());
+ }
+
+ @Test
+ public void testCreateViewWithoutAs() {
+ NereidsParser parser = new NereidsParser();
+ String sql = "CREATE VIEW t1 SELECT * FROM t2";
+ LogicalPlan logicalPlan = parser.parseSingle(sql);
+ Assertions.assertInstanceOf(CreateViewCommand.class, logicalPlan);
+ }
+
+ @Test
+ public void testCreateMvWithoutAs() {
+ NereidsParser parser = new NereidsParser();
+ String sql = "CREATE MATERIALIZED VIEW t1 SELECT * FROM t2";
+ LogicalPlan logicalPlan = parser.parseSingle(sql);
+ Assertions.assertInstanceOf(CreateMaterializedViewCommand.class,
logicalPlan);
+ }
+
+ @Test
+ public void testDropTemporaryTable() throws Exception {
+ NereidsParser parser = new NereidsParser();
+ Map<String, Boolean> mustTemporaryResult = ImmutableMap.of(
+ "DROP TEMPORARY TABLE t1", true,
+ "DROP TABLE t1", false);
+ for (Map.Entry<String, Boolean> entry :
mustTemporaryResult.entrySet()) {
+ LogicalPlan logicalPlan = parser.parseSingle(entry.getKey());
+ Assertions.assertInstanceOf(DropTableCommand.class, logicalPlan);
+ DropTableCommand dropTableCommand = (DropTableCommand) logicalPlan;
+ Field mustTemporary =
DropTableCommand.class.getDeclaredField("mustTemporary");
+ mustTemporary.setAccessible(true);
+ Object value = mustTemporary.get(dropTableCommand);
+ Assertions.assertEquals(entry.getValue(), (boolean) value);
+ }
+ }
}
diff --git a/regression-test/ctas_p0/ctas_aggregate_t1.groovy
b/regression-test/ctas_p0/ctas_aggregate_t1.groovy
index 869acf821ad..5a001683bb0 100644
--- a/regression-test/ctas_p0/ctas_aggregate_t1.groovy
+++ b/regression-test/ctas_p0/ctas_aggregate_t1.groovy
@@ -58,7 +58,7 @@ suite("ctas_aggregate_t1") {
PROPERTIES (
"replication_allocation" = "tag.location.default: 1"
)
- as select * from ${tbName};
+ select * from ${tbName};
"""
diff --git a/regression-test/suites/temp_table_p0/test_temp_table.groovy
b/regression-test/suites/temp_table_p0/test_temp_table.groovy
index b09875c96a8..4a3b8022887 100644
--- a/regression-test/suites/temp_table_p0/test_temp_table.groovy
+++ b/regression-test/suites/temp_table_p0/test_temp_table.groovy
@@ -676,6 +676,7 @@ suite('test_temp_table', 'p0') {
// clean
sql "use regression_test_temp_table_p0"
sql "drop table t_test_temp_table1"
+ sql "drop temporary table t_test_temp_table6"
sql "drop table t_test_table3_0"
sql "drop table t_test_table_no_partition"
sql "DROP USER IF EXISTS temp_table_test_user"
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]