-
Notifications
You must be signed in to change notification settings - Fork 65
/
sort.go
366 lines (331 loc) 路 8.44 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package arrowutils
import (
"bytes"
"cmp"
"context"
"errors"
"fmt"
"slices"
"sort"
"strconv"
"sync"
"github.com/apache/arrow/go/v14/arrow"
"github.com/apache/arrow/go/v14/arrow/array"
"github.com/apache/arrow/go/v14/arrow/compute"
"golang.org/x/sync/errgroup"
"github.com/polarsignals/frostdb/pqarrow/builder"
)
type Direction uint
const (
Ascending Direction = iota
Descending
)
func (d Direction) comparison() int {
switch d {
case Ascending:
return -1
case Descending:
return 1
default:
panic("unexpected direction value " + strconv.Itoa(int(d)) + " only -1 and 1 are allowed")
}
}
// SortingColumn describes a sorting column on a arrow.Record.
type SortingColumn struct {
Index int
Direction Direction
NullsFirst bool
}
// SortRecord sorts given arrow.Record by columns. Returns *array.Int32 of
// indices to sorted rows or record r.
//
// Comparison is made sequentially by each column. When rows are equal in the
// first column we compare the rows om the second column and so on and so forth
// until rows that are not equal are found.
func SortRecord(r arrow.Record, columns []SortingColumn) (*array.Int32, error) {
if len(columns) == 0 {
return nil, errors.New("pqarrow/arrowutils: at least one column is needed for sorting")
}
ms, err := newMultiColSorter(r, columns)
if err != nil {
return nil, err
}
defer ms.Release()
sort.Sort(ms)
return ms.indices.NewArray().(*array.Int32), nil
}
// Take uses indices which is an array of row index and returns a new record
// that only contains rows specified in indices.
//
// Use compute.WithAllocator to pass a custom memory.Allocator.
func Take(ctx context.Context, r arrow.Record, indices *array.Int32) (arrow.Record, error) {
// compute.Take doesn't support dictionaries. Use take on r when r does not have
// dictionary column.
var hasDictionary bool
for i := 0; i < int(r.NumCols()); i++ {
if r.Column(i).DataType().ID() == arrow.DICTIONARY {
hasDictionary = true
break
}
}
if !hasDictionary {
res, err := compute.Take(
ctx,
compute.TakeOptions{BoundsCheck: true},
compute.NewDatumWithoutOwning(r),
compute.NewDatumWithoutOwning(indices),
)
if err != nil {
return nil, err
}
return res.(*compute.RecordDatum).Value, nil
}
resArr := make([]arrow.Array, r.NumCols())
defer func() {
for _, a := range resArr {
if a != nil {
a.Release()
}
}
}()
var g errgroup.Group
for i := 0; i < int(r.NumCols()); i++ {
i := i
col := r.Column(i)
if d, ok := col.(*array.Dictionary); ok {
g.Go(func() error {
return takeDictColumn(ctx, d, i, resArr, indices)
})
} else {
g.Go(func() error {
return takeColumn(ctx, col, i, resArr, indices)
})
}
}
err := g.Wait()
if err != nil {
return nil, err
}
return array.NewRecord(r.Schema(), resArr, int64(indices.Len())), nil
}
func takeColumn(ctx context.Context, a arrow.Array, idx int, arr []arrow.Array, indices *array.Int32) error {
r, err := compute.TakeArray(ctx, a, indices)
if err != nil {
return err
}
arr[idx] = r
return nil
}
func takeDictColumn(ctx context.Context, a *array.Dictionary, idx int, arr []arrow.Array, indices *array.Int32) error {
var add func(b *array.BinaryDictionaryBuilder, idx int) error
switch e := a.Dictionary().(type) {
case *array.String:
add = func(b *array.BinaryDictionaryBuilder, idx int) error {
return b.AppendString(e.Value(idx))
}
case *array.Binary:
add = func(b *array.BinaryDictionaryBuilder, idx int) error {
return b.Append(e.Value(idx))
}
default:
panic(fmt.Sprintf("unexpected dictionary type %T for take", e))
}
r := array.NewBuilder(compute.GetAllocator(ctx), a.DataType()).(*array.BinaryDictionaryBuilder)
defer r.Release()
r.Reserve(indices.Len())
for _, i := range indices.Int32Values() {
if a.IsNull(int(i)) {
r.AppendNull()
continue
}
err := add(r, a.GetValueIndex(int(i)))
if err != nil {
return err
}
}
arr[idx] = r.NewArray()
return nil
}
type multiColSorter struct {
indices *builder.OptInt32Builder
comparisons []comparator
directions []int
nullsFirst []bool
}
func newMultiColSorter(
r arrow.Record,
columns []SortingColumn,
) (*multiColSorter, error) {
ms := multiColSorterPool.Get().(*multiColSorter)
if r.NumRows() <= 1 {
if r.NumRows() == 1 {
ms.indices.Append(0)
}
return ms, nil
}
ms.Reserve(int(r.NumRows()), len(columns))
for i := range columns {
ms.directions[i] = int(columns[i].Direction.comparison())
ms.nullsFirst[i] = columns[i].NullsFirst
}
for i, col := range columns {
switch e := r.Column(col.Index).(type) {
case *array.Int64:
ms.comparisons[i] = newOrderedSorter[int64](e, cmp.Compare)
case *array.Float64:
ms.comparisons[i] = newOrderedSorter[float64](e, cmp.Compare)
case *array.String:
ms.comparisons[i] = newOrderedSorter[string](e, cmp.Compare)
case *array.Binary:
ms.comparisons[i] = newOrderedSorter[[]byte](e, bytes.Compare)
case *array.Dictionary:
switch elem := e.Dictionary().(type) {
case *array.String:
ms.comparisons[i] = newOrderedSorter[string](
&stringDictionary{
dict: e,
elem: elem,
},
cmp.Compare,
)
case *array.Binary:
ms.comparisons[i] = newOrderedSorter[[]byte](
&binaryDictionary{
dict: e,
elem: elem,
},
bytes.Compare,
)
default:
ms.Release()
return nil, fmt.Errorf("unsupported dictionary column type for sorting %T", e)
}
default:
ms.Release()
return nil, fmt.Errorf("unsupported column type for sorting %T", e)
}
}
return ms, nil
}
func (m *multiColSorter) Reserve(rows, columns int) {
m.indices.Reserve(rows)
for i := 0; i < rows; i++ {
m.indices.Set(i, int32(i))
}
m.comparisons = slices.Grow(m.comparisons, columns)[:columns]
m.directions = slices.Grow(m.directions, columns)[:columns]
m.nullsFirst = slices.Grow(m.nullsFirst, columns)[:columns]
}
func (m *multiColSorter) Reset() {
m.indices.Reserve(0)
m.comparisons = m.comparisons[:0]
m.directions = m.directions[:0]
m.nullsFirst = m.nullsFirst[:0]
}
func (m *multiColSorter) Release() {
m.Reset()
multiColSorterPool.Put(m)
}
var multiColSorterPool = &sync.Pool{
New: func() any {
return &multiColSorter{
indices: builder.NewOptInt32Builder(arrow.PrimitiveTypes.Int32),
}
},
}
var _ sort.Interface = (*multiColSorter)(nil)
func (m *multiColSorter) Len() int { return m.indices.Len() }
func (m *multiColSorter) Less(i, j int) bool {
for idx := range m.comparisons {
cmp := m.compare(idx, int(m.indices.Value(i)), int(m.indices.Value(j)))
if cmp != 0 {
// Use direction to reorder the comparison. Direction determines if the list
// is in ascending or descending.
//
// For instance if comparison between i,j value is -1 and direction is -1
// this will resolve to true hence the list will be in ascending order. Same
// principle applies for descending.
return cmp == m.directions[idx]
}
// Try comparing the next column
}
return false
}
func (m *multiColSorter) compare(idx, i, j int) int {
x := m.comparisons[idx]
if x.IsNull(i) {
if x.IsNull(j) {
return 0
}
if m.directions[idx] == 1 {
if m.nullsFirst[idx] {
return 1
}
return -1
}
if m.nullsFirst[idx] {
return -1
}
return 1
}
if x.IsNull(j) {
if m.directions[idx] == 1 {
if m.nullsFirst[idx] {
return -1
}
return 1
}
if m.nullsFirst[idx] {
return 1
}
return -1
}
return x.Compare(i, j)
}
func (m *multiColSorter) Swap(i, j int) {
m.indices.Swap(i, j)
}
type comparator interface {
Compare(i, j int) int
IsNull(int) bool
}
type orderedArray[T any] interface {
Value(int) T
IsNull(int) bool
}
type orderedSorter[T any] struct {
array orderedArray[T]
compare func(T, T) int
}
func newOrderedSorter[T any](a orderedArray[T], compare func(T, T) int) *orderedSorter[T] {
return &orderedSorter[T]{
array: a,
compare: compare,
}
}
func (s *orderedSorter[T]) IsNull(i int) bool {
return s.array.IsNull(i)
}
func (s *orderedSorter[T]) Compare(i, j int) int {
return s.compare(s.array.Value(i), s.array.Value(j))
}
type stringDictionary struct {
dict *array.Dictionary
elem *array.String
}
func (s *stringDictionary) IsNull(i int) bool {
return s.dict.IsNull(i)
}
func (s *stringDictionary) Value(i int) string {
return s.elem.Value(s.dict.GetValueIndex(i))
}
type binaryDictionary struct {
dict *array.Dictionary
elem *array.Binary
}
func (s *binaryDictionary) IsNull(i int) bool {
return s.dict.IsNull(i)
}
func (s *binaryDictionary) Value(i int) []byte {
return s.elem.Value(s.dict.GetValueIndex(i))
}