Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 19 commits into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
31 changes: 24 additions & 7 deletions planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2937,11 +2937,8 @@ func (b *PlanBuilder) buildShow(ctx context.Context, show *ast.ShowStmt) (Plan,
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain why removing show.Pattern = nil a little bit?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is introduced in #31919 to fix case insentive with some show statment. It mean if we have newShowBaseExtractor , we will not build show.Pattern in line 3015. But in this case, we need extract patternLikeName in show.Pattern. So i remove it and add an extra check in line 3015 to make them behave same.

Copy link
Contributor

@xhebox xhebox Jun 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is safer to mark an extra flag like noShowPattern or somethine similar. There are some other extractors that may be introduced later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. An extra flag is more clearer.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. An extra flag is more clearer.

You don't need to add it to LogicalShow struct, a variable in buildShow is enough 🤣

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, yes. Just finished exercise, my head is a little dizzy

show.Pattern = nil
}
case ast.ShowCreateTable, ast.ShowCreateSequence, ast.ShowPlacementForTable, ast.ShowPlacementForPartition:
var err error
Expand Down Expand Up @@ -3012,7 +3009,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 && p.Extractor == nil {
show.Pattern.Expr = &ast.ColumnNameExpr{
Name: &ast.ColumnName{Name: p.OutputNames()[0].ColName},
}
Expand Down Expand Up @@ -4620,12 +4618,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 @@ -4842,3 +4848,14 @@ func (b *PlanBuilder) buildCompactTable(node *ast.CompactTableStmt) (Plan, error
}
return p, nil
}

func extractPatternLikeName(patternLike *ast.PatternLikeExpr) string {
if patternLike == nil {
return ""
}
switch patternLike.Pattern.(type) {
case *driver.ValueExpr:
return patternLike.Pattern.(*driver.ValueExpr).GetString()
}
likzn marked this conversation as resolved.
Show resolved Hide resolved
return ""
}