-
Notifications
You must be signed in to change notification settings - Fork 6
/
row.h
332 lines (288 loc) · 16.2 KB
/
row.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// Copyright (c) 2020 Unfolded, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#ifndef DECKGL_CORE_ARROW_ROW_H
#define DECKGL_CORE_ARROW_ROW_H
#include <arrow/array.h>
#include <arrow/table.h>
#include <memory>
#include <optional>
#include <string>
#include <tuple>
#include <vector>
#include "math.gl/core.h"
#include "probe.gl/core.h"
namespace deckgl {
/// \brief Utility structure that holds metadata of a generic list array.
struct ListArrayMetadata {
public:
ListArrayMetadata(int32_t offset, int32_t length, const std::shared_ptr<arrow::Array>& values)
: offset{offset}, length{length}, values{values} {}
int32_t offset;
int32_t length;
std::shared_ptr<arrow::Array> values;
};
/// \brief Represents a single row within a given table, which can be queried for easy access to typed data.
class Row {
public:
Row(const std::shared_ptr<arrow::Table>& table, int64_t rowIndex);
/// \brief Attempts to get an integer value in this row, for a given columnName.
/// If the value is of a different type, best effort will be used to convert it to an integer.
/// \param columnName Name of the column to get the data for.
/// \param defaultValue Default value that'll be returned if data could not be found.
/// \returns Integer value found in this row for the given columnName, or defaultValue if one isn't found.
auto getInt(const std::string& columnName, int defaultValue = 0) const -> int;
/// \brief Attempts to get a float value in this row, for a given columnName.
/// If the value is of a different type, best effort will be used to convert it to a float.
/// \param columnName Name of the column to get the data for.
/// \param defaultValue Default value that'll be returned if data could not be found.
/// \returns Float value found in this row for the given columnName, or defaultValue if one isn't found.
auto getFloat(const std::string& columnName, float defaultValue = 0.0) const -> float;
/// \brief Attempts to get a double value in this row, for a given columnName.
/// If the value is of a different type, best effort will be used to convert it to a double.
/// \param columnName Name of the column to get the data for.
/// \param defaultValue Default value that'll be returned if data could not be found.
/// \returns Double value found in this row for the given columnName, or defaultValue if one isn't found.
auto getDouble(const std::string& columnName, double defaultValue = 0.0) const -> double;
/// \brief Attempts to get a boolean value in this row, for a given columnName.
/// If the value is of a different type, best effort will be used to convert it to a boolean.
/// \param columnName Name of the column to get the data for.
/// \param defaultValue Default value that'll be returned if data could not be found.
/// \returns Boolean value found in this row for the given columnName, or defaultValue if one isn't found.
auto getBool(const std::string& columnName, bool defaultValue = false) const -> bool;
/// \brief Attempts to get a string value in this row, for a given columnName.
/// \param columnName Name of the column to get the data for.
/// \param defaultValue Default value that'll be returned if data could not be found.
/// \returns String value found in this row for the given columnName, or defaultValue if one isn't found.
auto getString(const std::string& columnName, const std::string& defaultValue = "") const -> std::string;
/// \brief Attempts to get vector data out of a list array found in this row, for a given columnName.
/// \param columnName Name of the column to get the data for.
/// \param defaultValue Default value that'll be returned if data could not be found.
/// \returns Vector data, potentially padded with zeros if the list was not of a sufficient length,
/// or defaultValue if data could not be found.
template <typename T>
auto getVector2(const std::string& columnName, const mathgl::Vector2<T>& defaultValue = {}) const
-> mathgl::Vector2<T> {
auto data = this->getListData<T>(columnName, {defaultValue.x, defaultValue.y});
return mathgl::Vector2<T>{data.size() > 0 ? data.at(0) : 0, data.size() > 1 ? data.at(1) : 0};
}
/// \brief Attempts to get vector data out of a list array found in this row, for a given columnName.
/// \param columnName Name of the column to get the data for.
/// \param defaultValue Default value that'll be returned if data could not be found.
/// \returns Vector data, potentially padded with zeros if the list was not of a sufficient length,
/// or defaultValue if data could not be found.
template <typename T>
auto getVector3(const std::string& columnName, const mathgl::Vector3<T>& defaultValue = {}) const
-> mathgl::Vector3<T> {
auto data = this->getListData<T>(columnName, {defaultValue.x, defaultValue.y, defaultValue.z});
return mathgl::Vector3<T>{data.size() > 0 ? data.at(0) : 0, data.size() > 1 ? data.at(1) : 0,
data.size() > 2 ? data.at(2) : 0};
}
/// \brief Attempts to get vector data out of a list array found in this row, for a given columnName.
/// \param columnName Name of the column to get the data for.
/// \param defaultValue Default value that'll be returned if data could not be found.
/// \returns Vector data, potentially padded with zeros if the list was not of a sufficient length,
/// or defaultValue if data could not be found.
template <typename T>
auto getVector4(const std::string& columnName, const mathgl::Vector4<T>& defaultValue = {}) const
-> mathgl::Vector4<T> {
auto data = this->getListData<T>(columnName, {defaultValue.x, defaultValue.y, defaultValue.z, defaultValue.w});
return mathgl::Vector4<T>{data.size() > 0 ? data.at(0) : 0, data.size() > 1 ? data.at(1) : 0,
data.size() > 2 ? data.at(2) : 0, data.size() > 3 ? data.at(3) : 0};
}
/// \brief Attempts to get a set of vector data out of a nested list array found in this row, for a given columnName.
/// \param columnName Name of the column to get the data for.
/// \param defaultValue Default value that'll be returned if data could not be found.
/// \returns A collection of vector data, potentially padded with zeros if the list was not of a sufficient length,
/// or defaultValue if data could not be found.
template <typename T>
auto getVector2List(const std::string& columnName, const std::vector<mathgl::Vector2<T>>& defaultValue = {}) const
-> std::vector<mathgl::Vector2<T>> {
auto listData = this->getNestedListData<T>(columnName, {});
std::vector<mathgl::Vector2<T>> vectorData;
vectorData.reserve(listData.size());
for (const auto& data : listData) {
vectorData.push_back(mathgl::Vector2<T>{data.size() > 0 ? data[0] : 0, data.size() > 1 ? data[1] : 0});
}
return vectorData;
}
/// \brief Attempts to get a set of vector data out of a nested list array found in this row, for a given columnName.
/// \param columnName Name of the column to get the data for.
/// \param defaultValue Default value that'll be returned if data could not be found.
/// \returns A collection of vector data, potentially padded with zeros if the list was not of a sufficient length,
/// or defaultValue if data could not be found.
template <typename T>
auto getVector3List(const std::string& columnName, const std::vector<mathgl::Vector3<T>>& defaultValue = {}) const
-> std::vector<mathgl::Vector3<T>> {
auto listData = this->getNestedListData<T>(columnName, {});
std::vector<mathgl::Vector3<T>> vectorData;
vectorData.reserve(listData.size());
for (const auto& data : listData) {
vectorData.push_back(mathgl::Vector3<T>{data.size() > 0 ? data[0] : 0, data.size() > 1 ? data[1] : 0,
data.size() > 2 ? data[2] : 0});
}
return vectorData;
}
/// \brief Attempts to get a set of vector data out of a nested list array found in this row, for a given columnName.
/// \param columnName Name of the column to get the data for.
/// \param defaultValue Default value that'll be returned if data could not be found.
/// \returns A collection of vector data, potentially padded with zeros if the list was not of a sufficient length,
/// or defaultValue if data could not be found.
template <typename T>
auto getVector4List(const std::string& columnName, const std::vector<mathgl::Vector4<T>>& defaultValue = {}) const
-> std::vector<mathgl::Vector4<T>> {
auto listData = this->getNestedListData<T>(columnName, {});
std::vector<mathgl::Vector4<T>> vectorData;
vectorData.reserve(listData.size());
for (const auto& data : listData) {
vectorData.push_back(mathgl::Vector4<T>{data.size() > 0 ? data[0] : 0, data.size() > 1 ? data[1] : 0,
data.size() > 2 ? data[2] : 0, data.size() > 3 ? data[3] : 0});
}
return vectorData;
}
/// \brief Attempts to get a variable sized collection of arbitrary data.
/// \param columnName Name of the column to get the data for.
/// \param defaultValue Default value that'll be returned if data could not be found.
/// \returns A collection of requested data type, or defaultValue if data could not be found.
template <typename T>
auto getListData(const std::string& columnName, const std::vector<T>& defaultValue = {}) const -> std::vector<T> {
if (!this->isValid(columnName)) {
probegl::WarningLog() << "Requested column data not valid, returning default value";
return defaultValue;
}
auto optionalMetadata = this->_getListArrayMetadata(this->_getChunk(columnName), this->_chunkRowIndex);
if (!optionalMetadata) {
probegl::WarningLog() << "Requested list type not supported, returning default value";
return defaultValue;
}
return this->_getListData(optionalMetadata.value(), defaultValue);
}
/// \brief Attempts to get a nested, variable sized collection of arbitrary data.
/// \param columnName Name of the column to get the data for.
/// \param defaultValue Default value that'll be returned if data could not be found.
/// \returns A collection of requested data type, or defaultValue if data could not be found.
template <typename T>
auto getNestedListData(const std::string& columnName, const std::vector<std::vector<T>>& defaultValue = {}) const
-> std::vector<std::vector<T>> {
if (!this->isValid(columnName)) {
probegl::WarningLog() << "Requested column data not valid, returning default value";
return defaultValue;
}
auto optionalMetadata = this->_getListArrayMetadata(this->_getChunk(columnName), this->_chunkRowIndex);
if (!optionalMetadata) {
probegl::WarningLog() << "Requested list type not supported, returning default value";
return defaultValue;
}
return this->_getNestedListData(optionalMetadata.value(), defaultValue);
}
/// \brief Checks whether value at this row, for columnName is valid and not null.
/// \param columnName Name of the column who's value to check.
/// \return true if the value for columnName is valid and not null, false otherwise.
auto isValid(const std::string& columnName) const -> bool;
/// \brief Increments current row index.
/// \param increment Amount to increment the current row index by.
void incrementRowIndex(uint64_t increment = 1);
private:
/// \brief Retrieves a chunk for a given columName.
/// \param columnName Name of the column to do the lookup for.
/// \return Returns a chunk within column named columnName, that this row belongs to.
/// \throw Throws an exception if columnName does not exist in the table, or if the chunk couldn't be found.
auto _getChunk(const std::string& columnName) const -> std::shared_ptr<arrow::Array>;
/// \brief Calculates a relative index of a row within the appropriate chunk.
/// \param table Table to do chunk lookup in.
/// \param rowIndex Index of the row to do lookup for.
/// \return Tuple containing index of the chunk this row belongs to, and a row index within the chunk.
/// \throw Throws an exception if rowIndex exceeds table bounds, or if the chunk couldn't be found.
auto _getRowChunkData(const std::shared_ptr<arrow::Table>& table, int64_t rowIndex) const -> std::tuple<int, int64_t>;
auto _getDouble(const std::shared_ptr<arrow::Array>& chunk) const -> std::optional<double>;
auto _getListArrayMetadata(const std::shared_ptr<arrow::Array>& array, int64_t index) const
-> std::optional<ListArrayMetadata>;
template <typename T>
auto _getListData(const ListArrayMetadata& metadata, const std::vector<T>& defaultValue = {}) const
-> std::vector<T> {
std::vector<T> data;
switch (metadata.values->type_id()) {
case arrow::Type::type::DOUBLE: {
auto doubleArray = std::static_pointer_cast<arrow::DoubleArray>(metadata.values);
for (int32_t i = 0; i < metadata.length; i++) {
data.push_back(static_cast<T>(doubleArray->Value(metadata.offset + i)));
}
break;
}
case arrow::Type::type::FLOAT: {
auto floatArray = std::static_pointer_cast<arrow::FloatArray>(metadata.values);
for (int32_t i = 0; i < metadata.length; i++) {
data.push_back(static_cast<T>(floatArray->Value(metadata.offset + i)));
}
break;
}
case arrow::Type::type::INT64: {
auto int64Array = std::static_pointer_cast<arrow::Int64Array>(metadata.values);
for (int32_t i = 0; i < metadata.length; i++) {
data.push_back(static_cast<T>(int64Array->Value(metadata.offset + i)));
}
break;
}
case arrow::Type::type::INT32: {
auto int32Array = std::static_pointer_cast<arrow::Int32Array>(metadata.values);
for (int32_t i = 0; i < metadata.length; i++) {
data.push_back(static_cast<T>(int32Array->Value(metadata.offset + i)));
}
break;
}
default:
probegl::WarningLog() << "Unsupported list value type, returning default value";
return defaultValue;
}
return data;
}
template <typename T>
auto _getNestedListData(const ListArrayMetadata& metadata, const std::vector<std::vector<T>>& defaultValue = {}) const
-> std::vector<std::vector<T>> {
std::vector<std::vector<T>> data;
switch (metadata.values->type_id()) {
case arrow::Type::type::LIST:
case arrow::Type::type::FIXED_SIZE_LIST: {
for (int32_t i = 0; i < metadata.length; i++) {
auto optionalMetadata = this->_getListArrayMetadata(metadata.values, metadata.offset + i);
if (!optionalMetadata) {
probegl::WarningLog() << "List type for nested array not supported, returning default value";
return defaultValue;
}
data.push_back(this->_getListData(optionalMetadata.value(), std::vector<T>{}));
}
break;
}
default:
probegl::WarningLog() << "Unsupported list value type, returning default value";
return defaultValue;
}
return data;
}
/// \brief Table that this row belongs to.
std::shared_ptr<arrow::Table> _table;
/// \brief Index of the row within table.
int64_t _rowIndex;
/// \brief Index of the chunk that this row belongs to.
int _chunkIndex;
/// \brief Relative row index in respect to the chunk.
int64_t _chunkRowIndex;
};
} // namespace deckgl
#endif // DECKGL_CORE_ARROW_ROW_H