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 fa10bdde5d9 [feat](nereids)support switch command in nereids (#45561)
fa10bdde5d9 is described below

commit fa10bdde5d9e4d105cf4d43c6298c9f712a46b53
Author: Petrichor <xiaowe...@selectdb.com>
AuthorDate: Wed Dec 18 11:29:49 2024 +0800

    [feat](nereids)support switch command in nereids (#45561)
    
    Issue Number: close https://github.com/apache/doris/issues/42525
---
 .../antlr4/org/apache/doris/nereids/DorisParser.g4 |  6 +-
 .../doris/nereids/parser/LogicalPlanBuilder.java   | 11 +++
 .../apache/doris/nereids/trees/plans/PlanType.java |  3 +-
 .../trees/plans/commands/use/SwitchCommand.java    | 94 ++++++++++++++++++++++
 .../trees/plans/visitor/CommandVisitor.java        |  5 ++
 .../ddl/use/switch_command_nereids.groovy          | 22 +++++
 6 files changed, 139 insertions(+), 2 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 11796c5df3b..64ff9209bfe 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
@@ -62,6 +62,7 @@ statementBase
     | supportedCancelStatement          #supportedCancelStatementAlias
     | supportedRecoverStatement         #supportedRecoverStatementAlias
     | supportedAdminStatement           #supportedAdminStatementAlias
+    | supportedUseStatement             #supportedUseStatementAlias
     | unsupportedStatement              #unsupported
     ;
 
@@ -867,10 +868,13 @@ supportedUnsetStatement
     | UNSET DEFAULT STORAGE VAULT
     ;
 
+supportedUseStatement
+     : SWITCH catalog=identifier                                               
       #switchCatalog
+     ;
+
 unsupportedUseStatement
     : USE (catalog=identifier DOT)? database=identifier                        
      #useDatabase
     | USE ((catalog=identifier DOT)? database=identifier)? ATSIGN 
cluster=identifier #useCloudCluster
-    | SWITCH catalog=identifier                                                
      #switchCatalog
     ;
 
 unsupportedDmlStatement
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 085ad4458d0..951c8b342d5 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
@@ -302,6 +302,7 @@ import 
org.apache.doris.nereids.DorisParser.StructLiteralContext;
 import org.apache.doris.nereids.DorisParser.SubqueryContext;
 import org.apache.doris.nereids.DorisParser.SubqueryExpressionContext;
 import org.apache.doris.nereids.DorisParser.SupportedUnsetStatementContext;
+import org.apache.doris.nereids.DorisParser.SwitchCatalogContext;
 import org.apache.doris.nereids.DorisParser.SyncContext;
 import org.apache.doris.nereids.DorisParser.SystemVariableContext;
 import org.apache.doris.nereids.DorisParser.TableAliasContext;
@@ -669,6 +670,7 @@ import 
org.apache.doris.nereids.trees.plans.commands.load.LoadSequenceClause;
 import org.apache.doris.nereids.trees.plans.commands.load.LoadWhereClause;
 import 
org.apache.doris.nereids.trees.plans.commands.refresh.RefreshCatalogCommand;
 import 
org.apache.doris.nereids.trees.plans.commands.refresh.RefreshDatabaseCommand;
+import org.apache.doris.nereids.trees.plans.commands.use.SwitchCommand;
 import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
 import org.apache.doris.nereids.trees.plans.logical.LogicalCTE;
 import org.apache.doris.nereids.trees.plans.logical.LogicalExcept;
@@ -5105,5 +5107,14 @@ public class LogicalPlanBuilder extends 
DorisParserBaseVisitor<Object> {
         String queryIdPath = stripQuotes(ctx.queryIdPath.getText());
         return new ShowQueryProfileCommand(queryIdPath);
     }
+
+    @Override
+    public Object visitSwitchCatalog(SwitchCatalogContext ctx) {
+        String catalogName = ctx.catalog.getText();
+        if (catalogName != null) {
+            return new SwitchCommand(catalogName);
+        }
+        throw new AnalysisException("catalog name can not be null");
+    }
 }
 
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 e9488239b8a..9ed408dfe05 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
@@ -259,5 +259,6 @@ public enum PlanType {
     CREATE_FILE_COMMAND,
     CREATE_ROUTINE_LOAD_COMMAND,
     SHOW_TABLE_CREATION_COMMAND,
-    SHOW_QUERY_PROFILE_COMMAND
+    SHOW_QUERY_PROFILE_COMMAND,
+    SWITCH_COMMAND
 }
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/use/SwitchCommand.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/use/SwitchCommand.java
new file mode 100644
index 00000000000..61e822e8c58
--- /dev/null
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/use/SwitchCommand.java
@@ -0,0 +1,94 @@
+// 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.use;
+
+import org.apache.doris.analysis.StmtType;
+import org.apache.doris.catalog.Env;
+import org.apache.doris.common.AnalysisException;
+import org.apache.doris.common.DdlException;
+import org.apache.doris.common.ErrorCode;
+import org.apache.doris.common.ErrorReport;
+import org.apache.doris.common.util.Util;
+import org.apache.doris.mysql.privilege.PrivPredicate;
+import org.apache.doris.nereids.trees.plans.PlanType;
+import org.apache.doris.nereids.trees.plans.commands.Command;
+import org.apache.doris.nereids.trees.plans.commands.NoForward;
+import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
+import org.apache.doris.qe.ConnectContext;
+import org.apache.doris.qe.StmtExecutor;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+/**
+ * switch command.
+ */
+public class SwitchCommand extends Command implements NoForward {
+    private static final Logger LOG = 
LogManager.getLogger(SwitchCommand.class);
+    private final String catalogName;
+
+    public SwitchCommand(String catalogName) {
+        super(PlanType.SWITCH_COMMAND);
+        this.catalogName = catalogName;
+    }
+
+    @Override
+    public void run(ConnectContext ctx, StmtExecutor executor) throws 
Exception {
+        // validate catalog access
+        validate(ctx);
+        handleSwitchStmt(ctx);
+    }
+
+    @Override
+    public <R, C> R accept(PlanVisitor<R, C> visitor, C context) {
+        return visitor.visitSwitchCommand(this, context);
+    }
+
+    @Override
+    public StmtType stmtType() {
+        return StmtType.SWITCH;
+    }
+
+    public String toSql() {
+        return "SWITCH `" + catalogName + "`";
+    }
+
+    private void validate(ConnectContext context) throws AnalysisException {
+        Util.checkCatalogAllRules(catalogName);
+
+        if (!Env.getCurrentEnv().getAccessManager().checkCtlPriv(
+                ConnectContext.get(), catalogName, PrivPredicate.SHOW)) {
+            ErrorReport.reportAnalysisException(
+                    ErrorCode.ERR_CATALOG_ACCESS_DENIED, 
context.getQualifiedUser(), catalogName);
+        }
+    }
+
+    /**
+     * Process switch catalog.
+     */
+    private void handleSwitchStmt(ConnectContext context) {
+        try {
+            context.getEnv().changeCatalog(context, catalogName);
+        } catch (DdlException e) {
+            LOG.warn("handle switch command failed! ", e);
+            context.getState().setError(e.getMysqlErrorCode(), e.getMessage());
+            return;
+        }
+        context.getState().setOk();
+    }
+}
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 ff71331b7af..eb764be57c0 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
@@ -144,6 +144,7 @@ import 
org.apache.doris.nereids.trees.plans.commands.insert.InsertOverwriteTable
 import 
org.apache.doris.nereids.trees.plans.commands.load.CreateRoutineLoadCommand;
 import 
org.apache.doris.nereids.trees.plans.commands.refresh.RefreshCatalogCommand;
 import 
org.apache.doris.nereids.trees.plans.commands.refresh.RefreshDatabaseCommand;
+import org.apache.doris.nereids.trees.plans.commands.use.SwitchCommand;
 
 /** CommandVisitor. */
 public interface CommandVisitor<R, C> {
@@ -662,4 +663,8 @@ public interface CommandVisitor<R, C> {
                                            C context) {
         return visitCommand(showQueryProfileCommand, context);
     }
+
+    default R visitSwitchCommand(SwitchCommand switchCommand, C context) {
+        return visitCommand(switchCommand, context);
+    }
 }
diff --git 
a/regression-test/suites/nereids_p0/ddl/use/switch_command_nereids.groovy 
b/regression-test/suites/nereids_p0/ddl/use/switch_command_nereids.groovy
new file mode 100644
index 00000000000..c7658983c61
--- /dev/null
+++ b/regression-test/suites/nereids_p0/ddl/use/switch_command_nereids.groovy
@@ -0,0 +1,22 @@
+// 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("switch_command_nereids") {
+    // switch internal;
+    checkNereidsExecute("""switch internal;""")
+    checkNereidsExecute("""switch internal;""")
+}
\ No newline at end of file


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

Reply via email to