forked from segmentio/parquet-go
/
config.go
563 lines (494 loc) · 17.3 KB
/
config.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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
package parquet
import (
"fmt"
"strings"
)
const (
DefaultCreatedBy = "github.com/segmentio/parquet-go"
DefaultColumnIndexSizeLimit = 16
DefaultColumnBufferSize = 1 * 1024 * 1024
DefaultPageBufferSize = 1 * 1024 * 1024
DefaultDataPageVersion = 2
DefaultDataPageStatistics = false
DefaultSkipPageIndex = false
DefaultSkipBloomFilters = false
)
// The FileConfig type carries configuration options for parquet files.
//
// FileConfig implements the FileOption interface so it can be used directly
// as argument to the OpenFile function when needed, for example:
//
// f, err := parquet.OpenFile(reader, size, &parquet.FileConfig{
// SkipPageIndex: true,
// SkipBloomFilters: true,
// })
//
type FileConfig struct {
SkipPageIndex bool
SkipBloomFilters bool
}
// DefaultFileConfig returns a new FileConfig value initialized with the
// default file configuration.
func DefaultFileConfig() *FileConfig {
return &FileConfig{
SkipPageIndex: DefaultSkipPageIndex,
SkipBloomFilters: DefaultSkipBloomFilters,
}
}
// NewFileConfig constructs a new file configuration applying the options passed
// as arguments.
//
// The function returns an non-nil error if some of the options carried invalid
// configuration values.
func NewFileConfig(options ...FileOption) (*FileConfig, error) {
config := DefaultFileConfig()
config.Apply(options...)
return config, config.Validate()
}
// Apply applies the given list of options to c.
func (c *FileConfig) Apply(options ...FileOption) {
for _, opt := range options {
opt.ConfigureFile(c)
}
}
// ConfigureFile applies configuration options from c to config.
func (c *FileConfig) ConfigureFile(config *FileConfig) {
*config = FileConfig{
SkipPageIndex: config.SkipPageIndex,
SkipBloomFilters: config.SkipBloomFilters,
}
}
// Validate returns a non-nil error if the configuration of c is invalid.
func (c *FileConfig) Validate() error {
return nil
}
// The ReaderConfig type carries configuration options for parquet readers.
//
// ReaderConfig implements the ReaderOption interface so it can be used directly
// as argument to the NewReader function when needed, for example:
//
// reader := parquet.NewReader(output, schema, &parquet.ReaderConfig{
// // ...
// })
//
type ReaderConfig struct {
Schema *Schema
}
// DefaultReaderConfig returns a new ReaderConfig value initialized with the
// default reader configuration.
func DefaultReaderConfig() *ReaderConfig {
return &ReaderConfig{}
}
// NewReaderConfig constructs a new reader configuration applying the options
// passed as arguments.
//
// The function returns an non-nil error if some of the options carried invalid
// configuration values.
func NewReaderConfig(options ...ReaderOption) (*ReaderConfig, error) {
config := DefaultReaderConfig()
config.Apply(options...)
return config, config.Validate()
}
// Apply applies the given list of options to c.
func (c *ReaderConfig) Apply(options ...ReaderOption) {
for _, opt := range options {
opt.ConfigureReader(c)
}
}
// ConfigureReader applies configuration options from c to config.
func (c *ReaderConfig) ConfigureReader(config *ReaderConfig) {
*config = ReaderConfig{
Schema: coalesceSchema(c.Schema, config.Schema),
}
}
// Validate returns a non-nil error if the configuration of c is invalid.
func (c *ReaderConfig) Validate() error {
return nil
}
// The WriterConfig type carries configuration options for parquet writers.
//
// WriterConfig implements the WriterOption interface so it can be used directly
// as argument to the NewWriter function when needed, for example:
//
// writer := parquet.NewWriter(output, schema, &parquet.WriterConfig{
// CreatedBy: "my test program",
// })
//
type WriterConfig struct {
CreatedBy string
ColumnPageBuffers PageBufferPool
ColumnIndexSizeLimit int
PageBufferPool PageBufferPool
PageBufferSize int
DataPageVersion int
DataPageStatistics bool
KeyValueMetadata map[string]string
Schema *Schema
BloomFilters []BloomFilterColumn
}
// DefaultWriterConfig returns a new WriterConfig value initialized with the
// default writer configuration.
func DefaultWriterConfig() *WriterConfig {
return &WriterConfig{
CreatedBy: DefaultCreatedBy,
ColumnPageBuffers: &defaultPageBufferPool,
ColumnIndexSizeLimit: DefaultColumnIndexSizeLimit,
PageBufferSize: DefaultPageBufferSize,
DataPageVersion: DefaultDataPageVersion,
DataPageStatistics: DefaultDataPageStatistics,
}
}
// NewWriterConfig constructs a new writer configuration applying the options
// passed as arguments.
//
// The function returns an non-nil error if some of the options carried invalid
// configuration values.
func NewWriterConfig(options ...WriterOption) (*WriterConfig, error) {
config := DefaultWriterConfig()
config.Apply(options...)
return config, config.Validate()
}
// Apply applies the given list of options to c.
func (c *WriterConfig) Apply(options ...WriterOption) {
for _, opt := range options {
opt.ConfigureWriter(c)
}
}
// ConfigureWriter applies configuration options from c to config.
func (c *WriterConfig) ConfigureWriter(config *WriterConfig) {
keyValueMetadata := config.KeyValueMetadata
if len(c.KeyValueMetadata) > 0 {
if keyValueMetadata == nil {
keyValueMetadata = make(map[string]string, len(c.KeyValueMetadata))
}
for k, v := range c.KeyValueMetadata {
keyValueMetadata[k] = v
}
}
*config = WriterConfig{
CreatedBy: coalesceString(c.CreatedBy, config.CreatedBy),
ColumnPageBuffers: coalescePageBufferPool(c.ColumnPageBuffers, config.ColumnPageBuffers),
ColumnIndexSizeLimit: coalesceInt(c.ColumnIndexSizeLimit, config.ColumnIndexSizeLimit),
PageBufferSize: coalesceInt(c.PageBufferSize, config.PageBufferSize),
DataPageVersion: coalesceInt(c.DataPageVersion, config.DataPageVersion),
DataPageStatistics: config.DataPageStatistics,
KeyValueMetadata: keyValueMetadata,
Schema: coalesceSchema(c.Schema, config.Schema),
BloomFilters: coalesceBloomFilters(c.BloomFilters, config.BloomFilters),
}
}
// Validate returns a non-nil error if the configuration of c is invalid.
func (c *WriterConfig) Validate() error {
const baseName = "parquet.(*WriterConfig)."
return errorInvalidConfiguration(
validateNotNil(baseName+"ColumnPageBuffers", c.ColumnPageBuffers),
validatePositiveInt(baseName+"ColumnIndexSizeLimit", c.ColumnIndexSizeLimit),
validatePositiveInt(baseName+"PageBufferSize", c.PageBufferSize),
validateOneOfInt(baseName+"DataPageVersion", c.DataPageVersion, 1, 2),
)
}
// The RowGroupConfig type carries configuration options for parquet row groups.
//
// RowGroupConfig implements the RowGroupOption interface so it can be used
// directly as argument to the NewBuffer function when needed, for example:
//
// buffer := parquet.NewBuffer(&parquet.RowGroupConfig{
// ColumnBufferSize: 8 * 1024 * 1024,
// })
//
type RowGroupConfig struct {
ColumnBufferSize int
SortingColumns []SortingColumn
Schema *Schema
}
// DefaultRowGroupConfig returns a new RowGroupConfig value initialized with the
// default row group configuration.
func DefaultRowGroupConfig() *RowGroupConfig {
return &RowGroupConfig{
ColumnBufferSize: DefaultColumnBufferSize,
}
}
// NewRowGroupConfig constructs a new row group configuration applying the
// options passed as arguments.
//
// The function returns an non-nil error if some of the options carried invalid
// configuration values.
func NewRowGroupConfig(options ...RowGroupOption) (*RowGroupConfig, error) {
config := DefaultRowGroupConfig()
config.Apply(options...)
return config, config.Validate()
}
// Validate returns a non-nil error if the configuration of c is invalid.
func (c *RowGroupConfig) Validate() error {
const baseName = "parquet.(*RowGroupConfig)."
return errorInvalidConfiguration(
validatePositiveInt(baseName+"ColumnBufferSize", c.ColumnBufferSize),
)
}
func (c *RowGroupConfig) Apply(options ...RowGroupOption) {
for _, opt := range options {
opt.ConfigureRowGroup(c)
}
}
func (c *RowGroupConfig) ConfigureRowGroup(config *RowGroupConfig) {
*config = RowGroupConfig{
ColumnBufferSize: coalesceInt(c.ColumnBufferSize, config.ColumnBufferSize),
SortingColumns: coalesceSortingColumns(c.SortingColumns, config.SortingColumns),
Schema: coalesceSchema(c.Schema, config.Schema),
}
}
// FileOption is an interface implemented by types that carry configuration
// options for parquet files.
type FileOption interface {
ConfigureFile(*FileConfig)
}
// ReaderOption is an interface implemented by types that carry configuration
// options for parquet readers.
type ReaderOption interface {
ConfigureReader(*ReaderConfig)
}
// WriterOption is an interface implemented by types that carry configuration
// options for parquet writers.
type WriterOption interface {
ConfigureWriter(*WriterConfig)
}
// RowGroupOption is an interface implemented by types that carry configuration
// options for parquet row groups.
type RowGroupOption interface {
ConfigureRowGroup(*RowGroupConfig)
}
// SkipPageIndex is a file configuration option which when set to true, prevents
// automatically reading the page index when opening a parquet file. This is
// useful as an optimization when programs know that they will not need to
// consume the page index.
//
// Defaults to false.
func SkipPageIndex(skip bool) FileOption {
return fileOption(func(config *FileConfig) { config.SkipPageIndex = skip })
}
// PageBufferSize configures the size of column page buffers on parquet writers.
//
// Note that the page buffer size refers to the in-memory buffers where pages
// are generated, not the size of pages after encoding and compression.
// This design choice was made to help control the amount of memory needed to
// read and write pages rather than controlling the space used by the encoded
// representation on disk.
//
// Defaults to 1 MiB.
func PageBufferSize(size int) WriterOption {
return writerOption(func(config *WriterConfig) { config.PageBufferSize = size })
}
// CreatedBy creates a configuration option which sets the name of the
// application that created a parquet file.
//
// By default, this information is omitted.
func CreatedBy(createdBy string) WriterOption {
return writerOption(func(config *WriterConfig) { config.CreatedBy = createdBy })
}
// ColumnPageBuffers creates a configuration option to customize the buffer pool
// used when constructing row groups. This can be used to provide on-disk buffers
// as swap space to ensure that the parquet file creation will no be bottlenecked
// on the amount of memory available.
//
// Defaults to using in-memory buffers.
func ColumnPageBuffers(buffers PageBufferPool) WriterOption {
return writerOption(func(config *WriterConfig) { config.ColumnPageBuffers = buffers })
}
// ColumnIndexSizeLimit creates a configuration option to customize the size
// limit of page boundaries recorded in column indexes.
//
// Defaults to 16.
func ColumnIndexSizeLimit(sizeLimit int) WriterOption {
return writerOption(func(config *WriterConfig) { config.ColumnIndexSizeLimit = sizeLimit })
}
// DataPageVersion creates a configuration option which configures the version of
// data pages used when creating a parquet file.
//
// Defaults to version 2.
func DataPageVersion(version int) WriterOption {
return writerOption(func(config *WriterConfig) { config.DataPageVersion = version })
}
// DataPageStatistics creates a configuration option which defines whether data
// page statistics are emitted. This option is useful when generating parquet
// files that intend to be backward compatible with older readers which may not
// have the ability to load page statistics from the column index.
//
// Defaults to false.
func DataPageStatistics(enabled bool) WriterOption {
return writerOption(func(config *WriterConfig) { config.DataPageStatistics = enabled })
}
// KeyValueMetadata creates a configuration option which adds key/value metadata
// to add to the metadata of parquet files.
//
// This option is additive, it may be used multiple times to add more than one
// key/value pair.
//
// Keys are assumed to be unique, if the same key is repeated multiple times the
// last value is retained. While the parquet format does not require unique keys,
// this design decision was made to optimize for the most common use case where
// applications leverage this extension mechanism to associate single values to
// keys. This may create incompatibilities with other parquet libraries, or may
// cause some key/value pairs to be lost when open parquet files written with
// repeated keys. We can revisit this decision if it ever becomes a blocker.
func KeyValueMetadata(key, value string) WriterOption {
return writerOption(func(config *WriterConfig) {
if config.KeyValueMetadata == nil {
config.KeyValueMetadata = map[string]string{key: value}
} else {
config.KeyValueMetadata[key] = value
}
})
}
// BloomFilters creates a configuration option which defines the bloom filters
// that parquet writers should generate.
//
// The compute and memory footprint of generating bloom filters for all columns
// of a parquet schema can be significant, so by default no filters are created
// and applications need to explicitly declare the columns that they want to
// create filters for.
func BloomFilters(filters ...BloomFilterColumn) WriterOption {
filters = append([]BloomFilterColumn{}, filters...)
return writerOption(func(config *WriterConfig) { config.BloomFilters = filters })
}
// ColumnBufferSize creates a configuration option which defines the size of
// row group column buffers.
//
// Defaults to 1 MiB.
func ColumnBufferSize(size int) RowGroupOption {
return rowGroupOption(func(config *RowGroupConfig) { config.ColumnBufferSize = size })
}
// SortingColumns creates a configuration option which defines the sorting order
// of columns in a row group.
//
// The order of sorting columns passed as argument defines the ordering
// hierarchy; when elements are equal in the first column, the second column is
// used to order rows, etc...
func SortingColumns(sortingColumns ...SortingColumn) RowGroupOption {
// Make a copy so that we do not retain the input slice generated implicitly
// for the variable argument list, and also avoid having a nil slice when
// the option is passed with no sorting columns, so we can differentiate it
// from it not being passed.
sortingColumns = append([]SortingColumn{}, sortingColumns...)
return rowGroupOption(func(config *RowGroupConfig) { config.SortingColumns = sortingColumns })
}
type fileOption func(*FileConfig)
func (opt fileOption) ConfigureFile(config *FileConfig) { opt(config) }
type readerOption func(*ReaderConfig)
func (opt readerOption) ConfigureReader(config *ReaderConfig) { opt(config) }
type writerOption func(*WriterConfig)
func (opt writerOption) ConfigureWriter(config *WriterConfig) { opt(config) }
type rowGroupOption func(*RowGroupConfig)
func (opt rowGroupOption) ConfigureRowGroup(config *RowGroupConfig) { opt(config) }
func coalesceInt(i1, i2 int) int {
if i1 != 0 {
return i1
}
return i2
}
func coalesceInt64(i1, i2 int64) int64 {
if i1 != 0 {
return i1
}
return i2
}
func coalesceString(s1, s2 string) string {
if s1 != "" {
return s1
}
return s2
}
func coalesceBytes(b1, b2 []byte) []byte {
if b1 != nil {
return b1
}
return b2
}
func coalescePageBufferPool(p1, p2 PageBufferPool) PageBufferPool {
if p1 != nil {
return p1
}
return p2
}
func coalesceSchema(s1, s2 *Schema) *Schema {
if s1 != nil {
return s1
}
return s2
}
func coalesceSortingColumns(s1, s2 []SortingColumn) []SortingColumn {
if s1 != nil {
return s1
}
return s2
}
func coalesceBloomFilters(f1, f2 []BloomFilterColumn) []BloomFilterColumn {
if f1 != nil {
return f1
}
return f2
}
func validatePositiveInt(optionName string, optionValue int) error {
if optionValue > 0 {
return nil
}
return errorInvalidOptionValue(optionName, optionValue)
}
func validatePositiveInt64(optionName string, optionValue int64) error {
if optionValue > 0 {
return nil
}
return errorInvalidOptionValue(optionName, optionValue)
}
func validateOneOfInt(optionName string, optionValue int, supportedValues ...int) error {
for _, value := range supportedValues {
if value == optionValue {
return nil
}
}
return errorInvalidOptionValue(optionName, optionValue)
}
func validateNotNil(optionName string, optionValue interface{}) error {
if optionValue != nil {
return nil
}
return errorInvalidOptionValue(optionName, optionValue)
}
func errorInvalidOptionValue(optionName string, optionValue interface{}) error {
return fmt.Errorf("invalid option value: %s: %v", optionName, optionValue)
}
func errorInvalidConfiguration(reasons ...error) error {
var err *invalidConfiguration
for _, reason := range reasons {
if reason != nil {
if err == nil {
err = new(invalidConfiguration)
}
err.reasons = append(err.reasons, reason)
}
}
if err != nil {
return err
}
return nil
}
type invalidConfiguration struct {
reasons []error
}
func (err *invalidConfiguration) Error() string {
errorMessage := new(strings.Builder)
for _, reason := range err.reasons {
errorMessage.WriteString(reason.Error())
errorMessage.WriteString("\n")
}
errorString := errorMessage.String()
if errorString != "" {
errorString = errorString[:len(errorString)-1]
}
return errorString
}
var (
_ FileOption = (*FileConfig)(nil)
_ ReaderOption = (*ReaderConfig)(nil)
_ WriterOption = (*WriterConfig)(nil)
_ RowGroupOption = (*RowGroupConfig)(nil)
)