Skip to content

Commit

Permalink
Bug fix for remaining failing test
Browse files Browse the repository at this point in the history
  • Loading branch information
zachmu committed Oct 5, 2023
1 parent 3ffc2ff commit 286fa79
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
11 changes: 8 additions & 3 deletions enginetest/queries/script_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"math"
"time"

"github.com/dolthub/go-mysql-server/enginetest/queries"
"github.com/dolthub/vitess/go/sqltypes"
querypb "github.com/dolthub/vitess/go/vt/proto/query"
"gopkg.in/src-d/go-errors.v1"
Expand Down Expand Up @@ -1327,19 +1328,23 @@ var ScriptTests = []ScriptTest{
},
{
Query: "select last_insert_id()",
Expected: []sql.Row{{1}},
Expected: []sql.Row{{uint64(1)}},
},
{
Query: "insert into a (x, y) values (1, 1) on duplicate key update y = 2, x=last_insert_id(x)",
Expected: []sql.Row{{types.OkResult{RowsAffected: 2, InsertID: 1}}},
},
{
Query: "select * from a order by x",
Expected: []sql.Row{{1,2}},
},
{
Query: "insert into a (y) values (100)",
Expected: []sql.Row{{types.OkResult{RowsAffected: 2, InsertID: 1}}},
Expected: []sql.Row{{types.OkResult{RowsAffected: 1, InsertID: 2}}},
},
{
Query: "select last_insert_id()",
Expected: []sql.Row{{1}},
Expected: []sql.Row{{uint64(2)}},
},
},
},
Expand Down
1 change: 1 addition & 0 deletions sql/rowexec/dml.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ func (b *BaseBuilder) buildRowUpdateAccumulator(ctx *sql.Context, n *plan.RowUpd
rowHandler = &insertRowHandler{
lastInsertIdGetter: insertItr.getAutoIncVal,
}
// TODO: some of these other row handlers also need to keep track of the last insert id
case plan.UpdateTypeReplace:
rowHandler = &replaceRowHandler{}
case plan.UpdateTypeDuplicateKeyUpdate:
Expand Down
8 changes: 8 additions & 0 deletions sql/rowexec/dml_iters.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,14 @@ func (a *accumulatorIter) Next(ctx *sql.Context) (r sql.Row, err error) {
}

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)
}

// By definition, ROW_COUNT() is equal to RowsAffected.
ctx.SetLastQueryInfo(sql.RowCount, int64(res.RowsAffected))
Expand Down

0 comments on commit 286fa79

Please sign in to comment.