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

*: support drop session binding #10287

Merged
Merged
Show file tree
Hide file tree
Changes from 6 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
6 changes: 6 additions & 0 deletions bindinfo/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,10 @@ func (s *testSuite) TestSessionBinding(c *C) {
c.Check(row.GetString(6), NotNil)
c.Check(row.GetString(7), NotNil)

_, err = tk.Exec("drop session binding for select * from t where i>99")
c.Assert(err, IsNil)
bindData = handle.GetBindRecord("select * from t where i > ?", "test")
c.Check(bindData, NotNil)
c.Check(bindData.OriginalSQL, Equals, "select * from t where i > ?")
c.Check(bindData.Status, Equals, "deleted")
}
30 changes: 17 additions & 13 deletions bindinfo/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func (h *BindHandle) DropBindRecord(record *BindRecord) (err error) {
return
}

hash, meta := h.newBindMetaWithoutAst(record)
hash, meta := newBindMetaWithoutAst(record)
h.removeBindMeta(hash, meta)
}()

Expand Down Expand Up @@ -226,18 +226,9 @@ func (h *BindHandle) Size() int {
return size
}

// GetBindRecord return the BindMeta of the (normdOrigSQL,db) if BindMeta exist.
// GetBindRecord return the bindMeta of the (normdOrigSQL,db) if bindMeta exist.
func (h *BindHandle) GetBindRecord(normdOrigSQL, db string) *BindMeta {
hash := parser.DigestHash(normdOrigSQL)
bindRecords := h.bindInfo.Load().(cache)[hash]
if bindRecords != nil {
for _, bindRecord := range bindRecords {
if bindRecord.OriginalSQL == normdOrigSQL && bindRecord.Db == db {
return bindRecord
}
}
}
return nil
return h.bindInfo.Load().(cache).getBindRecord(normdOrigSQL, db)
}

// GetAllBindRecord return all bind record in cache.
Expand All @@ -259,7 +250,7 @@ func (h *BindHandle) newBindMeta(record *BindRecord) (hash string, meta *BindMet
return hash, meta, nil
}

func (h *BindHandle) newBindMetaWithoutAst(record *BindRecord) (hash string, meta *BindMeta) {
func newBindMetaWithoutAst(record *BindRecord) (hash string, meta *BindMeta) {
hash = parser.DigestHash(record.OriginalSQL)
meta = &BindMeta{BindRecord: record}
return hash, meta
Expand Down Expand Up @@ -337,6 +328,19 @@ func (c cache) copy() cache {
return newCache
}

func (c cache) getBindRecord(normdOrigSQL, db string) *BindMeta {
hash := parser.DigestHash(normdOrigSQL)
bindRecords := c[hash]
if bindRecords != nil {
for _, bindRecord := range bindRecords {
if bindRecord.OriginalSQL == normdOrigSQL && bindRecord.Db == db {
return bindRecord
}
}
}
return nil
}

// isStale checks whether this BindMeta is stale compared with the other BindMeta.
func (m *BindMeta) isStale(other *BindMeta) bool {
return m.OriginalSQL == other.OriginalSQL && m.Db == other.Db &&
Expand Down
9 changes: 9 additions & 0 deletions bindinfo/session_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ func (h *SessionHandle) AddBindRecord(record *BindRecord) error {
return err
}

// DropBindRecord drops a BindRecord in the cache.
func (h *SessionHandle) DropBindRecord(record *BindRecord) {
meta := &BindMeta{BindRecord: record}
meta.Status = deleted
hash := parser.DigestHash(record.OriginalSQL)
h.ch.removeDeletedBindMeta(hash, meta)
alivxxx marked this conversation as resolved.
Show resolved Hide resolved
h.appendBindMeta(hash, meta)
}

// GetBindRecord return the BindMeta of the (normdOrigSQL,db) if BindMeta exist.
func (h *SessionHandle) GetBindRecord(normdOrigSQL, db string) *BindMeta {
hash := parser.DigestHash(normdOrigSQL)
Expand Down
9 changes: 5 additions & 4 deletions executor/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,15 @@ func (e *SQLBindExec) Next(ctx context.Context, req *chunk.RecordBatch) error {
}

func (e *SQLBindExec) dropSQLBind() error {
if !e.isGlobal {
return errors.New("drop non-global sql bind is not supported")
}

record := &bindinfo.BindRecord{
OriginalSQL: e.normdOrigSQL,
Db: e.ctx.GetSessionVars().CurrentDB,
}
if !e.isGlobal {
handle := e.ctx.Value(bindinfo.SessionBindInfoKeyType).(*bindinfo.SessionHandle)
handle.DropBindRecord(record)
return nil
}
return domain.GetDomain(e.ctx).BindHandle().DropBindRecord(record)
}

Expand Down