forked from segmentio/parquet-go
/
reader_go18.go
209 lines (176 loc) · 5.29 KB
/
reader_go18.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
//go:build go1.18
package parquet
import (
"io"
"reflect"
)
// GenericReader is similar to a Reader but uses a type parameter to define the
// Go type representing the schema of rows being read.
//
// See GenericWriter for details about the benefits over the classic Reader API.
type GenericReader[T any] struct {
base Reader
read readFunc[T]
}
// NewGenericReader is like NewReader but returns GenericReader[T] suited to write
// rows of Go type T.
//
// The type parameter T should be a map, struct, or any. Any other types will
// cause a panic at runtime. Type checking is a lot more effective when the
// generic parameter is a struct type, using map and interface types is somewhat
// similar to using a Writer.
//
// If the option list may explicitly declare a schema, it must be compatible
// with the schema generated from T.
func NewGenericReader[T any](input io.ReaderAt, options ...ReaderOption) *GenericReader[T] {
c, err := NewReaderConfig(options...)
if err != nil {
panic(err)
}
f, err := openFile(input)
if err != nil {
panic(err)
}
rowGroup := fileRowGroupOf(f)
t := typeOf[T]()
if c.Schema == nil {
if t == nil {
c.Schema = rowGroup.Schema()
} else {
c.Schema = schemaOf(dereference(t))
}
}
r := &GenericReader[T]{
base: Reader{
file: reader{
schema: c.Schema,
rowGroup: rowGroup,
},
},
}
if !nodesAreEqual(c.Schema, f.schema) {
r.base.file.rowGroup = convertRowGroupTo(r.base.file.rowGroup, c.Schema)
}
r.base.read.init(r.base.file.schema, r.base.file.rowGroup)
r.read = readFuncOf[T](t, r.base.file.schema)
return r
}
func NewGenericRowGroupReader[T any](rowGroup RowGroup, options ...ReaderOption) *GenericReader[T] {
c, err := NewReaderConfig(options...)
if err != nil {
panic(err)
}
t := typeOf[T]()
if c.Schema == nil {
if t == nil {
c.Schema = rowGroup.Schema()
} else {
c.Schema = schemaOf(dereference(t))
}
}
r := &GenericReader[T]{
base: Reader{
file: reader{
schema: c.Schema,
rowGroup: rowGroup,
},
},
}
if !nodesAreEqual(c.Schema, rowGroup.Schema()) {
r.base.file.rowGroup = convertRowGroupTo(r.base.file.rowGroup, c.Schema)
}
r.base.read.init(r.base.file.schema, r.base.file.rowGroup)
r.read = readFuncOf[T](t, r.base.file.schema)
return r
}
func (r *GenericReader[T]) Reset() {
r.base.Reset()
}
// Read reads the next rows from the reader into the given rows slice up to len(rows).
//
// The returned values are safe to reuse across Read calls and do not share
// memory with the reader's underlying page buffers.
//
// The method returns the number of rows read and io.EOF when no more rows
// can be read from the reader.
func (r *GenericReader[T]) Read(rows []T) (int, error) {
return r.read(r, rows)
}
func (r *GenericReader[T]) ReadRows(rows []Row) (int, error) {
return r.base.ReadRows(rows)
}
func (r *GenericReader[T]) Schema() *Schema {
return r.base.Schema()
}
func (r *GenericReader[T]) NumRows() int64 {
return r.base.NumRows()
}
func (r *GenericReader[T]) SeekToRow(rowIndex int64) error {
return r.base.SeekToRow(rowIndex)
}
func (r *GenericReader[T]) Close() error {
return r.base.Close()
}
// readRows reads the next rows from the reader into the given rows slice up to len(rows).
//
// The returned values are safe to reuse across readRows calls and do not share
// memory with the reader's underlying page buffers.
//
// The method returns the number of rows read and io.EOF when no more rows
// can be read from the reader.
func (r *GenericReader[T]) readRows(rows []T) (int, error) {
nRequest := len(rows)
if cap(r.base.rowbuf) < nRequest {
r.base.rowbuf = make([]Row, nRequest)
} else {
r.base.rowbuf = r.base.rowbuf[:nRequest]
}
var n, nTotal int
var err error
for {
// ReadRows reads the minimum remaining rows in a column page across all columns
// of the underlying reader, unless the length of the slice passed to it is smaller.
// In that case, ReadRows will read the number of rows equal to the length of the
// given slice argument. We limit that length to never be more than requested
// because sequential reads can cross page boundaries.
n, err = r.base.ReadRows(r.base.rowbuf[:nRequest-nTotal])
if n > 0 {
schema := r.base.Schema()
for i, row := range r.base.rowbuf[:n] {
if err2 := schema.Reconstruct(&rows[nTotal+i], row); err2 != nil {
return nTotal + i, err2
}
}
}
nTotal += n
if n == 0 || nTotal == nRequest || err != nil {
break
}
}
return nTotal, err
}
var (
_ Rows = (*GenericReader[any])(nil)
_ RowReaderWithSchema = (*Reader)(nil)
_ Rows = (*GenericReader[struct{}])(nil)
_ RowReaderWithSchema = (*GenericReader[struct{}])(nil)
_ Rows = (*GenericReader[map[struct{}]struct{}])(nil)
_ RowReaderWithSchema = (*GenericReader[map[struct{}]struct{}])(nil)
)
type readFunc[T any] func(*GenericReader[T], []T) (int, error)
func readFuncOf[T any](t reflect.Type, schema *Schema) readFunc[T] {
if t == nil {
return (*GenericReader[T]).readRows
}
switch t.Kind() {
case reflect.Interface, reflect.Map:
return (*GenericReader[T]).readRows
case reflect.Struct:
return (*GenericReader[T]).readRows
case reflect.Pointer:
if e := t.Elem(); e.Kind() == reflect.Struct {
return (*GenericReader[T]).readRows
}
}
panic("cannot create reader for values of type " + t.String())
}