Skip to content

Commit

Permalink
[Enhancement] (nereids)implement showTableStatusCommand in nereids
Browse files Browse the repository at this point in the history
  • Loading branch information
rijeshkp committed Dec 10, 2024
1 parent 71f96e6 commit e5232ad
Show file tree
Hide file tree
Showing 6 changed files with 295 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ supportedShowStatement
| SHOW TABLE CREATION ((FROM | IN) database=multipartIdentifier)?
(LIKE STRING_LITERAL)? #showTableCreation
| SHOW TABLET STORAGE FORMAT VERBOSE? #showTabletStorageFormat
| SHOW TABLE STATUS ((FROM | IN) database=multipartIdentifier)? wildWhere? #showTableStatus
;

supportedLoadStatement
Expand Down Expand Up @@ -311,7 +312,6 @@ unsupportedShowStatement
| SHOW STAGES #showStages
| SHOW STORAGE (VAULT | VAULTS) #showStorageVault
| 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
| SHOW FULL? VIEWS ((FROM | IN) database=multipartIdentifier)? wildWhere? #showViews
| SHOW (GLOBAL | SESSION | LOCAL)? STATUS wildWhere? #showStatus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,11 @@
import org.apache.doris.nereids.DorisParser.ShowStorageEnginesContext;
import org.apache.doris.nereids.DorisParser.ShowTableCreationContext;
import org.apache.doris.nereids.DorisParser.ShowTableIdContext;
<<<<<<< HEAD
import org.apache.doris.nereids.DorisParser.ShowTabletStorageFormatContext;
=======
import org.apache.doris.nereids.DorisParser.ShowTableStatusContext;
>>>>>>> ce618b3bd2 ([Enhancement] (nereids)implement showTableStatusCommand in nereids)
import org.apache.doris.nereids.DorisParser.ShowTabletsBelongContext;
import org.apache.doris.nereids.DorisParser.ShowTrashContext;
import org.apache.doris.nereids.DorisParser.ShowTriggersContext;
Expand Down Expand Up @@ -579,7 +583,11 @@
import org.apache.doris.nereids.trees.plans.commands.ShowStorageEnginesCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowTableCreationCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowTableIdCommand;
<<<<<<< HEAD
import org.apache.doris.nereids.trees.plans.commands.ShowTabletStorageFormatCommand;
=======
import org.apache.doris.nereids.trees.plans.commands.ShowTableStatusCommand;
>>>>>>> ce618b3bd2 ([Enhancement] (nereids)implement showTableStatusCommand in nereids)
import org.apache.doris.nereids.trees.plans.commands.ShowTabletsBelongCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowTrashCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowTriggersCommand;
Expand Down Expand Up @@ -4972,6 +4980,7 @@ public LogicalPlan visitShowCollation(ShowCollationContext ctx) {
}

