Skip to content

Commit 2e401ed

Browse files
committed
WIP DPL Analysis: support BinaryView
The idea is to be able to have BinaryViews on top of the CCDB object blobs which are already cached in shared memory, so that we can have a simple table with rows of the kind: (timestamp, blob-requested-ccdb-object-2, blob-requested-ccdb-object-2) which then can be joined via the timestamp to provide access to the requested CCDB Object. Filling of the table via TableBuilder seems to work, retrieving the blobs currently crashes.
1 parent 00279c7 commit 2e401ed

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

Framework/Core/include/Framework/ArrowTypes.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#ifndef O2_FRAMEWORK_ARROWTYPES_H
1313
#define O2_FRAMEWORK_ARROWTYPES_H
1414
#include "arrow/type_fwd.h"
15+
#include <span>
1516

1617
namespace o2::soa
1718
{
@@ -62,6 +63,10 @@ template <>
6263
struct arrow_array_for<double> {
6364
using type = arrow::DoubleArray;
6465
};
66+
template <>
67+
struct arrow_array_for<std::span<std::byte>> {
68+
using type = arrow::BinaryViewArray;
69+
};
6570
template <int N>
6671
struct arrow_array_for<float[N]> {
6772
using type = arrow::FixedSizeListArray;

Framework/Core/include/Framework/TableBuilder.h

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ O2_ARROW_STL_CONVERSION(long unsigned, UInt64Type)
9898
O2_ARROW_STL_CONVERSION(float, FloatType)
9999
O2_ARROW_STL_CONVERSION(double, DoubleType)
100100
O2_ARROW_STL_CONVERSION(std::string, StringType)
101+
O2_ARROW_STL_CONVERSION(std::span<std::byte>, BinaryViewType)
101102
} // namespace detail
102103

103104
void addLabelToSchema(std::shared_ptr<arrow::Schema>& schema, const char* label);
@@ -274,6 +275,29 @@ struct BuilderMaker<bool> {
274275
}
275276
};
276277

278+
template <>
279+
struct BuilderMaker<std::span<std::byte>> {
280+
using FillType = std::span<std::byte>;
281+
using STLValueType = std::span<std::byte>;
282+
using ArrowType = typename detail::ConversionTraits<std::span<std::byte>>::ArrowType;
283+
using BuilderType = typename arrow::TypeTraits<ArrowType>::BuilderType;
284+
285+
static std::unique_ptr<BuilderType> make(arrow::MemoryPool* pool)
286+
{
287+
return std::make_unique<BuilderType>(pool);
288+
}
289+
290+
static std::shared_ptr<arrow::DataType> make_datatype()
291+
{
292+
return arrow::TypeTraits<ArrowType>::type_singleton();
293+
}
294+
295+
static arrow::Status append(BuilderType& builder, std::span<std::byte> value)
296+
{
297+
return builder.Append((char*)value.data(), (int64_t)value.size());
298+
}
299+
};
300+
277301
template <typename ITERATOR>
278302
struct BuilderMaker<std::pair<ITERATOR, ITERATOR>> {
279303
using FillType = std::pair<ITERATOR, ITERATOR>;
@@ -338,6 +362,7 @@ struct BuilderMaker<T[N]> {
338362
}
339363
};
340364

365+
341366
template <typename T, int N>
342367
struct BuilderMaker<std::array<T, N>> {
343368
using FillType = T*;
@@ -422,6 +447,13 @@ struct DirectInsertion {
422447
return builder->Append(value);
423448
}
424449

450+
template <typename BUILDER>
451+
requires std::same_as<std::span<std::byte>, T>
452+
arrow::Status append(BUILDER& builder, T value)
453+
{
454+
return builder->Append((char*)value.data(), (int64_t)value.size());
455+
}
456+
425457
template <typename BUILDER>
426458
arrow::Status flush(BUILDER&)
427459
{
@@ -569,7 +601,7 @@ template <typename... ARGS>
569601
using IndexedHoldersTuple = decltype(makeHolderTypes<ARGS...>());
570602

571603
template <typename T>
572-
concept ShouldNotDeconstruct = std::is_bounded_array_v<T> || std::is_arithmetic_v<T> || framework::is_base_of_template_v<std::vector, T>;
604+
concept ShouldNotDeconstruct = std::is_bounded_array_v<T> || std::is_arithmetic_v<T> || framework::is_base_of_template_v<std::vector, T> || std::same_as<std::span<std::byte>, T>;
573605

574606
/// Helper class which creates a lambda suitable for building
575607
/// an arrow table from a tuple. This can be used, for example

Framework/Core/test/test_TableBuilder.cxx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
#include <arrow/ipc/writer.h>
2020
#include <arrow/ipc/reader.h>
2121

22+
#include <iostream>
23+
#include <string_view>
24+
2225
using namespace o2::framework;
2326

2427
// We use a different namespace to avoid clashes with the
@@ -27,10 +30,12 @@ namespace test2
2730
{
2831
DECLARE_SOA_COLUMN_FULL(X, x, uint64_t, "x");
2932
DECLARE_SOA_COLUMN_FULL(Y, y, uint64_t, "y");
33+
DECLARE_SOA_COLUMN_FULL(Blob, blob, std::span<std::byte>, "blob");
3034
DECLARE_SOA_COLUMN_FULL(Pos, pos, int[4], "pos");
3135
} // namespace test2
3236

3337
using TestTable = o2::soa::InPlaceTable<0, test2::X, test2::Y>;
38+
using SpanTable = o2::soa::InPlaceTable<0, test2::Blob>;
3439
using ArrayTable = o2::soa::InPlaceTable<0, test2::Pos>;
3540

3641
TEST_CASE("TestTableBuilder")
@@ -189,6 +194,32 @@ TEST_CASE("TestTableBuilderMore")
189194
REQUIRE(table->schema()->field(3)->type()->id() == arrow::boolean()->id());
190195
}
191196

197+
TEST_CASE("TestSpan")
198+
{
199+
TableBuilder builder;
200+
std::vector<std::byte> buffer{10, std::byte{'c'}};
201+
std::vector<std::byte> buffer1{10, std::byte{'a'}};
202+
203+
auto rowWriter = builder.persist<std::span<std::byte>>({"blob"});
204+
rowWriter(0, std::span(buffer));
205+
rowWriter(0, std::span(buffer));
206+
rowWriter(0, std::span(buffer1.data(), 3));
207+
rowWriter(0, std::span(buffer1.data(), 1));
208+
auto table = builder.finalize();
209+
210+
REQUIRE(table->num_columns() == 1);
211+
REQUIRE(table->num_rows() == 4);
212+
REQUIRE(table->schema()->field(0)->name() == "blob");
213+
REQUIRE(table->schema()->field(0)->type()->id() == arrow::binary_view()->id());
214+
215+
std::cout << table->ToString() << std::endl;
216+
217+
auto readBack = SpanTable{table};
218+
for (auto& row : readBack) {
219+
std::cout << row.blob().size() << std::endl;
220+
}
221+
}
222+
192223
TEST_CASE("TestSoAIntegration")
193224
{
194225
TableBuilder builder;
@@ -240,6 +271,7 @@ TEST_CASE("TestPodInjestion")
240271
}
241272
}
242273

274+
243275
TEST_CASE("TestColumnCount")
244276
{
245277
struct Foo {

0 commit comments

Comments
 (0)