forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable_builder-test.cc
More file actions
146 lines (117 loc) · 4.53 KB
/
Copy pathtable_builder-test.cc
File metadata and controls
146 lines (117 loc) · 4.53 KB
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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include <memory>
#include <string>
#include <vector>
#include "gtest/gtest.h"
#include "arrow/array.h"
#include "arrow/status.h"
#include "arrow/table.h"
#include "arrow/table_builder.h"
#include "arrow/test-common.h"
#include "arrow/test-util.h"
#include "arrow/type.h"
namespace arrow {
class TestRecordBatchBuilder : public TestBase {
public:
};
std::shared_ptr<Schema> ExampleSchema1() {
auto f0 = field("f0", int32());
auto f1 = field("f1", utf8());
auto f2 = field("f1", list(int8()));
return ::arrow::schema({f0, f1, f2});
}
template <typename BuilderType, typename T>
void AppendValues(BuilderType* builder, const std::vector<T>& values,
const std::vector<bool>& is_valid) {
for (size_t i = 0; i < values.size(); ++i) {
if (is_valid.size() == 0 || is_valid[i]) {
ASSERT_OK(builder->Append(values[i]));
} else {
ASSERT_OK(builder->AppendNull());
}
}
}
template <typename ValueType, typename T>
void AppendList(ListBuilder* builder, const std::vector<std::vector<T>>& values,
const std::vector<bool>& is_valid) {
auto values_builder = static_cast<ValueType*>(builder->value_builder());
for (size_t i = 0; i < values.size(); ++i) {
if (is_valid.size() == 0 || is_valid[i]) {
ASSERT_OK(builder->Append());
AppendValues<ValueType, T>(values_builder, values[i], {});
} else {
ASSERT_OK(builder->AppendNull());
}
}
}
TEST_F(TestRecordBatchBuilder, Basics) {
auto schema = ExampleSchema1();
std::unique_ptr<RecordBatchBuilder> builder;
ASSERT_OK(RecordBatchBuilder::Make(schema, pool_, &builder));
std::vector<bool> is_valid = {false, true, true, true};
std::vector<int32_t> f0_values = {0, 1, 2, 3};
std::vector<std::string> f1_values = {"a", "bb", "ccc", "dddd"};
std::vector<std::vector<int8_t>> f2_values = {{}, {0, 1}, {}, {2}};
std::shared_ptr<Array> a0, a1, a2;
// Make the expected record batch
auto AppendData = [&](Int32Builder* b0, StringBuilder* b1, ListBuilder* b2) {
AppendValues<Int32Builder, int32_t>(b0, f0_values, is_valid);
AppendValues<StringBuilder, std::string>(b1, f1_values, is_valid);
AppendList<Int8Builder, int8_t>(b2, f2_values, is_valid);
};
Int32Builder ex_b0;
StringBuilder ex_b1;
ListBuilder ex_b2(pool_, std::unique_ptr<Int8Builder>(new Int8Builder(pool_)));
AppendData(&ex_b0, &ex_b1, &ex_b2);
ASSERT_OK(ex_b0.Finish(&a0));
ASSERT_OK(ex_b1.Finish(&a1));
ASSERT_OK(ex_b2.Finish(&a2));
RecordBatch expected(schema, 4, {a0, a1, a2});
// Builder attributes
ASSERT_EQ(3, builder->num_fields());
ASSERT_EQ(schema.get(), builder->schema().get());
const int kIter = 3;
for (int i = 0; i < kIter; ++i) {
AppendData(builder->GetFieldAs<Int32Builder>(0),
static_cast<StringBuilder*>(builder->GetField(1)),
builder->GetFieldAs<ListBuilder>(2));
std::shared_ptr<RecordBatch> batch;
if (i == kIter - 1) {
// Do not flush in last iteration
ASSERT_OK(builder->Flush(false, &batch));
} else {
ASSERT_OK(builder->Flush(&batch));
}
ASSERT_BATCHES_EQUAL(expected, *batch);
}
// Test setting initial capacity
builder->SetInitialCapacity(4096);
ASSERT_EQ(4096, builder->initial_capacity());
}
TEST_F(TestRecordBatchBuilder, InvalidFieldLength) {
auto schema = ExampleSchema1();
std::unique_ptr<RecordBatchBuilder> builder;
ASSERT_OK(RecordBatchBuilder::Make(schema, pool_, &builder));
std::vector<bool> is_valid = {false, true, true, true};
std::vector<int32_t> f0_values = {0, 1, 2, 3};
AppendValues<Int32Builder, int32_t>(builder->GetFieldAs<Int32Builder>(0), f0_values,
is_valid);
std::shared_ptr<RecordBatch> dummy;
ASSERT_RAISES(Invalid, builder->Flush(&dummy));
}
} // namespace arrow