forked from cockroachdb/cockroach
/
returning.go
126 lines (113 loc) · 3.46 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
// 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 (
"fmt"
"github.com/cockroachdb/cockroach/sql/parser"
"github.com/cockroachdb/cockroach/sql/sqlbase"
)
// 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 []ResultColumn
// Processed copies of expressions from ReturningExprs.
exprs []parser.TypedExpr
qvals qvalMap
rowCount int
source *dataSourceInfo
}
func (p *planner) makeReturningHelper(
r parser.ReturningExprs,
desiredTypes []parser.Datum,
alias string,
tablecols []sqlbase.ColumnDescriptor,
) (returningHelper, error) {
rh := returningHelper{
p: p,
}
if len(r) == 0 {
return rh, nil
}
for _, e := range r {
if p.parser.AggregateInExpr(e.Expr) {
return rh, fmt.Errorf("aggregate functions are not allowed in RETURNING")
}
}
rh.columns = make([]ResultColumn, 0, len(r))
rh.source = newSourceInfoForSingleTable(alias, makeResultColumns(tablecols))
rh.qvals = make(qvalMap)
rh.exprs = make([]parser.TypedExpr, 0, len(r))
for i, target := range r {
if isStar, cols, typedExprs, err := checkRenderStar(target, rh.source, rh.qvals); err != nil {
return returningHelper{}, err
} else if isStar {
rh.exprs = append(rh.exprs, typedExprs...)
rh.columns = append(rh.columns, cols...)
continue
}
// When generating an output column name it should exactly match the original
// expression, so determine the output column name before we perform any
// manipulations to the expression.
outputName := getRenderColName(target)
desired := parser.NoTypePreference
if len(desiredTypes) > i {
desired = desiredTypes[i]
}
typedExpr, err := rh.p.analyzeExpr(target.Expr, multiSourceInfo{rh.source}, rh.qvals, desired, false, "")
if err != nil {
return returningHelper{}, err
}
rh.exprs = append(rh.exprs, typedExpr)
rh.columns = append(rh.columns, ResultColumn{Name: outputName, Typ: typedExpr.ReturnType()})
}
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.qvals.populateQVals(rh.source, 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) expandPlans() error {
for _, expr := range rh.exprs {
if err := rh.p.expandSubqueryPlans(expr); err != nil {
return err
}
}
return nil
}
func (rh *returningHelper) startPlans() error {
for _, expr := range rh.exprs {
if err := rh.p.startSubqueryPlans(expr); err != nil {
return err
}
}
return nil
}