Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat](Nereids) support show variables and show view command #43271

Merged
merged 4 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ statementBase
| supportedCreateStatement #supportedCreateStatementAlias
| supportedAlterStatement #supportedAlterStatementAlias
| materializedViewStatement #materializedViewStatementAlias
| supportedJobStatement #supportedJobStatementAlias
| supportedJobStatement #supportedJobStatementAlias
| constraintStatement #constraintStatementAlias
| supportedDropStatement #supportedDropStatementAlias
| supportedSetStatement #supportedSetStatementAlias
| supportedUnsetStatement #supportedUnsetStatementAlias
| supportedShowStatement #supportedShowStatementAlias
| unsupportedStatement #unsupported
;

Expand Down Expand Up @@ -189,6 +190,13 @@ supportedDropStatement
: DROP CATALOG RECYCLE BIN WHERE idType=STRING_LITERAL EQ id=INTEGER_VALUE #dropCatalogRecycleBin
;

supportedShowStatement
: SHOW (GLOBAL | SESSION | LOCAL)? VARIABLES wildWhere? #showVariables
| SHOW VIEW
(FROM |IN) tableName=multipartIdentifier
((FROM | IN) database=identifier)? #showView
;

unsupportedOtherStatement
: HELP mark=identifierOrText #help
| INSTALL PLUGIN FROM source=identifierOrText properties=propertyClause? #installPlugin
Expand Down Expand Up @@ -224,7 +232,6 @@ unsupportedShowStatement
| SHOW STORAGE (VAULT | VAULTS) #showStorageVault
| SHOW CREATE REPOSITORY FOR identifier #showCreateRepository
| SHOW WHITELIST #showWhitelist
| SHOW (GLOBAL | SESSION | LOCAL)? VARIABLES wildWhere? #showVariables
| SHOW OPEN TABLES ((FROM | IN) database=multipartIdentifier)? wildWhere? #showOpenTables
| SHOW TABLE STATUS ((FROM | IN) database=multipartIdentifier)? wildWhere? #showTableStatus
| SHOW FULL? TABLES ((FROM | IN) database=multipartIdentifier)? wildWhere? #showTables
Expand Down Expand Up @@ -302,9 +309,6 @@ unsupportedShowStatement
| SHOW (KEY | KEYS | INDEX | INDEXES)
(FROM |IN) tableName=multipartIdentifier
((FROM | IN) database=multipartIdentifier)? #showIndex
| SHOW VIEW
(FROM |IN) tableName=multipartIdentifier
((FROM | IN) database=multipartIdentifier)? #showView
| SHOW TRANSACTION ((FROM | IN) database=multipartIdentifier)? wildWhere? #showTransaction
| SHOW QUERY PROFILE queryIdPath=STRING_LITERAL #showQueryProfile
| SHOW LOAD PROFILE loadIdPath=STRING_LITERAL #showLoadProfile
Expand Down
13 changes: 13 additions & 0 deletions fe/fe-core/src/main/java/org/apache/doris/catalog/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,19 @@ public List<Table> getViews() {
return views;
}

public List<Table> getViewsOnIdOrder() {
List<Table> tables = idToTable.values().stream()
.sorted(Comparator.comparing(Table::getId))
.collect(Collectors.toList());
List<Table> views = new ArrayList<>();
for (Table table : tables) {
if (table.getType() == TableType.VIEW) {
views.add(table);
}
}
return views;
}

