Skip to content

Commit

Permalink
add support for DEFAULT in insert statements for sequences to the que…
Browse files Browse the repository at this point in the history
…ry planner

This will treat DEFAULT as "null" for the purposes of sequences when
running insert statements. This matches with MySQL's documented
behavior and observed resulted.

See for more information:
https://dev.mysql.com/doc/refman/8.0/en/data-type-defaults.html

Signed-off-by: Mark Caudill <[email protected]>
  • Loading branch information
mc0 committed Oct 11, 2019
1 parent f645c11 commit 53949d3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 21 deletions.
36 changes: 15 additions & 21 deletions go/vt/vtgate/planbuilder/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,23 @@ func generateInsertShardedQuery(node *sqlparser.Insert, eins *engine.Insert, val

// modifyForAutoinc modfies the AST and the plan to generate
// necessary autoinc values. It must be called only if eins.Table.AutoIncrement
// is set.
// is set. Bind variable names are generated using baseName.
func modifyForAutoinc(ins *sqlparser.Insert, eins *engine.Insert) error {
pos := findOrAddColumn(ins, eins.Table.AutoIncrement.Column)
autoIncValues, err := swapBindVariables(ins.Rows.(sqlparser.Values), pos, ":"+engine.SeqVarName)
if err != nil {
return err
colNum := findOrAddColumn(ins, eins.Table.AutoIncrement.Column)
autoIncValues := sqltypes.PlanValue{}
for rowNum, row := range ins.Rows.(sqlparser.Values) {
// Support the DEFAULT keyword by treating it as null
if _, ok := row[colNum].(*sqlparser.Default); ok {
row[colNum] = &sqlparser.NullVal{}
}
pv, err := sqlparser.NewPlanValue(row[colNum])
if err != nil {
return fmt.Errorf("could not compute value for vindex or auto-inc column: %v", err)
}
autoIncValues.Values = append(autoIncValues.Values, pv)
row[colNum] = sqlparser.NewValArg([]byte(":" + engine.SeqVarName + strconv.Itoa(rowNum)))
}

eins.Generate = &engine.Generate{
Keyspace: eins.Table.AutoIncrement.Sequence.Keyspace,
Query: fmt.Sprintf("select next :n values from %s", sqlparser.String(eins.Table.AutoIncrement.Sequence.Name)),
Expand All @@ -226,22 +236,6 @@ func modifyForAutoinc(ins *sqlparser.Insert, eins *engine.Insert) error {
return nil
}

// swapBindVariables swaps in bind variable names at the specified
// column position in the AST values and returns the converted values back.
// Bind variable names are generated using baseName.
func swapBindVariables(rows sqlparser.Values, colNum int, baseName string) (sqltypes.PlanValue, error) {
pv := sqltypes.PlanValue{}
for rowNum, row := range rows {
innerpv, err := sqlparser.NewPlanValue(row[colNum])
if err != nil {
return pv, fmt.Errorf("could not compute value for vindex or auto-inc column: %v", err)
}
pv.Values = append(pv.Values, innerpv)
row[colNum] = sqlparser.NewValArg([]byte(baseName + strconv.Itoa(rowNum)))
}
return pv, nil
}

// findOrAddColumn finds the position of a column in the insert. If it's
// absent it appends it to the with NULL values and returns that position.
func findOrAddColumn(ins *sqlparser.Insert, col sqlparser.ColIdent) int {
Expand Down
26 changes: 26 additions & 0 deletions go/vt/vtgate/planbuilder/testdata/dml_cases.txt
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,32 @@
}
}

# insert with default seq
"insert into user(id, nonid) values (default, 2)"
{
"Original": "insert into user(id, nonid) values (default, 2)",
"Instructions": {
"Opcode": "InsertSharded",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"Query": "insert into user(id, nonid, Name, Costly) values (:_Id0, 2, :_Name0, :_Costly0)",
"Values": [[[":__seq0"]],[[null]],[[null]]],
"Table": "user",
"Generate": {
"Keyspace": {
"Name": "main",
"Sharded": false
},
"Query": "select next :n values from seq",
"Values": [null]
},
"Prefix": "insert into user(id, nonid, Name, Costly) values ",
"Mid": ["(:_Id0, 2, :_Name0, :_Costly0)"]
}
}

# insert with non vindex bool value
"insert into user(nonid) values (true)"
{
Expand Down

0 comments on commit 53949d3

Please sign in to comment.