-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
sort.go
245 lines (219 loc) · 5.19 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
// Copyright 2017 PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package executor
import (
"container/heap"
"sort"
"github.com/juju/errors"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/plan"
"github.com/pingcap/tidb/util/types"
)
// orderByRow binds a row to its order values, so it can be sorted.
type orderByRow struct {
key []*types.Datum
row Row
}
// SortExec represents sorting executor.
type SortExec struct {
baseExecutor
ByItems []*plan.ByItems
Rows []*orderByRow
Idx int
fetched bool
err error
schema *expression.Schema
}
// Close implements the Executor Close interface.
func (e *SortExec) Close() error {
e.Rows = nil
return errors.Trace(e.children[0].Close())
}
// Open implements the Executor Open interface.
func (e *SortExec) Open() error {
e.fetched = false
e.Idx = 0
e.Rows = nil
return errors.Trace(e.children[0].Open())
}
// Len returns the number of rows.
func (e *SortExec) Len() int {
return len(e.Rows)
}
// Swap implements sort.Interface Swap interface.
func (e *SortExec) Swap(i, j int) {
e.Rows[i], e.Rows[j] = e.Rows[j], e.Rows[i]
}
// Less implements sort.Interface Less interface.
func (e *SortExec) Less(i, j int) bool {
sc := e.ctx.GetSessionVars().StmtCtx
for index, by := range e.ByItems {
v1 := e.Rows[i].key[index]
v2 := e.Rows[j].key[index]
ret, err := v1.CompareDatum(sc, v2)
if err != nil {
e.err = errors.Trace(err)
return true
}
if by.Desc {
ret = -ret
}
if ret < 0 {
return true
} else if ret > 0 {
return false
}
}
return false
}
// Next implements the Executor Next interface.
func (e *SortExec) Next() (Row, error) {
if !e.fetched {
for {
srcRow, err := e.children[0].Next()
if err != nil {
return nil, errors.Trace(err)
}
if srcRow == nil {
break
}
orderRow := &orderByRow{
row: srcRow,
key: make([]*types.Datum, len(e.ByItems)),
}
for i, byItem := range e.ByItems {
key, err := byItem.Expr.Eval(srcRow)
if err != nil {
return nil, errors.Trace(err)
}
orderRow.key[i] = &key
}
e.Rows = append(e.Rows, orderRow)
}
sort.Sort(e)
e.fetched = true
}
if e.err != nil {
return nil, errors.Trace(e.err)
}
if e.Idx >= len(e.Rows) {
return nil, nil
}
row := e.Rows[e.Idx].row
e.Idx++
return row, nil
}
// TopNExec implements a Top-N algorithm and it is built from a SELECT statement with ORDER BY and LIMIT.
// Instead of sorting all the rows fetched from the table, it keeps the Top-N elements only in a heap to reduce memory usage.
type TopNExec struct {
SortExec
limit *plan.Limit
totalCount int
heapSize int
}
// Less implements heap.Interface Less interface.
func (e *TopNExec) Less(i, j int) bool {
sc := e.ctx.GetSessionVars().StmtCtx
for index, by := range e.ByItems {
v1 := e.Rows[i].key[index]
v2 := e.Rows[j].key[index]
ret, err := v1.CompareDatum(sc, v2)
if err != nil {
e.err = errors.Trace(err)
return true
}
if by.Desc {
ret = -ret
}
if ret > 0 {
return true
} else if ret < 0 {
return false
}
}
return false
}
// Len implements heap.Interface Len interface.
func (e *TopNExec) Len() int {
return e.heapSize
}
// Push implements heap.Interface Push interface.
func (e *TopNExec) Push(x interface{}) {
e.Rows = append(e.Rows, x.(*orderByRow))
e.heapSize++
}
// Pop implements heap.Interface Pop interface.
func (e *TopNExec) Pop() interface{} {
e.heapSize--
return nil
}
// Next implements the Executor Next interface.
func (e *TopNExec) Next() (Row, error) {
if !e.fetched {
e.Idx = int(e.limit.Offset)
e.totalCount = int(e.limit.Offset + e.limit.Count)
cap := e.totalCount + 1
if cap > 1024 {
cap = 1024
}
e.Rows = make([]*orderByRow, 0, cap)
e.heapSize = 0
for {
srcRow, err := e.children[0].Next()
if err != nil {
return nil, errors.Trace(err)
}
if srcRow == nil {
break
}
// build orderRow from srcRow.
orderRow := &orderByRow{
row: srcRow,
key: make([]*types.Datum, len(e.ByItems)),
}
for i, byItem := range e.ByItems {
key, err := byItem.Expr.Eval(srcRow)
if err != nil {
return nil, errors.Trace(err)
}
orderRow.key[i] = &key
}
if e.totalCount == e.heapSize {
// An equivalent of Push and Pop. We don't use the standard Push and Pop
// to reduce the number of comparisons.
e.Rows = append(e.Rows, orderRow)
if e.Less(0, e.heapSize) {
e.Swap(0, e.heapSize)
heap.Fix(e, 0)
}
e.Rows = e.Rows[:e.heapSize]
} else {
heap.Push(e, orderRow)
}
}
if e.limit.Offset == 0 {
sort.Sort(&e.SortExec)
} else {
for i := 0; i < int(e.limit.Count) && e.Len() > 0; i++ {
heap.Pop(e)
}
}
e.fetched = true
}
if e.Idx >= len(e.Rows) {
return nil, nil
}
row := e.Rows[e.Idx].row
e.Idx++
return row, nil
}