forked from segmentio/parquet-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer_go18.go
177 lines (144 loc) · 4.46 KB
/
buffer_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
//go:build go1.18
package parquet
import (
"reflect"
"sort"
)
// GenericBuffer is similar to a Buffer but uses a type parameter to define the
// Go type representing the schema of rows in the buffer.
//
// See GenericWriter for details about the benefits over the classic Buffer API.
type GenericBuffer[T any] struct {
base Buffer
write bufferFunc[T]
}
// NewGenericBuffer is like NewBuffer but returns a GenericBuffer[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 using an interface type for the type parameter,
// then providing a schema at instantiation is required.
//
// If the option list may explicitly declare a schema, it must be compatible
// with the schema generated from T.
func NewGenericBuffer[T any](options ...RowGroupOption) *GenericBuffer[T] {
config, err := NewRowGroupConfig(options...)
if err != nil {
panic(err)
}
t := typeOf[T]()
if config.Schema == nil && t != nil {
config.Schema = schemaOf(dereference(t))
}
if config.Schema == nil {
panic("generic buffer must be instantiated with schema or concrete type.")
}
buf := &GenericBuffer[T]{
base: Buffer{config: config},
}
buf.base.configure(config.Schema)
buf.write = bufferFuncOf[T](t, config.Schema)
return buf
}
func typeOf[T any]() reflect.Type {
var v T
return reflect.TypeOf(v)
}
type bufferFunc[T any] func(*GenericBuffer[T], []T) (int, error)
func bufferFuncOf[T any](t reflect.Type, schema *Schema) bufferFunc[T] {
if t == nil {
return (*GenericBuffer[T]).writeRows
}
switch t.Kind() {
case reflect.Interface, reflect.Map:
return (*GenericBuffer[T]).writeRows
case reflect.Struct:
return makeBufferFunc[T](t, schema)
case reflect.Pointer:
if e := t.Elem(); e.Kind() == reflect.Struct {
return makeBufferFunc[T](t, schema)
}
}
panic("cannot create buffer for values of type " + t.String())
}
func makeBufferFunc[T any](t reflect.Type, schema *Schema) bufferFunc[T] {
writeRows := writeRowsFuncOf(t, schema, nil)
return func(buf *GenericBuffer[T], rows []T) (n int, err error) {
err = writeRows(buf.base.columns, makeArrayOf(rows), columnLevels{})
if err == nil {
n = len(rows)
}
return n, err
}
}
func (buf *GenericBuffer[T]) Size() int64 {
return buf.base.Size()
}
func (buf *GenericBuffer[T]) NumRows() int64 {
return buf.base.NumRows()
}
func (buf *GenericBuffer[T]) ColumnChunks() []ColumnChunk {
return buf.base.ColumnChunks()
}
func (buf *GenericBuffer[T]) ColumnBuffers() []ColumnBuffer {
return buf.base.ColumnBuffers()
}
func (buf *GenericBuffer[T]) SortingColumns() []SortingColumn {
return buf.base.SortingColumns()
}
func (buf *GenericBuffer[T]) Len() int {
return buf.base.Len()
}
func (buf *GenericBuffer[T]) Less(i, j int) bool {
return buf.base.Less(i, j)
}
func (buf *GenericBuffer[T]) Swap(i, j int) {
buf.base.Swap(i, j)
}
func (buf *GenericBuffer[T]) Reset() {
buf.base.Reset()
}
func (buf *GenericBuffer[T]) Write(rows []T) (int, error) {
if len(rows) == 0 {
return 0, nil
}
return buf.write(buf, rows)
}
func (buf *GenericBuffer[T]) WriteRows(rows []Row) (int, error) {
return buf.base.WriteRows(rows)
}
func (buf *GenericBuffer[T]) WriteRowGroup(rowGroup RowGroup) (int64, error) {
return buf.base.WriteRowGroup(rowGroup)
}
func (buf *GenericBuffer[T]) Rows() Rows {
return buf.base.Rows()
}
func (buf *GenericBuffer[T]) Schema() *Schema {
return buf.base.Schema()
}
func (buf *GenericBuffer[T]) writeRows(rows []T) (int, error) {
if cap(buf.base.rowbuf) < len(rows) {
buf.base.rowbuf = make([]Row, len(rows))
} else {
buf.base.rowbuf = buf.base.rowbuf[:len(rows)]
}
defer clearRows(buf.base.rowbuf)
schema := buf.base.Schema()
for i := range rows {
buf.base.rowbuf[i] = schema.Deconstruct(buf.base.rowbuf[i], &rows[i])
}
return buf.base.WriteRows(buf.base.rowbuf)
}
var (
_ RowGroup = (*GenericBuffer[any])(nil)
_ RowGroupWriter = (*GenericBuffer[any])(nil)
_ sort.Interface = (*GenericBuffer[any])(nil)
_ RowGroup = (*GenericBuffer[struct{}])(nil)
_ RowGroupWriter = (*GenericBuffer[struct{}])(nil)
_ sort.Interface = (*GenericBuffer[struct{}])(nil)
_ RowGroup = (*GenericBuffer[map[struct{}]struct{}])(nil)
_ RowGroupWriter = (*GenericBuffer[map[struct{}]struct{}])(nil)
_ sort.Interface = (*GenericBuffer[map[struct{}]struct{}])(nil)
)