/
util.go
149 lines (132 loc) · 2.9 KB
/
util.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
package query
import (
"strings"
"github.com/colega/zeropool"
"github.com/parquet-go/parquet-go"
)
func GetColumnIndexByPath(root *parquet.Column, s string) (index, depth int) {
colSelector := strings.Split(s, ".")
n := root
for len(colSelector) > 0 {
n = n.Column(colSelector[0])
if n == nil {
return -1, -1
}
colSelector = colSelector[1:]
depth++
}
return n.Index(), depth
}
func HasColumn(root *parquet.Column, s string) bool {
index, _ := GetColumnIndexByPath(root, s)
return index >= 0
}
func RowGroupBoundaries(groups []parquet.RowGroup) []int64 {
b := make([]int64, len(groups))
var o int64
for i := range b {
o += groups[i].NumRows()
b[i] = o
}
return b
}
func SplitRows(rows, groups []int64) [][]int64 {
switch len(groups) {
case 0:
return nil
case 1:
return [][]int64{rows}
}
// Sanity check: max row must be less than
// the number of rows in the last group.
if rows[len(rows)-1] >= groups[len(groups)-1] {
panic(ErrSeekOutOfRange)
}
split := make([][]int64, len(groups))
var j, r int
maxRow := groups[j]
for i, rn := range rows {
if rn < maxRow {
continue
}
split[j], rows = rows[:i-r], rows[i-r:]
r = i
// Find matching group.
for x, v := range groups[j:] {
if rn >= v {
continue
}
j += x
break
}
maxRow = groups[j]
}
// Last bit.
split[j] = rows
// Subtract group offset from the row numbers,
// which makes them local to the group.
for i, g := range split[1:] {
offset := groups[i]
for n := range g {
g[n] -= offset
}
}
return split
}
var parquetValuesPool = zeropool.New(func() []parquet.Value { return nil })
func CloneParquetValues(values []parquet.Value) []parquet.Value {
p := parquetValuesPool.Get()
if l := len(values); cap(p) < l {
p = make([]parquet.Value, 0, 2*l)
}
p = p[:len(values)]
for i, v := range values {
p[i] = v.Clone()
}
return p
}
func ReleaseParquetValues(b [][]parquet.Value) {
for _, s := range b {
if cap(s) > 0 {
parquetValuesPool.Put(s)
}
}
}
var uint64valuesPool = zeropool.New(func() []uint64 { return nil })
func CloneUint64ParquetValues(values []parquet.Value) []uint64 {
uint64s := uint64valuesPool.Get()
if l := len(values); cap(uint64s) < l {
uint64s = make([]uint64, 0, 2*l)
}
uint64s = uint64s[:len(values)]
for i, v := range values {
uint64s[i] = v.Uint64()
}
return uint64s
}
func ReleaseUint64Values(b [][]uint64) {
for _, s := range b {
if len(s) > 0 {
uint64valuesPool.Put(s)
}
}
}
var uint32valuesPool = zeropool.New(func() []uint32 { return nil })
func CloneUint32ParquetValues(values []parquet.Value) []uint32 {
uint32s := uint32valuesPool.Get()
if l := len(values); cap(uint32s) < l {
uint32s = make([]uint32, 0, 2*l)
}
uint32s = uint32s[:len(values)]
for i, v := range values {
uint32s[i] = v.Uint32()
}
return uint32s
}
func ReleaseUint32Values(b [][]uint32) {
for _, s := range b {
if len(s) > 0 {
uint32valuesPool.Put(s)
}
}
}