forked from segmentio/parquet-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi_row_group.go
290 lines (241 loc) · 5.64 KB
/
multi_row_group.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
package parquet
import (
"io"
)
// MultiRowGroup wraps multiple row groups to appear as if it was a single
// RowGroup. RowGroups must have the same schema or it will error.
func MultiRowGroup(rowGroups ...RowGroup) RowGroup {
return newMultiRowGroup(ReadModeSync, rowGroups...)
}
func newMultiRowGroup(pageReadMode ReadMode, rowGroups ...RowGroup) RowGroup {
if len(rowGroups) == 0 {
return &emptyRowGroup{}
}
if len(rowGroups) == 1 {
return rowGroups[0]
}
schema, err := compatibleSchemaOf(rowGroups)
if err != nil {
panic(err)
}
rowGroupsCopy := make([]RowGroup, len(rowGroups))
copy(rowGroupsCopy, rowGroups)
c := &multiRowGroup{
pageReadMode: pageReadMode,
}
c.init(schema, rowGroupsCopy)
return c
}
func (c *multiRowGroup) init(schema *Schema, rowGroups []RowGroup) error {
columns := make([]multiColumnChunk, len(schema.Columns()))
rowGroupColumnChunks := make([][]ColumnChunk, len(rowGroups))
for i, rowGroup := range rowGroups {
rowGroupColumnChunks[i] = rowGroup.ColumnChunks()
}
for i := range columns {
columns[i].rowGroup = c
columns[i].column = i
columns[i].chunks = make([]ColumnChunk, len(rowGroupColumnChunks))
for j, columnChunks := range rowGroupColumnChunks {
columns[i].chunks[j] = columnChunks[i]
}
}
c.schema = schema
c.rowGroups = rowGroups
c.columns = make([]ColumnChunk, len(columns))
for i := range columns {
c.columns[i] = &columns[i]
}
return nil
}
func compatibleSchemaOf(rowGroups []RowGroup) (*Schema, error) {
schema := rowGroups[0].Schema()
// Fast path: Many times all row groups have the exact same schema so a
// pointer comparison is cheaper.
samePointer := true
for _, rowGroup := range rowGroups[1:] {
if rowGroup.Schema() != schema {
samePointer = false
break
}
}
if samePointer {
return schema, nil
}
// Slow path: The schema pointers are not the same, but they still have to
// be compatible.
for _, rowGroup := range rowGroups[1:] {
if !nodesAreEqual(schema, rowGroup.Schema()) {
return nil, ErrRowGroupSchemaMismatch
}
}
return schema, nil
}
type multiRowGroup struct {
schema *Schema
rowGroups []RowGroup
columns []ColumnChunk
pageReadMode ReadMode
}
func (c *multiRowGroup) NumRows() (numRows int64) {
for _, rowGroup := range c.rowGroups {
numRows += rowGroup.NumRows()
}
return numRows
}
func (c *multiRowGroup) ColumnChunks() []ColumnChunk { return c.columns }
func (c *multiRowGroup) SortingColumns() []SortingColumn { return nil }
func (c *multiRowGroup) Schema() *Schema { return c.schema }
func (c *multiRowGroup) Rows() Rows { return newRowGroupRows(c, c.pageReadMode) }
type multiColumnChunk struct {
rowGroup *multiRowGroup
column int
chunks []ColumnChunk
}
func (c *multiColumnChunk) Type() Type {
if len(c.chunks) != 0 {
return c.chunks[0].Type() // all chunks should be of the same type
}
return nil
}
func (c *multiColumnChunk) NumValues() int64 {
n := int64(0)
for i := range c.chunks {
n += c.chunks[i].NumValues()
}
return n
}
func (c *multiColumnChunk) Column() int {
return c.column
}
func (c *multiColumnChunk) Pages() Pages {
return &multiPages{column: c}
}
func (c *multiColumnChunk) ColumnIndex() (ColumnIndex, error) {
// TODO: implement
return nil, nil
}
func (c *multiColumnChunk) OffsetIndex() (OffsetIndex, error) {
// TODO: implement
return nil, nil
}
func (c *multiColumnChunk) BloomFilter() BloomFilter {
return multiBloomFilter{c}
}
type multiBloomFilter struct{ *multiColumnChunk }
func (f multiBloomFilter) ReadAt(b []byte, off int64) (int, error) {
// TODO: add a test for this function
i := 0
for i < len(f.chunks) {
if r := f.chunks[i].BloomFilter(); r != nil {
size := r.Size()
if off < size {
break
}
off -= size
}
i++
}
if i == len(f.chunks) {
return 0, io.EOF
}
rn := int(0)
for len(b) > 0 {
if r := f.chunks[i].BloomFilter(); r != nil {
n, err := r.ReadAt(b, off)
rn += n
if err != nil {
return rn, err
}
if b = b[n:]; len(b) == 0 {
return rn, nil
}
off += int64(n)
}
i++
}
if i == len(f.chunks) {
return rn, io.EOF
}
return rn, nil
}
func (f multiBloomFilter) Size() int64 {
size := int64(0)
for _, c := range f.chunks {
if b := c.BloomFilter(); b != nil {
size += b.Size()
}
}
return size
}
func (f multiBloomFilter) Check(v Value) (bool, error) {
for _, c := range f.chunks {
if b := c.BloomFilter(); b != nil {
if ok, err := b.Check(v); ok || err != nil {
return ok, err
}
}
}
return false, nil
}
type multiPages struct {
pages Pages
index int
column *multiColumnChunk
}
func (m *multiPages) ReadPage() (Page, error) {
for {
if m.pages != nil {
p, err := m.pages.ReadPage()
if err == nil || err != io.EOF {
return p, err
}
if err := m.pages.Close(); err != nil {
return nil, err
}
m.pages = nil
}
if m.column == nil || m.index == len(m.column.chunks) {
return nil, io.EOF
}
m.pages = m.column.chunks[m.index].Pages()
m.index++
}
}
func (m *multiPages) SeekToRow(rowIndex int64) error {
if m.column == nil {
return io.ErrClosedPipe
}
if m.pages != nil {
if err := m.pages.Close(); err != nil {
return err
}
}
rowGroups := m.column.rowGroup.rowGroups
numRows := int64(0)
m.pages = nil
m.index = 0
for m.index < len(rowGroups) {
numRows = rowGroups[m.index].NumRows()
if rowIndex < numRows {
break
}
rowIndex -= numRows
m.index++
}
if m.index < len(rowGroups) {
m.pages = m.column.chunks[m.index].Pages()
m.index++
return m.pages.SeekToRow(rowIndex)
}
return nil
}
func (m *multiPages) Close() (err error) {
if m.pages != nil {
err = m.pages.Close()
}
m.pages = nil
m.index = 0
m.column = nil
return err
}