Skip to content

Commit

Permalink
txn: support multi-table join in nt-dml (#39139)
Browse files Browse the repository at this point in the history
ref #33485
  • Loading branch information
ekexium authored Nov 24, 2022
1 parent 7930c2c commit 84fa999
Show file tree
Hide file tree
Showing 5 changed files with 285 additions and 88 deletions.
3 changes: 3 additions & 0 deletions metrics/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,13 +332,15 @@ func GetTablePartitionCounter() TablePartitionUsageCounter {
// NonTransactionalStmtCounter records the usages of non-transactional statements.
type NonTransactionalStmtCounter struct {
DeleteCount int64 `json:"delete"`
UpdateCount int64 `json:"update"`
InsertCount int64 `json:"insert"`
}

// Sub returns the difference of two counters.
func (n NonTransactionalStmtCounter) Sub(rhs NonTransactionalStmtCounter) NonTransactionalStmtCounter {
return NonTransactionalStmtCounter{
DeleteCount: n.DeleteCount - rhs.DeleteCount,
UpdateCount: n.UpdateCount - rhs.UpdateCount,
InsertCount: n.InsertCount - rhs.InsertCount,
}
}
Expand All @@ -347,6 +349,7 @@ func (n NonTransactionalStmtCounter) Sub(rhs NonTransactionalStmtCounter) NonTra
func GetNonTransactionalStmtCounter() NonTransactionalStmtCounter {
return NonTransactionalStmtCounter{
DeleteCount: readCounter(NonTransactionalDMLCount.With(prometheus.Labels{LblType: "delete"})),
UpdateCount: readCounter(NonTransactionalDMLCount.With(prometheus.Labels{LblType: "update"})),
InsertCount: readCounter(NonTransactionalDMLCount.With(prometheus.Labels{LblType: "insert"})),
}
}
Expand Down
25 changes: 11 additions & 14 deletions parser/ast/dml.go
Original file line number Diff line number Diff line change
Expand Up @@ -2221,17 +2221,16 @@ func (n *InsertStmt) SetWhereExpr(e ExprNode) {
s.Where = e
}

// TableSource implements ShardableDMLStmt interface.
func (n *InsertStmt) TableSource() (*TableSource, bool) {
// TableRefsJoin implements ShardableDMLStmt interface.
func (n *InsertStmt) TableRefsJoin() (*Join, bool) {
if n.Select == nil {
return nil, false
}
s, ok := n.Select.(*SelectStmt)
if !ok {
return nil, false
}
table, ok := s.From.TableRefs.Left.(*TableSource)
return table, ok
return s.From.TableRefs, true
}

// DeleteStmt is a statement to delete rows from table.
Expand Down Expand Up @@ -2410,10 +2409,9 @@ func (n *DeleteStmt) SetWhereExpr(e ExprNode) {
n.Where = e
}

// TableSource implements ShardableDMLStmt interface.
func (n *DeleteStmt) TableSource() (*TableSource, bool) {
table, ok := n.TableRefs.TableRefs.Left.(*TableSource)
return table, ok
// TableRefsJoin implements ShardableDMLStmt interface.
func (n *DeleteStmt) TableRefsJoin() (*Join, bool) {
return n.TableRefs.TableRefs, true
}

const (
Expand All @@ -2426,8 +2424,8 @@ type ShardableDMLStmt = interface {
StmtNode
WhereExpr() ExprNode
SetWhereExpr(ExprNode)
// TableSource returns the *only* target table source in the statement.
TableSource() (table *TableSource, ok bool)
// TableRefsJoin returns the table refs in the statement.
TableRefsJoin() (refs *Join, ok bool)
}

var _ ShardableDMLStmt = &DeleteStmt{}
Expand Down Expand Up @@ -2649,10 +2647,9 @@ func (n *UpdateStmt) SetWhereExpr(e ExprNode) {
n.Where = e
}

// TableSource implements ShardableDMLStmt interface.
func (n *UpdateStmt) TableSource() (*TableSource, bool) {
table, ok := n.TableRefs.TableRefs.Left.(*TableSource)
return table, ok
// TableRefsJoin implements ShardableDMLStmt interface.
func (n *UpdateStmt) TableRefsJoin() (*Join, bool) {
return n.TableRefs.TableRefs, true
}

// Limit is the limit clause.
Expand Down
Loading

0 comments on commit 84fa999

Please sign in to comment.