-
Notifications
You must be signed in to change notification settings - Fork 0
/
concatenate_records_test.go
156 lines (144 loc) · 5.59 KB
/
concatenate_records_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
package arrowops
import (
"errors"
"fmt"
"testing"
"github.com/apache/arrow/go/v17/arrow"
"github.com/apache/arrow/go/v17/arrow/array"
"github.com/apache/arrow/go/v17/arrow/memory"
)
func BenchmarkConcatenateRecords(b *testing.B) {
numRecs := []int{10, 100}
recSizes := []int{100, 1_000, 10_000}
for _, recVal := range numRecs {
for _, sizeVal := range recSizes {
b.Run(fmt.Sprintf("records=%d|size=%d", recVal, sizeVal), func(b *testing.B) {
for idx := 0; idx < b.N; idx++ {
mem := memory.NewGoAllocator()
b.StopTimer()
records := make([]arrow.Record, recVal)
for recIdx := range recVal {
records[recIdx] = MockData(mem, sizeVal, "ascending")
}
b.StartTimer()
result, err := ConcatenateRecords(mem, records...)
if err != nil {
b.Errorf("received unexpected error: %s", err)
}
if result.NumRows() != int64(recVal*sizeVal) {
b.Errorf("expected a record with '%d' rows but received '%d'", recVal*sizeVal, result.NumRows())
}
}
})
}
}
}
func TestConcatenateRecords(t *testing.T) {
mem := memory.NewGoAllocator()
testCases := []struct {
records []arrow.Record
expectedRecord arrow.Record
expectedErr error
}{
{
records: []arrow.Record{MockData(mem, 10, "ascending")},
expectedRecord: MockData(mem, 10, "ascending"),
expectedErr: nil,
},
{
records: func() []arrow.Record {
rb1 := array.NewRecordBuilder(mem, arrow.NewSchema(
[]arrow.Field{
{Name: "a", Type: arrow.PrimitiveTypes.Uint32},
{Name: "b", Type: arrow.PrimitiveTypes.Float32},
{Name: "c", Type: arrow.BinaryTypes.String},
}, nil))
defer rb1.Release()
rb1.Field(0).(*array.Uint32Builder).AppendValues([]uint32{0, 1, 2}, nil)
rb1.Field(1).(*array.Float32Builder).AppendValues([]float32{0., 1., 2.}, nil)
rb1.Field(2).(*array.StringBuilder).AppendValues([]string{"s0", "s1", "s2"}, nil)
rb2 := array.NewRecordBuilder(mem, arrow.NewSchema(
[]arrow.Field{
{Name: "a", Type: arrow.PrimitiveTypes.Uint32},
{Name: "b", Type: arrow.PrimitiveTypes.Float32},
{Name: "c", Type: arrow.BinaryTypes.String},
}, nil))
defer rb2.Release()
rb2.Field(0).(*array.Uint32Builder).AppendValues([]uint32{0, 1, 2}, nil)
rb2.Field(1).(*array.Float32Builder).AppendValues([]float32{0., 1., 2.}, nil)
rb2.Field(2).(*array.StringBuilder).AppendValues([]string{"s0", "s1", "s2"}, nil)
return []arrow.Record{rb1.NewRecord(), rb2.NewRecord()}
}(),
expectedRecord: func() arrow.Record {
rb1 := array.NewRecordBuilder(mem, arrow.NewSchema(
[]arrow.Field{
{Name: "a", Type: arrow.PrimitiveTypes.Uint32},
{Name: "b", Type: arrow.PrimitiveTypes.Float32},
{Name: "c", Type: arrow.BinaryTypes.String},
}, nil))
defer rb1.Release()
rb1.Field(0).(*array.Uint32Builder).AppendValues([]uint32{0, 1, 2, 0, 1, 2}, nil)
rb1.Field(1).(*array.Float32Builder).AppendValues([]float32{0., 1., 2., 0., 1., 2.}, nil)
rb1.Field(2).(*array.StringBuilder).AppendValues([]string{"s0", "s1", "s2", "s0", "s1", "s2"}, nil)
return rb1.NewRecord()
}(),
},
{
records: func() []arrow.Record {
rb1 := array.NewRecordBuilder(mem, arrow.NewSchema(
[]arrow.Field{
{Name: "a", Type: arrow.PrimitiveTypes.Uint32},
{Name: "b", Type: arrow.PrimitiveTypes.Float32},
{Name: "c", Type: arrow.BinaryTypes.String},
}, nil))
defer rb1.Release()
rb1.Field(0).(*array.Uint32Builder).AppendValues([]uint32{0, 0, 2}, []bool{true, false, true})
rb1.Field(1).(*array.Float32Builder).AppendValues([]float32{0., 0., 2.}, []bool{true, false, true})
rb1.Field(2).(*array.StringBuilder).AppendValues([]string{"s0", "", "s2"}, []bool{true, false, true})
rb2 := array.NewRecordBuilder(mem, arrow.NewSchema(
[]arrow.Field{
{Name: "a", Type: arrow.PrimitiveTypes.Uint32},
{Name: "b", Type: arrow.PrimitiveTypes.Float32},
{Name: "c", Type: arrow.BinaryTypes.String},
}, nil))
defer rb2.Release()
rb2.Field(0).(*array.Uint32Builder).AppendValues([]uint32{0, 0, 2}, []bool{true, false, true})
rb2.Field(1).(*array.Float32Builder).AppendValues([]float32{0., 0., 2.}, []bool{true, false, true})
rb2.Field(2).(*array.StringBuilder).AppendValues([]string{"s0", "", "s2"}, []bool{true, false, true})
return []arrow.Record{rb1.NewRecord(), rb2.NewRecord()}
}(),
expectedRecord: func() arrow.Record {
rb1 := array.NewRecordBuilder(mem, arrow.NewSchema(
[]arrow.Field{
{Name: "a", Type: arrow.PrimitiveTypes.Uint32},
{Name: "b", Type: arrow.PrimitiveTypes.Float32},
{Name: "c", Type: arrow.BinaryTypes.String},
}, nil))
defer rb1.Release()
rb1.Field(0).(*array.Uint32Builder).AppendValues(
[]uint32{0, 1, 2, 0, 1, 2}, []bool{true, false, true, true, false, true},
)
rb1.Field(1).(*array.Float32Builder).AppendValues(
[]float32{0., 1., 2., 0., 1., 2.}, []bool{true, false, true, true, false, true},
)
rb1.Field(2).(*array.StringBuilder).AppendValues(
[]string{"s0", "s1", "s2", "s0", "s1", "s2"}, []bool{true, false, true, true, false, true},
)
return rb1.NewRecord()
}(),
},
}
for idx, testCase := range testCases {
t.Run(fmt.Sprintf("case_%d", idx), func(t *testing.T) {
result, err := ConcatenateRecords(mem, testCase.records...)
if !errors.Is(err, testCase.expectedErr) {
t.Errorf("expected error '%s' but received '%s'", testCase.expectedErr, err)
}
defer result.Release()
if !array.RecordEqual(testCase.expectedRecord, result) {
t.Log(result)
t.Error("result record does not match the expected record")
}
})
}
}