forked from segmentio/parquet-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_test.go
145 lines (124 loc) · 2.86 KB
/
file_test.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
package parquet_test
import (
"io"
"os"
"path/filepath"
"strings"
"testing"
"github.com/segmentio/parquet-go"
)
var testdataFiles []string
func init() {
entries, _ := os.ReadDir("testdata")
for _, e := range entries {
testdataFiles = append(testdataFiles, filepath.Join("testdata", e.Name()))
}
}
func TestOpenFile(t *testing.T) {
for _, path := range testdataFiles {
t.Run(path, func(t *testing.T) {
f, err := os.Open(path)
if err != nil {
t.Fatal(err)
}
defer f.Close()
s, err := f.Stat()
if err != nil {
t.Fatal(err)
}
p, err := parquet.OpenFile(f, s.Size())
if err != nil {
t.Fatal(err)
}
if size := p.Size(); size != s.Size() {
t.Errorf("file size mismatch: want=%d got=%d", s.Size(), size)
}
root := p.Root()
b := new(strings.Builder)
parquet.PrintSchema(b, root.Name(), root)
t.Log(b)
printColumns(t, p.Root(), "")
})
}
}
func printColumns(t *testing.T, col *parquet.Column, indent string) {
if t.Failed() {
return
}
path := strings.Join(col.Path(), ".")
if col.Leaf() {
t.Logf("%s%s %v %v", indent, path, col.Encoding(), col.Compression())
} else {
t.Logf("%s%s", indent, path)
}
indent += ". "
buffer := make([]parquet.Value, 42)
pages := col.Pages()
defer pages.Close()
for {
p, err := pages.ReadPage()
if err != nil {
if err != io.EOF {
t.Error(err)
}
break
}
values := p.Values()
numValues := int64(0)
nullCount := int64(0)
for {
n, err := values.ReadValues(buffer)
for _, v := range buffer[:n] {
if v.Column() != col.Index() {
t.Errorf("value read from page of column %d says it belongs to column %d", col.Index(), v.Column())
return
}
if v.IsNull() {
nullCount++
}
}
numValues += int64(n)
if err != nil {
if err != io.EOF {
t.Error(err)
return
}
break
}
}
if numValues != p.NumValues() {
t.Errorf("page of column %d declared %d values but %d were read", col.Index(), p.NumValues(), numValues)
return
}
if nullCount != p.NumNulls() {
t.Errorf("page of column %d declared %d nulls but %d were read", col.Index(), p.NumNulls(), nullCount)
return
}
}
for _, child := range col.Columns() {
printColumns(t, child, indent)
}
}
func TestFileKeyValueMetadata(t *testing.T) {
type Row struct {
Name string
}
f, err := createParquetFile(
makeRows([]Row{{Name: "A"}, {Name: "B"}, {Name: "C"}}),
parquet.KeyValueMetadata("hello", "ignore this one"),
parquet.KeyValueMetadata("hello", "world"),
parquet.KeyValueMetadata("answer", "42"),
)
if err != nil {
t.Fatal(err)
}
for _, want := range [][2]string{
{"hello", "world"},
{"answer", "42"},
} {
key, value := want[0], want[1]
if found, ok := f.Lookup(key); !ok || found != value {
t.Errorf("key/value metadata mismatch: want %q=%q but got %q=%q (found=%t)", key, value, key, found, ok)
}
}
}