This repository is currently being migrated. It's locked while the migration is in progress.
forked from parsyl/parquet
/
template_string_optional.go
154 lines (132 loc) · 3.49 KB
/
template_string_optional.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
package gen
var stringOptionalTpl = `{{define "stringOptionalField"}}
type StringOptionalField struct {
parquet.OptionalField
vals []string
read func(r {{.StructType}}, vals []{{removeStar .TypeName}}, def, rep []uint8) ([]{{removeStar .TypeName}}, []uint8, []uint8)
write func(r *{{.StructType}}, vals []{{removeStar .TypeName}}, def, rep []uint8) (int, int)
stats *stringOptionalStats
}
func NewStringOptionalField(read func(r {{.StructType}}, vals []{{removeStar .TypeName}}, def, rep []uint8) ([]{{removeStar .TypeName}}, []uint8, []uint8), write func(r *{{.StructType}}, vals []{{removeStar .TypeName}}, defs, reps []uint8) (int, int), path []string, types []int, opts ...func(*parquet.OptionalField)) *StringOptionalField {
return &StringOptionalField{
read: read,
write: write,
OptionalField: parquet.NewOptionalField(path, types, opts...),
stats: newStringOptionalStats(maxDef(types)),
}
}
func (f *StringOptionalField) Schema() parquet.Field {
return parquet.Field{Name: f.Name(), Path: f.Path(), Type: StringType, RepetitionType: f.RepetitionType, Types: f.Types}
}
func (f *StringOptionalField) Add(r {{.StructType}}) {
vals, defs, reps := f.read(r, f.vals, f.Defs, f.Reps)
f.stats.add(vals[len(f.vals):], defs[len(f.Defs):])
f.vals = vals
f.Defs = defs
f.Reps = reps
}
func (f *StringOptionalField) Scan(r *{{.StructType}}) {
if len(f.Defs) == 0 {
return
}
v, l := f.write(r, f.vals, f.Defs, f.Reps)
f.vals = f.vals[v:]
f.Defs = f.Defs[l:]
if len(f.Reps) > 0 {
f.Reps = f.Reps[l:]
}
}
func (f *StringOptionalField) Write(w io.Writer, meta *parquet.Metadata) error {
buf := buffpool.Get()
defer buffpool.Put(buf)
bs := make([]byte, 4)
for _, s := range f.vals {
binary.LittleEndian.PutUint32(bs, uint32(len(s)))
if _, err := buf.Write(bs); err != nil {
return err
}
buf.WriteString(s)
}
return f.DoWrite(w, meta, buf.Bytes(), len(f.Defs), f.stats)
}
func (f *StringOptionalField) Read(r io.ReadSeeker, pg parquet.Page) error {
rr, _, err := f.DoRead(r, pg)
if err != nil {
return err
}
for j := 0; j < f.Values(); j++ {
var x int32
if err := binary.Read(rr, binary.LittleEndian, &x); err != nil {
return err
}
s := make([]byte, x)
if _, err := rr.Read(s); err != nil {
return err
}
f.vals = append(f.vals, string(s))
}
return nil
}
func (f *StringOptionalField) Levels() ([]uint8, []uint8) {
return f.Defs, f.Reps
}
{{end}}`
var stringOptionalStatsTpl = `{{define "stringOptionalStats"}}
const nilOptString = "__#NIL#__"
type stringOptionalStats struct {
min string
max string
nils int64
maxDef uint8
}
func newStringOptionalStats(d uint8) *stringOptionalStats {
return &stringOptionalStats{
min: nilOptString,
max: nilOptString,
maxDef: d,
}
}
func (s *stringOptionalStats) add(vals []string, defs []uint8) {
var i int
for _, def := range defs {
if def < s.maxDef {
s.nils++
} else {
val := vals[i]
if s.min == nilOptString {
s.min = val
} else {
if val < s.min {
s.min = val
}
}
if s.max == nilOptString {
s.max = val
} else {
if val > s.max {
s.max = val
}
}
i++
}
}
}
func (s *stringOptionalStats) NullCount() *int64 {
return &s.nils
}
func (s *stringOptionalStats) DistinctCount() *int64 {
return nil
}
func (s *stringOptionalStats) Min() []byte {
if s.min == nilOptString {
return nil
}
return []byte(s.min)
}
func (s *stringOptionalStats) Max() []byte {
if s.max == nilOptString {
return nil
}
return []byte(s.max)
}
{{end}}`