-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
schema.go
301 lines (266 loc) · 7.13 KB
/
schema.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
// 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 arrow
import (
"fmt"
"sort"
"strings"
"github.com/apache/arrow/go/v17/arrow/endian"
)
type Metadata struct {
keys []string
values []string
}
func NewMetadata(keys, values []string) Metadata {
if len(keys) != len(values) {
panic("arrow: len mismatch")
}
n := len(keys)
if n == 0 {
return Metadata{}
}
md := Metadata{
keys: make([]string, n),
values: make([]string, n),
}
copy(md.keys, keys)
copy(md.values, values)
return md
}
func MetadataFrom(kv map[string]string) Metadata {
md := Metadata{
keys: make([]string, 0, len(kv)),
values: make([]string, 0, len(kv)),
}
for k := range kv {
md.keys = append(md.keys, k)
}
sort.Strings(md.keys)
for _, k := range md.keys {
md.values = append(md.values, kv[k])
}
return md
}
func (md Metadata) Len() int { return len(md.keys) }
func (md Metadata) Keys() []string { return md.keys }
func (md Metadata) Values() []string { return md.values }
func (md Metadata) ToMap() map[string]string {
m := make(map[string]string, len(md.keys))
for i := range md.keys {
m[md.keys[i]] = md.values[i]
}
return m
}
func (md Metadata) String() string {
o := new(strings.Builder)
fmt.Fprintf(o, "[")
for i := range md.keys {
if i > 0 {
fmt.Fprintf(o, ", ")
}
fmt.Fprintf(o, "%q: %q", md.keys[i], md.values[i])
}
fmt.Fprintf(o, "]")
return o.String()
}
// FindKey returns the index of the key-value pair with the provided key name,
// or -1 if such a key does not exist.
func (md Metadata) FindKey(k string) int {
for i, v := range md.keys {
if v == k {
return i
}
}
return -1
}
// GetValue returns the value associated with the provided key name.
// If the key does not exist, the second return value is false.
func (md Metadata) GetValue(k string) (string, bool) {
i := md.FindKey(k)
if i < 0 {
return "", false
}
return md.values[i], true
}
func (md Metadata) clone() Metadata {
if len(md.keys) == 0 {
return Metadata{}
}
o := Metadata{
keys: make([]string, len(md.keys)),
values: make([]string, len(md.values)),
}
copy(o.keys, md.keys)
copy(o.values, md.values)
return o
}
func (md Metadata) sortedIndices() []int {
idxes := make([]int, len(md.keys))
for i := range idxes {
idxes[i] = i
}
sort.Slice(idxes, func(i, j int) bool {
return md.keys[idxes[i]] < md.keys[idxes[j]]
})
return idxes
}
func (md Metadata) Equal(rhs Metadata) bool {
if md.Len() != rhs.Len() {
return false
}
idxes := md.sortedIndices()
rhsIdxes := rhs.sortedIndices()
for i := range idxes {
j := idxes[i]
k := rhsIdxes[i]
if md.keys[j] != rhs.keys[k] || md.values[j] != rhs.values[k] {
return false
}
}
return true
}
// Schema is a sequence of Field values, describing the columns of a table or
// a record batch.
type Schema struct {
fields []Field
index map[string][]int
meta Metadata
endianness endian.Endianness
}
// NewSchema returns a new Schema value from the slice of fields and metadata.
//
// NewSchema panics if there is a field with an invalid DataType.
func NewSchema(fields []Field, metadata *Metadata) *Schema {
return NewSchemaWithEndian(fields, metadata, endian.NativeEndian)
}
func NewSchemaWithEndian(fields []Field, metadata *Metadata, e endian.Endianness) *Schema {
sc := &Schema{
fields: make([]Field, 0, len(fields)),
index: make(map[string][]int, len(fields)),
endianness: e,
}
if metadata != nil {
sc.meta = metadata.clone()
}
for i, field := range fields {
if field.Type == nil {
panic("arrow: field with nil DataType")
}
sc.fields = append(sc.fields, field)
sc.index[field.Name] = append(sc.index[field.Name], i)
}
return sc
}
func (sc *Schema) WithEndianness(e endian.Endianness) *Schema {
return NewSchemaWithEndian(sc.fields, &sc.meta, e)
}
func (sc *Schema) Endianness() endian.Endianness { return sc.endianness }
func (sc *Schema) IsNativeEndian() bool { return sc.endianness == endian.NativeEndian }
func (sc *Schema) Metadata() Metadata { return sc.meta }
func (sc *Schema) Fields() []Field {
fields := make([]Field, len(sc.fields))
copy(fields, sc.fields)
return fields
}
func (sc *Schema) Field(i int) Field { return sc.fields[i] }
func (sc *Schema) NumFields() int { return len(sc.fields) }
func (sc *Schema) FieldsByName(n string) ([]Field, bool) {
indices, ok := sc.index[n]
if !ok {
return nil, ok
}
fields := make([]Field, 0, len(indices))
for _, v := range indices {
fields = append(fields, sc.fields[v])
}
return fields, ok
}
// FieldIndices returns the indices of the named field or nil.
func (sc *Schema) FieldIndices(n string) []int {
return sc.index[n]
}
func (sc *Schema) HasField(n string) bool { return len(sc.FieldIndices(n)) > 0 }
func (sc *Schema) HasMetadata() bool { return len(sc.meta.keys) > 0 }
// Equal returns whether two schema are equal.
// Equal does not compare the metadata.
func (sc *Schema) Equal(o *Schema) bool {
switch {
case sc == o:
return true
case sc == nil || o == nil:
return false
case len(sc.fields) != len(o.fields):
return false
case sc.endianness != o.endianness:
return false
}
for i := range sc.fields {
if !sc.fields[i].Equal(o.fields[i]) {
return false
}
}
return true
}
// AddField adds a field at the given index and return a new schema.
func (s *Schema) AddField(i int, field Field) (*Schema, error) {
if i < 0 || i > len(s.fields) {
return nil, fmt.Errorf("arrow: invalid field index %d", i)
}
fields := make([]Field, len(s.fields)+1)
copy(fields[:i], s.fields[:i])
fields[i] = field
copy(fields[i+1:], s.fields[i:])
return NewSchema(fields, &s.meta), nil
}
func (s *Schema) String() string {
o := new(strings.Builder)
fmt.Fprintf(o, "schema:\n fields: %d\n", s.NumFields())
for i, f := range s.fields {
if i > 0 {
o.WriteString("\n")
}
fmt.Fprintf(o, " - %v", f)
}
if s.endianness != endian.NativeEndian {
fmt.Fprintf(o, "\n endianness: %v", s.endianness)
}
if meta := s.Metadata(); meta.Len() > 0 {
fmt.Fprintf(o, "\n metadata: %v", meta)
}
return o.String()
}
func (s *Schema) Fingerprint() string {
if s == nil {
return ""
}
var b strings.Builder
b.WriteString("S{")
for _, f := range s.fields {
fieldFingerprint := f.Fingerprint()
if fieldFingerprint == "" {
return ""
}
b.WriteString(fieldFingerprint)
b.WriteByte(';')
}
if s.endianness == endian.LittleEndian {
b.WriteByte('L')
} else {
b.WriteByte('B')
}
b.WriteByte('}')
return b.String()
}