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 011f616c609 [Enhancement] (nereids)implement showDatabaseIdCommand in 
nereids (#44192)
011f616c609 is described below

commit 011f616c6093939c3df1e59fd0b021d86935a76f
Author: koarz <66543806+ko...@users.noreply.github.com>
AuthorDate: Thu Dec 5 11:02:17 2024 +0800

    [Enhancement] (nereids)implement showDatabaseIdCommand in nereids (#44192)
    
    Issue Number: close #42742
---
 .../antlr4/org/apache/doris/nereids/DorisParser.g4 |  2 +-
 .../doris/nereids/parser/LogicalPlanBuilder.java   |  7 ++
 .../apache/doris/nereids/trees/plans/PlanType.java |  1 +
 .../plans/commands/ShowDatabaseIdCommand.java      | 75 ++++++++++++++++++++++
 .../trees/plans/visitor/CommandVisitor.java        |  5 ++
 .../show/test_show_database_id_nereids.groovy      | 20 ++++++
 6 files changed, 109 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 1fb5897066f..fd40e2c1400 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
@@ -257,6 +257,7 @@ supportedShowStatement
     | SHOW FULL? TRIGGERS ((FROM | IN) database=multipartIdentifier)? 
wildWhere?    #showTriggers    
     | SHOW TABLET DIAGNOSIS tabletId=INTEGER_VALUE                             
     #showDiagnoseTablet
     | SHOW FRONTENDS name=identifier?                                          
     #showFrontends 
+    | SHOW DATABASE databaseId=INTEGER_VALUE                                   
     #showDatabaseId
     | SHOW TABLE tableId=INTEGER_VALUE                                         
     #showTableId
     | SHOW TRASH (ON backend=STRING_LITERAL)?                                  
     #showTrash
     | SHOW WHITELIST                                                           
     #showWhitelist
@@ -311,7 +312,6 @@ unsupportedShowStatement
         LEFT_PAREN functionArguments? RIGHT_PAREN
         ((FROM | IN) database=multipartIdentifier)?                            
     #showCreateFunction
     | SHOW (DATABASES | SCHEMAS) (FROM catalog=identifier)? wildWhere?         
     #showDatabases
-    | SHOW DATABASE databaseId=INTEGER_VALUE                                   
     #showDatabaseId
     | SHOW DATA TYPES                                                          
     #showDataTypes
     | SHOW CATALOGS wildWhere?                                                 
     #showCatalogs
     | SHOW CATALOG name=identifier                                             
     #showCatalog
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 9866b410c8f..9ed352b65e5 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
@@ -244,6 +244,7 @@ import 
org.apache.doris.nereids.DorisParser.ShowCreateProcedureContext;
 import org.apache.doris.nereids.DorisParser.ShowCreateRepositoryContext;
 import org.apache.doris.nereids.DorisParser.ShowCreateTableContext;
 import org.apache.doris.nereids.DorisParser.ShowCreateViewContext;
+import org.apache.doris.nereids.DorisParser.ShowDatabaseIdContext;
 import org.apache.doris.nereids.DorisParser.ShowDeleteContext;
 import org.apache.doris.nereids.DorisParser.ShowDiagnoseTabletContext;
 import org.apache.doris.nereids.DorisParser.ShowDynamicPartitionContext;
@@ -543,6 +544,7 @@ import 
org.apache.doris.nereids.trees.plans.commands.ShowCreateProcedureCommand;
 import 
org.apache.doris.nereids.trees.plans.commands.ShowCreateRepositoryCommand;
 import org.apache.doris.nereids.trees.plans.commands.ShowCreateTableCommand;
 import org.apache.doris.nereids.trees.plans.commands.ShowCreateViewCommand;
+import org.apache.doris.nereids.trees.plans.commands.ShowDatabaseIdCommand;
 import org.apache.doris.nereids.trees.plans.commands.ShowDeleteCommand;
 import org.apache.doris.nereids.trees.plans.commands.ShowDiagnoseTabletCommand;
 import 
org.apache.doris.nereids.trees.plans.commands.ShowDynamicPartitionCommand;
@@ -4739,6 +4741,11 @@ public class LogicalPlanBuilder extends 
DorisParserBaseVisitor<Object> {
     }
 
     @Override
+    public LogicalPlan visitShowDatabaseId(ShowDatabaseIdContext ctx) {
+        long dbId = (ctx.databaseId != null) ? 
Long.parseLong(ctx.databaseId.getText()) : -1;
+        return new ShowDatabaseIdCommand(dbId);
+    }
+
     public LogicalPlan visitCreateRole(CreateRoleContext ctx) {
         String comment = ctx.STRING_LITERAL() == null ? "" : 
LogicalPlanBuilderAssistant.escapeBackSlash(
                 ctx.STRING_LITERAL().getText().substring(1, 
ctx.STRING_LITERAL().getText().length() - 1));
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 969b51ee99a..aaa93d70c35 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
@@ -209,6 +209,7 @@ public enum PlanType {
     SHOW_CREATE_REPOSITORY_COMMAND,
     SHOW_CREATE_TABLE_COMMAND,
     SHOW_CREATE_VIEW_COMMAND,
+    SHOW_DATABASE_ID_COMMAND,
     SHOW_DELETE_COMMAND,
     SHOW_DIAGNOSE_TABLET_COMMAND,
     SHOW_DYNAMIC_PARTITION_COMMAND,
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ShowDatabaseIdCommand.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ShowDatabaseIdCommand.java
new file mode 100644
index 00000000000..f183ea9df60
--- /dev/null
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ShowDatabaseIdCommand.java
@@ -0,0 +1,75 @@
+// 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.catalog.Column;
+import org.apache.doris.catalog.DatabaseIf;
+import org.apache.doris.catalog.Env;
+import org.apache.doris.catalog.ScalarType;
+import org.apache.doris.common.ErrorCode;
+import org.apache.doris.common.ErrorReport;
+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.collect.Lists;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * show database id command
+ */
+public class ShowDatabaseIdCommand extends ShowCommand {
+    private final Long dbId;
+
+    /**
+     * constructor
+     */
+    public ShowDatabaseIdCommand(Long dbId) {
+        super(PlanType.SHOW_DATABASE_ID_COMMAND);
+        this.dbId = dbId;
+    }
+
+    @Override
+    public ShowResultSet doRun(ConnectContext ctx, StmtExecutor executor) 
throws Exception {
+        // check access first
+        if 
(!Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ConnectContext.get(), 
PrivPredicate.ADMIN)) {
+            
ErrorReport.reportAnalysisException(ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR, 
"SHOW DATABASE");
+        }
+        List<List<String>> rows = Lists.newArrayList();
+        DatabaseIf database = ctx.getCurrentCatalog().getDbNullable(dbId);
+        if (database != null) {
+            List<String> row = new ArrayList<>();
+            row.add(database.getFullName());
+            rows.add(row);
+        }
+        ShowResultSetMetaData.Builder builder = 
ShowResultSetMetaData.builder();
+        builder.addColumn(new Column("DBName", ScalarType.createVarchar(30)));
+        return new ShowResultSet(builder.build(), rows);
+    }
+
+    @Override
+    public <R, C> R accept(PlanVisitor<R, C> visitor, C context) {
+        return visitor.visitShowDatabaseIdCommand(this, context);
+    }
+}
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 0d8c24086a6..339bb4e4c6a 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
@@ -90,6 +90,7 @@ import 
org.apache.doris.nereids.trees.plans.commands.ShowCreateProcedureCommand;
 import 
org.apache.doris.nereids.trees.plans.commands.ShowCreateRepositoryCommand;
 import org.apache.doris.nereids.trees.plans.commands.ShowCreateTableCommand;
 import org.apache.doris.nereids.trees.plans.commands.ShowCreateViewCommand;
+import org.apache.doris.nereids.trees.plans.commands.ShowDatabaseIdCommand;
 import org.apache.doris.nereids.trees.plans.commands.ShowDeleteCommand;
 import org.apache.doris.nereids.trees.plans.commands.ShowDiagnoseTabletCommand;
 import 
org.apache.doris.nereids.trees.plans.commands.ShowDynamicPartitionCommand;
@@ -453,6 +454,10 @@ public interface CommandVisitor<R, C> {
         return visitCommand(alterRoleCommand, context);
     }
 
+    default R visitShowDatabaseIdCommand(ShowDatabaseIdCommand 
showDatabaseIdCommand, C context) {
+        return visitCommand(showDatabaseIdCommand, context);
+    }
+
     default R visitAlterWorkloadGroupCommand(AlterWorkloadGroupCommand 
alterWorkloadGroupCommand, C context) {
         return visitCommand(alterWorkloadGroupCommand, context);
     }
diff --git 
a/regression-test/suites/nereids_p0/show/test_show_database_id_nereids.groovy 
b/regression-test/suites/nereids_p0/show/test_show_database_id_nereids.groovy
new file mode 100644
index 00000000000..6cc5eb1aac1
--- /dev/null
+++ 
b/regression-test/suites/nereids_p0/show/test_show_database_id_nereids.groovy
@@ -0,0 +1,20 @@
+// 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_database_id_nereids") {
+    checkNereidsExecute("show database 0;")
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to