Skip to content

Commit 9210426

Browse files
committed
cleanup and out-of-line
1 parent ca6cc94 commit 9210426

File tree

3 files changed

+115
-119
lines changed

3 files changed

+115
-119
lines changed

Framework/Core/include/Framework/AnalysisHelpers.h

Lines changed: 6 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -66,82 +66,7 @@ inline constexpr int listSize(soa::IndexKind kind)
6666
} // namespace
6767

6868
struct IndexBuilder {
69-
template <bool Exclusive>
70-
static auto materialize(const char* label, std::vector<std::shared_ptr<arrow::Table>>&& tables, std::vector<soa::IndexRecord> const& records)
71-
{
72-
auto pool = arrow::default_memory_pool();
73-
std::vector<std::shared_ptr<framework::SelfIndexColumnBuilder>> builders;
74-
framework::SelfIndexColumnBuilder self{records[0].columnLabel.c_str(), pool};
75-
std::unique_ptr<framework::ChunkedArrayIterator> keyIndex = nullptr;
76-
if (records[0].kind != soa::IndexKind::IdxSelf) {
77-
keyIndex = std::make_unique<framework::ChunkedArrayIterator>(tables[0]->column(records[0].pos));
78-
}
79-
80-
for (auto i = 1U; i < records.size(); ++i) {
81-
if (records[i].kind == soa::IndexKind::IdxSelf) {
82-
builders.emplace_back(std::make_shared<framework::SelfIndexColumnBuilder>(records[i].columnLabel.c_str(), pool));
83-
} else {
84-
builders.emplace_back(std::make_shared<framework::IndexColumnBuilder>(tables[i]->column(records[i].pos), records[i].columnLabel.c_str(), listSize(records[i].kind), pool));
85-
}
86-
}
87-
88-
std::vector<bool> finds;
89-
finds.resize(builders.size());
90-
for (int64_t counter = 0; counter < tables[0]->num_rows(); ++counter) {
91-
int64_t idx = -1;
92-
if (keyIndex == nullptr) {
93-
idx = counter;
94-
} else {
95-
idx = keyIndex->valueAt(counter);
96-
}
97-
for (auto i = 0U; i < builders.size(); ++i) {
98-
if (records[i+1].kind == soa::IndexKind::IdxSelf) {
99-
finds[i] = builders[i]->find(idx);
100-
} else {
101-
finds[i] = std::static_pointer_cast<framework::IndexColumnBuilder>(builders[i])->find(idx);
102-
}
103-
}
104-
if constexpr (Exclusive) {
105-
if (std::none_of(finds.begin(), finds.end(), [](bool const x) { return x == false; })) {
106-
for (auto i = 0U; i < builders.size(); ++i) {
107-
if (records[i+1].kind == soa::IndexKind::IdxSelf) {
108-
builders[i]->fill(idx);
109-
} else {
110-
std::static_pointer_cast<framework::IndexColumnBuilder>(builders[i])->fill(idx);
111-
}
112-
}
113-
self.fill(counter);
114-
}
115-
} else {
116-
for (auto i = 0U; i < builders.size(); ++i) {
117-
if (records[i+1].kind == soa::IndexKind::IdxSelf) {
118-
builders[i]->fill(idx);
119-
} else {
120-
std::static_pointer_cast<framework::IndexColumnBuilder>(builders[i])->fill(idx);
121-
}
122-
}
123-
self.fill(counter);
124-
}
125-
}
126-
127-
std::vector<std::shared_ptr<arrow::ChunkedArray>> arrays;
128-
arrays.reserve(records.size());
129-
std::vector<std::shared_ptr<arrow::Field>> fields;
130-
fields.reserve(records.size());
131-
arrays.push_back(self.result());
132-
fields.push_back(self.field());
133-
for (auto i = 0U; i < builders.size(); ++i) {
134-
if (records[i+1].kind == soa::IndexKind::IdxSelf) {
135-
arrays.push_back(builders[i]->result());
136-
fields.push_back(builders[i]->field());
137-
} else {
138-
arrays.push_back(std::static_pointer_cast<framework::IndexColumnBuilder>(builders[i])->result());
139-
fields.push_back(std::static_pointer_cast<framework::IndexColumnBuilder>(builders[i])->field());
140-
}
141-
}
142-
143-
return framework::makeArrowTable(label, std::move(arrays), std::move(fields));
144-
}
69+
static std::shared_ptr<arrow::Table> materialize(const char* label, std::vector<std::shared_ptr<arrow::Table>>&& tables, std::vector<soa::IndexRecord> const& records, bool exclusive);
14570
};
14671
} // namespace o2::soa
14772

