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

planner: simplify the session binding handler code #51442

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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