Skip to content

Commit

Permalink
[feat](nereids)support switch command in nereids (#45561)
Browse files Browse the repository at this point in the history
Issue Number: close #42525
  • Loading branch information
vinlee19 authored Dec 18, 2024
1 parent 9a4253f commit fa10bdd
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ statementBase
| supportedCancelStatement #supportedCancelStatementAlias
| supportedRecoverStatement #supportedRecoverStatementAlias
| supportedAdminStatement #supportedAdminStatementAlias
| supportedUseStatement #supportedUseStatementAlias
| unsupportedStatement #unsupported
;

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@
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;
Expand Down Expand Up @@ -669,6 +670,7 @@
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;
Expand Down Expand Up @@ -5105,5 +5107,14 @@ public LogicalPlan visitShowQueryProfile(ShowQueryProfileContext ctx) {
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");
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
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> {
Expand Down Expand Up @@ -662,4 +663,8 @@ default R visitShowQueryProfileCommand(ShowQueryProfileCommand showQueryProfileC
C context) {
return visitCommand(showQueryProfileCommand, context);
}

default R visitSwitchCommand(SwitchCommand switchCommand, C context) {
return visitCommand(switchCommand, context);
}
}
Original file line number Diff line number Diff line change
@@ -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;""")
}

0 comments on commit fa10bdd

Please sign in to comment.