Skip to content

Commit 896a112

Browse files
EliSauderCopilot
andcommitted
fix(core): stop pairing INSERT ... SELECT items with columns after a star
A placeholder selected by INSERT ... SELECT took the type of the column at its position, but a star before it selects any number of columns, so INSERT INTO t (id, name, at, n) SELECT t2.*, @n typed @n as name. Items after a star are no longer paired. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent fe55ab3 commit 896a112

3 files changed

Lines changed: 22 additions & 2 deletions

File tree

‎internal/core/analyzer/dml.go‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,16 @@ func (a *analyzer) bindInsertValues(n ast.Node, rel scopeRel, targets []core.Cla
146146
// the statement's result, but the query still holds placeholders.
147147
if sel.ValuesLists == nil {
148148
// A placeholder selected as it is takes the type of the column it
149-
// is inserted into, as one in VALUES does.
149+
// is inserted into, as one in VALUES does. A star selects any
150+
// number of columns, so the items after one are not paired.
150151
if sel.Op == ast.None {
151152
for i, item := range listItems(sel.TargetList) {
152153
rt, ok := item.(*ast.ResTarget)
153154
if !ok || i >= len(targets) {
154-
continue
155+
break
156+
}
157+
if cr, ok := rt.Val.(*ast.ColumnRef); ok && isStar(flattenFields(cr.Fields)) {
158+
break
155159
}
156160
if p, ok := rt.Val.(*ast.ParamRef); ok {
157161
a.inferParam(p, columnType(rel, targets[i]))

‎internal/endtoend/testdata/analyze_insert_select_params/mssql/query.sql‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ INSERT INTO t (id, name, at, n) SELECT @a, @b, @c, @d;
33

44
-- name: CopyRow :exec
55
INSERT INTO t (id, name, at, n) SELECT @a, name, at, n + @inc FROM t WHERE id = @src;
6+
7+
-- name: CopyWithStar :exec
8+
INSERT INTO t (id, name, at, n) SELECT t2.*, @n FROM (SELECT id, name, at FROM t) t2;

‎internal/endtoend/testdata/analyze_insert_select_params/mssql/stdout.json‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,18 @@
9393
}
9494
}
9595
]
96+
},
97+
{
98+
"name": "CopyWithStar",
99+
"cmd": ":exec",
100+
"columns": [],
101+
"params": [
102+
{
103+
"number": 1,
104+
"column": {
105+
"name": "n"
106+
}
107+
}
108+
]
96109
}
97110
]

0 commit comments

Comments
 (0)