-
Notifications
You must be signed in to change notification settings - Fork 64
/
groupranges.go
304 lines (282 loc) 路 7.68 KB
/
groupranges.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
package arrowutils
import (
"bytes"
"container/heap"
"fmt"
"strings"
"github.com/apache/arrow/go/v16/arrow"
"github.com/apache/arrow/go/v16/arrow/array"
)
// GetGroupsAndOrderedSetRanges returns a min-heap of group ranges and ordered
// set ranges of the given arrow arrays in that order. For the given input with
// a single array:
// a a c d a b c
// This function will return [2, 3, 4, 5, 6] for the group ranges and [4] for
// the ordered set ranges. A group is a collection of values that are equal and
// an ordered set is a collection of groups that are in increasing order.
// The ranges are determined by iterating over the arrays and comparing the
// current group value for each column. The firstGroup to compare against must
// be provided (it can be initialized to the values at index 0 of each array).
// The last group found is returned.
func GetGroupsAndOrderedSetRanges(
firstGroup []any, arrs []arrow.Array,
) (*Int64Heap, *Int64Heap, []any, error) {
if len(firstGroup) != len(arrs) {
return nil,
nil,
nil,
fmt.Errorf(
"columns mismatch (%d != %d) when getting group ranges",
len(firstGroup),
len(arrs),
)
}
// Safe copy the group in order to not overwrite the input slice values.
curGroup := make([]any, len(firstGroup))
for i, v := range firstGroup {
switch concreteV := v.(type) {
case []byte:
curGroup[i] = append([]byte(nil), concreteV...)
default:
curGroup[i] = v
}
}
// groupRanges keeps track of the bounds of the group by columns.
groupRanges := &Int64Heap{}
heap.Init(groupRanges)
// setRanges keeps track of the bounds of ordered sets. i.e. in the
// following slice, (a, a, b, c) is an ordered set of three groups. The
// second ordered set is (a, e): [a, a, b, c, a, e]
setRanges := &Int64Heap{}
heap.Init(setRanges)
// handleCmpResult is a closure that encapsulates the handling of the result
// of comparing a current grouping column with a value in a group array.
handleCmpResult := func(cmp, column int, t arrow.Array, j int) error {
switch cmp {
case -1, 1:
// New group, append range index.
heap.Push(groupRanges, int64(j))
if cmp == 1 {
// New ordered set encountered.
heap.Push(setRanges, int64(j))
}
// And update the current group.
v := t.GetOneForMarshal(j)
switch concreteV := v.(type) {
case []byte:
// Safe copy, otherwise the value might get overwritten.
curGroup[column] = append([]byte(nil), concreteV...)
default:
curGroup[column] = v
}
case 0:
// Equal to group, do nothing.
}
return nil
}
for i, arr := range arrs {
switch t := arr.(type) {
case *array.Binary:
for j := 0; j < arr.Len(); j++ {
var curGroupValue []byte
if curGroup[i] != nil {
curGroupValue = curGroup[i].([]byte)
}
vIsNull := t.IsNull(j)
cmp, ok := nullComparison(curGroupValue == nil, vIsNull)
if !ok {
cmp = bytes.Compare(curGroupValue, t.Value(j))
}
if err := handleCmpResult(cmp, i, t, j); err != nil {
return nil, nil, nil, err
}
}
case *array.String:
for j := 0; j < arr.Len(); j++ {
var curGroupValue *string
if curGroup[i] != nil {
g := curGroup[i].(string)
curGroupValue = &g
}
vIsNull := t.IsNull(j)
cmp, ok := nullComparison(curGroupValue == nil, vIsNull)
if !ok {
cmp = strings.Compare(*curGroupValue, t.Value(j))
}
if err := handleCmpResult(cmp, i, t, j); err != nil {
return nil, nil, nil, err
}
}
case *array.Int64:
for j := 0; j < arr.Len(); j++ {
var curGroupValue *int64
if curGroup[i] != nil {
g := curGroup[i].(int64)
curGroupValue = &g
}
vIsNull := t.IsNull(j)
cmp, ok := nullComparison(curGroupValue == nil, vIsNull)
if !ok {
cmp = compareInt64(*curGroupValue, t.Value(j))
}
if err := handleCmpResult(cmp, i, t, j); err != nil {
return nil, nil, nil, err
}
}
case *array.Boolean:
for j := 0; j < arr.Len(); j++ {
var curGroupValue *bool
if curGroup[i] != nil {
g := curGroup[i].(bool)
curGroupValue = &g
}
vIsNull := t.IsNull(j)
cmp, ok := nullComparison(curGroupValue == nil, vIsNull)
if !ok {
cmp = compareBools(*curGroupValue, t.Value(j))
}
if err := handleCmpResult(cmp, i, t, j); err != nil {
return nil, nil, nil, err
}
}
case VirtualNullArray:
for j := 0; j < arr.Len(); j++ {
cmp, ok := nullComparison(curGroup[i] == nil, true)
if !ok {
return nil, nil, nil, fmt.Errorf(
"null comparison should always be valid but group was: %v", curGroup[i],
)
}
if err := handleCmpResult(cmp, i, t, j); err != nil {
return nil, nil, nil, err
}
}
case *array.Dictionary:
switch dict := t.Dictionary().(type) {
case *array.Binary:
for j := 0; j < arr.Len(); j++ {
var curGroupValue []byte
if curGroup[i] != nil {
curGroupValue = curGroup[i].([]byte)
}
vIsNull := t.IsNull(j)
cmp, ok := nullComparison(curGroupValue == nil, vIsNull)
if !ok {
cmp = bytes.Compare(curGroupValue, dict.Value(t.GetValueIndex(j)))
}
if err := handleCmpResult(cmp, i, t, j); err != nil {
return nil, nil, nil, err
}
}
case *array.String:
for j := 0; j < arr.Len(); j++ {
var curGroupValue *string
if curGroup[i] != nil {
g := curGroup[i].(string)
curGroupValue = &g
}
vIsNull := t.IsNull(j)
cmp, ok := nullComparison(curGroupValue == nil, vIsNull)
if !ok {
cmp = strings.Compare(*curGroupValue,
dict.Value(t.GetValueIndex(j)),
)
}
if err := handleCmpResult(cmp, i, t, j); err != nil {
return nil, nil, nil, err
}
}
default:
panic(fmt.Sprintf("unsupported dictionary type: %T", dict))
}
default:
panic(fmt.Sprintf("unsupported type: %T", t))
}
}
return groupRanges, setRanges, curGroup, nil
}
// nullComparison encapsulates null comparison. leftNull is whether the current
// Note that this function observes default SQL semantics as well as our own,
// i.e. nulls sort first.
// The comparison integer is returned, as well as whether either value was null.
// If the returned boolean is false, the comparison should be disregarded.
func nullComparison(leftNull, rightNull bool) (int, bool) {
if !leftNull && !rightNull {
// Both are not null, this implies that the null comparison should be
// disregarded.
return 0, false
}
if leftNull {
if !rightNull {
return -1, true
}
return 0, true
}
return 1, true
}
func compareInt64(a, b int64) int {
if a < b {
return -1
}
if a > b {
return 1
}
return 0
}
func compareBools(a, b bool) int {
if a == b {
return 0
}
if !a {
return -1
}
return 1
}
type Int64Heap []int64
func (h Int64Heap) Len() int {
return len(h)
}
func (h Int64Heap) Less(i, j int) bool {
return h[i] < h[j]
}
func (h Int64Heap) Swap(i, j int) {
h[i], h[j] = h[j], h[i]
}
func (h *Int64Heap) Push(x any) {
*h = append(*h, x.(int64))
}
func (h *Int64Heap) Pop() any {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return x
}
// PopNextNotEqual returns the next least element not equal to compare.
func (h *Int64Heap) PopNextNotEqual(compare int64) (int64, bool) {
for h.Len() > 0 {
v := heap.Pop(h).(int64)
if v != compare {
return v, true
}
}
return 0, false
}
// Unwrap unwraps the heap into the provided scratch space. The result is a
// slice that will have distinct ints in order. This helps with reiterating over
// the same heap.
func (h *Int64Heap) Unwrap(scratch []int64) []int64 {
scratch = scratch[:0]
if h.Len() == 0 {
return scratch
}
cmp := (*h)[0]
scratch = append(scratch, cmp)
for h.Len() > 0 {
if v := heap.Pop(h).(int64); v != cmp {
scratch = append(scratch, v)
cmp = v
}
}
return scratch
}