-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
row_group_writer.go
255 lines (230 loc) · 8.1 KB
/
row_group_writer.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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package file
import (
"github.com/apache/arrow/go/v14/parquet"
"github.com/apache/arrow/go/v14/parquet/internal/encryption"
"github.com/apache/arrow/go/v14/parquet/internal/utils"
"github.com/apache/arrow/go/v14/parquet/metadata"
"golang.org/x/xerrors"
)
// RowGroupWriter is the base interface for writing rowgroups, the actual writer
// will be either the SerialRowGroupWriter or the BufferedRowGroupWriter
type RowGroupWriter interface {
// Returns the number of columns for this row group writer
NumColumns() int
// returns the current number of rows that have been written.
// Returns an error if they are unequal between columns that have been written so far
NumRows() (int, error)
// The total compressed bytes so
TotalCompressedBytes() int64
// the total bytes written and flushed out
TotalBytesWritten() int64
// Closes any unclosed columnwriters, and closes the rowgroup, writing out
// the metadata. subsequent calls have no effect
// returns an error if columns contain unequal numbers of rows.
Close() error
// Buffered returns true if it's a BufferedRowGroupWriter and false for a
// SerialRowGroupWriter
Buffered() bool
}
// SerialRowGroupWriter expects each column to be written one after the other,
// data is flushed every time NextColumn is called and will panic if there is
// an unequal number of rows written per column.
type SerialRowGroupWriter interface {
RowGroupWriter
NextColumn() (ColumnChunkWriter, error)
// returns the current column being built, if buffered it will equal NumColumns
// if serialized then it will return which column is currenly being written
CurrentColumn() int
}
// BufferedRowGroupWriter allows writing to multiple columns simultaneously, data
// will not be flushed to the underlying writer until closing the RowGroupWriter.
//
// All columns must have equal numbers of rows before closing the row group or it will panic.
type BufferedRowGroupWriter interface {
RowGroupWriter
Column(i int) (ColumnChunkWriter, error)
}
type rowGroupWriter struct {
sink utils.WriterTell
metadata *metadata.RowGroupMetaDataBuilder
props *parquet.WriterProperties
bytesWritten int64
closed bool
ordinal int16
nextColumnIdx int
nrows int
buffered bool
fileEncryptor encryption.FileEncryptor
columnWriters []ColumnChunkWriter
pager PageWriter
}
func newRowGroupWriter(sink utils.WriterTell, metadata *metadata.RowGroupMetaDataBuilder, ordinal int16, props *parquet.WriterProperties, buffered bool, fileEncryptor encryption.FileEncryptor) *rowGroupWriter {
ret := &rowGroupWriter{
sink: sink,
metadata: metadata,
props: props,
ordinal: ordinal,
buffered: buffered,
fileEncryptor: fileEncryptor,
}
if buffered {
ret.initColumns()
} else {
ret.columnWriters = []ColumnChunkWriter{nil}
}
return ret
}
func (rg *rowGroupWriter) Buffered() bool { return rg.buffered }
func (rg *rowGroupWriter) checkRowsWritten() error {
if len(rg.columnWriters) == 0 {
return nil
}
if !rg.buffered && rg.columnWriters[0] != nil {
current := rg.columnWriters[0].RowsWritten()
if rg.nrows == 0 {
rg.nrows = current
} else if rg.nrows != current {
return xerrors.Errorf("row mismatch for unbuffered row group: %d, count expected: %d, actual: %d", rg.ordinal, current, rg.nrows)
}
} else if rg.buffered {
current := rg.columnWriters[0].RowsWritten()
for i, wr := range rg.columnWriters[1:] {
if current != wr.RowsWritten() {
return xerrors.Errorf("row mismatch for buffered row group: %d, column: %d, count expected: %d, actual: %d", rg.ordinal, i+1, current, wr.RowsWritten())
}
}
rg.nrows = current
}
return nil
}
func (rg *rowGroupWriter) NumColumns() int { return rg.metadata.NumColumns() }
func (rg *rowGroupWriter) NumRows() (int, error) {
err := rg.checkRowsWritten()
return rg.nrows, err
}
func (rg *rowGroupWriter) NextColumn() (ColumnChunkWriter, error) {
if rg.buffered {
panic("next column is not supported when a rowgroup is written by size")
}
if rg.columnWriters[0] != nil {
if err := rg.checkRowsWritten(); err != nil {
return nil, err
}
}
// throw an error if more columns are being written
colMeta := rg.metadata.NextColumnChunk()
if rg.columnWriters[0] != nil {
if err := rg.columnWriters[0].Close(); err != nil {
return nil, err
}
rg.bytesWritten += rg.columnWriters[0].TotalBytesWritten()
}
rg.nextColumnIdx++
path := colMeta.Descr().Path()
var (
metaEncryptor encryption.Encryptor
dataEncryptor encryption.Encryptor
)
if rg.fileEncryptor != nil {
metaEncryptor = rg.fileEncryptor.GetColumnMetaEncryptor(path)
dataEncryptor = rg.fileEncryptor.GetColumnDataEncryptor(path)
}
if rg.pager == nil {
var err error
rg.pager, err = NewPageWriter(rg.sink, rg.props.CompressionFor(path), rg.props.CompressionLevelFor(path), colMeta, rg.ordinal, int16(rg.nextColumnIdx-1), rg.props.Allocator(), false, metaEncryptor, dataEncryptor)
if err != nil {
return nil, err
}
} else {
rg.pager.Reset(rg.sink, rg.props.CompressionFor(path), rg.props.CompressionLevelFor(path), colMeta, rg.ordinal, int16(rg.nextColumnIdx-1), metaEncryptor, dataEncryptor)
}
rg.columnWriters[0] = NewColumnChunkWriter(colMeta, rg.pager, rg.props)
return rg.columnWriters[0], nil
}
func (rg *rowGroupWriter) Column(i int) (ColumnChunkWriter, error) {
if !rg.buffered {
panic("column is only supported when a bufferedrowgroup is being written")
}
if i >= 0 && i < len(rg.columnWriters) {
return rg.columnWriters[i], nil
}
return nil, xerrors.Errorf("invalid column number requested: %d", i)
}
func (rg *rowGroupWriter) CurrentColumn() int { return rg.metadata.CurrentColumn() }
func (rg *rowGroupWriter) TotalCompressedBytes() int64 {
total := int64(0)
for _, wr := range rg.columnWriters {
if wr != nil {
total += wr.TotalCompressedBytes()
}
}
return total
}
func (rg *rowGroupWriter) TotalBytesWritten() int64 {
total := int64(0)
for _, wr := range rg.columnWriters {
if wr != nil {
total += wr.TotalBytesWritten()
}
}
return total + rg.bytesWritten
}
func (rg *rowGroupWriter) Close() error {
if !rg.closed {
rg.closed = true
if err := rg.checkRowsWritten(); err != nil {
return err
}
for _, wr := range rg.columnWriters {
if wr != nil {
if err := wr.Close(); err != nil {
return err
}
rg.bytesWritten += wr.TotalBytesWritten()
}
}
rg.columnWriters = nil
rg.metadata.SetNumRows(rg.nrows)
rg.metadata.Finish(rg.bytesWritten, rg.ordinal)
}
return nil
}
func (rg *rowGroupWriter) initColumns() error {
if rg.columnWriters == nil {
rg.columnWriters = make([]ColumnChunkWriter, 0, rg.NumColumns())
}
for i := 0; i < rg.NumColumns(); i++ {
colMeta := rg.metadata.NextColumnChunk()
path := colMeta.Descr().Path()
var (
metaEncryptor encryption.Encryptor
dataEncryptor encryption.Encryptor
)
if rg.fileEncryptor != nil {
metaEncryptor = rg.fileEncryptor.GetColumnMetaEncryptor(path)
dataEncryptor = rg.fileEncryptor.GetColumnDataEncryptor(path)
}
pager, err := NewPageWriter(rg.sink, rg.props.CompressionFor(path), rg.props.CompressionLevelFor(path), colMeta, rg.ordinal, int16(rg.nextColumnIdx), rg.props.Allocator(), rg.buffered, metaEncryptor, dataEncryptor)
if err != nil {
return err
}
rg.nextColumnIdx++
rg.columnWriters = append(rg.columnWriters, NewColumnChunkWriter(colMeta, pager, rg.props))
}
return nil
}