Skip to content

Commit

Permalink
ddl: make output field name in show tables/databases stmt compatibl…
Browse files Browse the repository at this point in the history
…e with mysql (#35136)

close #35116
  • Loading branch information
likzn authored Jun 29, 2022
1 parent f0d5f6e commit 9bde478
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
4 changes: 4 additions & 0 deletions cmd/explaintest/r/show.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
show tables like '%xx';
Tables_in_test (%xx)
show databases like '%xx';
Database (%xx)
3 changes: 3 additions & 0 deletions cmd/explaintest/t/show.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# test show output field name
show tables like '%xx';
show databases like '%xx';
34 changes: 27 additions & 7 deletions planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2933,17 +2933,17 @@ func (b *PlanBuilder) buildShow(ctx context.Context, show *ast.ShowStmt) (Plan,
}.Init(b.ctx)
isView := false
isSequence := false
// It depends on ShowPredicateExtractor now
buildPattern := true

switch show.Tp {
case ast.ShowDatabases, ast.ShowVariables, ast.ShowTables, ast.ShowColumns, ast.ShowTableStatus, ast.ShowCollation:
if (show.Tp == ast.ShowTables || show.Tp == ast.ShowTableStatus) && p.DBName == "" {
return nil, ErrNoDB
}
extractor := newShowBaseExtractor(*show)
if extractor.Extract() {
if extractor := newShowBaseExtractor(*show); extractor.Extract() {
p.Extractor = extractor
// Avoid building Selection.
show.Pattern = nil
buildPattern = false
}
case ast.ShowCreateTable, ast.ShowCreateSequence, ast.ShowPlacementForTable, ast.ShowPlacementForPartition:
var err error
Expand Down Expand Up @@ -3019,7 +3019,8 @@ func (b *PlanBuilder) buildShow(ctx context.Context, show *ast.ShowStmt) (Plan,
var err error
var np LogicalPlan
np = p
if show.Pattern != nil {
// If we have ShowPredicateExtractor, we do not buildSelection with Pattern
if show.Pattern != nil && buildPattern {
show.Pattern.Expr = &ast.ColumnNameExpr{
Name: &ast.ColumnName{Name: p.OutputNames()[0].ColName},
}
Expand Down Expand Up @@ -4645,12 +4646,20 @@ func buildShowSchema(s *ast.ShowStmt, isView bool, isSequence bool) (schema *exp
case ast.ShowConfig:
names = []string{"Type", "Instance", "Name", "Value"}
case ast.ShowDatabases:
names = []string{"Database"}
fieldDB := "Database"
if patternName := extractPatternLikeName(s.Pattern); patternName != "" {
fieldDB = fmt.Sprintf("%s (%s)", fieldDB, patternName)
}
names = []string{fieldDB}
case ast.ShowOpenTables:
names = []string{"Database", "Table", "In_use", "Name_locked"}
ftypes = []byte{mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeLong, mysql.TypeLong}
case ast.ShowTables:
names = []string{fmt.Sprintf("Tables_in_%s", s.DBName)}
fieldTable := fmt.Sprintf("Tables_in_%s", s.DBName)
if patternName := extractPatternLikeName(s.Pattern); patternName != "" {
fieldTable = fmt.Sprintf("%s (%s)", fieldTable, patternName)
}
names = []string{fieldTable}
if s.Full {
names = append(names, "Table_type")
}
Expand Down Expand Up @@ -4870,3 +4879,14 @@ func (b *PlanBuilder) buildCompactTable(node *ast.CompactTableStmt) (Plan, error
}
return p, nil
}

func extractPatternLikeName(patternLike *ast.PatternLikeExpr) string {
if patternLike == nil {
return ""
}
switch v := patternLike.Pattern.(type) {
case *driver.ValueExpr:
return v.GetString()
}
return ""
}

0 comments on commit 9bde478

Please sign in to comment.