forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.go
236 lines (212 loc) · 6.3 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
// 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/encoding"
"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 columnOrdering
for _, o := range n.OrderBy {
index := -1
// 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
break
}
}
}
if index == -1 {
// 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
break
}
}
}
}
}
}
if index == -1 {
// 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
} 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) - 1
}
}
direction := encoding.Ascending
if o.Direction == parser.Descending {
direction = encoding.Descending
}
ordering = append(ordering, columnOrderInfo{index, direction})
}
return &sortNode{columns: columns, ordering: ordering}, nil
}
type sortNode struct {
plan planNode
columns []resultColumn
ordering columnOrdering
needSort bool
pErr *roachpb.Error
}
func (n *sortNode) Columns() []resultColumn {
return n.columns
}
func (n *sortNode) Ordering() orderingInfo {
if n == nil {
return orderingInfo{}
}
return orderingInfo{exactMatchCols: nil, ordering: n.ordering}
}
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.direction == encoding.Descending {
prefix = '-'
}
strs[i] = fmt.Sprintf("%c%s", prefix, columns[o.colIdx].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 := plan.Ordering()
if log.V(2) {
log.Infof("Sort: existing=%d desired=%d", existingOrdering, n.ordering)
}
match := computeOrderingMatch(n.ordering, existingOrdering, false)
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
}