Skip to content

Commit

Permalink
planner: simplify the session binding handler code (#51442)
Browse files Browse the repository at this point in the history
ref #51347
  • Loading branch information
qw4990 authored Mar 1, 2024
1 parent a1472a8 commit 8c2167e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 46 deletions.
2 changes: 2 additions & 0 deletions pkg/bindinfo/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ func prepareHints(sctx sessionctx.Context, binding *Binding) error {
if err != nil {
return err
}
tableNames := CollectTableNames(bindingStmt)
isFuzzy := isFuzzyBinding(bindingStmt)
if isFuzzy {
dbName = "*" // ues '*' for universal bindings
Expand Down Expand Up @@ -172,6 +173,7 @@ func prepareHints(sctx sessionctx.Context, binding *Binding) error {
}
binding.Hint = hintsSet
binding.ID = hintsStr
binding.TableNames = tableNames
return nil
}

Expand Down
21 changes: 5 additions & 16 deletions pkg/bindinfo/global_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,32 +493,21 @@ func newBinding(sctx sessionctx.Context, row chunk.Row) (string, Binding, error)
if status == Using {
status = Enabled
}
defaultDB := row.GetString(2)

bindSQL := row.GetString(1)
charset, collation := row.GetString(6), row.GetString(7)
stmt, err := parser.New().ParseOneStmt(bindSQL, charset, collation)
if err != nil {
return "", Binding{}, err
}
tableNames := CollectTableNames(stmt)

binding := Binding{
OriginalSQL: row.GetString(0),
Db: strings.ToLower(defaultDB),
BindSQL: bindSQL,
Db: strings.ToLower(row.GetString(2)),
BindSQL: row.GetString(1),
Status: status,
CreateTime: row.GetTime(4),
UpdateTime: row.GetTime(5),
Charset: charset,
Collation: collation,
Charset: row.GetString(6),
Collation: row.GetString(7),
Source: row.GetString(8),
SQLDigest: row.GetString(9),
PlanDigest: row.GetString(10),
TableNames: tableNames,
}
sqlDigest := parser.DigestNormalized(binding.OriginalSQL)
err = prepareHints(sctx, &binding)
err := prepareHints(sctx, &binding)
sctx.GetSessionVars().CurrentDB = binding.Db
return sqlDigest.String(), binding, err
}
Expand Down
33 changes: 3 additions & 30 deletions pkg/bindinfo/session_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"time"

"github.com/pingcap/errors"
"github.com/pingcap/tidb/pkg/bindinfo/norm"
"github.com/pingcap/tidb/pkg/metrics"
"github.com/pingcap/tidb/pkg/parser"
"github.com/pingcap/tidb/pkg/parser/ast"
Expand Down Expand Up @@ -56,13 +55,13 @@ type SessionBindingHandle interface {

// sessionBindingHandle is used to handle all session sql bind operations.
type sessionBindingHandle struct {
ch BindingCache
ch FuzzyBindingCache
}

// NewSessionBindingHandle creates a new SessionBindingHandle.
func NewSessionBindingHandle() SessionBindingHandle {
sessionHandle := &sessionBindingHandle{}
sessionHandle.ch = newBindCache()
sessionHandle.ch = newFuzzyBindingCache()
return sessionHandle
}

Expand Down Expand Up @@ -104,33 +103,7 @@ func (h *sessionBindingHandle) DropSessionBinding(sqlDigest string) error {

// MatchSessionBinding returns the matched binding for this statement.
func (h *sessionBindingHandle) MatchSessionBinding(sctx sessionctx.Context, fuzzyDigest string, tableNames []*ast.TableName) (matchedBinding Binding, isMatched bool) {
// The current implementation is simplistic, but session binding is only for test purpose, so
// there shouldn't be many session bindings, and to keep it simple, this implementation is acceptable.
leastWildcards := len(tableNames) + 1
bindings := h.ch.GetAllBindings()
enableFuzzyBinding := sctx.GetSessionVars().EnableFuzzyBinding
for _, binding := range bindings {
bindingStmt, err := parser.New().ParseOneStmt(binding.BindSQL, binding.Charset, binding.Collation)
if err != nil {
return
}
_, bindingFuzzyDigest := norm.NormalizeStmtForBinding(bindingStmt, norm.WithFuzz(true))
if bindingFuzzyDigest != fuzzyDigest {
continue
}
bindingTableNames := CollectTableNames(bindingStmt)

numWildcards, matched := fuzzyMatchBindingTableName(sctx.GetSessionVars().CurrentDB, tableNames, bindingTableNames)
if matched && numWildcards > 0 && sctx != nil && !enableFuzzyBinding {
continue // fuzzy binding is disabled, skip this binding
}
if matched && numWildcards < leastWildcards {
matchedBinding = binding
isMatched = true
leastWildcards = numWildcards
}
}
return
return h.ch.FuzzyMatchingBinding(sctx, fuzzyDigest, tableNames)
}

// GetAllSessionBindings return all session bind info.
Expand Down

0 comments on commit 8c2167e

Please sign in to comment.