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

Insert VALUES statements with subqueries index correctly #2263

Merged
merged 2 commits into from
Jan 16, 2024
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
23 changes: 23 additions & 0 deletions enginetest/queries/insert_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,29 @@ var SpatialInsertQueries = []WriteQueryTest{
}

var InsertScripts = []ScriptTest{
{
// https://github.com/dolthub/dolt/issues/7322
Name: "issue 7322: values expression is subquery",
SetUpScript: []string{
"create table xy (x int auto_increment primary key, y varchar(50) not null)",
"create table uv (u int auto_increment primary key, v varchar(50) not null, x_id int, constraint u_x_fk foreign key (x_id) references xy (x))",
"insert into xy values (1,'admin'), (2, 'standard')",
},
Assertions: []ScriptTestAssertion{
{
Query: "INSERT INTO uv(v, x_id) VALUES ('test', (SELECT x FROM xy WHERE y = 'admin'));",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to have a test of an error case here too -- what if there is more than one result from the subquery?

Expected: []sql.Row{{types.OkResult{RowsAffected: 1, InsertID: 1}}},
},
{
Query: "INSERT INTO uv(v, x_id) VALUES ('test', (SELECT x FROM xy WHERE x > 0));",
ExpectedErr: sql.ErrExpectedSingleRow,
},
{
Query: "select * from uv",
Expected: []sql.Row{{1, "test", 1}},
},
},
},
{
// https://github.com/dolthub/dolt/issues/6675
Name: "issue 6675: on duplicate rearranged getfield indexes from select source",
Expand Down
6 changes: 6 additions & 0 deletions sql/analyzer/inserts.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ func resolveInsertRows(ctx *sql.Context, a *Analyzer, n sql.Node, scope *plan.Sc
// TriggerExecutor has already been analyzed
if _, ok := insert.Source.(*plan.TriggerExecutor); !ok {
// Analyze the source of the insert independently
if _, ok := insert.Source.(*plan.Values); ok {
scope = scope.NewScope(plan.NewProject(
expression.SchemaToGetFields(insert.Source.Schema()[:len(insert.ColumnNames)]),
plan.NewSubqueryAlias("dummy", "", insert.Source),
))
}
source, _, err = a.analyzeWithSelector(ctx, insert.Source, scope, SelectAllBatches, newInsertSourceSelector(sel))
if err != nil {
return nil, transform.SameTree, err
Expand Down