ddl: make output field name in show tables/databases stmt compatible with mysql (#35136)

close pingcap/tidb#35116
This commit is contained in:
likzn
2022-06-29 15:48:39 +08:00
committed by GitHub
parent f0d5f6e9cd
commit 9bde478b22
3 changed files with 34 additions and 7 deletions

View File

@ -0,0 +1,4 @@
show tables like '%xx';
Tables_in_test (%xx)
show databases like '%xx';
Database (%xx)

View File

@ -0,0 +1,3 @@
# test show output field name
show tables like '%xx';
show databases like '%xx';

View File

@ -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
@ -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},
}
@ -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")
}
@ -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 ""
}