Skip to content

Commit

Permalink
[Feat](Nereids) support show variables and show view command
Browse files Browse the repository at this point in the history
  • Loading branch information
starocean999 committed Nov 5, 2024
1 parent da35d3d commit 59a206f
Show file tree
Hide file tree
Showing 14 changed files with 533 additions and 5 deletions.
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,41 @@ 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().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(InternalCatalog.INTERNAL_CATALOG_NAME);
sb.append(".");
sb.append(InfoSchemaDb.DATABASE_NAME);
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());
}
}

@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
@@ -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 {
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,73 @@
// 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 SetType type;
private 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

0 comments on commit 59a206f

Please sign in to comment.