39#pragma GCC diagnostic push
40#pragma GCC diagnostic ignored "-Wshadow"
41#pragma GCC diagnostic ignored "-Wunused-parameter"
43#include <arrow/table.h>
46#pragma GCC diagnostic pop
59struct RootConversionTraits {};
61#define ROOT_ARROW_STL_CONVERSION(c_type, ArrowType_) \
63 struct RootConversionTraits<c_type> { \
64 using ArrowType = ::arrow::ArrowType_; \
81class ArrayPtrVisitor :
public ::arrow::ArrayVisitor {
85 bool fCachedBool{
false};
87 RVec<float> fCachedRVecFloat;
88 RVec<double> fCachedRVecDouble;
89 RVec<ULong64_t> fCachedRVecULong64;
90 RVec<UInt_t> fCachedRVecUInt;
91 RVec<Long64_t> fCachedRVecLong64;
92 RVec<Int_t> fCachedRVecInt;
93 std::string fCachedString;
98 void *getTypeErasedPtrFrom(arrow::ListArray
const &array, int32_t entry, RVec<T> &cache)
100 using ArrowType =
typename RootConversionTraits<T>::ArrowType;
101 using ArrayType =
typename arrow::TypeTraits<ArrowType>::ArrayType;
102 auto values =
reinterpret_cast<ArrayType *
>(array.values().get());
103 auto offset = array.value_offset(entry);
106 RVec<T> tmp(
reinterpret_cast<T *
>((
void *)values->raw_values()) + offset, array.value_length(entry));
108 return (
void *)(&cache);
112 ArrayPtrVisitor(
void **result) : fResult{result}, fCurrentEntry{0} {}
114 void SetEntry(
ULong64_t entry) { fCurrentEntry = entry; }
117 virtual arrow::Status Visit(arrow::Int32Array
const &array)
final
119 *fResult = (
void *)(array.raw_values() + fCurrentEntry);
120 return arrow::Status::OK();
123 virtual arrow::Status Visit(arrow::Int64Array
const &array)
final
125 *fResult = (
void *)(array.raw_values() + fCurrentEntry);
126 return arrow::Status::OK();
130 virtual arrow::Status Visit(arrow::UInt32Array
const &array)
final
132 *fResult = (
void *)(array.raw_values() + fCurrentEntry);
133 return arrow::Status::OK();
136 virtual arrow::Status Visit(arrow::UInt64Array
const &array)
final
138 *fResult = (
void *)(array.raw_values() + fCurrentEntry);
139 return arrow::Status::OK();
142 virtual arrow::Status Visit(arrow::FloatArray
const &array)
final
144 *fResult = (
void *)(array.raw_values() + fCurrentEntry);
145 return arrow::Status::OK();
148 virtual arrow::Status Visit(arrow::DoubleArray
const &array)
final
150 *fResult = (
void *)(array.raw_values() + fCurrentEntry);
151 return arrow::Status::OK();
154 virtual arrow::Status Visit(arrow::BooleanArray
const &array)
final
156 fCachedBool = array.Value(fCurrentEntry);
157 *fResult =
reinterpret_cast<void *
>(&fCachedBool);
158 return arrow::Status::OK();
161 virtual arrow::Status Visit(arrow::StringArray
const &array)
final
163 fCachedString = array.GetString(fCurrentEntry);
164 *fResult =
reinterpret_cast<void *
>(&fCachedString);
165 return arrow::Status::OK();
168 virtual arrow::Status Visit(arrow::ListArray
const &array)
final
170 switch (array.value_type()->id()) {
171 case arrow::Type::FLOAT: {
172 *fResult = getTypeErasedPtrFrom(array, fCurrentEntry, fCachedRVecFloat);
173 return arrow::Status::OK();
175 case arrow::Type::DOUBLE: {
176 *fResult = getTypeErasedPtrFrom(array, fCurrentEntry, fCachedRVecDouble);
177 return arrow::Status::OK();
179 case arrow::Type::UINT32: {
180 *fResult = getTypeErasedPtrFrom(array, fCurrentEntry, fCachedRVecUInt);
181 return arrow::Status::OK();
183 case arrow::Type::UINT64: {
184 *fResult = getTypeErasedPtrFrom(array, fCurrentEntry, fCachedRVecULong64);
185 return arrow::Status::OK();
187 case arrow::Type::INT32: {
188 *fResult = getTypeErasedPtrFrom(array, fCurrentEntry, fCachedRVecInt);
189 return arrow::Status::OK();
191 case arrow::Type::INT64: {
192 *fResult = getTypeErasedPtrFrom(array, fCurrentEntry, fCachedRVecLong64);
193 return arrow::Status::OK();
195 default:
return arrow::Status::TypeError(
"Type not supported");
199 using ::arrow::ArrayVisitor::Visit;
205 std::vector<void *> fValuesPtrPerSlot;
206 std::vector<ULong64_t> fLastEntryPerSlot;
207 std::vector<ULong64_t> fLastChunkPerSlot;
208 std::vector<ULong64_t> fFirstEntryPerChunk;
209 std::vector<ArrayPtrVisitor> fArrayVisitorPerSlot;
213 std::vector<ULong64_t> fChunkIndex;
214 arrow::ArrayVector fChunks;
217 TValueGetter(
size_t slots, arrow::ArrayVector chunks)
218 : fValuesPtrPerSlot(slots, nullptr), fLastEntryPerSlot(slots, 0), fLastChunkPerSlot(slots, 0), fChunks{chunks}
220 fChunkIndex.reserve(fChunks.size());
222 for (
auto &chunk : chunks) {
223 fFirstEntryPerChunk.push_back(next);
224 next += chunk->length();
225 fChunkIndex.push_back(next);
227 for (
size_t si = 0, se = fValuesPtrPerSlot.size(); si != se; ++si) {
228 fArrayVisitorPerSlot.push_back(ArrayPtrVisitor{fValuesPtrPerSlot.data() + si});
233 std::vector<void *> SlotPtrs()
235 std::vector<void *> result;
236 for (
size_t i = 0; i < fValuesPtrPerSlot.size(); ++i) {
237 result.push_back(fValuesPtrPerSlot.data() + i);
244 void UncachedSlotLookup(
unsigned int slot,
ULong64_t entry)
250 assert(slot < fLastChunkPerSlot.size());
251 if (fLastEntryPerSlot[slot] < entry) {
252 ci = fLastChunkPerSlot.at(slot);
255 for (
size_t ce = fChunkIndex.size(); ci != ce; ++ci) {
256 if (entry < fChunkIndex[ci]) {
257 assert(slot < fLastChunkPerSlot.size());
258 fLastChunkPerSlot[slot] = ci;
265 auto chunk = fChunks.at(fLastChunkPerSlot[slot]);
266 assert(slot < fArrayVisitorPerSlot.size());
267 fArrayVisitorPerSlot[slot].SetEntry(entry - fFirstEntryPerChunk[fLastChunkPerSlot[slot]]);
268 fLastEntryPerSlot[slot] = entry;
269 auto status = chunk->Accept(fArrayVisitorPerSlot.data() + slot);
271 std::string msg =
"Could not get pointer for slot ";
272 msg += std::to_string(slot) +
" looking at entry " + std::to_string(entry);
273 throw std::runtime_error(msg);
278 void SetEntry(
unsigned int slot,
ULong64_t entry)
281 if (fLastEntryPerSlot[slot] == entry) {
284 UncachedSlotLookup(slot, entry);
296class RDFTypeNameGetter :
public ::arrow::TypeVisitor {
298 std::vector<std::string> fTypeName;
301 arrow::Status Visit(
const arrow::Int64Type &)
override
303 fTypeName.push_back(
"Long64_t");
304 return arrow::Status::OK();
306 arrow::Status Visit(
const arrow::Int32Type &)
override
308 fTypeName.push_back(
"Int_t");
309 return arrow::Status::OK();
311 arrow::Status Visit(
const arrow::UInt64Type &)
override
313 fTypeName.push_back(
"ULong64_t");
314 return arrow::Status::OK();
316 arrow::Status Visit(
const arrow::UInt32Type &)
override
318 fTypeName.push_back(
"UInt_t");
319 return arrow::Status::OK();
321 arrow::Status Visit(
const arrow::FloatType &)
override
323 fTypeName.push_back(
"float");
324 return arrow::Status::OK();
326 arrow::Status Visit(
const arrow::DoubleType &)
override
328 fTypeName.push_back(
"double");
329 return arrow::Status::OK();
331 arrow::Status Visit(
const arrow::StringType &)
override
333 fTypeName.push_back(
"string");
334 return arrow::Status::OK();
336 arrow::Status Visit(
const arrow::BooleanType &)
override
338 fTypeName.push_back(
"bool");
339 return arrow::Status::OK();
341 arrow::Status Visit(
const arrow::ListType &
l)
override
347 fTypeName.push_back(
"ROOT::VecOps::RVec<%s>");
348 return l.value_type()->Accept(
this);
353 std::string result =
"%s";
355 for (
size_t i = 0; i < fTypeName.size(); ++i) {
356 snprintf(buffer, 8192, result.c_str(), fTypeName[i].c_str());
362 using ::arrow::TypeVisitor::Visit;
366class VerifyValidColumnType :
public ::arrow::TypeVisitor {
369 virtual arrow::Status Visit(
const arrow::Int64Type &)
override {
return arrow::Status::OK(); }
370 virtual arrow::Status Visit(
const arrow::UInt64Type &)
override {
return arrow::Status::OK(); }
371 virtual arrow::Status Visit(
const arrow::Int32Type &)
override {
return arrow::Status::OK(); }
372 virtual arrow::Status Visit(
const arrow::UInt32Type &)
override {
return arrow::Status::OK(); }
373 virtual arrow::Status Visit(
const arrow::FloatType &)
override {
return arrow::Status::OK(); }
374 virtual arrow::Status Visit(
const arrow::DoubleType &)
override {
return arrow::Status::OK(); }
375 virtual arrow::Status Visit(
const arrow::StringType &)
override {
return arrow::Status::OK(); }
376 virtual arrow::Status Visit(
const arrow::BooleanType &)
override {
return arrow::Status::OK(); }
377 virtual arrow::Status Visit(
const arrow::ListType &)
override {
return arrow::Status::OK(); }
379 using ::arrow::TypeVisitor::Visit;
388 : fTable{inTable}, fColumnNames{inColumns}
395 auto filterWantedColumns = [&columnNames, &table]() {
396 if (columnNames.empty()) {
397 for (
auto &field : table->schema()->fields()) {
398 columnNames.push_back(field->name());
404 using ColumnType =
decltype(
fTable->column(0));
406 auto getRecordsFirstColumn = [&columnNames, &table]() {
407 if (columnNames.empty()) {
408 throw std::runtime_error(
"At least one column required");
410 const auto name = columnNames.front();
411 const auto columnIdx = table->schema()->GetFieldIndex(
name);
412 return table->column(columnIdx)->length();
416 auto verifyColumnSize = [&table](ColumnType column,
int columnIdx,
int nRecords) {
417 if (column->length() != nRecords) {
418 std::string msg =
"Column ";
419 msg += table->schema()->field(columnIdx)->name() +
" has a different number of entries.";
420 throw std::runtime_error(msg);
425 auto verifyColumnType = [&table](ColumnType column,
int columnIdx) {
426 auto verifyType = std::make_unique<VerifyValidColumnType>();
427 auto result = column->type()->Accept(verifyType.get());
428 if (result.ok() ==
false) {
429 std::string msg =
"Column ";
430 msg += table->schema()->field(columnIdx)->name() +
" contains an unsupported type.";
431 throw std::runtime_error(msg);
437 auto addColumnToGetterIndex = [&index](
int columnId) { index.push_back(std::make_pair(columnId, index.size())); };
441 auto resetGetterIndex = [&index]() { index.clear(); };
444 filterWantedColumns();
446 auto nRecords = getRecordsFirstColumn();
448 auto columnIdx =
fTable->schema()->GetFieldIndex(columnName);
449 addColumnToGetterIndex(columnIdx);
451 auto column =
fTable->column(columnIdx);
452 verifyColumnSize(column, columnIdx, nRecords);
453 verifyColumnType(column, columnIdx);
476 auto field =
fTable->schema()->GetFieldByName(std::string(colName));
478 std::string msg =
"The dataset does not have column ";
480 throw std::runtime_error(msg);
482 RDFTypeNameGetter typeGetter;
483 auto status = field->type()->Accept(&typeGetter);
484 if (status.ok() ==
false) {
485 std::string msg =
"RArrowDS does not support a column of type ";
486 msg += field->type()->name();
487 throw std::runtime_error(msg);
489 return typeGetter.result();
494 auto field =
fTable->schema()->GetFieldByName(std::string(colName));
505 getter->SetEntry(slot, entry);
514 getter->UncachedSlotLookup(slot, entry);
518void splitInEqualRanges(std::vector<std::pair<ULong64_t, ULong64_t>> &ranges,
int nRecords,
unsigned int nSlots)
521 const auto chunkSize = nRecords / nSlots;
522 const auto remainder = 1U == nSlots ? 0 : nRecords % nSlots;
528 ranges.emplace_back(start, end);
531 ranges.back().second += remainder;
534int getNRecords(std::shared_ptr<arrow::Table> &table, std::vector<std::string> &columnNames)
536 auto index = table->schema()->GetFieldIndex(columnNames.front());
537 return table->column(index)->length();
547std::shared_ptr<arrow::ChunkedArray>
548getData<std::shared_ptr<arrow::ChunkedArray>>(std::shared_ptr<arrow::ChunkedArray> p)
555 assert(0U ==
fNSlots &&
"Setting the number of slots even if the number of slots is different from zero.");
561 for (
size_t ci = 0; ci != nColumns; ++ci) {
563 fValueGetters.emplace_back(std::make_unique<ROOT::Internal::RDF::TValueGetter>(nSlots, chunkedArray->chunks()));
572 auto findGetterIndex = [&index](
unsigned int column) {
573 for (
auto &entry : index) {
574 if (entry.first == column) {
578 throw std::runtime_error(
"No column found at index " + std::to_string(column));
581 const int columnIdx =
fTable->schema()->GetFieldIndex(std::string(colName));
582 const int getterIdx = findGetterIndex(columnIdx);
583 assert(getterIdx != -1);
#define ROOT_ARROW_STL_CONVERSION(c_type, ArrowType_)
unsigned long long ULong64_t
typedef void((*Func_t)())
bool HasColumn(std::string_view colName) const override
Checks if the dataset has a certain column.
RArrowDS(std::shared_ptr< arrow::Table > table, std::vector< std::string > const &columns)
Constructor to create an Arrow RDataSource for RDataFrame.
void Initialise() override
Convenience method called before starting an event-loop.
std::string GetLabel() override
Return a string representation of the datasource type.
void SetNSlots(unsigned int nSlots) override
Inform RDataSource of the number of processing slots (i.e.
const std::vector< std::string > & GetColumnNames() const override
Returns a reference to the collection of the dataset's column names.
void InitSlot(unsigned int slot, ULong64_t firstEntry) override
Convenience method called at the start of the data processing associated to a slot.
std::vector< std::pair< ULong64_t, ULong64_t > > GetEntryRanges() override
Return ranges of entries to distribute to tasks.
std::shared_ptr< arrow::Table > fTable
std::vector< std::pair< size_t, size_t > > fGetterIndex
std::vector< std::unique_ptr< ROOT::Internal::RDF::TValueGetter > > fValueGetters
std::vector< void * > GetColumnReadersImpl(std::string_view name, const std::type_info &type) override
This needs to return a pointer to the pointer each value getter will point to.
std::vector< std::string > fColumnNames
std::string GetTypeName(std::string_view colName) const override
Type of a column as a string, e.g.
std::vector< std::pair< ULong64_t, ULong64_t > > fEntryRanges
bool SetEntry(unsigned int slot, ULong64_t entry) override
Advance the "cursors" returned by GetColumnReaders to the selected entry for a particular slot.
ROOT's RDataFrame offers a high level interface for analyses of data stored in TTrees,...
A pseudo container class which is a generator of indices.
basic_string_view< char > string_view
void swap(RDirectoryEntry &e1, RDirectoryEntry &e2) noexcept
void splitInEqualRanges(std::vector< std::pair< ULong64_t, ULong64_t > > &ranges, int nRecords, unsigned int nSlots)
int getNRecords(std::shared_ptr< arrow::Table > &table, std::vector< std::string > &columnNames)
std::shared_ptr< arrow::ChunkedArray > getData(T p)
RDataFrame MakeArrowDataFrame(std::shared_ptr< arrow::Table > table, std::vector< std::string > const &columns)
Factory method to create a Apache Arrow RDataFrame.
Namespace for new ROOT classes and functions.