ATLAS Offline Software
Loading...
Searching...
No Matches
ColumnVectorWrapper.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7
8//
9// includes
10//
11
13
18
19#include <boost/core/demangle.hpp>
20
21#include <algorithm>
22#include <stdexcept>
23
24//
25// method implementations
26//
27
28namespace columnar
29{
30 ColumnVectorHeader ::
31 ColumnVectorHeader ()
32 {
34
35 // we always reserve the first column as having a null value
36 m_elements.at(nullIndex).debugName = "<null>";
37 m_elements.at(nullIndex).isOptional = true;
38
39 // we always reserve the second column as the size column
40 m_elements.at(sizeIndex).debugName = "<size>";
41 m_elements.at(sizeIndex).type = &typeid(std::size_t);
42 m_elements.at(sizeIndex).sizeOfType = sizeof(std::size_t);
44 m_elements.at(sizeIndex).isOptional = false;
45 m_elements.at(sizeIndex).readOnly = true;
46 }
47
48
49
50 [[nodiscard]] std::size_t ColumnVectorHeader ::
51 addColumn (const ColumnInfo& columnInfo)
52 {
53 // Check for existing column with the same name (deduplication for multi-tool support)
54 auto iter = m_nameToIndex.find(columnInfo.name);
55 if (iter != m_nameToIndex.end())
56 {
57 auto& existingHeader = m_elements.at(iter->second);
58
59 // Check that relevant fields match
60 if (existingHeader.type != columnInfo.type)
61 throw std::runtime_error("column " + columnInfo.name + " type mismatch");
62 // for the "relaxed type matching" the strictest tool wins. note
63 // that size missmatches can't happen, due to the type check above.
64 existingHeader.typeCategory = mergeColumnTypeCategory (existingHeader.typeCategory, columnInfo.typeCategory);
65 if (existingHeader.offsetName != columnInfo.offsetName)
66 throw std::runtime_error("column " + columnInfo.name + " offset name mismatch: " + existingHeader.offsetName + " vs " + columnInfo.offsetName);
67 if (existingHeader.isOffset != columnInfo.isOffset)
68 throw std::runtime_error("column " + columnInfo.name + " isOffset mismatch");
69 if (existingHeader.fixedDimensions != columnInfo.fixedDimensions)
70 throw std::runtime_error("column " + columnInfo.name + " fixed dimensions mismatch");
71 if (existingHeader.isEventIndex != columnInfo.isEventIndex)
72 throw std::runtime_error("column " + columnInfo.name + " isEventIndex mismatch");
73
74 // Handle access mode conflicts
75 if (columnInfo.accessMode == ColumnAccessMode::output && existingHeader.readOnly)
76 throw std::runtime_error("column " + columnInfo.name + " already registered as input, cannot register as output");
77 // Promote to read-write if new column needs write access (update mode)
78 if (columnInfo.accessMode == ColumnAccessMode::update)
79 existingHeader.readOnly = false;
80 // If existing is already non-readOnly (output or update), keep it that way
81
82 // the column is required if any tool requires it
83 if (!columnInfo.isOptional)
84 existingHeader.isOptional = false;
85 return iter->second;
86 }
87
88 m_elements.emplace_back();
89 m_elements.at(sizeIndex).arraySize = m_elements.size();
90
91 auto& header = m_elements.back();
92 header.debugName = columnInfo.name;
93 header.type = columnInfo.type;
94 header.sizeOfType = columnInfo.sizeOfType;
95 header.typeCategory = columnInfo.typeCategory;
96 header.accessMode = columnInfo.accessMode;
97 header.offsetName = columnInfo.offsetName;
98 header.isEventIndex = columnInfo.isEventIndex;
99 header.fixedDimensions = columnInfo.fixedDimensions;
100
101 switch (columnInfo.accessMode)
102 {
104 break;
106 header.readOnly = false;
107 break;
109 header.readOnly = false;
110 break;
111 }
112 for (unsigned dim : columnInfo.fixedDimensions)
113 header.arraySize *= dim;
114 header.isOffset = columnInfo.isOffset;
115 if (!columnInfo.isOptional)
116 header.isOptional = false;
117 if (!columnInfo.offsetName.empty())
118 header.offsetIndex = unsetIndex;
119
120 const std::size_t newIndex = m_elements.size() - 1;
121 m_nameToIndex.emplace(columnInfo.name, newIndex);
122 return newIndex;
123 }
124
125
126
127 void ColumnVectorHeader ::
128 setOffsetColumn (std::size_t columnIndex, std::size_t offsetIndex)
129 {
130 if (columnIndex >= m_elements.size())
131 throw std::logic_error ("column index out of range");
132 if (offsetIndex >= m_elements.size())
133 throw std::logic_error ("offset index out of range");
134 auto& column = m_elements.at(columnIndex);
135 auto& offset = m_elements.at(offsetIndex);
136 if (!offset.isOffset)
137 throw std::runtime_error ("trying to set " + offset.debugName + " as offset column for " + column.debugName + ", but it is not marked as offset column");
138 if (column.offsetIndex != unsetIndex && column.offsetIndex != offsetIndex)
139 throw std::runtime_error ("trying to set " + offset.debugName + " as offset column for " + column.debugName + ", but it is already set to " + m_elements.at(column.offsetIndex).debugName);
140 column.offsetIndex = offsetIndex;
141 }
142
143
144
145 void ColumnVectorHeader ::
146 checkSelf () const
147 {
148 if (m_elements.size() < numFixedColumns)
149 throw std::logic_error ("header has too few m_elements, expected at least " + std::to_string(numFixedColumns) + " but got " + std::to_string(m_elements.size()));
150 if (m_elements[sizeIndex].arraySize != m_elements.size())
151 throw std::logic_error ("size column has wrong array size, expected " + std::to_string(m_elements.size()) + " but got " + std::to_string(m_elements[sizeIndex].arraySize));
152
153 // skipping the first column, which is always the null column
154 // and behaves in a funny manner
155 for (std::size_t columnIndex = 1u; columnIndex != m_elements.size(); ++ columnIndex)
156 {
157 const auto& column = m_elements[columnIndex];
158 if (column.debugName.empty())
159 throw std::logic_error ("column " + std::to_string(columnIndex) + " has no name");
160 if (column.type == nullptr)
161 throw std::logic_error ("column " + column.debugName + " has no type");
162 if (column.isOffset && *column.type != typeid(ColumnarOffsetType))
163 throw std::logic_error ("column " + column.debugName + " is an offset column that is of type " + boost::core::demangle(column.type->name()) + " instead of ColumnarOffsetType");
164 }
165
166 for (std::size_t columnIndex = 1u; columnIndex != m_elements.size(); ++ columnIndex)
167 {
168 const auto& column = m_elements[columnIndex];
169 if (column.offsetIndex != nullIndex)
170 {
171 if (column.offsetIndex == unsetIndex)
172 throw std::logic_error ("column " + column.debugName + " has an offset index that was never set");
173 if (column.offsetIndex >= m_elements.size())
174 throw std::logic_error ("column " + column.debugName + " has invalid offset index " + std::to_string(column.offsetIndex) + " (max is " + std::to_string(m_elements.size()-1) + ")");
175 const auto& offsetElement = m_elements[column.offsetIndex];
176 if (!offsetElement.isOffset)
177 throw std::logic_error ("column " + column.debugName + " has offset index that is not marked as offset");
178 }
179 }
180 }
181
182
183
184 void ColumnVectorHeader ::
185 checkData (std::span<const void* const> data) const
186 {
187 if (data.size() != m_elements.size())
188 throw std::logic_error ("data vector has wrong size, expected " + std::to_string(m_elements.size()) + " but got " + std::to_string(data.size()));
189 if (data[nullIndex] != nullptr)
190 throw std::logic_error ("null column is not set to a nullptr value");
191 const auto* sizeVector = static_cast<const std::size_t*>(data[sizeIndex]);
192 for (std::size_t columnIndex = 0u; columnIndex != m_elements.size(); ++ columnIndex)
193 {
194 const auto& column = m_elements[columnIndex];
195 if (data[columnIndex] == nullptr)
196 {
197 if (column.isOptional)
198 continue;
199 if (column.offsetIndex != nullIndex && data[column.offsetIndex] == nullptr)
200 continue;
201 throw std::logic_error ("column " + column.debugName + " was not set");
202 }
203 if (column.isOffset)
204 {
205 const auto size = sizeVector[columnIndex];
206 if (size < 1)
207 throw std::runtime_error ("offset column " + column.debugName + " has size " + std::to_string(size) + ", but needs at least 1 element");
208 auto *offsets = static_cast<const ColumnarOffsetType*>(data[columnIndex]);
209 if (offsets[0] != 0)
210 throw std::runtime_error ("offset column doesn't start with 0: " + column.debugName);
211 for (std::size_t i = 1u; i != size; ++ i)
212 {
213 if (offsets[i] < offsets[i-1])
214 throw std::runtime_error ("offset column " + column.debugName + " is not monotonically increasing at index " + std::to_string(i) + ": " + std::to_string(offsets[i-1]) + " > " + std::to_string(offsets[i]));
215 }
216 }
217 }
218 for (std::size_t columnIndex = 0u; columnIndex != m_elements.size(); ++ columnIndex)
219 {
220 if (data[columnIndex] == nullptr)
221 continue;
222 const auto& column = m_elements[columnIndex];
223 std::size_t expectedSize = 1u;
224 if (column.offsetIndex != ColumnVectorHeader::nullIndex)
225 {
226 if (data[column.offsetIndex] == nullptr)
227 throw std::runtime_error ("column " + column.debugName + " uses offset column " + m_elements[column.offsetIndex].debugName + " that is not set");
228 const auto offsetIndex = column.offsetIndex;
229 auto *offsetsPtr = static_cast<const ColumnarOffsetType*>(data[offsetIndex]);
230 expectedSize = offsetsPtr[sizeVector[offsetIndex]-1];
231 }
232 expectedSize *= column.arraySize;
233
234 if (column.isOffset)
235 expectedSize += 1u;
236
237 if (sizeVector[columnIndex] != expectedSize)
238 throw std::runtime_error ("column size doesn't match expected size: " + column.debugName + ", found " + std::to_string (sizeVector[columnIndex]) + " vs exptected=" + std::to_string (expectedSize) + " isOffset=" + std::to_string (column.isOffset));
239 }
240 }
241
242
243
244 namespace
245 {
252 void checkColumnType (const ColumnVectorElementHeader& header, const std::type_info *type, ColumnTypeCategory typeCategory, unsigned sizeOfType, bool isConst)
253 {
254 if (type != nullptr && header.type != nullptr && *type == *header.type)
255 return;
256 // boolean data is fine for any column that is only read
257 const bool readOnly = isConst || header.readOnly;
258 if (isColumnTypeCompatible (header.typeCategory, header.sizeOfType, typeCategory, sizeOfType, readOnly))
259 return;
260 std::string message = "invalid type for column: " + header.debugName + ", got ";
261 if (type != nullptr)
262 message += boost::core::demangle (type->name()) + " ";
263 message += "(" + std::string (columnTypeCategoryName (typeCategory)) + ", " + std::to_string (sizeOfType) + " byte(s)) but expected ";
264 if (header.type != nullptr)
265 message += boost::core::demangle (header.type->name()) + " ";
266 message += "(" + std::string (columnTypeCategoryName (header.typeCategory)) + ", " + std::to_string (header.sizeOfType) + " byte(s))";
267 if (!readOnly && typeCategory == ColumnTypeCategory::boolean)
268 message += ", boolean data can only be written to boolean columns";
269 throw std::runtime_error (message);
270 }
271 }
272
273
274
275 ColumnVectorData ::
276 ColumnVectorData (const ColumnVectorHeader *val_header)
277 : m_header (val_header),
278 m_data (val_header->numColumns(), nullptr),
279 m_dataSize (val_header->numColumns(), 0u)
280 {
282 }
283
284
285
286 void ColumnVectorData ::
287 setColumnVoid (std::size_t columnIndex, std::size_t size, const void *dataPtr, const std::type_info *type, ColumnTypeCategory typeCategory, unsigned sizeOfType, bool isConst)
288 {
289 if (columnIndex == ColumnVectorHeader::nullIndex)
290 throw std::logic_error ("cannot set the null column");
291 if (columnIndex >= m_header->numColumns())
292 throw std::logic_error ("invalid column index: " + std::to_string(columnIndex) + " (max is " + std::to_string(m_header->numColumns()-1) + ")");
293 auto& header = m_header->getColumn (columnIndex);
294
295 // If dataPtr is null, we use a dummy value to avoid issues in which
296 // we check whether a column exists by checking for a null pointer,
297 // which would return false for columns that were set with a
298 // nullptr.
299 if (dataPtr == nullptr)
300 {
301 if (size != 0) [[unlikely]]
302 throw std::logic_error ("dataPtr is null but size is not zero for column: " + header.debugName);
303 static const unsigned dummyValue = 0;
304 dataPtr = &dummyValue;
305 }
306
307 checkColumnType (header, type, typeCategory, sizeOfType, isConst);
308 if (isConst && !header.readOnly)
309 throw std::runtime_error ("assigning const vector to a column that is not read-only: " + header.debugName);
310 if (m_data[columnIndex] != nullptr)
311 throw std::runtime_error ("column filled multiple times: " + header.debugName);
312 auto *castDataPtr ATLAS_THREAD_SAFE = const_cast<void*>(dataPtr);
313 m_data[columnIndex] = castDataPtr;
314 m_dataSize[columnIndex] = size;
315 }
316
317
318
319 std::pair<std::size_t,const void*> ColumnVectorData ::
320 getColumnVoid (std::size_t columnIndex, const std::type_info *type, ColumnTypeCategory typeCategory, unsigned sizeOfType, bool isConst) const
321 {
322 if (columnIndex >= m_header->numColumns())
323 throw std::runtime_error ("invalid column index: " + std::to_string(columnIndex) + " (max is " + std::to_string(m_header->numColumns()-1) + ")");
324
325 auto& header = m_header->getColumn (columnIndex);
326 checkColumnType (header, type, typeCategory, sizeOfType, isConst);
327 if (!isConst && header.readOnly)
328 throw std::runtime_error ("retrieving non-const vector from a read-only column: " + header.debugName);
329 if (m_data[columnIndex] != nullptr)
330 return std::make_pair (m_dataSize[columnIndex], m_data[columnIndex]);
331 else
332 return std::make_pair (0u, nullptr);
333 }
334
335
336
337 void ColumnVectorData ::
338 callNoCheck (const IColumnarTool& tool)
339 {
340 tool.callVoid (m_data.data());
341 }
342
343
344
345 std::unordered_map<std::string, ColumnInfo> ColumnVectorHeader ::
346 getAllColumnInfo () const
347 {
348 std::unordered_map<std::string, ColumnInfo> result;
349 // Skip the fixed columns (null and size)
350 for (std::size_t i = numFixedColumns; i < m_elements.size(); ++i)
351 {
352 const auto& header = m_elements[i];
353 ColumnInfo info;
354 info.name = header.debugName;
355 info.index = static_cast<unsigned>(i);
356 info.type = header.type;
357 info.sizeOfType = header.sizeOfType;
358 info.typeCategory = header.typeCategory;
359 info.accessMode = header.accessMode;
360 info.offsetName = header.offsetName;
361 info.fixedDimensions = header.fixedDimensions;
362 info.isOffset = header.isOffset;
363 info.isOptional = header.isOptional;
364 info.isEventIndex = header.isEventIndex;
365 result.emplace(info.name, std::move(info));
366 }
367 return result;
368 }
369}
helper functions for working with the type information in columnar::ColumnInfo
size_t size() const
Number of registered mappings.
Define macros for attributes used to control the static checker.
#define ATLAS_THREAD_SAFE
const ColumnVectorHeader * m_header
void setColumn(std::size_t columnIndex, std::size_t size, CT *dataPtr)
set the data for the given column
std::vector< std::size_t > m_dataSize
the header information for the entire columnar data vector
static constexpr std::size_t nullIndex
the index used for an invalid index (always has to be 0)
static constexpr std::size_t numFixedColumns
the number of fix elements in the columnar data vector
std::unordered_map< std::string, std::size_t > m_nameToIndex
map from column name to index for deduplication
static constexpr std::size_t unsetIndex
the number used for an unset but non-null index
std::vector< ColumnVectorElementHeader > m_elements
the elements in the columnar data vector
static constexpr std::size_t sizeIndex
the index used for the column size column
an interface for tools that operate on columnar data
ColumnTypeCategory
the category of type we use for a column
Definition ColumnInfo.h:38
constexpr const char * columnTypeCategoryName(ColumnTypeCategory category) noexcept
a human readable name for the category (for messages)
constexpr ColumnTypeCategory mergeColumnTypeCategory(ColumnTypeCategory a, ColumnTypeCategory b) noexcept
merge the type categories of two users of the same column (with the same type), the strictest one win...
constexpr ColumnTypeCategory columnTypeCategoryFor() noexcept
the default ColumnTypeCategory for the given C++ type
Definition ColumnInfo.h:208
constexpr bool isColumnTypeCompatible(ColumnTypeCategory columnCategory, unsigned columnSize, ColumnTypeCategory dataCategory, unsigned dataSize, bool isConst) noexcept
whether data of the given category and size can be used for a column with the given category and size
@ update
an updateable column
Definition ColumnInfo.h:28
@ output
an output column
Definition ColumnInfo.h:25
@ input
an input column
Definition ColumnInfo.h:22
std::size_t ColumnarOffsetType
the type used for the size and offsets in the columnar data
#define unlikely(x)
a struct that contains meta-information about each column that's needed to interface the column with ...
Definition ColumnInfo.h:73
std::string offsetName
the name of the offset column used for this column (or empty string for none)
Definition ColumnInfo.h:120
ColumnTypeCategory typeCategory
the type category for the individual entries in the column
Definition ColumnInfo.h:100
std::vector< unsigned > fixedDimensions
the fixed dimensions this column has (if any)
Definition ColumnInfo.h:128
bool isOptional
whether this column is optional
Definition ColumnInfo.h:167
std::string name
the name of the column
Definition ColumnInfo.h:80
bool isEventIndex
whether this is the index of the event for each entry
Definition ColumnInfo.h:182
bool isOffset
whether this is an offset column
Definition ColumnInfo.h:138
ColumnAccessMode accessMode
the access mode for the column
Definition ColumnInfo.h:104
unsigned sizeOfType
the size (in bytes) of the individual entries in the column
Definition ColumnInfo.h:96
const std::type_info * type
the type of the individual entries in the column
Definition ColumnInfo.h:92
the header information for a single element in the columnar data vector