-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathcursor_test.go
177 lines (140 loc) · 3.24 KB
/
cursor_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
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
package orc
import (
"reflect"
"testing"
"github.com/scritchley/orc/proto"
)
func TestCursor(t *testing.T) {
r, err := Open("./examples/demo-11-zlib.orc")
if err != nil {
t.Fatal(err)
}
defer r.Close()
// Log the schema
t.Log(r.Schema())
// Select a single column from the file.
c := r.Select("_col0")
// Call Stripes to trigger reading the first stripe.
s := c.Stripes()
if !s {
t.Errorf("Test failed, expected true, got false")
}
// Call Next to initialise the readers.
n := c.Next()
if !n {
t.Errorf("Test failed, expected true, got false")
}
// There should be a data stream available for reading.
stream := c.Stripe.get(streamName{1, proto.Stream_DATA})
if stream == nil {
t.Errorf("Test failed, got nil stream")
}
// There should also be a row index.
stream = c.Stripe.get(streamName{1, proto.Stream_ROW_INDEX})
if stream == nil {
t.Errorf("Test failed, got nil stream")
}
if err := c.Err(); err != nil {
t.Fatal(err)
}
}
func TestCursorResets(t *testing.T) {
r, err := Open("./examples/demo-11-zlib.orc")
if err != nil {
t.Fatal(err)
}
defer r.Close()
// Select a single column from the file.
c := r.Select("_col0")
// Call Stripes to trigger reading the first stripe.
var values []interface{}
c.Stripes()
for c.Next() {
vals := c.Row()
values = append(values, vals...)
}
if err := c.Err(); err != nil {
t.Fatal(err)
}
// Select a single column from the file.
c = r.Select("_col0")
// Call Stripes to trigger reading the first stripe.
var valuesAgain []interface{}
c.Stripes()
for c.Next() {
vals := c.Row()
valuesAgain = append(valuesAgain, vals...)
}
if err := c.Err(); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(values, valuesAgain) {
t.Errorf("Test failed, expected values to be equal")
}
}
func TestCursorSelectError(t *testing.T) {
r, err := Open("./examples/demo-11-zlib.orc")
if err != nil {
t.Fatal(err)
}
defer r.Close()
// Try to select a column that doesn't exist.
c := r.Select("notfound")
var hasNext bool
for c.Next() {
hasNext = true
}
if hasNext {
t.Errorf("Next returned true, expected false")
}
err = c.Err()
if err == nil {
t.Errorf("Expected error")
}
if err.Error() != "no field with name: notfound" {
t.Errorf("Unexpected error: %s", err.Error())
}
}
func TestCursorSelectStripe(t *testing.T) {
r, err := Open("./examples/demo-11-none.orc")
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
expectedStripes := 385
numStripes, err := r.NumStripes()
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if numStripes != expectedStripes {
t.Fatalf("Expected %d stripes, got %d", expectedStripes, numStripes)
}
cols := r.Schema().Columns()
c := r.Select(cols...)
err = c.SelectStripe(numStripes - 1)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
var row []interface{}
for c.Next() {
row = c.Row()
}
expectedLastRow := []interface{}{
int64(1920800),
"F",
"U",
"Unknown",
int64(10000),
"Unknown",
int64(6),
int64(6),
int64(6),
}
if len(row) != len(expectedLastRow) {
t.Fatalf("Expected %d elements, got %d", len(expectedLastRow), len(row))
}
for idx, e := range expectedLastRow {
if e != row[idx] {
t.Fatalf("Expected %v, got %v", e, row[idx])
}
}
}