Skip to content

Commit

Permalink
executor: fix show database like case sensitive issue#34766 (#34925)
Browse files Browse the repository at this point in the history
close #34766
  • Loading branch information
e1ijah1 committed May 30, 2022
1 parent 5d895ee commit b3d7a8e
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 0 deletions.
19 changes: 19 additions & 0 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,11 +402,30 @@ func (e *ShowExec) fetchShowDatabases() error {
dbs := e.is.AllSchemaNames()
checker := privilege.GetPrivilegeManager(e.ctx)
sort.Strings(dbs)
var (
fieldPatternsLike collate.WildcardPattern
FieldFilterEnable bool
fieldFilter string
)

if e.Extractor != nil {
extractor := (e.Extractor).(*plannercore.ShowDatabaseExtractor)
if extractor.FieldPatterns != "" {
fieldPatternsLike = collate.GetCollatorByID(collate.CollationName2ID(mysql.UTF8MB4DefaultCollation)).Pattern()
fieldPatternsLike.Compile(extractor.FieldPatterns, byte('\\'))
}
FieldFilterEnable = extractor.Field != ""
fieldFilter = extractor.Field
}
// let information_schema be the first database
moveInfoSchemaToFront(dbs)
for _, d := range dbs {
if checker != nil && !checker.DBIsVisible(e.ctx.GetSessionVars().ActiveRoles, d) {
continue
} else if FieldFilterEnable && strings.ToLower(d) != fieldFilter {
continue
} else if fieldPatternsLike != nil && !fieldPatternsLike.DoMatch(strings.ToLower(d)) {
continue
}
e.appendRow([]interface{}{
d,
Expand Down
17 changes: 17 additions & 0 deletions executor/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1831,3 +1831,20 @@ func TestShowBindingCacheStatus(t *testing.T) {
tk.MustQuery("show binding_cache status").Check(testkit.Rows(
"1 1 198 Bytes 250 Bytes"))
}

func TestShowDatabasesLike(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()

tk := testkit.NewTestKit(t, store)
require.True(t, tk.Session().Auth(&auth.UserIdentity{
Username: "root", Hostname: "%"}, nil, nil))

tk.MustExec("DROP DATABASE IF EXISTS `TEST_$1`")
tk.MustExec("DROP DATABASE IF EXISTS `test_$2`")
tk.MustExec("CREATE DATABASE `TEST_$1`;")
tk.MustExec("CREATE DATABASE `test_$2`;")

tk.MustQuery("SHOW DATABASES LIKE 'TEST_%'").Check(testkit.Rows("TEST_$1", "test_$2"))
tk.MustQuery("SHOW DATABASES LIKE 'test_%'").Check(testkit.Rows("TEST_$1", "test_$2"))
}
7 changes: 7 additions & 0 deletions planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3012,6 +3012,13 @@ func (b *PlanBuilder) buildShow(ctx context.Context, show *ast.ShowStmt) (Plan,
if tableInfo.Meta().TempTableType != model.TempTableNone {
return nil, ErrOptOnTemporaryTable.GenWithStackByArgs("show table regions")
}
case ast.ShowDatabases:
var extractor ShowDatabaseExtractor
if extractor.Extract(show) {
p.Extractor = &extractor
// Avoid building Selection.
show.Pattern = nil
}
}
if show.Tp == ast.ShowVariables {
var extractor ShowVariablesExtractor
Expand Down
23 changes: 23 additions & 0 deletions planner/core/show_predicate_extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var (
_ ShowPredicateExtractor = &ShowColumnsTableExtractor{}
_ ShowPredicateExtractor = &ShowTablesTableExtractor{}
_ ShowPredicateExtractor = &ShowVariablesExtractor{}
_ ShowPredicateExtractor = &ShowDatabaseExtractor{}
)

// ShowPredicateExtractor is used to extract some predicates from `PatternLikeExpr` clause
Expand Down Expand Up @@ -167,3 +168,25 @@ func (e *ShowVariablesExtractor) explainInfo() string {
}
return s
}

// ShowDatabaseExtractor is used to extract some predicates of databases.
type ShowDatabaseExtractor struct {
ShowBaseExtractor
}

func (e *ShowDatabaseExtractor) explainInfo() string {
r := new(bytes.Buffer)
if len(e.Field) > 0 {
r.WriteString(fmt.Sprintf("database:[%s], ", e.Field))
}
if len(e.FieldPatterns) > 0 {
r.WriteString(fmt.Sprintf("database_pattern:[%s], ", e.FieldPatterns))
}

// remove the last ", " in the message info
s := r.String()
if len(s) > 2 {
return s[:len(s)-2]
}
return s
}
16 changes: 16 additions & 0 deletions planner/core/stringer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ func TestPlanStringer(t *testing.T) {
sql: "show tables in test like '%T%'",
plan: "Show(table_pattern:[%t%])",
},
{
sql: "show databases like 't'",
plan: "Show(database:[t])",
},
{
sql: "show databases like 'T'",
plan: "Show(database:[t])",
},
{
sql: "show databases like 't%'",
plan: "Show(database_pattern:[t%])",
},
{
sql: "show databases like '%T%'",
plan: "Show(database_pattern:[%t%])",
},
}
parser := parser.New()
for _, tt := range tests {
Expand Down

0 comments on commit b3d7a8e

Please sign in to comment.