Skip to content

Commit af9d166

Browse files
authored
Merge pull request #3017 from dolthub/angela/returning
Support `*` in `returning` clauses
2 parents 9ad3489 + ff52e52 commit af9d166

File tree

3 files changed

+9
-14
lines changed

3 files changed

+9
-14
lines changed

enginetest/queries/insert_queries.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2301,7 +2301,7 @@ var InsertScripts = []ScriptTest{
23012301
Expected: []sql.Row{{1, "Cat"}},
23022302
},
23032303
{
2304-
Query: "insert into auto_pk values (NULL, 'Dog'),(5, 'Fish'),(NULL, 'Horse') returning pk,name",
2304+
Query: "insert into auto_pk values (NULL, 'Dog'),(5, 'Fish'),(NULL, 'Horse') returning *",
23052305
Expected: []sql.Row{{2, "Dog"}, {5, "Fish"}, {6, "Horse"}},
23062306
},
23072307
},

sql/planbuilder/dml.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,10 @@ func (b *Builder) buildInsert(inScope *scope, i *ast.Insert) (outScope *scope) {
150150
ins := plan.NewInsertInto(db, plan.NewInsertDestination(sch, dest), srcScope.node, isReplace, columns, onDupExprs, ignore)
151151
ins.LiteralValueSource = srcLiteralOnly
152152

153-
if i.Returning != nil {
154-
returningExprs := make([]sql.Expression, len(i.Returning))
155-
for i, selectExpr := range i.Returning {
156-
returningExprs[i] = b.selectExprToExpression(destScope, selectExpr)
157-
}
158-
ins.Returning = returningExprs
153+
if len(i.Returning) > 0 {
154+
// TODO: read returning results from outScope instead of ins.Returning so that there is no need to return list
155+
// of expressions
156+
ins.Returning = b.analyzeSelectList(destScope, destScope, i.Returning)
159157
}
160158

161159
b.validateInsert(ins)
@@ -583,11 +581,7 @@ func (b *Builder) buildUpdate(inScope *scope, u *ast.Update) (outScope *scope) {
583581
}
584582

585583
if len(u.Returning) > 0 {
586-
returningExprs := make([]sql.Expression, len(u.Returning))
587-
for i, selectExpr := range u.Returning {
588-
returningExprs[i] = b.selectExprToExpression(outScope, selectExpr)
589-
}
590-
update.Returning = returningExprs
584+
update.Returning = b.analyzeSelectList(outScope, outScope, u.Returning)
591585
}
592586

593587
outScope.node = update.WithChecks(checks)

sql/planbuilder/project.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ func (b *Builder) analyzeProjectionList(inScope, outScope *scope, selectExprs as
2929
b.analyzeSelectList(inScope, outScope, selectExprs)
3030
}
3131

32-
func (b *Builder) analyzeSelectList(inScope, outScope *scope, selectExprs ast.SelectExprs) {
33-
// todo ideally we would not create new expressions here.
32+
func (b *Builder) analyzeSelectList(inScope, outScope *scope, selectExprs ast.SelectExprs) (expressions []sql.Expression) {
33+
// TODO: ideally we would not create new expressions here.
3434
// we want to in-place identify aggregations, expand stars.
3535
// use inScope to construct projections for projScope
3636

@@ -160,6 +160,7 @@ func (b *Builder) analyzeSelectList(inScope, outScope *scope, selectExprs ast.Se
160160
}
161161

162162
inScope.parent = tempScope.parent
163+
return exprs
163164
}
164165

165166
// selectExprToExpression binds dependencies in a scalar expression in a SELECT clause.

0 commit comments

Comments
 (0)