Repository: matrixorigin/matrixone
Pull request #7279 (2022-12-21 03:30:24)

#6671 support some show stmt for dev
## What type of PR is this?

- [ ] API-change
- [ ] BUG
- [ ] Improvement
- [ ] Documentation
- [x] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue #6671 

## What this PR does / why we need it:
support some show stmt for dev


Patch:
diff --git a/pkg/frontend/authenticate.go b/pkg/frontend/authenticate.go
index e066c070f..8f3fa3265 100644
--- a/pkg/frontend/authenticate.go
+++ b/pkg/frontend/authenticate.go
@@ -3400,7 +3400,8 @@ func determinePrivilegeSetOfStatement(stmt tree.Statement) *privilege {
 		typs = append(typs, PrivilegeTypeIndex, PrivilegeTypeTableAll, PrivilegeTypeTableOwnership)
 		writeDatabaseAndTableDirectly = true
 	case *tree.ShowProcessList, *tree.ShowErrors, *tree.ShowWarnings, *tree.ShowVariables,
-		*tree.ShowStatus, *tree.ShowTarget, *tree.ShowTableStatus, *tree.ShowGrants, *tree.ShowCollation, *tree.ShowIndex:
+		*tree.ShowStatus, *tree.ShowTarget, *tree.ShowTableStatus, *tree.ShowGrants, *tree.ShowCollation, *tree.ShowIndex,
+		*tree.ShowTableNumber, *tree.ShowColumnNumber, *tree.ShowTableValues:
 		objType = objectTypeNone
 		kind = privilegeKindNone
 	case *tree.ExplainFor, *tree.ExplainAnalyze, *tree.ExplainStmt:
diff --git a/pkg/frontend/mysql_cmd_executor.go b/pkg/frontend/mysql_cmd_executor.go
index f6d4ab8f3..20c15e638 100644
--- a/pkg/frontend/mysql_cmd_executor.go
+++ b/pkg/frontend/mysql_cmd_executor.go
@@ -3490,7 +3490,7 @@ func (mce *MysqlCmdExecutor) doComQuery(requestCtx context.Context, sql string)
 			*tree.ShowCreateTable, *tree.ShowCreateDatabase, *tree.ShowTables, *tree.ShowDatabases, *tree.ShowColumns,
 			*tree.ShowProcessList, *tree.ShowStatus, *tree.ShowTableStatus, *tree.ShowGrants,
 			*tree.ShowIndex, *tree.ShowCreateView, *tree.ShowTarget, *tree.ShowCollation,
-			*tree.ExplainFor, *tree.ExplainStmt:
+			*tree.ExplainFor, *tree.ExplainStmt, *tree.ShowTableNumber, *tree.ShowColumnNumber, *tree.ShowTableValues:
 			columns, err = cw.GetColumns()
 			if err != nil {
 				logErrorf(ses.GetConciseProfile(), "GetColumns from Computation handler failed. error: %v", err)
diff --git a/pkg/sql/plan/build.go b/pkg/sql/plan/build.go
index 99e45a457..efbbe4866 100644
--- a/pkg/sql/plan/build.go
+++ b/pkg/sql/plan/build.go
@@ -120,6 +120,10 @@ func BuildPlan(ctx CompilerContext, stmt tree.Statement) (*Plan, error) {
 		return buildShowStatus(stmt, ctx)
 	case *tree.ShowProcessList:
 		return buildShowProcessList(stmt, ctx)
+	case *tree.ShowTableNumber:
+		return buildShowTableNumber(stmt, ctx)
+	case *tree.ShowColumnNumber:
+		return buildShowColumnNumber(stmt, ctx)
 	case *tree.SetVar:
 		return buildSetVariables(stmt, ctx)
 	case *tree.Execute:
diff --git a/pkg/sql/plan/build_show.go b/pkg/sql/plan/build_show.go
index 796020886..d9bd6c7a0 100644
--- a/pkg/sql/plan/build_show.go
+++ b/pkg/sql/plan/build_show.go
@@ -337,6 +337,46 @@ func buildShowTables(stmt *tree.ShowTables, ctx CompilerContext) (*Plan, error)
 	return returnByRewriteSQL(ctx, sql, ddlType)
 }
 
+func buildShowTableNumber(stmt *tree.ShowTableNumber, ctx CompilerContext) (*Plan, error) {
+	dbName := stmt.DbName
+	if stmt.DbName == "" {
+		dbName = ctx.DefaultDatabase()
+	} else if !ctx.DatabaseExists(dbName) {
+		return nil, moerr.NewBadDB(ctx.GetContext(), dbName)
+	}
+
+	if dbName == "" {
+		return nil, moerr.NewNoDB(ctx.GetContext())
+	}
+
+	ddlType := plan.DataDefinition_SHOW_TABLES
+
+	sql := "SELECT '%s', count(relname) `Number of tables in %s` FROM %s.mo_tables WHERE reldatabase = '%s' and relname != '%s' and relname not like '%s'"
+	sql = fmt.Sprintf(sql, dbName, dbName, MO_CATALOG_DB_NAME, dbName, "%!%mo_increment_columns", "__mo_cpkey_unique_0_%")
+
+	return returnByRewriteSQL(ctx, sql, ddlType)
+}
+
+func buildShowColumnNumber(stmt *tree.ShowColumnNumber, ctx CompilerContext) (*Plan, error) {
+	dbName := stmt.Table.GetDBName()
+	if dbName == "" {
+		dbName = ctx.DefaultDatabase()
+	} else if !ctx.DatabaseExists(dbName) {
+		return nil, moerr.NewBadDB(ctx.GetContext(), dbName)
+	}
+
+	tblName := string(stmt.Table.ToTableName().ObjectName)
+	_, tableDef := ctx.Resolve(dbName, tblName)
+	if tableDef == nil {
+		return nil, moerr.NewNoSuchTable(ctx.GetContext(), dbName, tblName)
+	}
+
+	ddlType := plan.DataDefinition_SHOW_COLUMNS
+	sql := "SELECT '%s', count(attname) `Number of columns in %s` FROM %s.mo_columns WHERE att_database = '%s' AND att_relname = '%s'"
+	sql = fmt.Sprintf(sql, tblName, tblName, MO_CATALOG_DB_NAME, dbName, tblName)
+
+	return returnByRewriteSQL(ctx, sql, ddlType)
+}
 func buildShowColumns(stmt *tree.ShowColumns, ctx CompilerContext) (*Plan, error) {
 	if stmt.Like != nil && stmt.Where != nil {
 		return nil, moerr.NewSyntaxError(ctx.GetContext(), "like clause and where clause cannot exist at the same time")


Test patch:
diff --git a/test/distributed/cases/dml/show/show.result b/test/distributed/cases/dml/show/show.result
index 62931fcc8..4cd7e99f6 100755
--- a/test/distributed/cases/dml/show/show.result
+++ b/test/distributed/cases/dml/show/show.result
@@ -215,4 +215,24 @@ t1	CREATE TABLE `t1` (\n`c1` INT NOT NULL AUTO_INCREMENT,\n`c2` TINYINT DEFAULT
 SHOW TRIGGERS;
 Trigger    Event    Table    Statement    Timing    Created    sql_mode    Definer    character_set_client    collation_connection    Database Collation
 SHOW TRIGGERS like '*%';
-Trigger    Event    Table    Statement    Timing    Created    sql_mode    Definer    character_set_client    collation_connection    Database Collation
\ No newline at end of file
+Trigger    Event    Table    Statement    Timing    Created    sql_mode    Definer    character_set_client    collation_connection    Database Collation
+use mo_catalog;
+show tables;
+tables_in_mo_catalog
+mo_database
+mo_tables
+mo_columns
+mo_user
+mo_account
+mo_role
+mo_user_grant
+mo_role_grant
+mo_role_privs
+show table_number from mo_catalog;
+mo_catalog    Number of tables in mo_catalog
+mo_catalog    9
+show column_number from mo_database;
+mo_database    Number of columns in mo_database
+mo_database    8
+show column_number from hdjshdj;
+no such table mo_catalog.hdjshdj
\ No newline at end of file
diff --git a/test/distributed/cases/dml/show/show.test b/test/distributed/cases/dml/show/show.test
index b2daff204..b740d546e 100755
--- a/test/distributed/cases/dml/show/show.test
+++ b/test/distributed/cases/dml/show/show.test
@@ -116,4 +116,10 @@ create table t1(c1 int primary key auto_increment,c2 tinyint not null default 4,
 show create table t1;
 
 SHOW TRIGGERS;
-SHOW TRIGGERS like '*%';
\ No newline at end of file
+SHOW TRIGGERS like '*%';
+
+use mo_catalog;
+show tables;
+show table_number from mo_catalog;
+show column_number from mo_database;
+show column_number from hdjshdj;
\ No newline at end of file