-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.go
executable file
·121 lines (102 loc) · 2.66 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
// Copyright 2021 Dolthub, 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,
// 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.
package expression
import (
"container/heap"
"github.com/Rock-liyi/p2pdb-store/sql"
)
type Sorter struct {
SortFields []sql.SortField
Rows []sql.Row
LastError error
Ctx *sql.Context
}
func (s *Sorter) Len() int {
return len(s.Rows)
}
func (s *Sorter) Swap(i, j int) {
s.Rows[i], s.Rows[j] = s.Rows[j], s.Rows[i]
}
func (s *Sorter) Less(i, j int) bool {
if s.LastError != nil {
return false
}
a := s.Rows[i]
b := s.Rows[j]
for _, sf := range s.SortFields {
typ := sf.Column.Type()
av, err := sf.Column.Eval(s.Ctx, a)
if err != nil {
s.LastError = sql.ErrUnableSort.Wrap(err)
return false
}
bv, err := sf.Column.Eval(s.Ctx, b)
if err != nil {
s.LastError = sql.ErrUnableSort.Wrap(err)
return false
}
if sf.Order == sql.Descending {
av, bv = bv, av
}
if av == nil && bv == nil {
continue
} else if av == nil {
return sf.NullOrdering == sql.NullsFirst
} else if bv == nil {
return sf.NullOrdering != sql.NullsFirst
}
cmp, err := typ.Compare(av, bv)
if err != nil {
s.LastError = err
return false
}
switch cmp {
case -1:
return true
case 1:
return false
}
}
return false
}
// TopRowsHeap implements heap.Interface based on Sorter. It inverts the Less()
// function so that it can be used to implement TopN. heap.Push() rows into it,
// and if Len() > MAX; heap.Pop() the current min row. Then, at the end of
// seeing all the rows, call Rows(). Rows() will return the rows which come
// back from heap.Pop() in reverse order, correctly restoring the order for the
// TopN elements.
type TopRowsHeap struct {
Sorter
}
func (h *TopRowsHeap) Less(i, j int) bool {
return !h.Sorter.Less(i, j)
}
func (h *TopRowsHeap) Push(x interface{}) {
h.Sorter.Rows = append(h.Sorter.Rows, x.(sql.Row))
}
func (h *TopRowsHeap) Pop() interface{} {
old := h.Sorter.Rows
n := len(old)
res := old[n-1]
h.Sorter.Rows = old[0 : n-1]
return res
}
func (h *TopRowsHeap) Rows() ([]sql.Row, error) {
l := h.Len()
res := make([]sql.Row, l)
for i := l - 1; i >= 0; i-- {
res[i] = heap.Pop(h).(sql.Row)
}
return res, h.LastError
}