@@ -150,6 +75,7 @@ namespace o2::framework
15075
std::string serializeProjectors(std::vector<framework::expressions::Projector>& projectors);
15176
std::string serializeSchema(std::shared_ptr<arrow::Schema> schema);
15277
std::string serializeIndexRecords(std::vector<o2::soa::IndexRecord>& irs);
78+
std::vector<std::shared_ptr<arrow::Table>> extractSources(ProcessingContext& pc, std::vector<std::string> const& labels);
15379

15480
struct Spawner {
15581
std::string binding;
@@ -163,32 +89,9 @@ struct Spawner {
16389
header::DataDescription description;
16490
header::DataHeader::SubSpecificationType version;
16591

166-
std::shared_ptr<arrow::Table> materialize(ProcessingContext& pc) const
167-
{
168-
std::vector<std::shared_ptr<arrow::Table>> originals;
169-
for (auto const& label : labels) {
170-
originals.push_back(pc.inputs().get<TableConsumer>(label)->asArrowTable());
171-
}
172-
auto fullTable = soa::ArrowHelpers::joinTables(std::move(originals), std::span{labels.begin(), labels.size()});
173-
if (fullTable->num_rows() == 0) {
174-
return arrow::Table::MakeEmpty(schema).ValueOrDie();
175-
}
176-
177-
return spawnerHelper(fullTable, schema, binding.c_str(), schema->num_fields(), projector);
178-
}
92+
std::shared_ptr<arrow::Table> materialize(ProcessingContext& pc) const;
17993
};
18094

181-
namespace {
182-
static inline auto extractSources(ProcessingContext& pc, std::vector<std::string> const& labels)
183-
{
184-
std::vector<std::shared_ptr<arrow::Table>> tables;
185-
for (auto const& label : labels) {
186-
tables.emplace_back(pc.inputs().get<TableConsumer>(label.c_str())->asArrowTable());
187-
}
188-
return tables;
189-
}
190-
}
191-
19295
struct Builder {
19396
bool exclusive;
19497
std::string binding;
@@ -198,17 +101,7 @@ struct Builder {
198101
header::DataDescription description;
199102
header::DataHeader::SubSpecificationType version;
200103

201-
std::shared_ptr<arrow::Table> materialize(ProcessingContext& pc) const
202-
{
203-
std::shared_ptr<arrow::Table> result;
204-
auto tables = extractSources(pc, labels);
205-
if (exclusive) {
206-
result = o2::soa::IndexBuilder::materialize<true>(binding.c_str(), std::move(tables), records);
207-
} else {
208-
result = o2::soa::IndexBuilder::materialize<false>(binding.c_str(), std::move(tables), records);
209-
}
210-
return result;
211-
}
104+
std::shared_ptr<arrow::Table> materialize(ProcessingContext& pc) const;
212105
};
213106
} // namespace o2::framework
214107

@@ -761,7 +654,7 @@ struct Builds : decltype(transformBase<T>()) {
761654
using Ts = typename T::rest_t;
762655
using index_pack_t = metadata::index_pack_t;
763656

764-
std::vector<soa::IndexRecord> map;
657+
std::vector<soa::IndexRecord> map = soa::getIndexMapping<metadata>();
765658

766659
T* operator->()
767660
{
@@ -785,10 +678,7 @@ struct Builds : decltype(transformBase<T>()) {
785678

786679
auto build(std::vector<std::shared_ptr<arrow::Table>>&& tables)
787680
{
788-
if (map.empty()) {
789-
map = soa::getIndexMapping<metadata>();
790-
}
791-
this->table = std::make_shared<T>(soa::IndexBuilder::materialize<metadata::exclusive>(o2::aod::label<T::ref>(), std::forward<std::vector<std::shared_ptr<arrow::Table>>>(tables), map));
681+
this->table = std::make_shared<T>(soa::IndexBuilder::materialize(o2::aod::label<T::ref>(), std::forward<std::vector<std::shared_ptr<arrow::Table>>>(tables), map, metadata::exclusive));
792682
return (this->table != nullptr);
793683
}
794684
};

Framework/Core/src/AnalysisHelpers.cxx

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,84 @@
1313
#include "ExpressionJSONHelpers.h"
1414
#include "IndexJSONHelpers.h"
1515

16+
namespace o2::soa {
17+
std::shared_ptr<arrow::Table> IndexBuilder::materialize(const char* label, std::vector<std::shared_ptr<arrow::Table>>&& tables, std::vector<soa::IndexRecord> const& records, bool exclusive)
18+
{
19+
auto pool = arrow::default_memory_pool();
20+
std::vector<std::shared_ptr<framework::SelfIndexColumnBuilder>> builders;
21+
framework::SelfIndexColumnBuilder self{records[0].columnLabel.c_str(), pool};
22+
std::unique_ptr<framework::ChunkedArrayIterator> keyIndex = nullptr;
23+
if (records[0].kind != soa::IndexKind::IdxSelf) {
24+
keyIndex = std::make_unique<framework::ChunkedArrayIterator>(tables[0]->column(records[0].pos));
25+
}
26+
27+
for (auto i = 1U; i < records.size(); ++i) {
28+
if (records[i].kind == soa::IndexKind::IdxSelf) {
29+
builders.emplace_back(std::make_shared<framework::SelfIndexColumnBuilder>(records[i].columnLabel.c_str(), pool));
30+
} else {
31+
builders.emplace_back(std::make_shared<framework::IndexColumnBuilder>(tables[i]->column(records[i].pos), records[i].columnLabel.c_str(), listSize(records[i].kind), pool));
32+
}
33+
}
34+
35+
std::vector<bool> finds;
36+
finds.resize(builders.size());
37+
for (int64_t counter = 0; counter < tables[0]->num_rows(); ++counter) {
38+
int64_t idx = -1;
39+
if (keyIndex == nullptr) {
40+
idx = counter;
41+
} else {
42+
idx = keyIndex->valueAt(counter);
43+
}
44+
for (auto i = 0U; i < builders.size(); ++i) {
45+
if (records[i+1].kind == soa::IndexKind::IdxSelf) {
46+
finds[i] = builders[i]->find(idx);
47+
} else {
48+
finds[i] = std::static_pointer_cast<framework::IndexColumnBuilder>(builders[i])->find(idx);
49+
}
50+
}
51+
if (exclusive) {
52+
if (std::none_of(finds.begin(), finds.end(), [](bool const x) { return x == false; })) {
53+
for (auto i = 0U; i < builders.size(); ++i) {
54+
if (records[i+1].kind == soa::IndexKind::IdxSelf) {
55+
builders[i]->fill(idx);
56+
} else {
57+
std::static_pointer_cast<framework::IndexColumnBuilder>(builders[i])->fill(idx);
58+
}
59+
}
60+
self.fill(counter);
61+
}
62+
} else {
63+
for (auto i = 0U; i < builders.size(); ++i) {
64+
if (records[i+1].kind == soa::IndexKind::IdxSelf) {
65+
builders[i]->fill(idx);
66+
} else {
67+
std::static_pointer_cast<framework::IndexColumnBuilder>(builders[i])->fill(idx);
68+
}
69+
}
70+
self.fill(counter);
71+
}
72+
}
73+
74+
std::vector<std::shared_ptr<arrow::ChunkedArray>> arrays;
75+
arrays.reserve(records.size());
76+
std::vector<std::shared_ptr<arrow::Field>> fields;
77+
fields.reserve(records.size());
78+
arrays.push_back(self.result());
79+
fields.push_back(self.field());
80+
for (auto i = 0U; i < builders.size(); ++i) {
81+
if (records[i+1].kind == soa::IndexKind::IdxSelf) {
82+
arrays.push_back(builders[i]->result());
83+
fields.push_back(builders[i]->field());
84+
} else {
85+
arrays.push_back(std::static_pointer_cast<framework::IndexColumnBuilder>(builders[i])->result());
86+
fields.push_back(std::static_pointer_cast<framework::IndexColumnBuilder>(builders[i])->field());
87+
}
88+
}
89+
90+
return framework::makeArrowTable(label, std::move(arrays), std::move(fields));
91+
}
92+
} // namespace o2::soa
93+
1694
namespace o2::framework
1795
{
1896
void initializePartitionCaches(std::set<uint32_t> const& hashes, std::shared_ptr<arrow::Schema> const& schema, expressions::Filter const& filter, gandiva::NodePtr& tree, gandiva::FilterPtr& gfilter)
@@ -50,4 +128,32 @@ std::string serializeIndexRecords(std::vector<o2::soa::IndexRecord>& irs)
50128
IndexJSONHelpers::write(osm, irs);
51129
return osm.str();
52130
}
131+
132+
std::vector<std::shared_ptr<arrow::Table>> extractSources(ProcessingContext& pc, std::vector<std::string> const& labels)
133+
{
134+
std::vector<std::shared_ptr<arrow::Table>> tables;
135+
for (auto const& label : labels) {
136+
tables.emplace_back(pc.inputs().get<TableConsumer>(label.c_str())->asArrowTable());
137+
}
138+
return tables;
139+
}
140+
141+
std::shared_ptr<arrow::Table> Spawner::materialize(ProcessingContext& pc) const
142+
{
143+
auto tables = extractSources(pc, labels);
144+
auto fullTable = soa::ArrowHelpers::joinTables(std::move(tables), std::span{labels.begin(), labels.size()});
145+
if (fullTable->num_rows() == 0) {
146+
return arrow::Table::MakeEmpty(schema).ValueOrDie();
147+
}
148+
149+
return spawnerHelper(fullTable, schema, binding.c_str(), schema->num_fields(), projector);
150+
}
151+
152+
std::shared_ptr<arrow::Table> Builder::materialize(ProcessingContext& pc) const
153+
{
154+
std::shared_ptr<arrow::Table> result;
155+
auto tables = extractSources(pc, labels);
156+
result = o2::soa::IndexBuilder::materialize(binding.c_str(), std::move(tables), records, exclusive);
157+
return result;
158+
}
53159
} // namespace o2::framework

Framework/Core/test/test_IndexBuilder.cxx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ TEST_CASE("TestIndexBuilder")
103103
Categorys st4{t4};
104104

105105
auto map = getIndexMapping<o2::aod::MetadataTrait<o2::aod::Hash<"Index1/0"_h>>::metadata>();
106-
auto t5 = IndexBuilder::materialize<true>("test1a", {t1, t2, t3, t4}, map);
106+
auto t5 = IndexBuilder::materialize("test1a", {t1, t2, t3, t4}, map, true);
107107
REQUIRE(t5->num_rows() == 4);
108108
IDXs idxt{t5};
109109
idxt.bindExternalIndices(&st1, &st2, &st3, &st4);
@@ -114,7 +114,7 @@ TEST_CASE("TestIndexBuilder")
114114
}
115115

116116
map = getIndexMapping<o2::aod::MetadataTrait<o2::aod::Hash<"Index2/0"_h>>::metadata>();
117-
auto t6 = IndexBuilder::materialize<false>("test2", {t2, t1, t3, t4}, map);
117+
auto t6 = IndexBuilder::materialize("test2", {t2, t1, t3, t4}, map, false);
118118
REQUIRE(t6->num_rows() == st2.size());
119119
IDX2s idxs{t6};
120120
std::array<int, 7> fs{0, 1, 2, -1, -1, 4, -1};
@@ -213,7 +213,7 @@ TEST_CASE("AdvancedIndexTables")
213213
{8, 31, 42, 46, 58}}};
214214

215215
auto map = getIndexMapping<o2::aod::MetadataTrait<o2::aod::Hash<"Index3/0"_h>>::metadata>();
216-
auto t3 = IndexBuilder::materialize<false>("test3", {t1, t2, tc}, map);
216+
auto t3 = IndexBuilder::materialize("test3", {t1, t2, tc}, map, false);
217217
REQUIRE(t3->num_rows() == st1.size());
218218
IDX3s idxs{t3};
219219
idxs.bindExternalIndices(&st1, &st2, &st3);

0 commit comments

Comments
 (0)