forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.go
257 lines (231 loc) · 6.7 KB
/
sort.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
257
// 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"
"sort"
"strings"
"github.com/cockroachdb/cockroach/roachpb"
"github.com/cockroachdb/cockroach/sql/parser"
"github.com/cockroachdb/cockroach/util/log"
)
// orderBy constructs a sortNode based on the ORDER BY clause. Construction of
// the sortNode might adjust the number of render targets in the scanNode if
// any ordering expressions are specified.
func (p *planner) orderBy(n *parser.Select, s *scanNode) (*sortNode, *roachpb.Error) {
if n.OrderBy == nil {
return nil, nil
}
// We grab a copy of columns here because we might add new render targets
// below. This is the set of columns requested by the query.
columns := s.Columns()
var ordering []int
for _, o := range n.OrderBy {
index := 0
// Normalize the expression which has the side-effect of evaluating
// constant expressions and unwrapping expressions like "((a))" to "a".
expr, err := p.parser.NormalizeExpr(p.evalCtx, o.Expr)
if err != nil {
return nil, roachpb.NewError(err)
}
if qname, ok := expr.(*parser.QualifiedName); ok {
if len(qname.Indirect) == 0 {
// Look for an output column that matches the qualified name. This
// handles cases like:
//
// SELECT a AS b FROM t ORDER BY b
target := string(qname.Base)
for j, col := range columns {
if equalName(target, col.name) {
index = j + 1
break
}
}
}
if index == 0 {
// No output column matched the qualified name, so look for an existing
// render target that matches the column name. This handles cases like:
//
// SELECT a AS b FROM t ORDER BY a
if err := qname.NormalizeColumnName(); err != nil {
return nil, roachpb.NewError(err)
}
if qname.Table() == "" || equalName(s.desc.Alias, qname.Table()) {
for j, r := range s.render {
if qval, ok := r.(*qvalue); ok {
if equalName(qval.col.Name, qname.Column()) {
index = j + 1
break
}
}
}
}
}
}
if index == 0 {
// The order by expression matched neither an output column nor an
// existing render target.
if col, err := s.colIndex(expr); err != nil {
return nil, roachpb.NewError(err)
} else if col >= 0 {
index = col + 1
} else {
// Add a new render expression to use for ordering. This handles cases
// were the expression is either not a qualified name or is a qualified
// name that is otherwise not referenced by the query:
//
// SELECT a FROM t ORDER by b
// SELECT a, b FROM t ORDER by a+b
if err := s.addRender(parser.SelectExpr{Expr: expr}); err != nil {
return nil, err
}
index = len(s.columns)
}
}
if o.Direction == parser.Descending {
index = -index
}
ordering = append(ordering, index)
}
return &sortNode{columns: columns, ordering: ordering}, nil
}
type sortNode struct {
plan planNode
columns []resultColumn
ordering []int
needSort bool
pErr *roachpb.Error
}
func (n *sortNode) Columns() []resultColumn {
return n.columns
}
func (n *sortNode) Ordering() ([]int, int) {
if n == nil {
return nil, 0
}
return n.ordering, 0
}
func (n *sortNode) Values() parser.DTuple {
// If an ordering expression was used the number of columns in each row might
// differ from the number of columns requested, so trim the result.
v := n.plan.Values()
return v[:len(n.columns)]
}
func (n *sortNode) Next() bool {
if n.needSort {
n.needSort = false
if !n.initValues() {
return false
}
}
return n.plan.Next()
}
func (n *sortNode) PErr() *roachpb.Error {
return n.pErr
}
func (n *sortNode) ExplainPlan() (name, description string, children []planNode) {
if n.needSort {
name = "sort"
} else {
name = "nosort"
}
columns := n.plan.Columns()
strs := make([]string, len(n.ordering))
for i, o := range n.ordering {
prefix := '+'
if o < 0 {
o = -o
prefix = '-'
}
strs[i] = fmt.Sprintf("%c%s", prefix, columns[o-1].name)
}
description = strings.Join(strs, ",")
return name, description, []planNode{n.plan}
}
// wrap the supplied planNode with the sortNode if sorting is required.
func (n *sortNode) wrap(plan planNode) planNode {
if n != nil {
// Check to see if the requested ordering is compatible with the existing
// ordering.
existingOrdering, prefix := plan.Ordering()
if log.V(2) {
log.Infof("Sort: existing=%d (%d) desired=%d", existingOrdering, prefix, n.ordering)
}
match := computeOrderingMatch(n.ordering, existingOrdering, prefix, +1)
if match < len(n.ordering) {
n.plan = plan
n.needSort = true
return n
}
if len(n.columns) < len(plan.Columns()) {
// No sorting required, but we have to strip off the extra render
// expressions we added.
n.plan = plan
return n
}
}
if log.V(2) {
log.Infof("Sort: no sorting required")
}
return plan
}
func (n *sortNode) initValues() bool {
// TODO(pmattis): If the result set is large, we might need to perform the
// sort on disk.
var v *valuesNode
if x, ok := n.plan.(*valuesNode); ok {
v = x
v.ordering = n.ordering
} else {
v = &valuesNode{ordering: n.ordering}
// TODO(andrei): If we're scanning an index with a prefix matching an
// ordering prefix, we should only accumulate values for equal fields
// in this prefix, then sort the accumulated chunk and output.
for n.plan.Next() {
values := n.plan.Values()
valuesCopy := make(parser.DTuple, len(values))
copy(valuesCopy, values)
v.rows = append(v.rows, valuesCopy)
}
n.pErr = n.plan.PErr()
if n.pErr != nil {
return false
}
}
sort.Sort(v)
n.plan = v
return true
}
func computeOrderingMatch(desired, existing []int, prefix, reverse int) int {
match := 0
for match < len(desired) && match < len(existing) {
if desired[match] == reverse*existing[match] {
// The existing ordering matched the desired ordering.
prefix = 0
match++
continue
}
// The existing ordering did not match the desired ordering. Check if we're
// still considering a prefix of the existing ordering for which there was
// an exact match (and thus ordering is inconsequential).
if prefix == 0 {
break
}
prefix--
existing = existing[1:]
}
return match
}