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

Bug fix: record last_insert_id when 0 is specified for the auto_increment column value #2822

Merged
merged 1 commit into from
Jan 17, 2025
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
4 changes: 3 additions & 1 deletion enginetest/queries/insert_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,8 +556,10 @@ var InsertQueries = []WriteQueryTest{
Dialect: "mysql",
},
{
// When 0 is specified for the auto_increment column (and SQL_MODE does not include
// NO_AUTO_VALUE_ON_ZERO), then an auto_increment value will be filled in.
WriteQuery: "INSERT INTO auto_increment_tbl values (0, 44)",
ExpectedWriteResult: []sql.Row{{types.OkResult{RowsAffected: 1, InsertID: 0}}},
ExpectedWriteResult: []sql.Row{{types.OkResult{RowsAffected: 1, InsertID: 4}}},
SelectQuery: "SELECT * FROM auto_increment_tbl ORDER BY pk",
ExpectedSelect: []sql.Row{
{1, 11},
Expand Down
20 changes: 19 additions & 1 deletion sql/analyzer/inserts.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,10 @@ func wrapRowSource(ctx *sql.Context, insertSource sql.Node, destTbl sql.Table, s
break
}
if lit, isLit := expr.(*expression.Literal); isLit {
if types.Null.Equals(lit.Type()) {
// If a literal NULL or if 0 is specified and the NO_AUTO_VALUE_ON_ZERO SQL mode is
// not active, then MySQL will fill in an auto_increment value.
if types.Null.Equals(lit.Type()) ||
(!sql.LoadSqlMode(ctx).ModeEnabled(sql.NoAutoValueOnZero) && isZero(lit)) {
firstGeneratedAutoIncRowIdx = ii
break
}
Expand Down Expand Up @@ -222,6 +225,21 @@ func wrapRowSource(ctx *sql.Context, insertSource sql.Node, destTbl sql.Table, s
return plan.NewProject(projExprs, insertSource), firstGeneratedAutoIncRowIdx, nil
}

// isZero returns true if the specified literal value |lit| has a value equal to 0.
func isZero(lit *expression.Literal) bool {
if !types.IsNumber(lit.Type()) {
return false
}

convert, inRange, err := types.Int8.Convert(lit.Value())
if err != nil {
// Ignore any conversion errors, since that means the value isn't 0
// and the values are validated in other parts of the analyzer anyway.
return false
}
return bool(inRange) && convert == int8(0)
}

// insertAutoUuidExpression transforms the specified |expr| for |autoUuidCol| and inserts an AutoUuid
// expression above the UUID() function call, so that the auto generated UUID value can be captured and
// saved to the session's query info.
Expand Down
Loading