Repository: pingcap/tidb
File: pkg/executor/builder.go (Go)

Code before:

func (b *executorBuilder) buildInsert(v *plannercore.Insert) exec.Executor {
	b.inInsertStmt = true
	if b.err = b.updateForUpdateTS(); b.err != nil {
		return nil
	}

	selectExec := b.build(v.SelectPlan)
	if b.err != nil {
		return nil
	}
	var children []exec.Executor
	if selectExec != nil {
		children = append(children, selectExec)
	}
	baseExec := exec.NewBaseExecutor(b.ctx, nil, v.ID(), children...)
	baseExec.SetInitCap(chunk.ZeroCapacity)

	op, sourceStmt := "INSERT", tables.MLogSourceInsert
	if v.IsReplace {
		op, sourceStmt = "REPLACE", tables.MLogSourceReplace
	}
	sessionVars := b.ctx.GetSessionVars()
	tblInfo := v.Table.Meta()
	// Planner already rejects DML on MV/mlog tables; this catches bypass bugs.
	intest.AssertNoError(sessionVars.CheckMViewUpdatable(tblInfo, tblInfo.Name.O, op))
	insertTable := b.wrapTableWithMLogIfExists(v.Table, sourceStmt)
	if b.err != nil {
		return nil
	}

	ivs := &InsertValues{
		BaseExecutor:              baseExec,
		Table:                     insertTable,
		Columns:                   v.Columns,
		Lists:                     v.Lists,
		GenExprs:                  v.GenCols.Exprs,
		allAssignmentsAreConstant: v.AllAssignmentsAreConstant,
		hasRefCols:                v.NeedFillDefaultValue,
		SelectExec:                selectExec,
		rowLen:                    v.RowLen,
		ignoreErr:                 v.IgnoreErr,
	}
	err := ivs.initInsertColumns()
	if err != nil {
		b.err = err
		return nil
	}
	ivs.fkChecks, b.err = buildFKCheckExecs(b.ctx, ivs.Table, v.FKChecks)
	if b.err != nil {
		return nil

Review comment (suggestion):
This will call `CheckMViewUpdatable` even if it is not necessary in production release.

How about:
```
intest.AssertFunc(func() bool {
    intest.AssertNoErr(sessionVars.CheckMViewUpdatable(tblInfo, tblInfo.Name.O, op))
})
```

Code after:
	}
}

func (b *executorBuilder) buildInsert(v *plannercore.Insert) exec.Executor {
	b.inInsertStmt = true
	if b.err = b.updateForUpdateTS(); b.err != nil {
		return nil
	}

	selectExec := b.build(v.SelectPlan)
	if b.err != nil {
		return nil
	}
	var children []exec.Executor
	if selectExec != nil {
		children = append(children, selectExec)
	}
	baseExec := exec.NewBaseExecutor(b.ctx, nil, v.ID(), children...)
	baseExec.SetInitCap(chunk.ZeroCapacity)

	op, sourceStmt := "INSERT", tables.MLogSourceInsert
	if v.IsReplace {
		op, sourceStmt = "REPLACE", tables.MLogSourceReplace
	}
	tblInfo := v.Table.Meta()
	// Planner already rejects DML on MV/mlog tables; this catches bypass bugs.
	intest.AssertFunc(func() bool {
		sv := b.ctx.GetSessionVars()
		intest.AssertNoError(plannercore.CheckMViewUpdatable(sv, tblInfo, "", op))
		return true
	})
	insertTable := b.wrapTableWithMLogIfExists(v.Table, sourceStmt)
	if b.err != nil {
		return nil
	}

	ivs := &InsertValues{
		BaseExecutor:              baseExec,
		Table:                     insertTable,
		Columns:                   v.Columns,
		Lists:                     v.Lists,
		GenExprs:                  v.GenCols.Exprs,
		allAssignmentsAreConstant: v.AllAssignmentsAreConstant,
		hasRefCols:                v.NeedFillDefaultValue,
		SelectExec:                selectExec,
		rowLen:                    v.RowLen,
		ignoreErr:                 v.IgnoreErr,
	}
	err := ivs.initInsertColumns()
	if err != nil {
		b.err = err