Skip to content

Commit

Permalink
*: add pushdown for ShowStmt and implement for show columns (#31742) (#…
Browse files Browse the repository at this point in the history
…31866)

* cherry pick #31742 to release-5.4

Signed-off-by: ti-srebot <[email protected]>
  • Loading branch information
ti-srebot committed Feb 15, 2022
1 parent 55f3b24 commit d60865a
Show file tree
Hide file tree
Showing 11 changed files with 656 additions and 174 deletions.
1 change: 1 addition & 0 deletions executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,7 @@ func (b *executorBuilder) buildShow(v *plannercore.PhysicalShow) Executor {
IfNotExists: v.IfNotExists,
GlobalScope: v.GlobalScope,
Extended: v.Extended,
Extractor: v.Extractor,
}
if e.Tp == ast.ShowMasterStatus {
// show master status need start ts.
Expand Down
22 changes: 19 additions & 3 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
gjson "encoding/json"
"fmt"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -84,6 +85,7 @@ type ShowExec struct {
Flag int // Some flag parsed from sql, such as FULL.
Roles []*auth.RoleIdentity // Used for show grants.
User *auth.UserIdentity // Used by show grants, show create user.
Extractor plannercore.ShowPredicateExtractor

is infoschema.InfoSchema

Expand Down Expand Up @@ -514,10 +516,23 @@ func (e *ShowExec) fetchShowTableStatus(ctx context.Context) error {

func (e *ShowExec) fetchShowColumns(ctx context.Context) error {
tb, err := e.getTable()

if err != nil {
return errors.Trace(err)
}
var (
fieldPatternsRegexp *regexp.Regexp
FieldFilterEnable bool
fieldFilter string
)
if e.Extractor != nil {
extractor := (e.Extractor).(*plannercore.ShowColumnsTableExtractor)
if extractor.FieldPatterns != "" {
fieldPatternsRegexp = regexp.MustCompile(extractor.FieldPatterns)
}
FieldFilterEnable = extractor.Field != ""
fieldFilter = extractor.Field
}

checker := privilege.GetPrivilegeManager(e.ctx)
activeRoles := e.ctx.GetSessionVars().ActiveRoles
if checker != nil && e.ctx.GetSessionVars().User != nil && !checker.RequestVerification(activeRoles, e.DBName.O, tb.Meta().Name.O, "", mysql.InsertPriv|mysql.SelectPriv|mysql.UpdatePriv|mysql.ReferencesPriv) {
Expand All @@ -536,10 +551,11 @@ func (e *ShowExec) fetchShowColumns(ctx context.Context) error {
return err
}
for _, col := range cols {
if e.Column != nil && e.Column.Name.L != col.Name.L {
if FieldFilterEnable && col.Name.L != fieldFilter {
continue
} else if fieldPatternsRegexp != nil && !fieldPatternsRegexp.MatchString(col.Name.L) {
continue
}

desc := table.NewColDesc(col)
var columnDefault interface{}
if desc.DefaultValue != nil {
Expand Down
6 changes: 4 additions & 2 deletions planner/cascades/implementation_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,14 @@ func (r *ImplShow) Match(expr *memo.GroupExpr, prop *property.PhysicalProperty)
func (r *ImplShow) OnImplement(expr *memo.GroupExpr, reqProp *property.PhysicalProperty) ([]memo.Implementation, error) {
logicProp := expr.Group.Prop
show := expr.ExprNode.(*plannercore.LogicalShow)

// TODO(zz-jason): unifying LogicalShow and PhysicalShow to a single
// struct. So that we don't need to create a new PhysicalShow object, which
// can help us to reduce the gc pressure of golang runtime and improve the
// overall performance.
showPhys := plannercore.PhysicalShow{ShowContents: show.ShowContents}.Init(show.SCtx())
showPhys := plannercore.PhysicalShow{
ShowContents: show.ShowContents,
Extractor: show.Extractor,
}.Init(show.SCtx())
showPhys.SetSchema(logicProp.Schema)
return []memo.Implementation{impl.NewShowImpl(showPhys)}, nil
}
Expand Down
2 changes: 1 addition & 1 deletion planner/core/find_best_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (p *LogicalShow) findBestTask(prop *property.PhysicalProperty, planCounter
if !prop.IsEmpty() || planCounter.Empty() {
return invalidTask, 0, nil
}
pShow := PhysicalShow{ShowContents: p.ShowContents}.Init(p.ctx)
pShow := PhysicalShow{ShowContents: p.ShowContents, Extractor: p.Extractor}.Init(p.ctx)
pShow.SetSchema(p.schema)
planCounter.Dec(1)
return &rootTask{p: pShow}, 1, nil
Expand Down
2 changes: 2 additions & 0 deletions planner/core/logical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,8 @@ type ShowContents struct {
type LogicalShow struct {
logicalSchemaProducer
ShowContents

Extractor ShowPredicateExtractor
}

// LogicalShowDDLJobs is for showing DDL job list.
Expand Down
Loading

0 comments on commit d60865a

Please sign in to comment.