/**
* this method is used for get existed table list by table id list, if table not exist, just ignore it.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@
import org.apache.doris.catalog.AggregateType;
import org.apache.doris.catalog.BuiltinAggregateFunctions;
import org.apache.doris.catalog.Env;
import org.apache.doris.catalog.InfoSchemaDb;
import org.apache.doris.catalog.KeysType;
import org.apache.doris.catalog.ScalarType;
import org.apache.doris.common.Config;
import org.apache.doris.common.FeConstants;
import org.apache.doris.common.Pair;
import org.apache.doris.datasource.InternalCatalog;
import org.apache.doris.job.common.IntervalUnit;
import org.apache.doris.load.loadv2.LoadTask;
import org.apache.doris.mtmv.MTMVPartitionInfo.MTMVPartitionType;
Expand Down Expand Up @@ -194,6 +196,8 @@
import org.apache.doris.nereids.DorisParser.ShowCreateMTMVContext;
import org.apache.doris.nereids.DorisParser.ShowCreateProcedureContext;
import org.apache.doris.nereids.DorisParser.ShowProcedureStatusContext;
import org.apache.doris.nereids.DorisParser.ShowVariablesContext;
import org.apache.doris.nereids.DorisParser.ShowViewContext;
import org.apache.doris.nereids.DorisParser.SimpleColumnDefContext;
import org.apache.doris.nereids.DorisParser.SimpleColumnDefsContext;
import org.apache.doris.nereids.DorisParser.SingleStatementContext;
Expand Down Expand Up @@ -423,6 +427,8 @@
import org.apache.doris.nereids.trees.plans.commands.ShowCreateMTMVCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowCreateProcedureCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowProcedureStatusCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowVariablesCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowViewCommand;
import org.apache.doris.nereids.trees.plans.commands.UnsetDefaultStorageVaultCommand;
import org.apache.doris.nereids.trees.plans.commands.UnsetVariableCommand;
import org.apache.doris.nereids.trees.plans.commands.UnsupportedCommand;
Expand Down Expand Up @@ -3999,4 +4005,45 @@ public SetUserPropertiesCommand visitSetUserProperties(SetUserPropertiesContext
public SetDefaultStorageVaultCommand visitSetDefaultStorageVault(SetDefaultStorageVaultContext ctx) {
return new SetDefaultStorageVaultCommand(stripQuotes(ctx.identifier().getText()));
}

@Override
public LogicalPlan visitShowVariables(ShowVariablesContext ctx) {
SetType type = SetType.DEFAULT;
if (ctx.GLOBAL() != null) {
type = SetType.GLOBAL;
} else if (ctx.LOCAL() != null || ctx.SESSION() != null) {
type = SetType.SESSION;
}
if (ctx.wildWhere() != null) {
if (ctx.wildWhere().LIKE() != null) {
return new ShowVariablesCommand(type, stripQuotes(ctx.wildWhere().STRING_LITERAL().getText()));
} else {
StringBuilder sb = new StringBuilder();
sb.append("SELECT `VARIABLE_NAME` AS `Variable_name`, `VARIABLE_VALUE` AS `Value` FROM ");
sb.append("`").append(InternalCatalog.INTERNAL_CATALOG_NAME).append("`");
sb.append(".");
sb.append("`").append(InfoSchemaDb.DATABASE_NAME).append("`");
sb.append(".");
if (type == SetType.GLOBAL) {
sb.append("`global_variables` ");
} else {
sb.append("`session_variables` ");
}
sb.append(getOriginSql(ctx.wildWhere()));
return new NereidsParser().parseSingle(sb.toString());
}
} else {
return new ShowVariablesCommand(type, null);
}
}

@Override
public ShowViewCommand visitShowView(ShowViewContext ctx) {
List<String> tableNameParts = visitMultipartIdentifier(ctx.tableName);
String databaseName = null;
if (ctx.database != null) {
databaseName = stripQuotes(ctx.database.getText());
}
return new ShowViewCommand(databaseName, new TableNameInfo(tableNameParts));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,7 @@ public enum PlanType {
PREPARED_COMMAND,
EXECUTE_COMMAND,
SHOW_CONFIG_COMMAND,
SHOW_VARIABLES_COMMAND,
SHOW_VIEW_COMMAND,
REPLAY_COMMAND
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.apache.doris.nereids.trees.plans.commands;

import org.apache.doris.common.Config;
import org.apache.doris.common.DdlException;
import org.apache.doris.nereids.memo.GroupExpression;
import org.apache.doris.nereids.properties.LogicalProperties;
import org.apache.doris.nereids.trees.expressions.Expression;
Expand Down Expand Up @@ -116,4 +118,16 @@ public String treeString() {
public Plan withGroupExpression(Optional<GroupExpression> groupExpression) {
throw new RuntimeException("Command do not implement withGroupExpression");
}

public void verifyCommandSupported() throws DdlException {
// check command has been supported in cloud mode
if (Config.isCloudMode()) {
checkSupportedInCloudMode();
}
}

// check if the command is supported in cloud mode
// see checkStmtSupported() in fe/fe-core/src/main/java/org/apache/doris/qe/ShowExecutor.java
// override this method if the command is not supported in cloud mode
protected void checkSupportedInCloudMode() throws DdlException {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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.StmtType;
import org.apache.doris.nereids.trees.plans.PlanType;
import org.apache.doris.qe.ConnectContext;
import org.apache.doris.qe.ShowResultSet;
import org.apache.doris.qe.StmtExecutor;

/**
* base class for all show commands
*/
public abstract class ShowCommand extends Command implements NoForward {
morrySnow marked this conversation as resolved.
Show resolved Hide resolved
public ShowCommand(PlanType type) {
super(type);
}

@Override
public StmtType stmtType() {
return StmtType.SHOW;
}

@Override
public void run(ConnectContext ctx, StmtExecutor executor) throws Exception {
ShowResultSet resultSet = doRun(ctx, executor);
if (resultSet != null) {
if (executor.isProxy()) {
executor.setProxyShowResultSet(resultSet);
} else {
executor.sendResultSet(resultSet);
}
}
}

public abstract ShowResultSet doRun(ConnectContext ctx, StmtExecutor executor) throws Exception;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// 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.SetType;
import org.apache.doris.catalog.Column;
import org.apache.doris.catalog.ScalarType;
import org.apache.doris.common.CaseSensibility;
import org.apache.doris.common.PatternMatcher;
import org.apache.doris.common.PatternMatcherWrapper;
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 org.apache.doris.qe.VariableMgr;

import java.util.List;

/**
* ShowVariablesCommand
*/
public class ShowVariablesCommand extends ShowCommand {
private static final String NAME_COL = "Variable_name";
private static final String VALUE_COL = "Value";
private static final String DEFAULT_VALUE_COL = "Default_Value";
private static final String CHANGED_COL = "Changed";
private static final ShowResultSetMetaData META_DATA = ShowResultSetMetaData.builder()
.addColumn(new Column(NAME_COL, ScalarType.createVarchar(20)))
.addColumn(new Column(VALUE_COL, ScalarType.createVarchar(20)))
.addColumn(new Column(DEFAULT_VALUE_COL, ScalarType.createVarchar(20)))
.addColumn(new Column(CHANGED_COL, ScalarType.createVarchar(20)))
.build();

private final SetType type;
private final String pattern;

public ShowVariablesCommand(SetType type, String pattern) {
super(PlanType.SHOW_VARIABLES_COMMAND);
this.type = type == null ? SetType.DEFAULT : type;
this.pattern = pattern;
}

@Override
public <R, C> R accept(PlanVisitor<R, C> visitor, C context) {
return visitor.visitShowVariablesCommand(this, context);
}

@Override
public ShowResultSet doRun(ConnectContext ctx, StmtExecutor executor) throws Exception {
PatternMatcher matcher = null;
if (pattern != null) {
matcher = PatternMatcherWrapper.createMysqlPattern(pattern, CaseSensibility.VARIABLES.getCaseSensibility());
}
List<List<String>> rows = VariableMgr.dump(type, ctx.getSessionVariable(), matcher);
return new ShowResultSet(META_DATA, rows);
}
}
Loading
Loading