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 10 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
25 changes: 25 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,27 @@ 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
func (s *testSequenceSuite) TestBenchInsertCacheDefaultExpr(c *C) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test is quite slow, it will cost more than 1 minute. Maybe we only need to run it in bench test instead of unit test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sense, thank u every much, addressed.

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 += ",()"
}
}
for i := 0; i < 100; i++ {
s.tk.MustExec(sql)
}
}
35 changes: 29 additions & 6 deletions executor/insert_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ type InsertValues struct {
}

type defaultVal struct {
// common default value is a constant.
val types.Datum
// default expr(nextval(seq)) should be evaluated row by row.
expr ast.ExprNode
// valid indicates whether the val is evaluated. We evaluate the default value lazily.
valid bool
}
Expand Down Expand Up @@ -508,16 +511,36 @@ func (e *InsertValues) getRowInPlace(ctx context.Context, vals []types.Datum, ro

// getColDefaultValue gets the column default value.
func (e *InsertValues) getColDefaultValue(idx int, col *table.Column) (d types.Datum, err error) {
if !col.DefaultIsExpr && e.colDefaultVals != nil && e.colDefaultVals[idx].valid {
return e.colDefaultVals[idx].val, nil
if e.colDefaultVals != nil && e.colDefaultVals[idx].valid {
if !col.DefaultIsExpr {
return e.colDefaultVals[idx].val, nil
}
return table.EvalColDefaultExpr(e.ctx, col.ToInfo(), e.colDefaultVals[idx].expr)
}
var (
defaultVal types.Datum
defaultExpr ast.ExprNode
)
if !col.DefaultIsExpr {
defaultVal, err = table.GetColDefaultValue(e.ctx, col.ToInfo())
} else {
defaultExpr, err = table.GetColDefaultExpr(col.ToInfo())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about cache the default expression in TableCommon?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's practicable, but i think it's ok here

if err != nil {
return types.Datum{}, err
}
defaultVal, err = table.EvalColDefaultExpr(e.ctx, col.ToInfo(), defaultExpr)
}
defaultVal, err := table.GetColDefaultValue(e.ctx, col.ToInfo())
if err != nil {
return types.Datum{}, err
}
if initialized := e.lazilyInitColDefaultValBuf(); initialized && !col.DefaultIsExpr {
e.colDefaultVals[idx].val = defaultVal
e.colDefaultVals[idx].valid = true
if initialized := e.lazilyInitColDefaultValBuf(); initialized {
if !col.DefaultIsExpr {
e.colDefaultVals[idx].val = defaultVal
e.colDefaultVals[idx].valid = true
} else {
e.colDefaultVals[idx].expr = defaultExpr
e.colDefaultVals[idx].valid = true
}
}

return defaultVal, nil
Expand Down
26 changes: 26 additions & 0 deletions table/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,32 @@ func GetColDefaultValue(ctx sessionctx.Context, col *model.ColumnInfo) (types.Da
return getColDefaultExprValue(ctx, col, defaultValue.(string))
}

// GetColDefaultExpr gets default expr node of the column with expr default value.
func GetColDefaultExpr(col *model.ColumnInfo) (ast.ExprNode, error) {
var defaultExpr ast.ExprNode
expr := fmt.Sprintf("select %s", col.GetDefaultValue().(string))
stmts, _, err := parser.New().Parse(expr, "", "")
if err != nil {
return nil, err
}
defaultExpr = stmts[0].(*ast.SelectStmt).Fields.Fields[0].Expr
return defaultExpr, nil
}

// 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