@Override
<<<<<<< HEAD
public LogicalPlan visitAdminCheckTablets(AdminCheckTabletsContext ctx) {
List<Long> tabletIdLists = new ArrayList<>();
if (ctx.tabletList() != null) {
Expand Down Expand Up @@ -5014,5 +5023,31 @@ public LogicalPlan visitAdminShowTabletStorageFormat(AdminShowTabletStorageForma
public LogicalPlan visitShowTabletStorageFormat(ShowTabletStorageFormatContext ctx) {
return new ShowTabletStorageFormatCommand(ctx.VERBOSE() != null);
}

@Override
public LogicalPlan visitShowTableStatus(ShowTableStatusContext ctx) {
String databaseName = null;
String ctlName = null;
String wild = null;

if (ctx.database != null) {
List<String> nameParts = visitMultipartIdentifier(ctx.database);
if (nameParts.size() == 1) {
databaseName = nameParts.get(0);
} else if (nameParts.size() == 2) {
ctlName = nameParts.get(0);
databaseName = nameParts.get(1);
} else {
throw new AnalysisException("nameParts in create table should be [ctl.][db.]");
}
}

if (ctx.wildWhere() != null) {
if (ctx.wildWhere().LIKE() != null) {
wild = stripQuotes(ctx.wildWhere().STRING_LITERAL().getText());
}
}
return new ShowTableStatusCommand(ctlName, databaseName, wild);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ public enum PlanType {
SHOW_ROLE_COMMAND,
SHOW_SMALL_FILES_COMMAND,
SHOW_STORAGE_ENGINES_COMMAND,
SHOW_TABLE_STATUS_COMMAND,
SHOW_TABLE_ID_COMMAND,
SHOW_TRASH_COMMAND,
SHOW_TABLET_STORAGE_FORMAT_COMMAND,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
// 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.PrimitiveType;
import org.apache.doris.catalog.ScalarType;
import org.apache.doris.catalog.TableIf;
import org.apache.doris.common.CaseSensibility;
import org.apache.doris.common.ErrorCode;
import org.apache.doris.common.ErrorReport;
import org.apache.doris.common.PatternMatcher;
import org.apache.doris.common.PatternMatcherWrapper;
import org.apache.doris.common.util.TimeUtils;
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.base.Strings;
import com.google.common.collect.Lists;

import java.util.List;

/**
* ShowVariablesCommand
*/
public class ShowTableStatusCommand extends ShowCommand {
private static final ShowResultSetMetaData META_DATA = ShowResultSetMetaData.builder()
.addColumn(new Column("Name", ScalarType.createVarchar(64)))
.addColumn(new Column("Engine", ScalarType.createVarchar(10)))
.addColumn(new Column("Version", ScalarType.createType(PrimitiveType.BIGINT)))
.addColumn(new Column("Row_format", ScalarType.createVarchar(64)))
.addColumn(new Column("Rows", ScalarType.createType(PrimitiveType.BIGINT)))
.addColumn(new Column("Avg_row_length", ScalarType.createType(PrimitiveType.BIGINT)))
.addColumn(new Column("Data_length", ScalarType.createType(PrimitiveType.BIGINT)))
.addColumn(new Column("Max_data_length", ScalarType.createType(PrimitiveType.BIGINT)))
.addColumn(new Column("Index_length", ScalarType.createType(PrimitiveType.BIGINT)))
.addColumn(new Column("Data_free", ScalarType.createType(PrimitiveType.BIGINT)))
.addColumn(new Column("Auto_increment", ScalarType.createType(PrimitiveType.BIGINT)))
.addColumn(new Column("Create_time", ScalarType.createType(PrimitiveType.DATETIME)))
.addColumn(new Column("Update_time", ScalarType.createType(PrimitiveType.DATETIME)))
.addColumn(new Column("Check_time", ScalarType.createType(PrimitiveType.DATETIME)))
.addColumn(new Column("Collation", ScalarType.createVarchar(64)))
.addColumn(new Column("Checksum", ScalarType.createType(PrimitiveType.BIGINT)))
.addColumn(new Column("Create_options", ScalarType.createVarchar(64)))
.addColumn(new Column("Comment", ScalarType.createVarchar(64)))
.build();

private String catalog;
private String db;
private String wild;

public ShowTableStatusCommand(String catalog, String db, String wild) {
super(PlanType.SHOW_TABLE_STATUS_COMMAND);
this.catalog = catalog;
this.db = db;
this.wild = wild;
}

public String getCatalog() {
return catalog;
}

public String getDb() {
return db;
}

public String getPattern() {
return wild;
}

public ShowResultSetMetaData getMetaData() {
return META_DATA;
}

@Override
public ShowResultSet doRun(ConnectContext ctx, StmtExecutor executor) throws Exception {
if (Strings.isNullOrEmpty(db)) {
db = ctx.getDatabase();
if (Strings.isNullOrEmpty(db)) {
ErrorReport.reportAnalysisException(ErrorCode.ERR_NO_DB_ERROR);
}
}
if (Strings.isNullOrEmpty(catalog)) {
catalog = ctx.getDefaultCatalog();
if (Strings.isNullOrEmpty(catalog)) {
ErrorReport.reportAnalysisException(ErrorCode.ERR_WRONG_NAME_FOR_CATALOG);
}
}

if (!Env.getCurrentEnv().getAccessManager().checkDbPriv(ctx,
catalog, db, PrivPredicate.SHOW)) {
ErrorReport.reportAnalysisException(ErrorCode.ERR_DBACCESS_DENIED_ERROR,
PrivPredicate.ADMIN.getPrivs().toString(), db);
}

List<List<String>> rows = Lists.newArrayList();
DatabaseIf<TableIf> dbName = ctx.getEnv().getCatalogMgr()
.getCatalogOrAnalysisException(getCatalog())
.getDbOrAnalysisException(getDb());
if (dbName != null) {
PatternMatcher matcher = null;
if (getPattern() != null) {
matcher = PatternMatcherWrapper.createMysqlPattern(getPattern(),
CaseSensibility.TABLE.getCaseSensibility());
}
for (TableIf table : dbName.getTables()) {
if (matcher != null && !matcher.match(table.getName())) {
continue;
}

// check tbl privs
if (!Env.getCurrentEnv().getAccessManager()
.checkTblPriv(ctx, getCatalog(),
dbName.getFullName(), table.getName(), PrivPredicate.SHOW)) {
continue;
}
List<String> row = Lists.newArrayList();
// Name
row.add(table.getName());
// Engine
row.add(table.getEngine());
// version
row.add(null);
// Row_format
row.add(null);
// Rows
row.add(String.valueOf(table.getCachedRowCount()));
// Avg_row_length
row.add(String.valueOf(table.getAvgRowLength()));
// Data_length
row.add(String.valueOf(table.getDataLength()));
// Max_data_length
row.add(null);
// Index_length
row.add(null);
// Data_free
row.add(null);
// Auto_increment
row.add(null);
// Create_time
row.add(TimeUtils.longToTimeString(table.getCreateTime() * 1000));
// Update_time
if (table.getUpdateTime() > 0) {
row.add(TimeUtils.longToTimeString(table.getUpdateTime()));
} else {
row.add(null);
}
// Check_time
if (table.getLastCheckTime() > 0) {
row.add(TimeUtils.longToTimeString(table.getLastCheckTime()));
} else {
row.add(null);
}
// Collation
row.add("utf-8");
// Checksum
row.add(null);
// Create_options
row.add(null);

row.add(table.getComment());
rows.add(row);
}
}
// sort by table name
rows.sort((x, y) -> {
return x.get(0).compareTo(y.get(0));
});
return new ShowResultSet(getMetaData(), rows);
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,11 @@
import org.apache.doris.nereids.trees.plans.commands.ShowStorageEnginesCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowTableCreationCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowTableIdCommand;
<<<<<<< HEAD
import org.apache.doris.nereids.trees.plans.commands.ShowTabletStorageFormatCommand;
=======
import org.apache.doris.nereids.trees.plans.commands.ShowTableStatusCommand;
>>>>>>> ce618b3bd2 ([Enhancement] (nereids)implement showTableStatusCommand in nereids)
import org.apache.doris.nereids.trees.plans.commands.ShowTabletsBelongCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowTrashCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowTriggersCommand;
Expand Down Expand Up @@ -621,4 +625,8 @@ default R visitShowTabletStorageFormatCommand(ShowTabletStorageFormatCommand sho
C context) {
return visitCommand(showTabletStorageFormatCommand, context);
}

default R visitShowTableStatusCommand(ShowTableStatusCommand showTableStatusCommand, C context) {
return visitCommand(showTableStatusCommand, context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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_nereids_show_table_status") {
def table = "test_nereids_show_table_status"
String dbName = "${table}_db"
sql "CREATE DATABASE IF NOT EXISTS ${dbName}"
sql "use ${dbName}"
// create table and insert data
sql """ drop table if exists ${table} force"""
sql """
create table ${table} (
`id` int(11),
`name` varchar(128),
`da` date
)
engine=olap
duplicate key(id)
partition by range(da)(
PARTITION p3 VALUES LESS THAN ('2023-01-01'),
PARTITION p4 VALUES LESS THAN ('2024-01-01'),
PARTITION p5 VALUES LESS THAN ('2025-01-01')
)
distributed by hash(id) buckets 2
properties(
"replication_num"="1",
"light_schema_change"="true"
);
"""


checkNereidsExecute("show table status")
checkNereidsExecute("show table status from ${dbName}")
checkNereidsExecute("show table status from ${dbName} like '%${table}%'")

}

0 comments on commit e5232ad

Please sign in to comment.