Project
Loading...
Searching...
No Matches
test_TableBuilder.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
12#include <catch_amalgamated.hpp>
13
15#include "Framework/Output.h"
16#include <arrow/table.h>
17#include <arrow/ipc/writer.h>
18#include <arrow/io/memory.h>
19#include <arrow/ipc/writer.h>
20#include <arrow/ipc/reader.h>
21
22using namespace o2::framework;
23
24// We use a different namespace to avoid clashes with the
25// test_ASoA.cxx test.
26namespace test2
27{
28DECLARE_SOA_COLUMN_FULL(X, x, uint64_t, "x");
29DECLARE_SOA_COLUMN_FULL(Y, y, uint64_t, "y");
30DECLARE_SOA_COLUMN_FULL(Pos, pos, int[4], "pos");
31} // namespace test2
32
35
36TEST_CASE("TestTableBuilder")
37{
38 using namespace o2::framework;
39 TableBuilder builder;
40 auto rowWriter = builder.persist<uint64_t, uint64_t>({"x", "y"});
41 rowWriter(0, 0, 0);
42 rowWriter(0, 10, 1);
43 rowWriter(0, 20, 2);
44 rowWriter(0, 30, 3);
45 rowWriter(0, 40, 4);
46 rowWriter(0, 50, 5);
47 rowWriter(0, 60, 6);
48 rowWriter(0, 70, 7);
49 auto table = builder.finalize();
50 REQUIRE(table->num_columns() == 2);
51 REQUIRE(table->num_rows() == 8);
52 REQUIRE(table->schema()->field(0)->name() == "x");
53 REQUIRE(table->schema()->field(1)->name() == "y");
54 REQUIRE(table->schema()->field(0)->type()->id() == arrow::uint64()->id());
55 REQUIRE(table->schema()->field(1)->type()->id() == arrow::uint64()->id());
56
57 auto readBack = TestTable{table};
58
59 auto readBackTable = readBack.asArrowTable();
60 REQUIRE(readBackTable->num_columns() == 2);
61 REQUIRE(readBackTable->num_rows() == 8);
62 REQUIRE(readBackTable->schema()->field(0)->name() == "x");
63 REQUIRE(readBackTable->schema()->field(1)->name() == "y");
64 REQUIRE(readBackTable->schema()->field(0)->type()->id() == arrow::uint64()->id());
65 REQUIRE(readBackTable->schema()->field(1)->type()->id() == arrow::uint64()->id());
66 size_t i = 0;
67 SECTION("Check")
68 {
69 for (auto const& row : readBack) {
70 REQUIRE(row.x() == i * 10);
71 REQUIRE(row.y() == i);
72 ++i;
73 }
74 }
75}
76
77TEST_CASE("TestTableBuilderArray")
78{
79 using namespace o2::framework;
80 TableBuilder builder;
81 const int numElem = 4;
82 auto rowWriter = builder.persist<int[numElem]>({"pos"});
83 int a[numElem] = {1, 10, 300, 350};
84 int b[numElem] = {0, 20, 30, 40};
85 rowWriter(0, a);
86 rowWriter(0, b);
87 using v3 = std::array<int, numElem>;
88 rowWriter(0, v3{0, 11, 123, 256}.data());
89 auto table = builder.finalize();
90
91 REQUIRE(table->num_columns() == 1);
92 REQUIRE(table->num_rows() == 3);
93 REQUIRE(table->schema()->field(0)->name() == "pos");
94 REQUIRE(table->schema()->field(0)->type()->id() == arrow::fixed_size_list(arrow::int32(), numElem)->id());
95
96 auto chunkToUse = table->column(0)->chunk(0);
97 chunkToUse = std::static_pointer_cast<arrow::FixedSizeListArray>(chunkToUse)->values();
98 auto data = chunkToUse->data();
99
100 REQUIRE(data->GetValues<int>(1)[0] == 1);
101 REQUIRE(data->GetValues<int>(1)[1] == 10);
102 REQUIRE(data->GetValues<int>(1)[2] == 300);
103 REQUIRE(data->GetValues<int>(1)[3] == 350);
104 REQUIRE(data->GetValues<int>(1)[4] == 0);
105 REQUIRE(data->GetValues<int>(1)[5] == 20);
106 REQUIRE(data->GetValues<int>(1)[6] == 30);
107 REQUIRE(data->GetValues<int>(1)[7] == 40);
108
109 auto readBack = ArrayTable{table};
110 auto row = readBack.begin();
111
112 REQUIRE(row.pos()[0] == 1);
113 REQUIRE(row.pos()[1] == 10);
114 REQUIRE(row.pos()[2] == 300);
115 REQUIRE(row.pos()[3] == 350);
116
117 row++;
118 REQUIRE(row.pos()[0] == 0);
119 REQUIRE(row.pos()[1] == 20);
120 REQUIRE(row.pos()[2] == 30);
121 REQUIRE(row.pos()[3] == 40);
122
123 row++;
124 REQUIRE(row.pos()[0] == 0);
125 REQUIRE(row.pos()[1] == 11);
126 REQUIRE(row.pos()[2] == 123);
127 REQUIRE(row.pos()[3] == 256);
128}
129
130TEST_CASE("TestTableBuilderStruct")
131{
132 using namespace o2::framework;
133 TableBuilder builder;
134 struct Foo {
135 uint64_t x;
136 uint64_t y;
137 };
138 auto rowWriter = builder.persist<Foo>({"x", "y"});
139 rowWriter(0, Foo{0, 0});
140 rowWriter(0, Foo{10, 1});
141 rowWriter(0, Foo{20, 2});
142 rowWriter(0, Foo{30, 3});
143 rowWriter(0, Foo{40, 4});
144 rowWriter(0, Foo{50, 5});
145 rowWriter(0, Foo{60, 6});
146 rowWriter(0, Foo{70, 7});
147 auto table = builder.finalize();
148 REQUIRE(table->num_columns() == 2);
149 REQUIRE(table->num_rows() == 8);
150 REQUIRE(table->schema()->field(0)->name() == "x");
151 REQUIRE(table->schema()->field(1)->name() == "y");
152 REQUIRE(table->schema()->field(0)->type()->id() == arrow::uint64()->id());
153 REQUIRE(table->schema()->field(1)->type()->id() == arrow::uint64()->id());
154
155 auto readBack = TestTable{table};
156
157 size_t i = 0;
158 for (auto& row : readBack) {
159 REQUIRE(row.x() == i * 10);
160 REQUIRE(row.y() == i);
161 ++i;
162 }
163}
164
165TEST_CASE("TestTableBuilderMore")
166{
167 using namespace o2::framework;
168 TableBuilder builder;
169 auto rowWriter = builder.persist<int, float, std::string, bool>({"x", "y", "s", "b"});
171 rowWriter(0, 0, 0., "foo", true);
172 rowWriter(0, 1, 1., "bar", false);
173 rowWriter(0, 2, 2., "fbr", false);
174 rowWriter(0, 3, 3., "bar", false);
175 rowWriter(0, 4, 4., "abr", true);
176 rowWriter(0, 5, 5., "aaa", false);
177 rowWriter(0, 6, 6., "bbb", true);
178 rowWriter(0, 7, 7., "ccc", false);
179 auto table = builder.finalize();
180 REQUIRE(table->num_columns() == 4);
181 REQUIRE(table->num_rows() == 8);
182 REQUIRE(table->schema()->field(0)->name() == "x");
183 REQUIRE(table->schema()->field(1)->name() == "y");
184 REQUIRE(table->schema()->field(2)->name() == "s");
185 REQUIRE(table->schema()->field(3)->name() == "b");
186 REQUIRE(table->schema()->field(0)->type()->id() == arrow::int32()->id());
187 REQUIRE(table->schema()->field(1)->type()->id() == arrow::float32()->id());
188 REQUIRE(table->schema()->field(2)->type()->id() == arrow::utf8()->id());
189 REQUIRE(table->schema()->field(3)->type()->id() == arrow::boolean()->id());
190}
191
192TEST_CASE("TestSoAIntegration")
193{
194 TableBuilder builder;
195 auto rowWriter = builder.cursor<TestTable>();
196 rowWriter(0, 0, 0);
197 rowWriter(0, 10, 1);
198 rowWriter(0, 20, 2);
199 rowWriter(0, 30, 3);
200 rowWriter(0, 40, 4);
201 rowWriter(0, 50, 5);
202 auto table = builder.finalize();
203 auto readBack = TestTable{table};
204
205 size_t i = 0;
206 for (auto& row : readBack) {
207 REQUIRE(row.x() == i * 10);
208 REQUIRE(row.y() == i);
209 ++i;
210 }
211}
212
213TEST_CASE("TestDataAllocatorReturnType")
214{
215 const Output output{"TST", "DUMMY", 0};
216}
217
218TEST_CASE("TestPodInjestion")
219{
220 struct A {
221 uint64_t x;
222 uint64_t y;
223 };
224 TableBuilder builder;
225 auto rowWriter = builder.cursor<TestTable, A>();
226 rowWriter(0, A{0, 0});
227 rowWriter(0, A{10, 1});
228 rowWriter(0, A{20, 2});
229 rowWriter(0, A{30, 3});
230 rowWriter(0, A{40, 4});
231 rowWriter(0, A{50, 5});
232 auto table = builder.finalize();
233 auto readBack = TestTable{table};
234
235 size_t i = 0;
236 for (auto& row : readBack) {
237 REQUIRE(row.x() == i * 10);
238 REQUIRE(row.y() == i);
239 ++i;
240 }
241}
242
243TEST_CASE("TestColumnCount")
244{
245 struct Foo {
246 int x;
247 int y;
248 };
249 struct Bar {
250 int x;
251 int y;
252 std::string s;
253 };
254 struct FooBar {
255 int x;
256 int y;
257 float f;
258 };
259 REQUIRE(TableBuilder::countColumns<Foo>() == 2);
260 REQUIRE(TableBuilder::countColumns<Bar>() == 3);
261 REQUIRE(TableBuilder::countColumns<FooBar>() == 3);
262 int count = TableBuilder::countColumns<float, int>();
263 REQUIRE(count == 2);
264 int count2 = TableBuilder::countColumns<float, int, char[3]>();
265 REQUIRE(count2 == 3);
266}
#define DECLARE_SOA_COLUMN_FULL(_Name_, _Getter_, _Type_, _Label_)
Definition ASoA.h:2293
int32_t i
void output(const std::map< std::string, ChannelStat > &channels)
Definition rawdump.cxx:197
uint16_t pos
Definition RawData.h:3
Definition A.h:16
auto reserve(o2::framework::pack< ARGS... > &&, int s)
auto persist(std::array< char const *, sizeof...(ARGS)+1 > const &columnNames)
std::shared_ptr< arrow::Table > finalize()
unfiltered_iterator begin()
Definition ASoA.h:1974
std::shared_ptr< arrow::Table > asArrowTable() const
Return a type erased arrow table backing store for / the type safe table.
Definition ASoA.h:2016
GLint GLenum GLint x
Definition glcorearb.h:403
GLint GLsizei count
Definition glcorearb.h:399
GLdouble f
Definition glcorearb.h:310
GLboolean GLboolean GLboolean b
Definition glcorearb.h:1233
GLint y
Definition glcorearb.h:270
GLboolean * data
Definition glcorearb.h:298
GLfloat GLfloat GLfloat GLfloat v3
Definition glcorearb.h:814
GLboolean GLboolean GLboolean GLboolean a
Definition glcorearb.h:1233
Defining PrimaryVertex explicitly as messageable.
Definition TFIDInfo.h:20
TEST_CASE("test_prepareArguments")
o2::soa::InPlaceTable< 0, test2::X, test2::Y > TestTable
std::vector< int > row