-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathsort.go
More file actions
36 lines (33 loc) · 1.15 KB
/
sort.go
File metadata and controls
36 lines (33 loc) · 1.15 KB
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
package plugin
import (
"cmp"
"slices"
"github.com/apache/arrow-go/v18/arrow"
"github.com/apache/arrow-go/v18/arrow/array"
"github.com/cloudquery/plugin-sdk/v4/schema"
)
// sort records by specific column. This is intended for testing purposes only.
// Because "id" is auto-incrementing in the test data generator, if passed "id"
// this should result in records being returned in insertion order.
// nolint:unparam
func sortRecords(table *schema.Table, records []arrow.RecordBatch, columnName string) {
sch := table.ToArrowSchema()
if !sch.HasField(columnName) {
panic("table has no '" + columnName + "' column to sort on")
}
colIndex := sch.FieldIndices(columnName)[0]
slices.SortFunc(records, func(a, b arrow.RecordBatch) int {
switch a.Column(colIndex).DataType().(type) {
case *arrow.Int64Type:
v1 := a.Column(colIndex).(*array.Int64).Value(0)
v2 := b.Column(colIndex).(*array.Int64).Value(0)
return cmp.Compare(v1, v2)
case *arrow.StringType:
v1 := a.Column(colIndex).(*array.String).Value(0)
v2 := b.Column(colIndex).(*array.String).Value(0)
return cmp.Compare(v1, v2)
default:
panic("unsupported type for sorting")
}
})
}