forked from parquet-go/parquet-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolumn_chunk.go
More file actions
347 lines (299 loc) · 9.57 KB
/
Copy pathcolumn_chunk.go
File metadata and controls
347 lines (299 loc) · 9.57 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
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
package parquet
import (
"errors"
"io"
)
var (
ErrMissingBloomFilter = errors.New("missing bloom filter")
ErrMissingColumnIndex = errors.New("missing column index")
ErrMissingOffsetIndex = errors.New("missing offset index")
)
// The ColumnChunk interface represents individual columns of a row group.
type ColumnChunk interface {
// Returns the column type.
Type() Type
// Returns the index of this column in its parent row group.
Column() int
// Returns a reader exposing the pages of the column.
Pages() Pages
// Returns the components of the page index for this column chunk,
// containing details about the content and location of pages within the
// chunk.
//
// Note that the returned value may be the same across calls to these
// methods, programs must treat those as read-only.
//
// If the column chunk does not have a column or offset index, the methods return
// ErrMissingColumnIndex or ErrMissingOffsetIndex respectively.
//
// Prior to v0.20, these methods did not return an error because the page index
// for a file was either fully read when the file was opened, or skipped
// completely using the parquet.SkipPageIndex option. Version v0.20 introduced a
// change that the page index can be read on-demand at any time, even if a file
// was opened with the parquet.SkipPageIndex option. Since reading the page index
// can fail, these methods now return an error.
ColumnIndex() (ColumnIndex, error)
OffsetIndex() (OffsetIndex, error)
BloomFilter() BloomFilter
// Returns the number of values in the column chunk.
//
// This quantity may differ from the number of rows in the parent row group
// because repeated columns may hold zero or more values per row.
NumValues() int64
}
// AsyncColumnChunk returns a ColumnChunk that reads pages asynchronously.
func AsyncColumnChunk(columnChunk ColumnChunk) ColumnChunk {
return &asyncColumnChunk{columnChunk}
}
type asyncColumnChunk struct {
ColumnChunk
}
func (c *asyncColumnChunk) Pages() Pages {
return AsyncPages(c.ColumnChunk.Pages())
}
// NewColumnChunkRowReader creates a new ColumnChunkRowReader for the given
// column chunks.
func NewColumnChunkRowReader(columns []ColumnChunk) RowReadSeekCloser {
return newRowGroupRows(nil, columns, defaultValueBufferSize)
}
// ColumnChunkValueReader is an interface for reading values from a column chunk.
type ColumnChunkValueReader interface {
ValueReader
RowSeeker
io.Closer
}
// NewColumnChunkValueReader creates a new ColumnChunkValueReader for the given
// column chunk.
func NewColumnChunkValueReader(column ColumnChunk) ColumnChunkValueReader {
return &columnChunkValueReader{pages: column.Pages()}
}
type columnChunkValueReader struct {
pages Pages
page Page
values ValueReader
detach bool
}
func (r *columnChunkValueReader) clear() {
if r.page != nil {
if r.detach {
releaseAndDetachValues(r.page)
} else {
Release(r.page)
}
r.page = nil
r.values = nil
}
}
func (r *columnChunkValueReader) Reset() {
if r.pages != nil {
// Ignore errors because we are resetting the reader, if the error
// persists we will see it on the next read, and otherwise we can
// read back from the beginning.
r.pages.SeekToRow(0)
}
r.clear()
}
func (r *columnChunkValueReader) Close() error {
var err error
if r.pages != nil {
err = r.pages.Close()
r.pages = nil
}
r.clear()
return err
}
func (r *columnChunkValueReader) ReadValues(values []Value) (int, error) {
if r.pages == nil {
return 0, io.EOF
}
for {
if r.values == nil {
p, err := r.pages.ReadPage()
if err != nil {
return 0, err
}
r.page = p
r.values = p.Values()
}
n, err := r.values.ReadValues(values)
if n > 0 {
return n, nil
}
if err == nil {
return 0, io.ErrNoProgress
}
if err != io.EOF {
return 0, err
}
r.clear()
}
}
func (r *columnChunkValueReader) SeekToRow(rowIndex int64) error {
if r.pages == nil {
return io.ErrClosedPipe
}
if err := r.pages.SeekToRow(rowIndex); err != nil {
return err
}
r.clear()
return nil
}
type pageAndValueWriter interface {
PageWriter
ValueWriter
}
type readRowsFunc func(*rowGroupRows, []Row, byte) (int, error)
func readRowsFuncOf(node Node, columnIndex int, repetitionDepth byte) (int, readRowsFunc) {
var read readRowsFunc
if node.Repeated() {
repetitionDepth++
}
if node.Leaf() {
columnIndex, read = readRowsFuncOfLeaf(columnIndex, repetitionDepth)
} else {
columnIndex, read = readRowsFuncOfGroup(node, columnIndex, repetitionDepth)
}
if node.Repeated() {
read = readRowsFuncOfRepeated(read, repetitionDepth)
}
return columnIndex, read
}
//go:noinline
func readRowsFuncOfRepeated(read readRowsFunc, repetitionDepth byte) readRowsFunc {
return func(r *rowGroupRows, rows []Row, repetitionLevel byte) (int, error) {
for i := range rows {
// Repeated columns have variable number of values, we must process
// them one row at a time because we cannot predict how many values
// need to be consumed in each iteration.
row := rows[i : i+1]
// The first pass looks for values marking the beginning of a row by
// having a repetition level equal to the current level.
n, err := read(r, row, repetitionLevel)
if err != nil {
// The error here may likely be io.EOF, the read function may
// also have successfully read a row, which is indicated by a
// non-zero count. In this case, we increment the index to
// indicate to the caller than rows up to i+1 have been read.
if n > 0 {
i++
}
return i, err
}
// The read function may return no errors and also read no rows in
// case where it had more values to read but none corresponded to
// the current repetition level. This is an indication that we will
// not be able to read more rows at this stage, we must return to
// the caller to let it set the repetition level to its current
// depth, which may allow us to read more values when called again.
if n == 0 {
return i, nil
}
// When we reach this stage, we have successfully read the first
// values of a row of repeated columns. We continue consuming more
// repeated values until we get the indication that we consumed
// them all (the read function returns zero and no errors).
for {
n, err := read(r, row, repetitionDepth)
if err != nil {
return i + 1, err
}
if n == 0 {
break
}
}
}
return len(rows), nil
}
}
//go:noinline
func readRowsFuncOfGroup(node Node, columnIndex int, repetitionDepth byte) (int, readRowsFunc) {
fields := node.Fields()
// Empty groups (groups with no fields) are valid structural elements
// that don't contain column data. This function shouldn't be called in
// practice since empty groups have no leaf columns to read from.
if len(fields) == 0 {
return columnIndex, func(r *rowGroupRows, rows []Row, repetitionLevel byte) (int, error) {
// Return 0 since there are no columns to read
return 0, nil
}
}
if len(fields) == 1 {
// Small optimization for a somewhat common case of groups with a single
// column (like nested list elements for example); there is no need to
// loop over the group of a single element, we can simply skip to calling
// the inner read function.
return readRowsFuncOf(fields[0], columnIndex, repetitionDepth)
}
group := make([]readRowsFunc, len(fields))
for i := range group {
columnIndex, group[i] = readRowsFuncOf(fields[i], columnIndex, repetitionDepth)
}
return columnIndex, func(r *rowGroupRows, rows []Row, repetitionLevel byte) (int, error) {
// When reading a group, we use the first column as an indicator of how
// may rows can be read during this call.
n, err := group[0](r, rows, repetitionLevel)
if n > 0 {
// Read values for all rows that the group is able to consume.
// Getting io.EOF from calling the read functions indicate that
// we consumed all values of that particular column, but there may
// be more to read in other columns, therefore we must always read
// all columns and cannot stop on the first error.
for _, read := range group[1:] {
_, err2 := read(r, rows[:n], repetitionLevel)
if err2 != nil && err2 != io.EOF {
return 0, err2
}
}
}
return n, err
}
}
//go:noinline
func readRowsFuncOfLeaf(columnIndex int, repetitionDepth byte) (int, readRowsFunc) {
var read readRowsFunc
if repetitionDepth == 0 {
read = func(r *rowGroupRows, rows []Row, _ byte) (int, error) {
// When the repetition depth is zero, we know that there is exactly
// one value per row for this column, and therefore we can consume
// as many values as there are rows to fill.
col := &r.columns[columnIndex]
buf := r.buffer(columnIndex)
for i := range rows {
if col.offset == col.length {
n, err := col.reader.ReadValues(buf)
col.offset = 0
col.length = int32(n)
if n == 0 && err != nil {
return 0, err
}
}
rows[i] = append(rows[i], buf[col.offset])
col.offset++
}
return len(rows), nil
}
} else {
read = func(r *rowGroupRows, rows []Row, repetitionLevel byte) (int, error) {
// When the repetition depth is not zero, we know that we will be
// called with a single row as input. We attempt to read at most one
// value of a single row and return to the caller.
col := &r.columns[columnIndex]
buf := r.buffer(columnIndex)
if col.offset == col.length {
n, err := col.reader.ReadValues(buf)
col.offset = 0
col.length = int32(n)
if n == 0 && err != nil {
return 0, err
}
}
if buf[col.offset].repetitionLevel != repetitionLevel {
return 0, nil
}
rows[0] = append(rows[0], buf[col.offset])
col.offset++
return 1, nil
}
}
return columnIndex + 1, read
}