This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathmerge.go
315 lines (265 loc) · 6.8 KB
/
merge.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
package parquet
import (
"container/heap"
"fmt"
"io"
)
// MergeRowGroups constructs a row group which is a merged view of rowGroups. If
// rowGroups are sorted and the passed options include sorting, the merged row
// group will also be sorted.
//
// The function validates the input to ensure that the merge operation is
// possible, ensuring that the schemas match or can be converted to an
// optionally configured target schema passed as argument in the option list.
//
// The sorting columns of each row group are also consulted to determine whether
// the output can be represented. If sorting columns are configured on the merge
// they must be a prefix of sorting columns of all row groups being merged.
func MergeRowGroups(rowGroups []RowGroup, options ...RowGroupOption) (RowGroup, error) {
config, err := NewRowGroupConfig(options...)
if err != nil {
return nil, err
}
schema := config.Schema
if len(rowGroups) == 0 {
return newEmptyRowGroup(schema), nil
}
if schema == nil {
schema = rowGroups[0].Schema()
for _, rowGroup := range rowGroups[1:] {
if !nodesAreEqual(schema, rowGroup.Schema()) {
return nil, ErrRowGroupSchemaMismatch
}
}
}
mergedRowGroups := make([]RowGroup, len(rowGroups))
copy(mergedRowGroups, rowGroups)
for i, rowGroup := range mergedRowGroups {
if rowGroupSchema := rowGroup.Schema(); !nodesAreEqual(schema, rowGroupSchema) {
conv, err := Convert(schema, rowGroupSchema)
if err != nil {
return nil, fmt.Errorf("cannot merge row groups: %w", err)
}
mergedRowGroups[i] = ConvertRowGroup(rowGroup, conv)
}
}
m := &mergedRowGroup{sorting: config.Sorting.SortingColumns}
m.init(schema, mergedRowGroups)
if len(m.sorting) == 0 {
// When the row group has no ordering, use a simpler version of the
// merger which simply concatenates rows from each of the row groups.
// This is preferable because it makes the output deterministic, the
// heap merge may otherwise reorder rows across groups.
return &m.multiRowGroup, nil
}
for _, rowGroup := range m.rowGroups {
if !sortingColumnsHavePrefix(rowGroup.SortingColumns(), m.sorting) {
return nil, ErrRowGroupSortingColumnsMismatch
}
}
m.compare = compareRowsFuncOf(schema, m.sorting)
return m, nil
}
type mergedRowGroup struct {
multiRowGroup
sorting []SortingColumn
compare func(Row, Row) int
}
func (m *mergedRowGroup) SortingColumns() []SortingColumn {
return m.sorting
}
func (m *mergedRowGroup) Rows() Rows {
// The row group needs to respect a sorting order; the merged row reader
// uses a heap to merge rows from the row groups.
rows := make([]Rows, len(m.rowGroups))
for i := range rows {
rows[i] = m.rowGroups[i].Rows()
}
return &mergedRowGroupRows{
merge: mergedRowReader{
compare: m.compare,
readers: makeBufferedRowReaders(len(rows), func(i int) RowReader { return rows[i] }),
},
rows: rows,
schema: m.schema,
}
}
type mergedRowGroupRows struct {
merge mergedRowReader
rowIndex int64
seekToRow int64
rows []Rows
schema *Schema
}
func (r *mergedRowGroupRows) readInternal(rows []Row) (int, error) {
n, err := r.merge.ReadRows(rows)
r.rowIndex += int64(n)
return n, err
}
func (r *mergedRowGroupRows) Close() (lastErr error) {
r.merge.close()
r.rowIndex = 0
r.seekToRow = 0
for _, rows := range r.rows {
if err := rows.Close(); err != nil {
lastErr = err
}
}
return lastErr
}
func (r *mergedRowGroupRows) ReadRows(rows []Row) (int, error) {
for r.rowIndex < r.seekToRow {
n := int(r.seekToRow - r.rowIndex)
if n > len(rows) {
n = len(rows)
}
n, err := r.readInternal(rows[:n])
if err != nil {
return 0, err
}
}
return r.readInternal(rows)
}
func (r *mergedRowGroupRows) SeekToRow(rowIndex int64) error {
if rowIndex >= r.rowIndex {
r.seekToRow = rowIndex
return nil
}
return fmt.Errorf("SeekToRow: merged row reader cannot seek backward from row %d to %d", r.rowIndex, rowIndex)
}
func (r *mergedRowGroupRows) Schema() *Schema {
return r.schema
}
// MergeRowReader constructs a RowReader which creates an ordered sequence of
// all the readers using the given compare function as the ordering predicate.
func MergeRowReaders(readers []RowReader, compare func(Row, Row) int) RowReader {
return &mergedRowReader{
compare: compare,
readers: makeBufferedRowReaders(len(readers), func(i int) RowReader { return readers[i] }),
}
}
func makeBufferedRowReaders(numReaders int, readerAt func(int) RowReader) []*bufferedRowReader {
buffers := make([]bufferedRowReader, numReaders)
readers := make([]*bufferedRowReader, numReaders)
for i := range readers {
buffers[i].rows = readerAt(i)
readers[i] = &buffers[i]
}
return readers
}
type mergedRowReader struct {
compare func(Row, Row) int
readers []*bufferedRowReader
initialized bool
}
func (m *mergedRowReader) initialize() error {
for i, r := range m.readers {
switch err := r.read(); err {
case nil:
case io.EOF:
m.readers[i] = nil
default:
m.readers = nil
return err
}
}
n := 0
for _, r := range m.readers {
if r != nil {
m.readers[n] = r
n++
}
}
clear := m.readers[n:]
for i := range clear {
clear[i] = nil
}
m.readers = m.readers[:n]
heap.Init(m)
return nil
}
func (m *mergedRowReader) close() {
for _, r := range m.readers {
r.close()
}
m.readers = nil
}
func (m *mergedRowReader) ReadRows(rows []Row) (n int, err error) {
if !m.initialized {
m.initialized = true
if err := m.initialize(); err != nil {
return 0, err
}
}
for n < len(rows) && len(m.readers) != 0 {
r := m.readers[0]
rows[n] = append(rows[n][:0], r.head()...)
n++
if err := r.next(); err != nil {
if err != io.EOF {
return n, err
}
heap.Pop(m)
} else {
heap.Fix(m, 0)
}
}
if len(m.readers) == 0 {
err = io.EOF
}
return n, err
}
func (m *mergedRowReader) Less(i, j int) bool {
return m.compare(m.readers[i].head(), m.readers[j].head()) < 0
}
func (m *mergedRowReader) Len() int {
return len(m.readers)
}
func (m *mergedRowReader) Swap(i, j int) {
m.readers[i], m.readers[j] = m.readers[j], m.readers[i]
}
func (m *mergedRowReader) Push(x interface{}) {
panic("NOT IMPLEMENTED")
}
func (m *mergedRowReader) Pop() interface{} {
i := len(m.readers) - 1
r := m.readers[i]
m.readers = m.readers[:i]
return r
}
type bufferedRowReader struct {
rows RowReader
off int32
end int32
buf [10]Row
}
func (r *bufferedRowReader) head() Row {
return r.buf[r.off]
}
func (r *bufferedRowReader) next() error {
if r.off++; r.off == r.end {
r.off = 0
r.end = 0
return r.read()
}
return nil
}
func (r *bufferedRowReader) read() error {
if r.rows == nil {
return io.EOF
}
n, err := r.rows.ReadRows(r.buf[r.end:])
if err != nil && n == 0 {
return err
}
r.end += int32(n)
return nil
}
func (r *bufferedRowReader) close() {
r.rows = nil
r.off = 0
r.end = 0
}
var (
_ RowReaderWithSchema = (*mergedRowGroupRows)(nil)
)