Skip to content

Handle insert...returning... queries #3012

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

Merged
merged 10 commits into from
Jun 6, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
20 changes: 20 additions & 0 deletions enginetest/queries/insert_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -2276,6 +2276,26 @@ var InsertScripts = []ScriptTest{
},
},
},
{
Name: "insert...returning... statements",
SetUpScript: []string{
"CREATE TABLE animals (id int, name varchar(20))",
},
Assertions: []ScriptTestAssertion{
{
Query: "insert into animals (id) values (2) returning id",
Expected: []sql.Row{{2}},
},
{
Query: "insert into animals(id,name) values (1, 'Dog'),(2,'Lion'),(3,'Tiger'),(4,'Leopard') returning id, id+id",
Expected: []sql.Row{{1, 2}, {2, 4}, {3, 6}, {4, 8}},
},
{
Query: "insert into animals set id=1,name='Bear' returning id,name",
Expected: []sql.Row{{1, "Bear"}},
},
},
},
}

var InsertDuplicateKeyKeyless = []ScriptTest{
Expand Down
5 changes: 4 additions & 1 deletion server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,10 @@ func (h *Handler) ComPrepare(ctx context.Context, c *mysql.Conn, query string, p
// than they will at execution time.
func nodeReturnsOkResultSchema(node sql.Node) bool {
switch node.(type) {
case *plan.InsertInto, *plan.Update, *plan.UpdateJoin, *plan.DeleteFrom:
case *plan.InsertInto:
insertNode, _ := node.(*plan.InsertInto)
return insertNode.Returning == nil
case *plan.Update, *plan.UpdateJoin, *plan.DeleteFrom:
return true
}
return false
Expand Down
2 changes: 1 addition & 1 deletion sql/plan/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type InsertInto struct {
LiteralValueSource bool

// Returning is a list of expressions to return after the insert operation. This feature is not supported
// in MySQL's syntax, but is exposed through PostgreSQL's syntax.
// in MySQL's syntax, but is exposed through PostgreSQL's and MariaDB's syntax.
Returning []sql.Expression

// FirstGenerateAutoIncRowIdx is the index of the first row inserted that increments last_insert_id.
Expand Down
Loading