@@ -1232,28 +1232,34 @@ using ConcatBase = decltype(concat(std::declval<T1>(), std::declval<T2>()));
12321232#define DECLARE_SOA_TABLE (_Name_, _Origin_, _Description_, ...) \
12331233 DECLARE_SOA_TABLE_FULL (_Name_, #_Name_, _Origin_, _Description_, __VA_ARGS__);
12341234
1235- #define DECLARE_SOA_EXTENDED_TABLE (_Name_, _Table_, _Description_, ...) \
1236- using _Name_ = o2::soa::JoinBase<_Table_, o2::soa::Table<__VA_ARGS__>>; \
1237- \
1238- struct _Name_ ##Metadata : o2::soa::TableMetadata<_Name_##Metadata> { \
1239- using table_t = _Name_; \
1240- using base_table_t = _Table_; \
1241- using expression_pack_t = framework::pack<__VA_ARGS__>; \
1242- static constexpr char const * mLabel = #_Name_; \
1243- static constexpr char const mOrigin [4 ] = " DYN " ; \
1244- static constexpr char const mDescription [16 ] = _Description_; \
1245- }; \
1246- \
1247- template <> \
1248- struct MetadataTrait <_Name_> { \
1249- using metadata = _Name_##Metadata; \
1250- }; \
1251- \
1252- template <> \
1253- struct MetadataTrait <_Name_::unfiltered_iterator> { \
1254- using metadata = _Name_##Metadata; \
1235+ #define DECLARE_SOA_EXTENDED_TABLE_FULL (_Name_, _Table_, _Origin_, _Description_, ...) \
1236+ using _Name_ = o2::soa::JoinBase<_Table_, o2::soa::Table<__VA_ARGS__>>; \
1237+ \
1238+ struct _Name_ ##Metadata : o2::soa::TableMetadata<_Name_##Metadata> { \
1239+ using table_t = _Name_; \
1240+ using base_table_t = _Table_; \
1241+ using expression_pack_t = framework::pack<__VA_ARGS__>; \
1242+ static constexpr char const * mLabel = #_Name_; \
1243+ static constexpr char const mOrigin [4 ] = _Origin_; \
1244+ static constexpr char const mDescription [16 ] = _Description_; \
1245+ }; \
1246+ \
1247+ template <> \
1248+ struct MetadataTrait <_Name_> { \
1249+ using metadata = _Name_##Metadata; \
1250+ }; \
1251+ \
1252+ template <> \
1253+ struct MetadataTrait <_Name_::unfiltered_iterator> { \
1254+ using metadata = _Name_##Metadata; \
12551255 };
12561256
1257+ #define DECLARE_SOA_EXTENDED_TABLE (_Name_, _Table_, _Description_, ...) \
1258+ DECLARE_SOA_EXTENDED_TABLE_FULL (_Name_, _Table_, " DYN" , _Description_, __VA_ARGS__)
1259+
1260+ #define DECLARE_SOA_EXTENDED_TABLE_USER (_Name_, _Table_, _Description_, ...) \
1261+ DECLARE_SOA_EXTENDED_TABLE_FULL (_Name_, _Table_, " AOD" , _Description_, __VA_ARGS__)
1262+
12571263namespace o2::soa
12581264{
12591265template <typename ... Ts>
@@ -1443,6 +1449,52 @@ auto filter(T&& t, framework::expressions::Filter const& expr)
14431449 return Filtered<T>(t.asArrowTable (), expr);
14441450}
14451451
1452+ // / Expression-based column generator to materialize columns
1453+ template <typename ... C>
1454+ auto spawner (framework::pack<C...> columns, arrow::Table* atable)
1455+ {
1456+ arrow::TableBatchReader reader (*atable);
1457+ std::shared_ptr<arrow::RecordBatch> batch;
1458+ arrow::ArrayVector v;
1459+ std::vector<arrow::ArrayVector> chunks (sizeof ...(C));
1460+
1461+ auto projectors = framework::expressions::createProjectors (columns, atable->schema ());
1462+ while (true ) {
1463+ auto s = reader.ReadNext (&batch);
1464+ if (!s.ok ()) {
1465+ throw std::runtime_error (fmt::format (" Cannot read batches from table {}" , s.ToString ()));
1466+ }
1467+ if (batch == nullptr ) {
1468+ break ;
1469+ }
1470+ s = projectors->Evaluate (*batch, arrow::default_memory_pool (), &v);
1471+ if (!s.ok ()) {
1472+ throw std::runtime_error (fmt::format (" Cannot apply projector {}" , s.ToString ()));
1473+ }
1474+ for (auto i = 0u ; i < sizeof ...(C); ++i) {
1475+ chunks[i].emplace_back (v.at (i));
1476+ }
1477+ }
1478+ std::vector<std::shared_ptr<arrow::ChunkedArray>> arrays (sizeof ...(C));
1479+ for (auto i = 0u ; i < sizeof ...(C); ++i) {
1480+ arrays[i] = std::make_shared<arrow::ChunkedArray>(chunks[i]);
1481+ }
1482+
1483+ auto extra_schema = o2::soa::createSchemaFromColumns (columns);
1484+ std::vector<std::shared_ptr<arrow::Field>> new_fields;
1485+ std::vector<std::shared_ptr<arrow::ChunkedArray>> new_columns;
1486+ auto original_columns = atable->columns ();
1487+ auto original_fields = atable->schema ()->fields ();
1488+ std::copy (original_fields.begin (), original_fields.end (), std::back_inserter (new_fields));
1489+ std::copy (original_columns.begin (), original_columns.end (), std::back_inserter (new_columns));
1490+ for (auto i = 0u ; i < framework::pack_size (columns); ++i) {
1491+ new_columns.push_back (arrays[i]);
1492+ new_fields.emplace_back (extra_schema->field (i));
1493+ }
1494+ auto new_schema = std::make_shared<arrow::Schema>(new_fields);
1495+ return arrow::Table::Make (new_schema, new_columns);
1496+ }
1497+
14461498} // namespace o2::soa
14471499
14481500#endif // O2_FRAMEWORK_ASOA_H_
0 commit comments