forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
values.go
155 lines (138 loc) · 4.01 KB
/
values.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
// Copyright 2015 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: Peter Mattis (peter@cockroachlabs.com)
package sql
import (
"fmt"
"strconv"
"github.com/cockroachdb/cockroach/roachpb"
"github.com/cockroachdb/cockroach/sql/parser"
)
// Values constructs a valuesNode from a VALUES expression.
func (p *planner) Values(n parser.Values) (planNode, *roachpb.Error) {
v := &valuesNode{
rows: make([]parser.DTuple, 0, len(n)),
}
for num, tuple := range n {
if num == 0 {
v.columns = make([]resultColumn, 0, len(tuple))
} else if a, e := len(tuple), len(v.columns); a != e {
return nil, roachpb.NewUErrorf("VALUES lists must all be the same length, %d for %d", a, e)
}
for i := range tuple {
var pErr *roachpb.Error
tuple[i], pErr = p.expandSubqueries(tuple[i], 1)
if pErr != nil {
return nil, pErr
}
var err error
typ, err := tuple[i].TypeCheck(p.evalCtx.Args)
if err != nil {
return nil, roachpb.NewError(err)
}
tuple[i], err = p.parser.NormalizeExpr(p.evalCtx, tuple[i])
if err != nil {
return nil, roachpb.NewError(err)
}
if num == 0 {
v.columns = append(v.columns, resultColumn{name: "column" + strconv.Itoa(i+1), typ: typ})
} else if v.columns[i].typ == parser.DNull {
v.columns[i].typ = typ
} else if typ != parser.DNull && typ != v.columns[i].typ {
return nil, roachpb.NewUErrorf("VALUES list type mismatch, %s for %s", typ.Type(), v.columns[i].typ.Type())
}
}
data, err := tuple.Eval(p.evalCtx)
if err != nil {
return nil, roachpb.NewError(err)
}
vals, ok := data.(parser.DTuple)
if !ok {
return nil, roachpb.NewUErrorf("expected a tuple, but found %T", data)
}
v.rows = append(v.rows, vals)
}
return v, nil
}
type valuesNode struct {
columns []resultColumn
ordering []int
rows []parser.DTuple
nextRow int // The index of the next row.
}
func (n *valuesNode) Columns() []resultColumn {
return n.columns
}
func (n *valuesNode) Ordering() ([]int, int) {
return nil, 0
}
func (n *valuesNode) Values() parser.DTuple {
return n.rows[n.nextRow-1]
}
func (n *valuesNode) Next() bool {
if n.nextRow >= len(n.rows) {
return false
}
n.nextRow++
return true
}
func (*valuesNode) PErr() *roachpb.Error {
return nil
}
func (n *valuesNode) Len() int {
return len(n.rows)
}
func (n *valuesNode) Less(i, j int) bool {
// TODO(pmattis): An alternative to this type of field-based comparison would
// be to construct a sort-key per row using encodeTableKey(). Using a
// sort-key approach would likely fit better with a disk-based sort.
ra, rb := n.rows[i], n.rows[j]
for _, k := range n.ordering {
var da, db parser.Datum
if k < 0 {
da = rb[-(k + 1)]
db = ra[-(k + 1)]
} else {
da = ra[k-1]
db = rb[k-1]
}
// TODO(pmattis): This is assuming that the datum types are compatible. I'm
// not sure this always holds as `CASE` expressions can return different
// types for a column for different rows. Investigate how other RDBMs
// handle this.
if c := da.Compare(db); c < 0 {
return true
} else if c > 0 {
return false
}
}
return true
}
func (n *valuesNode) Swap(i, j int) {
n.rows[i], n.rows[j] = n.rows[j], n.rows[i]
}
func (n *valuesNode) ExplainPlan() (name, description string, children []planNode) {
name = "values"
pluralize := func(n int) string {
if n == 1 {
return ""
}
return "s"
}
description = fmt.Sprintf("%d column%s, %d row%s",
len(n.columns), pluralize(len(n.columns)),
len(n.rows), pluralize(len(n.rows)))
return name, description, nil
}