Skip to content
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
24 changes: 11 additions & 13 deletions enginetest/queries/insert_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ var InsertQueries = []WriteQueryTest{
},
{
WriteQuery: "INSERT INTO auto_increment_tbl values (0, 44)",
ExpectedWriteResult: []sql.Row{{types.OkResult{RowsAffected: 1, InsertID: 4}}},
ExpectedWriteResult: []sql.Row{{types.OkResult{RowsAffected: 1, InsertID: 0}}},
SelectQuery: "SELECT * FROM auto_increment_tbl ORDER BY pk",
ExpectedSelect: []sql.Row{
{1, 11},
Expand All @@ -555,7 +555,7 @@ var InsertQueries = []WriteQueryTest{
},
{
WriteQuery: "INSERT INTO auto_increment_tbl values (5, 44)",
ExpectedWriteResult: []sql.Row{{types.OkResult{RowsAffected: 1, InsertID: 5}}},
ExpectedWriteResult: []sql.Row{{types.OkResult{RowsAffected: 1, InsertID: 0}}},
SelectQuery: "SELECT * FROM auto_increment_tbl ORDER BY pk",
ExpectedSelect: []sql.Row{
{1, 11},
Expand Down Expand Up @@ -610,11 +610,9 @@ var InsertQueries = []WriteQueryTest{
},
},
{
WriteQuery: `INSERT INTO auto_increment_tbl VALUES ('4', 44)`,
ExpectedWriteResult: []sql.Row{
{types.OkResult{InsertID: 4, RowsAffected: 1}},
},
SelectQuery: `SELECT * from auto_increment_tbl where pk=4`,
WriteQuery: `INSERT INTO auto_increment_tbl VALUES ('4', 44)`,
ExpectedWriteResult: []sql.Row{{types.NewOkResult(1)}},
SelectQuery: `SELECT * from auto_increment_tbl where pk=4`,
ExpectedSelect: []sql.Row{
{4, 44},
},
Expand Down Expand Up @@ -1282,10 +1280,10 @@ var InsertScripts = []ScriptTest{
{
Name: "sql_mode=NO_AUTO_VALUE_ON_ZERO",
SetUpScript: []string{
"set @old_sql_mode=@@sql_mode",
"set @@sql_mode='NO_AUTO_VALUE_ON_ZERO'",
"create table auto (i int auto_increment, index (i))",
"create table auto_pk (i int auto_increment primary key)",
"set @old_sql_mode=@@sql_mode;",
"set @@sql_mode='NO_AUTO_VALUE_ON_ZERO';",
"create table auto (i int auto_increment, index (i));",
"create table auto_pk (i int auto_increment primary key);",
},
Assertions: []ScriptTestAssertion{
{
Expand Down Expand Up @@ -1317,7 +1315,7 @@ var InsertScripts = []ScriptTest{
{
Query: "insert into auto values (1)",
Expected: []sql.Row{
{types.OkResult{RowsAffected: 1, InsertID: 1}},
{types.OkResult{RowsAffected: 1, InsertID: 0}},
},
},
{
Expand All @@ -1336,7 +1334,7 @@ var InsertScripts = []ScriptTest{
{
Query: "insert into auto_pk values (0), (1), (NULL), ()",
Expected: []sql.Row{
{types.NewOkResult(4)},
{types.OkResult{RowsAffected: 4, InsertID: 2}},
},
},
{
Expand Down
13 changes: 6 additions & 7 deletions enginetest/queries/script_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package queries

import (
"math"
"time"

"github.com/dolthub/vitess/go/sqltypes"
Expand Down Expand Up @@ -1899,7 +1898,7 @@ CREATE TABLE tab3 (
},
{
Query: "insert into a (x,y) values (1,1)",
Expected: []sql.Row{{types.OkResult{RowsAffected: 1, InsertID: 1}}},
Expected: []sql.Row{{types.OkResult{RowsAffected: 1, InsertID: 0}}},
},
{
Query: "select last_insert_id()",
Expand All @@ -1924,7 +1923,7 @@ CREATE TABLE tab3 (
},
{
Query: "insert into b (x) values (1), (2)",
Expected: []sql.Row{{types.NewOkResult(2)}},
Expected: []sql.Row{{types.OkResult{RowsAffected: 2, InsertID: 3}}},
},
{
// The above query doesn't have an auto increment column, so last_insert_id is unchanged
Expand All @@ -1935,7 +1934,7 @@ CREATE TABLE tab3 (
Query: "insert into a (x, y) values (-100, 10)",
Expected: []sql.Row{{types.OkResult{
RowsAffected: 1,
InsertID: uint64(math.MaxUint64 - uint(100-1)),
InsertID: 3,
}}},
},
{
Expand All @@ -1947,7 +1946,7 @@ CREATE TABLE tab3 (
Query: "insert into a (x, y) values (100, 10)",
Expected: []sql.Row{{types.OkResult{
RowsAffected: 1,
InsertID: 100,
InsertID: 3,
}}},
},
{
Expand Down Expand Up @@ -2040,7 +2039,7 @@ CREATE TABLE tab3 (

{
Query: "insert into t(pk) values (10), (default);",
Expected: []sql.Row{{types.OkResult{RowsAffected: 2, InsertID: 10}}},
Expected: []sql.Row{{types.OkResult{RowsAffected: 2, InsertID: 11}}},
},
{
Query: "select last_insert_id()",
Expand All @@ -2064,7 +2063,7 @@ CREATE TABLE tab3 (

{
Query: "insert into t(pk) values (20), (default), (default);",
Expected: []sql.Row{{types.OkResult{RowsAffected: 3, InsertID: 20}}},
Expected: []sql.Row{{types.OkResult{RowsAffected: 3, InsertID: 21}}},
},
{
Query: "select last_insert_id()",
Expand Down
4 changes: 2 additions & 2 deletions enginetest/queries/transaction_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ var TransactionTests = []TransactionTest{
// Client a does a skip ahead
{
Query: "/* client a */ insert into t values (10, 10)",
Expected: []sql.Row{{types.OkResult{RowsAffected: 1, InsertID: 10}}},
Expected: []sql.Row{{types.OkResult{RowsAffected: 1, InsertID: 12}}},
},
{
Query: "/* client b */ insert into t (y) values (11)",
Expand All @@ -846,7 +846,7 @@ var TransactionTests = []TransactionTest{
// Client c skips ahead
{
Query: "/* client c */ insert into t values (50, 50)",
Expected: []sql.Row{{types.OkResult{RowsAffected: 1, InsertID: 50}}},
Expected: []sql.Row{{types.OkResult{RowsAffected: 1, InsertID: 11}}},
},
{
Query: "/* client b */ insert into t (y) values (51)",
Expand Down
4 changes: 2 additions & 2 deletions enginetest/queries/trigger_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -2127,7 +2127,7 @@ end;`,
},
{
Query: "INSERT INTO `film` VALUES (3,'ADAPTATION HOLES','An Astounding Reflection in A Baloon Factory'),(4,'AFFAIR PREJUDICE','A Fanciful Documentary in A Shark Tank')",
Expected: []sql.Row{{types.OkResult{RowsAffected: 2, InsertID: 3}}},
Expected: []sql.Row{{types.OkResult{RowsAffected: 2, InsertID: 0}}},
},
{
Query: "SELECT COUNT(*) FROM film",
Expand Down Expand Up @@ -2188,7 +2188,7 @@ INSERT INTO t0 (v1, v2) VALUES (i, s); END;`,
{
SkipResultCheckOnServerEngine: true, // call depends on stored procedure stmt for whether to use 'query' or 'exec' from go sql driver.
Query: "CALL add_entry(4, 'aaa');",
Expected: []sql.Row{{types.OkResult{RowsAffected: 1, InsertID: 3}}},
Expected: []sql.Row{{types.OkResult{RowsAffected: 1, InsertID: 1}}},
},
{
Query: "SELECT * FROM t0;",
Expand Down
10 changes: 1 addition & 9 deletions sql/rowexec/dml.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,15 +328,7 @@ func (b *BaseBuilder) buildRowUpdateAccumulator(ctx *sql.Context, n *plan.RowUpd
var rowHandler accumulatorRowHandler
switch n.RowUpdateType {
case plan.UpdateTypeInsert:
insertItr, err := findInsertIter(rowIter)
if err != nil {
return nil, err
}

rowHandler = &insertRowHandler{
lastInsertIdGetter: insertItr.getAutoIncVal,
}
// TODO: some of these other row handlers also need to keep track of the last insert id
rowHandler = &insertRowHandler{}
case plan.UpdateTypeReplace:
rowHandler = &replaceRowHandler{}
case plan.UpdateTypeDuplicateKeyUpdate:
Expand Down
20 changes: 3 additions & 17 deletions sql/rowexec/dml_iters.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,18 +295,13 @@ type insertRowHandler struct {
}

func (i *insertRowHandler) handleRowUpdate(row sql.Row) error {
if !i.updatedAutoIncrementValue {
i.updatedAutoIncrementValue = true
i.lastInsertId = uint64(i.lastInsertIdGetter(row))
}
i.rowsAffected++
return nil
}

func (i *insertRowHandler) okResult() types.OkResult {
return types.OkResult{
RowsAffected: uint64(i.rowsAffected),
InsertID: i.lastInsertId,
}
}

Expand Down Expand Up @@ -498,11 +493,6 @@ func (a *accumulatorIter) Next(ctx *sql.Context) (r sql.Row, err error) {
return nil, io.EOF
}

oldLastInsertId := ctx.Session.GetLastQueryInfoInt(sql.LastInsertId)
if oldLastInsertId != 0 {
ctx.Session.SetLastQueryInfoInt(sql.LastInsertId, -1)
}

// We close our child iterator before returning any results. In
// particular, the LOAD DATA source iterator needs to be closed before
// results are returned.
Expand Down Expand Up @@ -533,19 +523,15 @@ func (a *accumulatorIter) Next(ctx *sql.Context) (r sql.Row, err error) {
ctx.SetLastQueryInfoInt(sql.FoundRows, ma.RowsMatched())
}

newLastInsertId := ctx.Session.GetLastQueryInfoInt(sql.LastInsertId)
if newLastInsertId == -1 {
ctx.Session.SetLastQueryInfoInt(sql.LastInsertId, oldLastInsertId)
}

res := a.updateRowHandler.okResult() // TODO: Should add warnings here

// For some update accumulators, we don't accurately track the last insert ID in the handler and need to set
// it manually in the result by getting it from the session. This doesn't work correctly in all cases and needs
// to be fixed. See comment in buildRowUpdateAccumulator in rowexec/dml.go
switch a.updateRowHandler.(type) {
case *onDuplicateUpdateHandler, *replaceRowHandler:
res.InsertID = uint64(newLastInsertId)
case *onDuplicateUpdateHandler, *replaceRowHandler, *insertRowHandler:
lastInsertId := ctx.Session.GetLastQueryInfoInt(sql.LastInsertId)
res.InsertID = uint64(lastInsertId)
}

// By definition, ROW_COUNT() is equal to RowsAffected.
Expand Down