forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreturning.go
More file actions
103 lines (93 loc) · 3.11 KB
/
Copy pathreturning.go
File metadata and controls
103 lines (93 loc) · 3.11 KB
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
// 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 ([email protected])
package sql
import "github.com/cockroachdb/cockroach/sql/parser"
// returningNode accumulates the results for a RETURNING clause. If the rows are empty, we just
// keep track of the count.
type returningNode struct {
valuesNode
rowCount int
}
// 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
results *returningNode
// Processed copies of expressions from ReturningExprs.
exprs parser.Exprs
qvals qvalMap
}
func makeReturningHelper(p *planner, r parser.ReturningExprs,
alias string, tablecols []ColumnDescriptor) (returningHelper, error) {
rh := returningHelper{p: p, results: &returningNode{}}
if len(r) == 0 {
return rh, nil
}
rh.results.columns = make([]ResultColumn, 0, len(r))
table := tableInfo{
columns: makeResultColumns(tablecols, 0),
alias: alias,
}
rh.qvals = make(qvalMap)
rh.exprs = make([]parser.Expr, 0, len(r))
for _, target := range r {
if isStar, cols, exprs, err := checkRenderStar(target, &table, rh.qvals); err != nil {
return returningHelper{}, err
} else if isStar {
rh.exprs = append(rh.exprs, exprs...)
rh.results.columns = append(rh.results.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)
expr, err := resolveQNames(&table, rh.qvals, target.Expr)
if err != nil {
return returningHelper{}, err
}
typ, err := expr.TypeCheck(rh.p.evalCtx.Args)
if err != nil {
return returningHelper{}, err
}
rh.exprs = append(rh.exprs, expr)
rh.results.columns = append(rh.results.columns, ResultColumn{Name: outputName, Typ: typ})
}
return rh, nil
}
// append adds a result row. The row is computed according to the ReturningExprs, with input values
// from rowVals.
func (rh *returningHelper) append(rowVals parser.DTuple) error {
if rh.exprs == nil {
rh.results.rowCount++
return nil
}
rh.qvals.populateQVals(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 err
}
resrow[i] = d
}
rh.results.rows = append(rh.results.rows, resrow)
return nil
}
// getResults returns the results as a returningNode.
func (rh *returningHelper) getResults() *returningNode {
return rh.results
}