-
Notifications
You must be signed in to change notification settings - Fork 53
/
page_v1.go
196 lines (158 loc) · 4.82 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
package goparquet
import (
"bytes"
"io"
"github.com/fraugster/parquet-go/parquet"
"github.com/pkg/errors"
)
type dataPageReaderV1 struct {
ph *parquet.PageHeader
valuesCount int32
encoding parquet.Encoding
dDecoder, rDecoder levelDecoder
valuesDecoder valuesDecoder
fn getValueDecoderFn
position int
}
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, errors.Wrap(err, "read repetition levels failed")
}
var notNull int
dLevel, notNull, err = decodePackedArray(dp.dDecoder, size)
if err != nil {
return nil, nil, nil, errors.Wrap(err, "read definition levels failed")
}
val := make([]interface{}, notNull)
if notNull != 0 {
if n, err := dp.valuesDecoder.decodeValues(val); err != nil {
return nil, nil, nil, errors.Wrapf(err, "read values from page failed, need %d value read %d", notNull, n)
}
}
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) (err error) {
if ph.DataPageHeader == nil {
return errors.Errorf("null DataPageHeader in %+v", ph)
}
if dp.valuesCount = ph.DataPageHeader.NumValues; dp.valuesCount < 0 {
return errors.Errorf("negative NumValues in DATA_PAGE: %d", dp.valuesCount)
}
reader, err := createDataReader(r, codec, ph.GetCompressedPageSize(), ph.GetUncompressedPageSize())
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 {
col *Column
codec parquet.CompressionCodec
dictionary bool
}
func (dp *dataPageWriterV1) init(schema SchemaWriter, col *Column, codec parquet.CompressionCodec) error {
dp.col = col
dp.codec = codec
return nil
}
func (dp *dataPageWriterV1) getHeader(comp, unComp int) *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: nil,
DataPageHeader: &parquet.DataPageHeader{
NumValues: dp.col.data.values.numValues() + dp.col.data.values.nullValueCount(),
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: nil,
},
}
return ph
}
func (dp *dataPageWriterV1) write(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.col.data.rLevels); 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.col.data.dLevels); err != nil {
return 0, 0, err
}
}
enc := dp.col.data.encoding()
if dp.dictionary {
enc = parquet.Encoding_RLE_DICTIONARY
}
encoder, err := getValuesEncoder(enc, dp.col.Element(), dp.col.data.values)
if err != nil {
return 0, 0, err
}
err = encodeValue(dataBuf, encoder, dp.col.data.values.assemble())
if err != nil {
return 0, 0, err
}
comp, err := compressBlock(dataBuf.Bytes(), dp.codec)
if err != nil {
return 0, 0, errors.Wrapf(err, "compressing data failed with %s method", dp.codec)
}
compSize, unCompSize := len(comp), len(dataBuf.Bytes())
header := dp.getHeader(compSize, unCompSize)
if err := writeThrift(header, w); err != nil {
return 0, 0, err
}
return compSize, unCompSize, writeFull(w, comp)
}
func newDataPageV1Writer(useDict bool) pageWriter {
return &dataPageWriterV1{
dictionary: useDict,
}
}