This is an automated email from the ASF dual-hosted git repository.
starocean999 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 4bd1cc1a01b [Enhancement] (nereids)implement showCreateDatabaseCommand
in nereids (#43034)
4bd1cc1a01b is described below
commit 4bd1cc1a01b82ea7e74d99b94c5369ae77d08336
Author: Sridhar R Manikarnike <[email protected]>
AuthorDate: Mon Dec 2 08:26:11 2024 +0530
[Enhancement] (nereids)implement showCreateDatabaseCommand in nereids
(#43034)
Issue Number: close #42739
---
.../antlr4/org/apache/doris/nereids/DorisParser.g4 | 2 +-
.../doris/nereids/parser/LogicalPlanBuilder.java | 20 ++++
.../apache/doris/nereids/trees/plans/PlanType.java | 1 +
.../plans/commands/ShowCreateDatabaseCommand.java | 122 +++++++++++++++++++++
.../trees/plans/visitor/CommandVisitor.java | 5 +
.../show/test_show_create_db_nereids.out | 7 ++
.../show/test_show_create_db_nereids.groovy | 42 +++++++
7 files changed, 198 insertions(+), 1 deletion(-)
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 1b1e218a958..f78a1c4b531 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
@@ -214,6 +214,7 @@ supportedDropStatement
supportedShowStatement
: SHOW (GLOBAL | SESSION | LOCAL)? VARIABLES wildWhere?
#showVariables
| SHOW AUTHORS
#showAuthors
+ | SHOW CREATE (DATABASE | SCHEMA) name=multipartIdentifier
#showCreateDatabase
| SHOW BROKER
#showBroker
| SHOW DYNAMIC PARTITION TABLES ((FROM | IN)
database=multipartIdentifier)? #showDynamicPartition
| SHOW EVENTS ((FROM | IN) database=multipartIdentifier)? wildWhere?
#showEvents
@@ -295,7 +296,6 @@ unsupportedShowStatement
| SHOW FULL? PROCESSLIST
#showProcessList
| SHOW (GLOBAL | SESSION | LOCAL)? STATUS wildWhere?
#showStatus
| SHOW CREATE MATERIALIZED VIEW name=multipartIdentifier
#showMaterializedView
- | SHOW CREATE (DATABASE | SCHEMA) name=multipartIdentifier
#showCreateDatabase
| SHOW CREATE (GLOBAL | SESSION | LOCAL)? FUNCTION functionIdentifier
LEFT_PAREN functionArguments? RIGHT_PAREN
((FROM | IN) database=multipartIdentifier)?
#showCreateFunction
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 e5f4322225d..d52d2696a29 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
@@ -20,6 +20,7 @@ package org.apache.doris.nereids.parser;
import org.apache.doris.analysis.ArithmeticExpr.Operator;
import org.apache.doris.analysis.BrokerDesc;
import org.apache.doris.analysis.ColumnNullableType;
+import org.apache.doris.analysis.DbName;
import org.apache.doris.analysis.EncryptKeyName;
import org.apache.doris.analysis.PassVar;
import org.apache.doris.analysis.SetType;
@@ -226,6 +227,7 @@ import
org.apache.doris.nereids.DorisParser.ShowBrokerContext;
import org.apache.doris.nereids.DorisParser.ShowConfigContext;
import org.apache.doris.nereids.DorisParser.ShowConstraintContext;
import org.apache.doris.nereids.DorisParser.ShowCreateCatalogContext;
+import org.apache.doris.nereids.DorisParser.ShowCreateDatabaseContext;
import org.apache.doris.nereids.DorisParser.ShowCreateMTMVContext;
import org.apache.doris.nereids.DorisParser.ShowCreateMaterializedViewContext;
import org.apache.doris.nereids.DorisParser.ShowCreateProcedureContext;
@@ -512,6 +514,7 @@ import
org.apache.doris.nereids.trees.plans.commands.ShowBrokerCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowConfigCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowConstraintsCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowCreateCatalogCommand;
+import org.apache.doris.nereids.trees.plans.commands.ShowCreateDatabaseCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowCreateMTMVCommand;
import
org.apache.doris.nereids.trees.plans.commands.ShowCreateMaterializedViewCommand;
import
org.apache.doris.nereids.trees.plans.commands.ShowCreateProcedureCommand;
@@ -4617,6 +4620,23 @@ public class LogicalPlanBuilder extends
DorisParserBaseVisitor<Object> {
return new ShowFrontendsCommand(detail);
}
+ @Override
+ public LogicalPlan visitShowCreateDatabase(ShowCreateDatabaseContext ctx) {
+ List<String> nameParts = visitMultipartIdentifier(ctx.name);
+ String databaseName = "";
+ String catalogName = "";
+ if (nameParts.size() == 2) {
+ // The identifier is in the form "internalcatalog.databasename"
+ catalogName = nameParts.get(0);
+ databaseName = nameParts.get(1);
+ } else if (nameParts.size() == 1) {
+ // The identifier is in the form "databasename"
+ databaseName = nameParts.get(0);
+ }
+
+ return new ShowCreateDatabaseCommand(new DbName(catalogName,
databaseName));
+ }
+
@Override
public LogicalPlan visitCleanAllProfile(CleanAllProfileContext ctx) {
return new CleanAllProfileCommand();
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PlanType.java
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PlanType.java
index 8f474ea395f..8625d103f3b 100644
---
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PlanType.java
+++
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PlanType.java
@@ -198,6 +198,7 @@ public enum PlanType {
SHOW_BROKER_COMMAND,
SHOW_CONFIG_COMMAND,
SHOW_CREATE_CATALOG_COMMAND,
+ SHOW_CREATE_DATABASE_COMMAND,
SHOW_CREATE_MATERIALIZED_VIEW_COMMAND,
SHOW_CREATE_TABLE_COMMAND,
SHOW_CREATE_VIEW_COMMAND,
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ShowCreateDatabaseCommand.java
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ShowCreateDatabaseCommand.java
new file mode 100644
index 00000000000..29186e9e5cd
--- /dev/null
+++
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ShowCreateDatabaseCommand.java
@@ -0,0 +1,122 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.trees.plans.commands;
+
+import org.apache.doris.analysis.DbName;
+import org.apache.doris.catalog.Column;
+import org.apache.doris.catalog.DatabaseIf;
+import org.apache.doris.catalog.Env;
+import org.apache.doris.catalog.ScalarType;
+import org.apache.doris.cluster.ClusterNamespace;
+import org.apache.doris.common.ErrorCode;
+import org.apache.doris.common.ErrorReport;
+import org.apache.doris.common.util.PrintableMap;
+import org.apache.doris.datasource.CatalogIf;
+import org.apache.doris.datasource.hive.HMSExternalCatalog;
+import org.apache.doris.datasource.iceberg.IcebergExternalCatalog;
+import org.apache.doris.datasource.iceberg.IcebergExternalDatabase;
+import org.apache.doris.mysql.privilege.PrivPredicate;
+import org.apache.doris.nereids.trees.plans.PlanType;
+import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
+import org.apache.doris.qe.ConnectContext;
+import org.apache.doris.qe.ShowResultSet;
+import org.apache.doris.qe.ShowResultSetMetaData;
+import org.apache.doris.qe.StmtExecutor;
+
+import com.google.common.base.Strings;
+import com.google.common.collect.Lists;
+
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Represents the command for SHOW CREATE DATABASE.
+ */
+public class ShowCreateDatabaseCommand extends ShowCommand {
+ private static final ShowResultSetMetaData META_DATA =
+ ShowResultSetMetaData.builder()
+ .addColumn(new Column("Database",
ScalarType.createVarchar(20)))
+ .addColumn(new Column("Create Database",
ScalarType.createVarchar(30)))
+ .build();
+
+ private final String databaseName;
+ private final String catalogName;
+
+ public ShowCreateDatabaseCommand(DbName dbName) {
+ super(PlanType.SHOW_CREATE_DATABASE_COMMAND);
+ this.databaseName = Objects.requireNonNull(dbName.getDb(), "Database
name cannot be null");
+ this.catalogName = dbName.getCtl();
+ }
+
+ @Override
+ public ShowResultSet doRun(ConnectContext ctx, StmtExecutor executor)
throws Exception {
+ String ctlgName = catalogName;
+ if (Strings.isNullOrEmpty(catalogName)) {
+ ctlgName = Env.getCurrentEnv().getCurrentCatalog().getName();
+ }
+ if (Strings.isNullOrEmpty(databaseName)) {
+ ErrorReport.reportAnalysisException(ErrorCode.ERR_WRONG_DB_NAME,
databaseName);
+ }
+
+ if
(!Env.getCurrentEnv().getAccessManager().checkDbPriv(ConnectContext.get(),
ctlgName, databaseName,
+ PrivPredicate.SHOW)) {
+
ErrorReport.reportAnalysisException(ErrorCode.ERR_DB_ACCESS_DENIED_ERROR,
+ PrivPredicate.SHOW.getPrivs().toString(), databaseName);
+ }
+
+ List<List<String>> rows = Lists.newArrayList();
+
+ StringBuilder sb = new StringBuilder();
+ CatalogIf<?> catalog =
Env.getCurrentEnv().getCatalogMgr().getCatalogOrAnalysisException(ctlgName);
+ if (catalog instanceof HMSExternalCatalog) {
+ String simpleDBName =
ClusterNamespace.getNameFromFullName(databaseName);
+ org.apache.hadoop.hive.metastore.api.Database db =
((HMSExternalCatalog) catalog).getClient()
+ .getDatabase(simpleDBName);
+ sb.append("CREATE DATABASE `").append(simpleDBName).append("`")
+ .append(" LOCATION '")
+ .append(db.getLocationUri())
+ .append("'");
+ } else if (catalog instanceof IcebergExternalCatalog) {
+ IcebergExternalDatabase db = (IcebergExternalDatabase)
catalog.getDbOrAnalysisException(databaseName);
+ sb.append("CREATE DATABASE `").append(databaseName).append("`")
+ .append(" LOCATION '")
+ .append(db.getLocation())
+ .append("'");
+ } else {
+ DatabaseIf db = catalog.getDbOrAnalysisException(databaseName);
+ sb.append("CREATE DATABASE
`").append(ClusterNamespace.getNameFromFullName(databaseName)).append("`");
+ if (db.getDbProperties().getProperties().size() > 0) {
+ sb.append("\nPROPERTIES (\n");
+ sb.append(new
PrintableMap<>(db.getDbProperties().getProperties(), "=", true, true, false));
+ sb.append("\n)");
+ }
+ }
+
+
rows.add(Lists.newArrayList(ClusterNamespace.getNameFromFullName(databaseName),
sb.toString()));
+ return new ShowResultSet(this.getMetaData(), rows);
+ }
+
+ @Override
+ public <R, C> R accept(PlanVisitor<R, C> visitor, C context) {
+ return visitor.visitShowCreateDatabaseCommand(this, context);
+ }
+
+ public ShowResultSetMetaData getMetaData() {
+ return META_DATA;
+ }
+}
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/visitor/CommandVisitor.java
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/visitor/CommandVisitor.java
index c00c0ef90ca..49485cda51d 100644
---
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/visitor/CommandVisitor.java
+++
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/visitor/CommandVisitor.java
@@ -75,6 +75,7 @@ import
org.apache.doris.nereids.trees.plans.commands.ShowBrokerCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowConfigCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowConstraintsCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowCreateCatalogCommand;
+import org.apache.doris.nereids.trees.plans.commands.ShowCreateDatabaseCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowCreateMTMVCommand;
import
org.apache.doris.nereids.trees.plans.commands.ShowCreateMaterializedViewCommand;
import
org.apache.doris.nereids.trees.plans.commands.ShowCreateProcedureCommand;
@@ -398,6 +399,10 @@ public interface CommandVisitor<R, C> {
return visitCommand(showCreateMtlzViewCommand, context);
}
+ default R visitShowCreateDatabaseCommand(ShowCreateDatabaseCommand
showCreateDatabaseCommand, C context) {
+ return visitCommand(showCreateDatabaseCommand, context);
+ }
+
default R visitShowCreateViewCommand(ShowCreateViewCommand
showCreateViewCommand, C context) {
return visitCommand(showCreateViewCommand, context);
}
diff --git
a/regression-test/data/nereids_p0/show/test_show_create_db_nereids.out
b/regression-test/data/nereids_p0/show/test_show_create_db_nereids.out
new file mode 100644
index 00000000000..df88b46f8fb
--- /dev/null
+++ b/regression-test/data/nereids_p0/show/test_show_create_db_nereids.out
@@ -0,0 +1,7 @@
+-- This file is automatically generated. You should know what you did if you
want to edit this
+-- !cmd --
+db_test_show_create CREATE DATABASE `db_test_show_create`
+
+-- !cmd --
+db_test_show_create CREATE DATABASE `db_test_show_create`\nPROPERTIES
(\n"property_key" = "property_value"\n)
+
diff --git
a/regression-test/suites/nereids_p0/show/test_show_create_db_nereids.groovy
b/regression-test/suites/nereids_p0/show/test_show_create_db_nereids.groovy
new file mode 100644
index 00000000000..bfc84c9a60f
--- /dev/null
+++ b/regression-test/suites/nereids_p0/show/test_show_create_db_nereids.groovy
@@ -0,0 +1,42 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+suite("test_show_create_db_nereids", "query,create_database") {
+ String dbName = "db_test_show_create";
+
+ try {
+ // Create a new database to test the SHOW CREATE DATABASE command
+ sql "CREATE DATABASE IF NOT EXISTS ${dbName}"
+
+ // Run the SHOW CREATE DATABASE command and validate the output using
checkNereidsExecute and qt_cmd
+ checkNereidsExecute("""SHOW CREATE DATABASE ${dbName}""")
+ qt_cmd("""SHOW CREATE DATABASE ${dbName}""")
+
+ // Drop the database and verify that the command runs successfully
+ sql "DROP DATABASE IF EXISTS ${dbName}"
+
+ // Re-create the database with additional properties
+ sql "CREATE DATABASE IF NOT EXISTS ${dbName} PROPERTIES
('property_key'='property_value')"
+
+ // Verify the SHOW CREATE DATABASE command captures the properties
using checkNereidsExecute and qt_cmd
+ checkNereidsExecute("""SHOW CREATE DATABASE ${dbName}""")
+ qt_cmd("""SHOW CREATE DATABASE ${dbName}""")
+ } finally {
+ // Clean up by dropping the database if it still exists
+ try_sql("DROP DATABASE IF EXISTS ${dbName}")
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]