Skip to content

Commit 9a1bb50

Browse files
athumscritchley
authored andcommitted
Add NumStripes() and SelectStripe() to Reader and Cursor (#56)
* Add NumStripes() and GetStripe() to Reader and Cursor * Fix stripe numbers to start from idx 0 instead of 1 * Address PR comments * Remove error variable declaration * Remove go-cmp from cursor test
1 parent 029306d commit 9a1bb50

File tree

4 files changed

+99
-7
lines changed

4 files changed

+99
-7
lines changed

cursor.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ func (c *Cursor) Select(fields ...string) *Cursor {
4444
return c
4545
}
4646

47+
// SelectStripe retrieves the stream information for the specified stripe.
48+
func (c *Cursor) SelectStripe(n int) error {
49+
stripe, err := c.Reader.getStripe(n, c.included...)
50+
if err != nil {
51+
return err
52+
}
53+
c.Stripe = stripe
54+
c.stripeOffset = n
55+
return c.prepareStreamReaders()
56+
}
57+
4758
// prepareStreamReaders prepares TreeReaders for each of the columns
4859
// that will be read.
4960
func (c *Cursor) prepareStreamReaders() error {
@@ -65,10 +76,11 @@ func (c *Cursor) prepareNextStripe() error {
6576
// and creating the required readers for each of the
6677
// required columns.
6778
var err error
68-
c.Stripe, err = c.Reader.getStripe(c.stripeOffset, c.included...)
79+
stripe, err := c.Reader.getStripe(c.stripeOffset, c.included...)
6980
if err != nil {
7081
return err
7182
}
83+
c.Stripe = stripe
7284
c.stripeOffset++ // Increment in order to fetch the next stripe.
7385
return c.prepareStreamReaders()
7486
}

cursor_test.go

+57-6
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ func TestCursorResets(t *testing.T) {
9393

9494
}
9595

96-
9796
func TestCursorSelectError(t *testing.T) {
9897

9998
r, err := Open("./examples/demo-11-zlib.orc")
@@ -104,23 +103,75 @@ func TestCursorSelectError(t *testing.T) {
104103

105104
// Try to select a column that doesn't exist.
106105
c := r.Select("notfound")
107-
106+
108107
var hasNext bool
109108
for c.Next() {
110109
hasNext = true
111110
}
112-
111+
113112
if hasNext {
114113
t.Errorf("Next returned true, expected false")
115114
}
116-
115+
117116
err = c.Err()
118117
if err == nil {
119118
t.Errorf("Expected error")
120119
}
121120
if err.Error() != "no field with name: notfound" {
122121
t.Errorf("Unexpected error: %s", err.Error())
123122
}
124-
125-
123+
124+
}
125+
126+
func TestCursorSelectStripe(t *testing.T) {
127+
r, err := Open("./examples/demo-11-none.orc")
128+
if err != nil {
129+
t.Fatalf("Expected no error, got %v", err)
130+
}
131+
132+
expectedStripes := 385
133+
134+
numStripes, err := r.NumStripes()
135+
if err != nil {
136+
t.Fatalf("Expected no error, got %v", err)
137+
}
138+
139+
if numStripes != expectedStripes {
140+
t.Fatalf("Expected %d stripes, got %d", expectedStripes, numStripes)
141+
}
142+
143+
cols := r.Schema().Columns()
144+
c := r.Select(cols...)
145+
146+
err = c.SelectStripe(numStripes - 1)
147+
if err != nil {
148+
t.Fatalf("Expected no error, got %v", err)
149+
}
150+
151+
var row []interface{}
152+
for c.Next() {
153+
row = c.Row()
154+
}
155+
156+
expectedLastRow := []interface{}{
157+
int64(1920800),
158+
"F",
159+
"U",
160+
"Unknown",
161+
int64(10000),
162+
"Unknown",
163+
int64(6),
164+
int64(6),
165+
int64(6),
166+
}
167+
168+
if len(row) != len(expectedLastRow) {
169+
t.Fatalf("Expected %d elements, got %d", len(expectedLastRow), len(row))
170+
}
171+
172+
for idx, e := range expectedLastRow {
173+
if e != row[idx] {
174+
t.Fatalf("Expected %v, got %v", e, row[idx])
175+
}
176+
}
126177
}

reader.go

+9
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,15 @@ func (r *Reader) NumRows() int {
341341
return int(r.footer.GetNumberOfRows())
342342
}
343343

344+
func (r *Reader) NumStripes() (int, error) {
345+
stripes, err := r.getStripes()
346+
if err != nil {
347+
return 0, err
348+
}
349+
350+
return len(stripes), nil
351+
}
352+
344353
type Stripe struct {
345354
included []int
346355
*proto.StripeInformation

reader_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,23 @@ func TestReadNullAtEnd(t *testing.T) {
3737
}
3838
}
3939
}
40+
41+
func TestNumStripes(t *testing.T) {
42+
expectedStripes := 385
43+
44+
r, err := Open("examples/demo-11-none.orc")
45+
if err != nil {
46+
t.Fatalf("Expected no error, got %v", err)
47+
}
48+
49+
defer r.Close()
50+
51+
n, err := r.NumStripes()
52+
if err != nil {
53+
t.Fatalf("Expected no error, got %v", err)
54+
}
55+
56+
if n != expectedStripes {
57+
t.Fatalf("Expected %d stripes, got %d", expectedStripes, n)
58+
}
59+
}

0 commit comments

Comments
 (0)