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

dml: support default expression cache when insert #15216

Merged
merged 18 commits into from
Mar 19, 2020
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
27 changes: 27 additions & 0 deletions ddl/sequence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type testSequenceSuite struct{ *testDBSuite }
func (s *testSequenceSuite) TestCreateSequence(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use test")
s.tk.MustExec("drop sequence if exists seq")
s.tk.MustGetErrCode("create sequence `seq `", mysql.ErrWrongTableName)

// increment should not be set as 0.
Expand Down Expand Up @@ -744,3 +745,29 @@ func (s *testSequenceSuite) TestUnflodSequence(c *C) {
// `select nextval(seq), a from t1 union select nextval(seq), a from t2`
// The executing order of nextval and lastval is implicit, don't make any assumptions on it.
}

// before this PR:
// single insert consume: 50.498672ms
// after this PR:
// single insert consume: 33.213615ms
// Notice: use go test -check.b Benchmarkxxx to test it.
func (s *testSequenceSuite) BenchmarkInsertCacheDefaultExpr(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use test")
s.tk.MustExec("drop sequence if exists seq")
s.tk.MustExec("drop table if exists t")
s.tk.MustExec("create sequence seq")
s.tk.MustExec("create table t(a int default next value for seq)")
sql := "insert into t values "
for i := 0; i < 1000; i++ {
if i == 0 {
sql += "()"
} else {
sql += ",()"
}
}
c.ResetTimer()
for i := 0; i < c.N; i++ {
s.tk.MustExec(sql)
}
}
8 changes: 7 additions & 1 deletion executor/insert_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,13 @@ func (e *InsertValues) getColDefaultValue(idx int, col *table.Column) (d types.D
if !col.DefaultIsExpr && e.colDefaultVals != nil && e.colDefaultVals[idx].valid {
return e.colDefaultVals[idx].val, nil
}
defaultVal, err := table.GetColDefaultValue(e.ctx, col.ToInfo())

var defaultVal types.Datum
if col.DefaultIsExpr && col.DefaultExpr != nil {
defaultVal, err = table.EvalColDefaultExpr(e.ctx, col.ToInfo(), col.DefaultExpr)
} else {
defaultVal, err = table.GetColDefaultValue(e.ctx, col.ToInfo())
}
if err != nil {
return types.Datum{}, err
}
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,6 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/pty v1.1.5/go.mod h1:9r2w37qlBe7rQ6e1fg1S/9xpWHSnaqNdHD3WcMdbPDA=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lawyerphx/parser v0.0.0-20200316023126-7af13c9e0b85 h1:my5oTuBLzT1knvQnwaF7RXxNuzvzqhioNM8OcHYF2/A=
github.com/lawyerphx/parser v0.0.0-20200316023126-7af13c9e0b85/go.mod h1:9v0Edh8IbgjGYW2ArJr19E+bvL8zKahsFp+ixWeId+4=
github.com/leodido/go-urn v1.1.0/go.mod h1:+cyI34gQWZcE1eQU7NVgKkkzdXDQHr1dBMtdAPozLkw=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
Expand Down
10 changes: 9 additions & 1 deletion planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1817,7 +1817,15 @@ func collectVisitInfoFromGrantStmt(sctx sessionctx.Context, vi []visitInfo, stmt
}

func (b *PlanBuilder) getDefaultValue(col *table.Column) (*expression.Constant, error) {
value, err := table.GetColDefaultValue(b.ctx, col.ToInfo())
var (
value types.Datum
err error
)
if col.DefaultIsExpr && col.DefaultExpr != nil {
value, err = table.EvalColDefaultExpr(b.ctx, col.ToInfo(), col.DefaultExpr)
} else {
value, err = table.GetColDefaultValue(b.ctx, col.ToInfo())
}
if err != nil {
return nil, err
}
Expand Down
17 changes: 17 additions & 0 deletions table/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ type Column struct {
*model.ColumnInfo
// If this column is a generated column, the expression will be stored here.
GeneratedExpr ast.ExprNode
// If this column has default expr value, this expression will be stored here.
DefaultExpr ast.ExprNode
}

// String implements fmt.Stringer interface.
Expand Down Expand Up @@ -82,6 +84,7 @@ func ToColumn(col *model.ColumnInfo) *Column {
return &Column{
col,
nil,
nil,
}
}

Expand Down Expand Up @@ -379,6 +382,20 @@ func GetColDefaultValue(ctx sessionctx.Context, col *model.ColumnInfo) (types.Da
return getColDefaultExprValue(ctx, col, defaultValue.(string))
}

// EvalColDefaultExpr eval default expr node to explicit default value.
func EvalColDefaultExpr(ctx sessionctx.Context, col *model.ColumnInfo, defaultExpr ast.ExprNode) (types.Datum, error) {
d, err := expression.EvalAstExpr(ctx, defaultExpr)
if err != nil {
return types.Datum{}, err
}
// Check the evaluated data type by cast.
value, err := CastValue(ctx, d, col)
if err != nil {
return types.Datum{}, err
}
return value, nil
}

func getColDefaultExprValue(ctx sessionctx.Context, col *model.ColumnInfo, defaultValue string) (types.Datum, error) {
var defaultExpr ast.ExprNode
expr := fmt.Sprintf("select %s", defaultValue)
Expand Down
8 changes: 8 additions & 0 deletions table/tables/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ func TableFromMeta(allocs autoid.Allocators, tblInfo *model.TableInfo) (table.Ta
}
col.GeneratedExpr = expr
}
// default value is expr.
if col.DefaultIsExpr {
expr, err := parseExpression(colInfo.DefaultValue.(string))
if err != nil {
return nil, err
}
col.DefaultExpr = expr
}
columns = append(columns, col)
}

Expand Down