forked from cockroachdb/cockroach
/
returning.go
127 lines (114 loc) · 3.81 KB
/
returning.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
// 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.
//
// Author: Matt Jibson (mjibson@cockroachlabs.com)
package sql
import (
"bytes"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/util"
)
// returningHelper implements the logic used for statements with RETURNING clauses. It accumulates
// result rows, one for each call to append().
type returningHelper struct {
p *planner
// Expected columns.
columns ResultColumns
// Processed copies of expressions from ReturningExprs.
exprs []parser.TypedExpr
rowCount int
source *dataSourceInfo
curSourceRow parser.DTuple
// 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
}
// newReturningHelper creates a new returningHelper for use by an
// insert/update node.
func (p *planner) newReturningHelper(
r parser.ReturningExprs,
desiredTypes []parser.Type,
alias string,
tablecols []sqlbase.ColumnDescriptor,
) (*returningHelper, error) {
rh := &returningHelper{
p: p,
}
if len(r) == 0 {
return rh, nil
}
for _, e := range r {
if err := p.parser.AssertNoAggregationOrWindowing(
e.Expr, "RETURNING", p.session.SearchPath,
); err != nil {
return nil, err
}
}
rh.columns = make(ResultColumns, 0, len(r))
aliasTableName := parser.TableName{TableName: parser.Name(alias)}
rh.source = newSourceInfoForSingleTable(aliasTableName, makeResultColumns(tablecols))
rh.exprs = make([]parser.TypedExpr, 0, len(r))
ivarHelper := parser.MakeIndexedVarHelper(rh, len(tablecols))
for _, target := range r {
cols, typedExprs, _, err := p.computeRender(target, parser.TypeAny, rh.source, ivarHelper, true)
if err != nil {
return nil, err
}
rh.columns = append(rh.columns, cols...)
rh.exprs = append(rh.exprs, typedExprs...)
}
return rh, nil
}
// cookResultRow prepares a row according to the ReturningExprs, with input values
// from rowVals.
func (rh *returningHelper) cookResultRow(rowVals parser.DTuple) (parser.DTuple, error) {
if rh.exprs == nil {
rh.rowCount++
return rowVals, nil
}
rh.curSourceRow = rowVals
resRow := make(parser.DTuple, len(rh.exprs))
for i, e := range rh.exprs {
d, err := e.Eval(&rh.p.evalCtx)
if err != nil {
return nil, err
}
resRow[i] = d
}
return resRow, nil
}
func (rh *returningHelper) startPlans() error {
for _, expr := range rh.exprs {
if err := rh.p.startSubqueryPlans(expr); err != nil {
return err
}
}
return nil
}
// IndexedVarEval implements the parser.IndexedVarContainer interface.
func (rh *returningHelper) IndexedVarEval(idx int, ctx *parser.EvalContext) (parser.Datum, error) {
return rh.curSourceRow[idx].Eval(ctx)
}
// IndexedVarResolvedType implements the parser.IndexedVarContainer interface.
func (rh *returningHelper) IndexedVarResolvedType(idx int) parser.Type {
return rh.source.sourceColumns[idx].Typ
}
// IndexedVarFormat implements the parser.IndexedVarContainer interface.
func (rh *returningHelper) IndexedVarFormat(buf *bytes.Buffer, f parser.FmtFlags, idx int) {
rh.source.FormatVar(buf, f, idx)
}