-
Notifications
You must be signed in to change notification settings - Fork 53
/
page_v1.go
231 lines (188 loc) · 5.69 KB
/
page_v1.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
package goparquet
import (
"bytes"
"context"
"errors"
"fmt"
"hash/crc32"
"io"
"math/bits"
"github.com/fraugster/parquet-go/parquet"
)
type dataPageReaderV1 struct {
ph *parquet.PageHeader
valuesCount int32
encoding parquet.Encoding
dDecoder, rDecoder levelDecoder
valuesDecoder valuesDecoder
fn getValueDecoderFn
position int
alloc *allocTracker
}
func (dp *dataPageReaderV1) numValues() int32 {
return dp.valuesCount
}
func (dp *dataPageReaderV1) readValues(size int) (values []interface{}, dLevel *packedArray, rLevel *packedArray, err error) {
if rem := int(dp.valuesCount) - dp.position; rem < size {
size = rem
}
if size == 0 {
return nil, nil, nil, nil
}
rLevel, _, err = decodePackedArray(dp.rDecoder, size)
if err != nil {
return nil, nil, nil, fmt.Errorf("read repetition levels failed: %w", err)
}
var notNull int
dLevel, notNull, err = decodePackedArray(dp.dDecoder, size)
if err != nil {
return nil, nil, nil, fmt.Errorf("read definition levels failed: %w", err)
}
val := make([]interface{}, notNull)
if notNull != 0 {
if n, err := dp.valuesDecoder.decodeValues(val); err != nil {
return nil, nil, nil, fmt.Errorf("read values from page failed, need %d value read %d: %w", notNull, n, err)
}
}
dp.position += size
return val, dLevel, rLevel, nil
}
func (dp *dataPageReaderV1) init(dDecoder, rDecoder getLevelDecoder, values getValueDecoderFn) error {
if dp.ph.DataPageHeader == nil {
return errors.New("page header is missing data page header")
}
var err error
dp.rDecoder, err = rDecoder(dp.ph.DataPageHeader.RepetitionLevelEncoding)
if err != nil {
return err
}
dp.dDecoder, err = dDecoder(dp.ph.DataPageHeader.DefinitionLevelEncoding)
if err != nil {
return err
}
dp.fn = values
dp.position = 0
return nil
}
func (dp *dataPageReaderV1) read(r io.Reader, ph *parquet.PageHeader, codec parquet.CompressionCodec, validateCRC bool) (err error) {
if ph.DataPageHeader == nil {
return fmt.Errorf("null DataPageHeader in %+v", ph)
}
if dp.valuesCount = ph.DataPageHeader.NumValues; dp.valuesCount < 0 {
return fmt.Errorf("negative NumValues in DATA_PAGE: %d", dp.valuesCount)
}
dataPageBlock, err := readPageBlock(r, codec, ph.GetCompressedPageSize(), ph.GetUncompressedPageSize(), validateCRC, ph.Crc, dp.alloc)
if err != nil {
return err
}
reader, err := newBlockReader(dataPageBlock, codec, ph.GetCompressedPageSize(), ph.GetUncompressedPageSize(), dp.alloc)
if err != nil {
return err
}
dp.encoding = ph.DataPageHeader.Encoding
dp.ph = ph
if dp.valuesDecoder, err = dp.fn(dp.encoding); err != nil {
return err
}
if err := dp.rDecoder.initSize(reader); err != nil {
return err
}
if err := dp.dDecoder.initSize(reader); err != nil {
return err
}
return dp.valuesDecoder.init(reader)
}
type dataPageWriterV1 struct {
dictValues []interface{}
col *Column
codec parquet.CompressionCodec
page *dataPage
dictionary bool
enableCRC bool
}
func (dp *dataPageWriterV1) init(col *Column, codec parquet.CompressionCodec) error {
dp.col = col
dp.codec = codec
return nil
}
func (dp *dataPageWriterV1) getHeader(comp, unComp int, pageStats *parquet.Statistics, crc32Checksum *int32) *parquet.PageHeader {
enc := dp.col.data.encoding()
if dp.dictionary {
enc = parquet.Encoding_RLE_DICTIONARY
}
ph := &parquet.PageHeader{
Type: parquet.PageType_DATA_PAGE,
UncompressedPageSize: int32(unComp),
CompressedPageSize: int32(comp),
Crc: crc32Checksum,
DataPageHeader: &parquet.DataPageHeader{
NumValues: int32(dp.page.numValues) + int32(dp.page.nullValues),
Encoding: enc,
// Only RLE supported for now, not sure if we need support for more encoding
DefinitionLevelEncoding: parquet.Encoding_RLE,
RepetitionLevelEncoding: parquet.Encoding_RLE,
Statistics: pageStats,
},
}
return ph
}
func (dp *dataPageWriterV1) write(ctx context.Context, w io.Writer) (int, int, error) {
dataBuf := &bytes.Buffer{}
// Only write repetition value higher than zero
if dp.col.MaxRepetitionLevel() > 0 {
if err := encodeLevelsV1(dataBuf, dp.col.MaxRepetitionLevel(), dp.page.rL); err != nil {
return 0, 0, err
}
}
// Only write definition value higher than zero
if dp.col.MaxDefinitionLevel() > 0 {
if err := encodeLevelsV1(dataBuf, dp.col.MaxDefinitionLevel(), dp.page.dL); err != nil {
return 0, 0, err
}
}
enc := dp.col.data.encoding()
if dp.dictionary {
enc = parquet.Encoding_RLE_DICTIONARY
}
if dp.dictionary {
encoder := newDictEncoder(dataBuf, bits.Len(uint(len(dp.dictValues))))
if err := encoder.encodeIndices(dp.page.indexList); err != nil {
return 0, 0, err
}
if err := encoder.Close(); err != nil {
return 0, 0, err
}
} else {
encoder, err := getValuesEncoder(enc, dp.col.Element(), dp.dictValues)
if err != nil {
return 0, 0, err
}
err = encodeValue(dataBuf, encoder, dp.page.values)
if err != nil {
return 0, 0, err
}
}
comp, err := compressBlock(dataBuf.Bytes(), dp.codec)
if err != nil {
return 0, 0, fmt.Errorf("compressing data failed with %s method: %w", dp.codec, err)
}
compSize, unCompSize := len(comp), len(dataBuf.Bytes())
var crc32Checksum *int32
if dp.enableCRC {
v := int32(crc32.ChecksumIEEE(comp))
crc32Checksum = &v
}
header := dp.getHeader(compSize, unCompSize, dp.page.stats, crc32Checksum)
if err := writeThrift(ctx, header, w); err != nil {
return 0, 0, err
}
return compSize, unCompSize, writeFull(w, comp)
}
func newDataPageV1Writer(useDict bool, dictValues []interface{}, page *dataPage, enableCRC bool) pageWriter {
return &dataPageWriterV1{
dictionary: useDict,
dictValues: dictValues,
page: page,
enableCRC: enableCRC,
}
}