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

executor: refactor executor and baseExecutor #45065

Merged
merged 7 commits into from
Jul 3, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/design/2021-04-18-common-table-expression.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ type LogicalCTETable struct {

```Go
type CTEExec struct {
baseExecutor
exec.BaseExecutor

seedExec Executor
recursiveExec Executor
Expand All @@ -150,7 +150,7 @@ type CTEExec struct {

```Go
type CTETableReaderExec struct {
baseExecutor
exec.BaseExecutor

iterInTbl cteutil.Storage
chkIdx int
Expand Down
2 changes: 2 additions & 0 deletions executor/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ go_library(
"//executor/asyncloaddata",
"//executor/importer",
"//executor/internal/builder",
"//executor/internal/exec",
"//executor/internal/mpp",
"//executor/internal/util",
"//executor/metrics",
Expand Down Expand Up @@ -381,6 +382,7 @@ go_test(
"//executor/aggfuncs",
"//executor/importer",
"//executor/internal/builder",
"//executor/internal/exec",
"//expression",
"//expression/aggregation",
"//infoschema",
Expand Down
37 changes: 19 additions & 18 deletions executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/ddl/placement"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/executor/internal/exec"
executor_metrics "github.com/pingcap/tidb/executor/metrics"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/infoschema"
Expand Down Expand Up @@ -86,7 +87,7 @@ type processinfoSetter interface {
// recordSet wraps an executor, implements sqlexec.RecordSet interface
type recordSet struct {
fields []*ast.ResultField
executor Executor
executor exec.Executor
stmt *ExecStmt
lastErr error
txnStartTS uint64
Expand Down Expand Up @@ -172,8 +173,8 @@ func (a *recordSet) NewChunk(alloc chunk.Allocator) *chunk.Chunk {
return newFirstChunk(a.executor)
}

base := a.executor.base()
return alloc.Alloc(base.retFieldTypes, base.initCap, base.maxChunkSize)
base := a.executor.Base()
return alloc.Alloc(base.RetFieldTypes(), base.InitCap(), base.MaxChunkSize())
}

func (a *recordSet) Close() error {
Expand Down Expand Up @@ -601,7 +602,7 @@ func (a *ExecStmt) getSQLForProcessInfo() string {
return sql
}

func (a *ExecStmt) handleStmtForeignKeyTrigger(ctx context.Context, e Executor) error {
func (a *ExecStmt) handleStmtForeignKeyTrigger(ctx context.Context, e exec.Executor) error {
stmtCtx := a.Ctx.GetSessionVars().StmtCtx
if stmtCtx.ForeignKeyTriggerCtx.HasFKCascades {
// If the ExecStmt has foreign key cascade to be executed, we need call `StmtCommit` to commit the ExecStmt itself
Expand All @@ -626,7 +627,7 @@ func (a *ExecStmt) handleStmtForeignKeyTrigger(ctx context.Context, e Executor)

var maxForeignKeyCascadeDepth = 15

func (a *ExecStmt) handleForeignKeyTrigger(ctx context.Context, e Executor, depth int) error {
func (a *ExecStmt) handleForeignKeyTrigger(ctx context.Context, e exec.Executor, depth int) error {
exec, ok := e.(WithForeignKeyTrigger)
if !ok {
return nil
Expand Down Expand Up @@ -709,7 +710,7 @@ func (a *ExecStmt) handleForeignKeyCascade(ctx context.Context, fkc *FKCascadeEx

// prepareFKCascadeContext records a transaction savepoint for foreign key cascade when this ExecStmt has foreign key
// cascade behaviour and this ExecStmt is in transaction.
func (a *ExecStmt) prepareFKCascadeContext(e Executor) {
func (a *ExecStmt) prepareFKCascadeContext(e exec.Executor) {
exec, ok := e.(WithForeignKeyTrigger)
if !ok || !exec.HasFKCascades() {
return
Expand Down Expand Up @@ -753,7 +754,7 @@ func (a *ExecStmt) handleFKTriggerError(sc *stmtctx.StatementContext) error {
return nil
}

func (a *ExecStmt) handleNoDelay(ctx context.Context, e Executor, isPessimistic bool) (handled bool, rs sqlexec.RecordSet, err error) {
func (a *ExecStmt) handleNoDelay(ctx context.Context, e exec.Executor, isPessimistic bool) (handled bool, rs sqlexec.RecordSet, err error) {
sc := a.Ctx.GetSessionVars().StmtCtx
defer func() {
// If the stmt have no rs like `insert`, The session tracker detachment will be directly
Expand Down Expand Up @@ -832,7 +833,7 @@ type chunkRowRecordSet struct {
rows []chunk.Row
idx int
fields []*ast.ResultField
e Executor
e exec.Executor
execStmt *ExecStmt
}

Expand All @@ -858,15 +859,15 @@ func (c *chunkRowRecordSet) NewChunk(alloc chunk.Allocator) *chunk.Chunk {
return newFirstChunk(c.e)
}

base := c.e.base()
return alloc.Alloc(base.retFieldTypes, base.initCap, base.maxChunkSize)
base := c.e.Base()
return alloc.Alloc(base.RetFieldTypes(), base.InitCap(), base.MaxChunkSize())
}

func (c *chunkRowRecordSet) Close() error {
return c.execStmt.CloseRecordSet(c.execStmt.Ctx.GetSessionVars().TxnCtx.StartTS, nil)
}

func (a *ExecStmt) handlePessimisticSelectForUpdate(ctx context.Context, e Executor) (_ sqlexec.RecordSet, retErr error) {
func (a *ExecStmt) handlePessimisticSelectForUpdate(ctx context.Context, e exec.Executor) (_ sqlexec.RecordSet, retErr error) {
if snapshotTS := a.Ctx.GetSessionVars().SnapshotTS; snapshotTS != 0 {
terror.Log(e.Close())
return nil, errors.New("can not execute write statement when 'tidb_snapshot' is set")
Expand Down Expand Up @@ -910,7 +911,7 @@ func (a *ExecStmt) handlePessimisticSelectForUpdate(ctx context.Context, e Execu
}
}

func (a *ExecStmt) runPessimisticSelectForUpdate(ctx context.Context, e Executor) (sqlexec.RecordSet, error) {
func (a *ExecStmt) runPessimisticSelectForUpdate(ctx context.Context, e exec.Executor) (sqlexec.RecordSet, error) {
defer func() {
terror.Log(e.Close())
}()
Expand All @@ -935,7 +936,7 @@ func (a *ExecStmt) runPessimisticSelectForUpdate(ctx context.Context, e Executor
return nil, err
}

func (a *ExecStmt) handleNoDelayExecutor(ctx context.Context, e Executor) (sqlexec.RecordSet, error) {
func (a *ExecStmt) handleNoDelayExecutor(ctx context.Context, e exec.Executor) (sqlexec.RecordSet, error) {
sctx := a.Ctx
r, ctx := tracing.StartRegionEx(ctx, "executor.handleNoDelayExecutor")
defer r.End()
Expand Down Expand Up @@ -968,7 +969,7 @@ func (a *ExecStmt) handleNoDelayExecutor(ctx context.Context, e Executor) (sqlex
return nil, err
}

func (a *ExecStmt) handlePessimisticDML(ctx context.Context, e Executor) (err error) {
func (a *ExecStmt) handlePessimisticDML(ctx context.Context, e exec.Executor) (err error) {
sctx := a.Ctx
// Do not activate the transaction here.
// When autocommit = 0 and transaction in pessimistic mode,
Expand Down Expand Up @@ -1082,7 +1083,7 @@ func (a *ExecStmt) handlePessimisticDML(ctx context.Context, e Executor) (err er
}

// handlePessimisticLockError updates TS and rebuild executor if the err is write conflict.
func (a *ExecStmt) handlePessimisticLockError(ctx context.Context, lockErr error) (_ Executor, err error) {
func (a *ExecStmt) handlePessimisticLockError(ctx context.Context, lockErr error) (_ exec.Executor, err error) {
if lockErr == nil {
return nil, nil
}
Expand Down Expand Up @@ -1157,7 +1158,7 @@ type pessimisticTxn interface {
}

// buildExecutor build an executor from plan, prepared statement may need additional procedure.
func (a *ExecStmt) buildExecutor() (Executor, error) {
func (a *ExecStmt) buildExecutor() (exec.Executor, error) {
defer func(start time.Time) { a.phaseBuildDurations[0] += time.Since(start) }(time.Now())
ctx := a.Ctx
stmtCtx := ctx.GetSessionVars().StmtCtx
Expand Down Expand Up @@ -1201,7 +1202,7 @@ func (a *ExecStmt) buildExecutor() (Executor, error) {
return e, nil
}

func (a *ExecStmt) openExecutor(ctx context.Context, e Executor) (err error) {
func (a *ExecStmt) openExecutor(ctx context.Context, e exec.Executor) (err error) {
defer func() {
if r := recover(); r != nil {
err = errors.New(fmt.Sprint(r))
Expand All @@ -1213,7 +1214,7 @@ func (a *ExecStmt) openExecutor(ctx context.Context, e Executor) (err error) {
return err
}

func (a *ExecStmt) next(ctx context.Context, e Executor, req *chunk.Chunk) error {
func (a *ExecStmt) next(ctx context.Context, e exec.Executor, req *chunk.Chunk) error {
start := time.Now()
err := Next(ctx, e, req)
a.phaseNextDurations[0] += time.Since(start)
Expand Down