-
Notifications
You must be signed in to change notification settings - Fork 1
/
upsert.go
256 lines (228 loc) · 8.06 KB
/
upsert.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Copyright 2016 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package sql
import (
"bytes"
"fmt"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/util"
)
// upsertExcludedTable is the name of a synthetic table used in an upsert's set
// expressions to refer to the values that would be inserted for a row if it
// didn't conflict.
// Example: `INSERT INTO kv VALUES (1, 2) ON CONFLICT (k) DO UPDATE SET v = excluded.v`
var upsertExcludedTable = parser.TableName{TableName: "excluded"}
type upsertHelper struct {
p *planner
evalExprs []parser.TypedExpr
whereExpr parser.TypedExpr
sourceInfo *dataSourceInfo
excludedSourceInfo *dataSourceInfo
curSourceRow parser.Datums
curExcludedRow parser.Datums
// This struct must be allocated on the heap and its location stay
// stable after construction because it implements
// IndexedVarContainer and the IndexedVar objects in sub-expressions
// will link to it by reference after checkRenderStar / analyzeExpr.
// Enforce this using NoCopy.
noCopy util.NoCopy
}
var _ tableUpsertEvaler = (*upsertHelper)(nil)
// IndexedVarEval implements the parser.IndexedVarContainer interface.
func (uh *upsertHelper) IndexedVarEval(idx int, ctx *parser.EvalContext) (parser.Datum, error) {
numSourceColumns := len(uh.sourceInfo.sourceColumns)
if idx >= numSourceColumns {
return uh.curExcludedRow[idx-numSourceColumns].Eval(ctx)
}
return uh.curSourceRow[idx].Eval(ctx)
}
// IndexedVarResolvedType implements the parser.IndexedVarContainer interface.
func (uh *upsertHelper) IndexedVarResolvedType(idx int) parser.Type {
numSourceColumns := len(uh.sourceInfo.sourceColumns)
if idx >= numSourceColumns {
return uh.excludedSourceInfo.sourceColumns[idx-numSourceColumns].Typ
}
return uh.sourceInfo.sourceColumns[idx].Typ
}
// IndexedVarFormat implements the parser.IndexedVarContainer interface.
func (uh *upsertHelper) IndexedVarFormat(buf *bytes.Buffer, f parser.FmtFlags, idx int) {
numSourceColumns := len(uh.sourceInfo.sourceColumns)
if idx >= numSourceColumns {
uh.excludedSourceInfo.FormatVar(buf, f, idx-numSourceColumns)
} else {
uh.sourceInfo.FormatVar(buf, f, idx)
}
}
func (p *planner) makeUpsertHelper(
ctx context.Context,
tn *parser.TableName,
tableDesc *sqlbase.TableDescriptor,
insertCols []sqlbase.ColumnDescriptor,
updateCols []sqlbase.ColumnDescriptor,
updateExprs parser.UpdateExprs,
upsertConflictIndex *sqlbase.IndexDescriptor,
whereClause *parser.Where,
) (*upsertHelper, error) {
defaultExprs, err := sqlbase.MakeDefaultExprs(updateCols, &p.parser, &p.evalCtx)
if err != nil {
return nil, err
}
untupledExprs := make(parser.Exprs, 0, len(updateExprs))
i := 0
for _, updateExpr := range updateExprs {
if updateExpr.Tuple {
if t, ok := updateExpr.Expr.(*parser.Tuple); ok {
for _, e := range t.Exprs {
e = fillDefault(e, i, defaultExprs)
untupledExprs = append(untupledExprs, e)
i++
}
}
} else {
e := fillDefault(updateExpr.Expr, i, defaultExprs)
untupledExprs = append(untupledExprs, e)
i++
}
}
sourceInfo := newSourceInfoForSingleTable(
*tn, sqlbase.ResultColumnsFromColDescs(tableDesc.Columns),
)
excludedSourceInfo := newSourceInfoForSingleTable(
upsertExcludedTable, sqlbase.ResultColumnsFromColDescs(insertCols),
)
helper := &upsertHelper{
p: p,
sourceInfo: sourceInfo,
excludedSourceInfo: excludedSourceInfo,
}
var evalExprs []parser.TypedExpr
ivarHelper := parser.MakeIndexedVarHelper(helper, len(sourceInfo.sourceColumns)+len(excludedSourceInfo.sourceColumns))
sources := multiSourceInfo{sourceInfo, excludedSourceInfo}
for i, expr := range untupledExprs {
typ := updateCols[i].Type.ToDatumType()
normExpr, err := p.analyzeExpr(ctx, expr, sources, ivarHelper, typ, true, "ON CONFLICT")
if err != nil {
return nil, err
}
evalExprs = append(evalExprs, normExpr)
}
helper.evalExprs = evalExprs
if whereClause != nil {
whereExpr, err := p.analyzeExpr(
ctx, whereClause.Expr, sources, ivarHelper, parser.TypeBool, true /* requireType */, "WHERE",
)
if err != nil {
return nil, err
}
// Make sure there are no aggregation/window functions in the filter
// (after subqueries have been expanded).
if err := p.parser.AssertNoAggregationOrWindowing(
whereExpr, "WHERE", p.session.SearchPath,
); err != nil {
return nil, err
}
helper.whereExpr = whereExpr
}
return helper, nil
}
func (uh *upsertHelper) walkExprs(walk func(desc string, index int, expr parser.TypedExpr)) {
for i, evalExpr := range uh.evalExprs {
walk("eval", i, evalExpr)
}
}
// eval returns the values for the update case of an upsert, given the row
// that would have been inserted and the existing (conflicting) values.
func (uh *upsertHelper) eval(
insertRow parser.Datums, existingRow parser.Datums,
) (parser.Datums, error) {
uh.curSourceRow = existingRow
uh.curExcludedRow = insertRow
var err error
ret := make([]parser.Datum, len(uh.evalExprs))
for i, evalExpr := range uh.evalExprs {
ret[i], err = evalExpr.Eval(&uh.p.evalCtx)
if err != nil {
return nil, err
}
}
return ret, nil
}
// shouldUpdate returns the result of evaluating the WHERE clause of the
// ON CONFLICT ... DO UPDATE clause.
func (uh *upsertHelper) shouldUpdate(
insertRow parser.Datums, existingRow parser.Datums,
) (bool, error) {
uh.curSourceRow = existingRow
uh.curExcludedRow = insertRow
return sqlbase.RunFilter(uh.whereExpr, &uh.p.evalCtx)
}
// upsertExprsAndIndex returns the upsert conflict index and the (possibly
// synthetic) SET expressions used when a row conflicts.
func upsertExprsAndIndex(
tableDesc *sqlbase.TableDescriptor,
onConflict parser.OnConflict,
insertCols []sqlbase.ColumnDescriptor,
) (parser.UpdateExprs, *sqlbase.IndexDescriptor, error) {
if onConflict.IsUpsertAlias() {
// The UPSERT syntactic sugar is the same as the longhand specifying the
// primary index as the conflict index and SET expressions for the columns
// in insertCols minus any columns in the conflict index. Example:
// `UPSERT INTO abc VALUES (1, 2, 3)` is syntactic sugar for
// `INSERT INTO abc VALUES (1, 2, 3) ON CONFLICT a DO UPDATE SET b = 2, c = 3`.
conflictIndex := &tableDesc.PrimaryIndex
indexColSet := make(map[sqlbase.ColumnID]struct{}, len(conflictIndex.ColumnIDs))
for _, colID := range conflictIndex.ColumnIDs {
indexColSet[colID] = struct{}{}
}
updateExprs := make(parser.UpdateExprs, 0, len(insertCols))
for _, c := range insertCols {
if _, ok := indexColSet[c.ID]; !ok {
names := parser.UnresolvedNames{
parser.UnresolvedName{parser.Name(c.Name)},
}
expr := &parser.ColumnItem{
TableName: upsertExcludedTable,
ColumnName: parser.Name(c.Name),
}
updateExprs = append(updateExprs, &parser.UpdateExpr{Names: names, Expr: expr})
}
}
return updateExprs, conflictIndex, nil
}
indexMatch := func(index sqlbase.IndexDescriptor) bool {
if !index.Unique {
return false
}
if len(index.ColumnNames) != len(onConflict.Columns) {
return false
}
for i, colName := range index.ColumnNames {
if colName != string(onConflict.Columns[i]) {
return false
}
}
return true
}
if indexMatch(tableDesc.PrimaryIndex) {
return onConflict.Exprs, &tableDesc.PrimaryIndex, nil
}
for _, index := range tableDesc.Indexes {
if indexMatch(index) {
return onConflict.Exprs, &index, nil
}
}
return nil, nil, fmt.Errorf("there is no unique or exclusion constraint matching the ON CONFLICT specification")
}