dataclass-native 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. dataclass_native/__init__.py +60 -0
  2. dataclass_native/__main__.py +5 -0
  3. dataclass_native/_arrow.py +822 -0
  4. dataclass_native/_json.py +783 -0
  5. dataclass_native/_mode.py +57 -0
  6. dataclass_native/_msgpack.py +207 -0
  7. dataclass_native/_native.py +234 -0
  8. dataclass_native/_runtime.py +607 -0
  9. dataclass_native/_version.py +24 -0
  10. dataclass_native/_wire.py +901 -0
  11. dataclass_native/arrays.py +13 -0
  12. dataclass_native/backends/__init__.py +0 -0
  13. dataclass_native/backends/bindings.py +74 -0
  14. dataclass_native/backends/cpp.py +574 -0
  15. dataclass_native/backends/templates/bindings.cpp.j2 +58 -0
  16. dataclass_native/backends/templates/extensions.cmake.j2 +12 -0
  17. dataclass_native/backends/templates/header.hpp.j2 +75 -0
  18. dataclass_native/cli.py +125 -0
  19. dataclass_native/cmake/dataclass_native-config.cmake +67 -0
  20. dataclass_native/include/dataclass_native/arrow.hpp +2022 -0
  21. dataclass_native/include/dataclass_native/json.hpp +1903 -0
  22. dataclass_native/include/dataclass_native/msgpack.hpp +1513 -0
  23. dataclass_native/include/dataclass_native/python.hpp +594 -0
  24. dataclass_native/include/dataclass_native/reflect.hpp +63 -0
  25. dataclass_native/include/dataclass_native/types.hpp +417 -0
  26. dataclass_native/introspect.py +811 -0
  27. dataclass_native/py.typed +0 -0
  28. dataclass_native/schema.py +553 -0
  29. dataclass_native/types.py +103 -0
  30. dataclass_native-1.0.0.dist-info/METADATA +339 -0
  31. dataclass_native-1.0.0.dist-info/RECORD +35 -0
  32. dataclass_native-1.0.0.dist-info/WHEEL +5 -0
  33. dataclass_native-1.0.0.dist-info/entry_points.txt +2 -0
  34. dataclass_native-1.0.0.dist-info/licenses/LICENSE +21 -0
  35. dataclass_native-1.0.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,2022 @@
1
+ // Holds objects of the generated types in Arrow tables, and reads them from such, in the layout of ARROW_FORMAT.md:
2
+ //
3
+ // auto tables = dataclass_native::arrow::to_arrow<Trade>(trades); // tables.root, and tables.shared
4
+ // std::vector<Trade> read = dataclass_native::arrow::from_arrow<Trade>(schema, array, shared);
5
+ //
6
+ // A table is a struct array, as the Arrow C data interface has it: an ArrowSchema and an ArrowArray, which any Arrow
7
+ // library imports and exports without copying. Nothing here depends on such a library, or on Python.
8
+ #pragma once
9
+
10
+ #include <array>
11
+ #include <bit>
12
+ #include <chrono>
13
+ #include <concepts>
14
+ #include <cstddef>
15
+ #include <cstdint>
16
+ #include <cstring>
17
+ #include <map>
18
+ #include <memory>
19
+ #include <optional>
20
+ #include <set>
21
+ #include <span>
22
+ #include <stdexcept>
23
+ #include <string>
24
+ #include <string_view>
25
+ #include <tuple>
26
+ #include <type_traits>
27
+ #include <unordered_map>
28
+ #include <utility>
29
+ #include <variant>
30
+ #include <vector>
31
+
32
+ #include "dataclass_native/msgpack.hpp"
33
+ #include "dataclass_native/reflect.hpp"
34
+ #include "dataclass_native/types.hpp"
35
+
36
+ // The Arrow C data interface, as its specification gives it to be copied:
37
+ // https://arrow.apache.org/docs/format/CDataInterface.html
38
+ #ifndef ARROW_C_DATA_INTERFACE
39
+ #define ARROW_C_DATA_INTERFACE
40
+
41
+ #define ARROW_FLAG_DICTIONARY_ORDERED 1
42
+ #define ARROW_FLAG_NULLABLE 2
43
+ #define ARROW_FLAG_MAP_KEYS_SORTED 4
44
+
45
+ struct ArrowSchema
46
+ {
47
+ const char* format;
48
+ const char* name;
49
+ const char* metadata;
50
+ int64_t flags;
51
+ int64_t n_children;
52
+ struct ArrowSchema** children;
53
+ struct ArrowSchema* dictionary;
54
+ void (*release)(struct ArrowSchema*);
55
+ void* private_data;
56
+ };
57
+
58
+ struct ArrowArray
59
+ {
60
+ int64_t length;
61
+ int64_t null_count;
62
+ int64_t offset;
63
+ int64_t n_buffers;
64
+ int64_t n_children;
65
+ const void** buffers;
66
+ struct ArrowArray** children;
67
+ struct ArrowArray* dictionary;
68
+ void (*release)(struct ArrowArray*);
69
+ void* private_data;
70
+ };
71
+
72
+ #endif // ARROW_C_DATA_INTERFACE
73
+
74
+ namespace dataclass_native::arrow
75
+ {
76
+ // The tables do not hold the objects being read; or a value cannot be held in one
77
+ class Error : public std::invalid_argument
78
+ {
79
+ public:
80
+ using std::invalid_argument::invalid_argument;
81
+ };
82
+
83
+ // The tables are not laid out as this reads them, though they may hold the objects: e.g. with large lists
84
+ class Unsupported : public Error
85
+ {
86
+ public:
87
+ using Error::Error;
88
+ };
89
+
90
+ // A table: its schema and array, which are released with it unless they have been moved from
91
+ struct Table
92
+ {
93
+ ArrowSchema schema{};
94
+ ArrowArray array{};
95
+
96
+ Table() = default;
97
+
98
+ Table(ArrowSchema schema, ArrowArray array) : schema(schema), array(array)
99
+ {
100
+ }
101
+
102
+ Table(Table&& other) noexcept
103
+ : schema(std::exchange(other.schema, {})), array(std::exchange(other.array, {}))
104
+ {
105
+ }
106
+
107
+ Table& operator=(Table&& other) noexcept
108
+ {
109
+ std::swap(schema, other.schema);
110
+ std::swap(array, other.array);
111
+ return *this;
112
+ }
113
+
114
+ ~Table()
115
+ {
116
+ if (schema.release)
117
+ schema.release(&schema);
118
+ if (array.release)
119
+ array.release(&array);
120
+ }
121
+ };
122
+
123
+ // The table of the objects, and that of each class which they may share: by its tag
124
+ struct Tables
125
+ {
126
+ Table root;
127
+ std::vector<std::pair<std::string_view, Table>> shared;
128
+ };
129
+
130
+ // A table of shared objects which is to be read
131
+ struct SharedTable
132
+ {
133
+ std::string_view tag;
134
+ const ArrowSchema* schema;
135
+ const ArrowArray* array;
136
+ };
137
+
138
+ inline constexpr std::string_view tag_key = "dataclass_native:tag";
139
+ inline constexpr std::string_view shared_key = "dataclass_native:shared";
140
+
141
+ namespace detail
142
+ {
143
+ using msgpack::detail::Kind;
144
+
145
+ using Buffer = std::vector<std::uint8_t>;
146
+
147
+ template <class P>
148
+ void push(Buffer& buffer, P value)
149
+ {
150
+ const auto* bytes = reinterpret_cast<const std::uint8_t*>(&value);
151
+ buffer.insert(buffer.end(), bytes, bytes + sizeof(P));
152
+ }
153
+
154
+ // Bits, the least significant of each byte first: of which values are not null, or of bools
155
+ struct Bits
156
+ {
157
+ Buffer bytes;
158
+ std::int64_t length = 0;
159
+ std::int64_t zeros = 0;
160
+
161
+ void append(bool bit)
162
+ {
163
+ if (length % 8 == 0)
164
+ bytes.push_back(0);
165
+ if (bit)
166
+ bytes.back() = static_cast<std::uint8_t>(bytes.back() | (1u << (length % 8)));
167
+ else
168
+ ++zeros;
169
+ ++length;
170
+ }
171
+
172
+ // So many bits which are set
173
+ void append_set(std::int64_t count)
174
+ {
175
+ for (; count && length % 8; --count)
176
+ append(true);
177
+ bytes.insert(bytes.end(), static_cast<std::size_t>(count / 8), 0xFF);
178
+ length += count - count % 8;
179
+ for (count %= 8; count; --count)
180
+ append(true);
181
+ }
182
+ };
183
+
184
+ inline constexpr std::int64_t day_microseconds = 86'400'000'000;
185
+
186
+ // What a schema owns
187
+ struct SchemaNode
188
+ {
189
+ std::string format;
190
+ std::string name;
191
+ std::string metadata;
192
+ std::vector<ArrowSchema> children;
193
+ std::vector<ArrowSchema*> pointers;
194
+ ArrowSchema dictionary{};
195
+ };
196
+
197
+ inline void release_schema(ArrowSchema* schema)
198
+ {
199
+ auto* node = static_cast<SchemaNode*>(schema->private_data);
200
+ // Not those which have been moved from, as the interface allows
201
+ for (ArrowSchema& child : node->children)
202
+ if (child.release)
203
+ child.release(&child);
204
+ if (node->dictionary.release)
205
+ node->dictionary.release(&node->dictionary);
206
+ delete node;
207
+ schema->release = nullptr;
208
+ }
209
+
210
+ // The one entry of a schema's metadata, as the interface encodes it
211
+ inline std::string metadata(std::string_view key, std::string_view value)
212
+ {
213
+ std::string encoded;
214
+ const auto number = [&](std::size_t n)
215
+ {
216
+ const auto value32 = static_cast<std::int32_t>(n);
217
+ encoded.append(reinterpret_cast<const char*>(&value32), sizeof(value32));
218
+ };
219
+ number(1);
220
+ number(key.size());
221
+ encoded.append(key);
222
+ number(value.size());
223
+ encoded.append(value);
224
+ return encoded;
225
+ }
226
+
227
+ inline ArrowSchema make_schema(std::string format, std::string_view name, std::vector<ArrowSchema> children = {},
228
+ std::int64_t flags = ARROW_FLAG_NULLABLE, std::string metadata = {},
229
+ const ArrowSchema* dictionary = nullptr)
230
+ {
231
+ auto* node = new SchemaNode{std::move(format), std::string(name), std::move(metadata), std::move(children),
232
+ {}, {}};
233
+ for (ArrowSchema& child : node->children)
234
+ node->pointers.push_back(&child);
235
+ if (dictionary)
236
+ node->dictionary = *dictionary;
237
+
238
+ ArrowSchema schema{};
239
+ schema.format = node->format.c_str();
240
+ schema.name = node->name.c_str();
241
+ schema.metadata = node->metadata.empty() ? nullptr : node->metadata.data();
242
+ schema.flags = flags;
243
+ schema.n_children = static_cast<std::int64_t>(node->children.size());
244
+ schema.children = node->pointers.empty() ? nullptr : node->pointers.data();
245
+ schema.dictionary = dictionary ? &node->dictionary : nullptr;
246
+ schema.release = release_schema;
247
+ schema.private_data = node;
248
+ return schema;
249
+ }
250
+
251
+ // What an array owns
252
+ struct ArrayNode
253
+ {
254
+ std::vector<Buffer> buffers;
255
+ std::vector<const void*> pointers;
256
+ std::vector<ArrowArray> children;
257
+ std::vector<ArrowArray*> child_pointers;
258
+ ArrowArray dictionary{};
259
+ };
260
+
261
+ inline void release_array(ArrowArray* array)
262
+ {
263
+ auto* node = static_cast<ArrayNode*>(array->private_data);
264
+ for (ArrowArray& child : node->children)
265
+ if (child.release)
266
+ child.release(&child);
267
+ if (node->dictionary.release)
268
+ node->dictionary.release(&node->dictionary);
269
+ delete node;
270
+ array->release = nullptr;
271
+ }
272
+
273
+ // An array, of which the first buffer is which values are not null, if it has that buffer
274
+ inline ArrowArray make_array(std::int64_t length, std::int64_t nulls, std::vector<Buffer> buffers,
275
+ std::vector<ArrowArray> children = {}, const ArrowArray* dictionary = nullptr,
276
+ bool validity = true)
277
+ {
278
+ auto* node = new ArrayNode{std::move(buffers), {}, std::move(children), {}, {}};
279
+ for (std::size_t i = 0; i != node->buffers.size(); ++i)
280
+ {
281
+ // Never null but for validity, which is when there are no nulls
282
+ node->buffers[i].reserve(8);
283
+ node->pointers.push_back(validity && i == 0 && !nulls ? nullptr : node->buffers[i].data());
284
+ }
285
+ for (ArrowArray& child : node->children)
286
+ node->child_pointers.push_back(&child);
287
+ if (dictionary)
288
+ node->dictionary = *dictionary;
289
+
290
+ ArrowArray array{};
291
+ array.length = length;
292
+ array.null_count = nulls;
293
+ array.n_buffers = static_cast<std::int64_t>(node->buffers.size());
294
+ array.n_children = static_cast<std::int64_t>(node->children.size());
295
+ array.buffers = node->pointers.data();
296
+ array.children = node->child_pointers.empty() ? nullptr : node->child_pointers.data();
297
+ array.dictionary = dictionary ? &node->dictionary : nullptr;
298
+ array.release = release_array;
299
+ array.private_data = node;
300
+ return array;
301
+ }
302
+
303
+ // What names a union's member, as a field of the struct which the union is: its tag, else what msgpack writes
304
+ // it as
305
+ template <class M>
306
+ constexpr std::string_view member_name()
307
+ {
308
+ if (const std::string_view tag = msgpack::detail::tag_of<M>(); !tag.empty())
309
+ return tag;
310
+ return msgpack::detail::Codec<M>::kind == Kind::Map ? "map" : "array";
311
+ }
312
+
313
+ // The type of each member which a tuple of Fields is of
314
+ template <class F>
315
+ struct MemberOf;
316
+
317
+ template <class S, class M>
318
+ struct MemberOf<Field<S, M>>
319
+ {
320
+ using type = M;
321
+ };
322
+
323
+ template <class T>
324
+ constexpr bool shares = msgpack::detail::Codec<T>::shares;
325
+
326
+ // The format of a number
327
+ template <class P>
328
+ constexpr const char* format_of()
329
+ {
330
+ if constexpr (std::floating_point<P>)
331
+ return sizeof(P) == 4 ? "f" : "g";
332
+ else
333
+ {
334
+ constexpr const char* formats[] = {"c", "s", "i", "l", "C", "S", "I", "L"};
335
+ return formats[(std::unsigned_integral<P> ? 4 : 0) + std::countr_zero(sizeof(P))];
336
+ }
337
+ }
338
+
339
+ struct Writer;
340
+
341
+ // The objects of a class which are shared, in the order met
342
+ struct SharedBase
343
+ {
344
+ std::string_view tag;
345
+
346
+ explicit SharedBase(std::string_view tag) : tag(tag)
347
+ {
348
+ }
349
+
350
+ virtual ~SharedBase() = default;
351
+ virtual Table build(Writer& writer) = 0;
352
+ };
353
+
354
+ struct Writer
355
+ {
356
+ std::vector<std::unique_ptr<SharedBase>> tables;
357
+
358
+ template <class S>
359
+ S& table(std::string_view tag)
360
+ {
361
+ for (auto& table : tables)
362
+ if (table->tag == tag)
363
+ return static_cast<S&>(*table);
364
+ throw Error("A class is shared which its Reflect does not say may be");
365
+ }
366
+ };
367
+
368
+ // Builds the column of a type. Each has:
369
+ //
370
+ // discover Finds the objects which a value shares, so that each has its row before any column is built
371
+ // append Adds a value, and append_null a null
372
+ // schema The Arrow type, as a field
373
+ // finish The array of what has been added
374
+ template <class T>
375
+ struct Column;
376
+
377
+ // Values of a fixed size
378
+ template <class P>
379
+ struct Fixed
380
+ {
381
+ Bits validity;
382
+ Buffer data;
383
+
384
+ void put(P value)
385
+ {
386
+ validity.append(true);
387
+ push(data, value);
388
+ }
389
+
390
+ void put(std::span<const P> values)
391
+ {
392
+ validity.append_set(static_cast<std::int64_t>(values.size()));
393
+ const auto* bytes = reinterpret_cast<const std::uint8_t*>(values.data());
394
+ data.insert(data.end(), bytes, bytes + values.size_bytes());
395
+ }
396
+
397
+ void append_null()
398
+ {
399
+ validity.append(false);
400
+ push(data, P{});
401
+ }
402
+
403
+ ArrowArray finish()
404
+ {
405
+ std::vector<Buffer> buffers;
406
+ buffers.push_back(std::move(validity.bytes));
407
+ buffers.push_back(std::move(data));
408
+ return make_array(validity.length, validity.zeros, std::move(buffers));
409
+ }
410
+ };
411
+
412
+ // Values of bytes: strings and binary
413
+ struct Variable
414
+ {
415
+ Bits validity;
416
+ Buffer offsets;
417
+ Buffer data;
418
+
419
+ Variable()
420
+ {
421
+ push(offsets, std::int32_t{0});
422
+ }
423
+
424
+ void put(const void* bytes, std::size_t size)
425
+ {
426
+ const auto* first = static_cast<const std::uint8_t*>(bytes);
427
+ data.insert(data.end(), first, first + size);
428
+ end(true);
429
+ }
430
+
431
+ void append_null()
432
+ {
433
+ end(false);
434
+ }
435
+
436
+ ArrowArray finish()
437
+ {
438
+ std::vector<Buffer> buffers;
439
+ buffers.push_back(std::move(validity.bytes));
440
+ buffers.push_back(std::move(offsets));
441
+ buffers.push_back(std::move(data));
442
+ return make_array(validity.length, validity.zeros, std::move(buffers));
443
+ }
444
+
445
+ private:
446
+ void end(bool valid)
447
+ {
448
+ if (data.size() > 0x7FFF'FFFF)
449
+ throw Error("A column of str or bytes cannot hold more than 2147483647 bytes");
450
+ validity.append(valid);
451
+ push(offsets, static_cast<std::int32_t>(data.size()));
452
+ }
453
+ };
454
+
455
+ template <class T>
456
+ requires std::integral<T> || std::floating_point<T>
457
+ struct Column<T> : Fixed<T>
458
+ {
459
+ static void discover(const T&, Writer&)
460
+ {
461
+ }
462
+
463
+ void append(const T& value, Writer&)
464
+ {
465
+ this->put(value);
466
+ }
467
+
468
+ static ArrowSchema schema(std::string_view name, std::int64_t flags = ARROW_FLAG_NULLABLE)
469
+ {
470
+ return make_schema(format_of<T>(), name, {}, flags);
471
+ }
472
+ };
473
+
474
+ template <>
475
+ struct Column<bool>
476
+ {
477
+ Bits validity;
478
+ Bits values;
479
+
480
+ static void discover(bool, Writer&)
481
+ {
482
+ }
483
+
484
+ void append(bool value, Writer&)
485
+ {
486
+ validity.append(true);
487
+ values.append(value);
488
+ }
489
+
490
+ void append_null()
491
+ {
492
+ validity.append(false);
493
+ values.append(false);
494
+ }
495
+
496
+ static ArrowSchema schema(std::string_view name, std::int64_t flags = ARROW_FLAG_NULLABLE)
497
+ {
498
+ return make_schema("b", name, {}, flags);
499
+ }
500
+
501
+ ArrowArray finish()
502
+ {
503
+ std::vector<Buffer> buffers;
504
+ buffers.push_back(std::move(validity.bytes));
505
+ buffers.push_back(std::move(values.bytes));
506
+ return make_array(validity.length, validity.zeros, std::move(buffers));
507
+ }
508
+ };
509
+
510
+ template <>
511
+ struct Column<std::string> : Variable
512
+ {
513
+ static void discover(const std::string&, Writer&)
514
+ {
515
+ }
516
+
517
+ void append(const std::string& value, Writer&)
518
+ {
519
+ if (!msgpack::detail::is_utf8(value))
520
+ throw Error("A str which is not UTF-8 cannot be held");
521
+ put(value.data(), value.size());
522
+ }
523
+
524
+ static ArrowSchema schema(std::string_view name, std::int64_t flags = ARROW_FLAG_NULLABLE)
525
+ {
526
+ return make_schema("u", name, {}, flags);
527
+ }
528
+ };
529
+
530
+ // bytes, and a tuple of UInt8
531
+ template <>
532
+ struct Column<Bytes> : Variable
533
+ {
534
+ static void discover(const Bytes&, Writer&)
535
+ {
536
+ }
537
+
538
+ void append(const Bytes& value, Writer&)
539
+ {
540
+ put(value.data(), value.size());
541
+ }
542
+
543
+ static ArrowSchema schema(std::string_view name, std::int64_t flags = ARROW_FLAG_NULLABLE)
544
+ {
545
+ return make_schema("z", name, {}, flags);
546
+ }
547
+ };
548
+
549
+ // A Decimal: Arrow's decimal128, of 18 digits, which is its integer in 128 bits
550
+ template <int Places>
551
+ struct Column<Decimal<Places>> : Fixed<std::array<std::int64_t, 2>>
552
+ {
553
+ static void discover(const Decimal<Places>&, Writer&)
554
+ {
555
+ }
556
+
557
+ void append(Decimal<Places> value, Writer&)
558
+ {
559
+ if (!value.fits())
560
+ throw Error("A Decimal of more than 18 digits cannot be held");
561
+ const std::array<std::int64_t, 2> words{value.unscaled, value.unscaled < 0 ? -1 : 0};
562
+ this->put(words);
563
+ }
564
+
565
+ static ArrowSchema schema(std::string_view name, std::int64_t flags = ARROW_FLAG_NULLABLE)
566
+ {
567
+ return make_schema("d:18," + std::to_string(Places), name, {}, flags);
568
+ }
569
+ };
570
+
571
+ // A UUID: its 16 bytes, named as Arrow's arrow.uuid
572
+ template <>
573
+ struct Column<Uuid> : Fixed<Uuid>
574
+ {
575
+ static constexpr const char* format = "w:16";
576
+
577
+ static void discover(const Uuid&, Writer&)
578
+ {
579
+ }
580
+
581
+ void append(const Uuid& value, Writer&)
582
+ {
583
+ put(value);
584
+ }
585
+
586
+ static ArrowSchema schema(std::string_view name, std::int64_t flags = ARROW_FLAG_NULLABLE)
587
+ {
588
+ return make_schema(format, name, {}, flags, metadata("ARROW:extension:name", "arrow.uuid"));
589
+ }
590
+ };
591
+
592
+ // A date or time, held as a number of the unit which its format names
593
+ template <class T, class P>
594
+ struct Temporal : Fixed<P>
595
+ {
596
+ static void discover(const T&, Writer&)
597
+ {
598
+ }
599
+
600
+ static ArrowSchema schema(std::string_view name, std::int64_t flags = ARROW_FLAG_NULLABLE)
601
+ {
602
+ return make_schema(Column<T>::format, name, {}, flags);
603
+ }
604
+ };
605
+
606
+ template <>
607
+ struct Column<Date> : Temporal<Date, std::int32_t>
608
+ {
609
+ static constexpr const char* format = "tdD";
610
+
611
+ void append(Date value, Writer&)
612
+ {
613
+ put(static_cast<std::int32_t>(value.time_since_epoch().count()));
614
+ }
615
+ };
616
+
617
+ template <>
618
+ struct Column<TimeOfDay> : Temporal<TimeOfDay, std::int64_t>
619
+ {
620
+ static constexpr const char* format = "ttu";
621
+
622
+ void append(TimeOfDay value, Writer&)
623
+ {
624
+ const std::int64_t microseconds = value.since_midnight.count();
625
+ if (microseconds < 0 || microseconds >= day_microseconds)
626
+ throw Error("A time which is not within a day cannot be held");
627
+ put(microseconds);
628
+ }
629
+ };
630
+
631
+ template <>
632
+ struct Column<DateTime> : Temporal<DateTime, std::int64_t>
633
+ {
634
+ static constexpr const char* format = "tsu:UTC";
635
+
636
+ void append(DateTime value, Writer&)
637
+ {
638
+ put(value.time_since_epoch().count());
639
+ }
640
+ };
641
+
642
+ // A timestamp of no timezone
643
+ template <>
644
+ struct Column<NaiveDateTime> : Temporal<NaiveDateTime, std::int64_t>
645
+ {
646
+ static constexpr const char* format = "tsu:";
647
+
648
+ void append(NaiveDateTime value, Writer&)
649
+ {
650
+ put(value.time_since_epoch().count());
651
+ }
652
+ };
653
+
654
+ template <>
655
+ struct Column<TimeDelta> : Temporal<TimeDelta, std::int64_t>
656
+ {
657
+ static constexpr const char* format = "tDu";
658
+
659
+ void append(TimeDelta value, Writer&)
660
+ {
661
+ put(value.count());
662
+ }
663
+ };
664
+
665
+ // As the position of its member, in a dictionary of their names
666
+ template <Enum T>
667
+ struct Column<T> : Fixed<std::int32_t>
668
+ {
669
+ static void discover(T, Writer&)
670
+ {
671
+ }
672
+
673
+ void append(T value, Writer&)
674
+ {
675
+ std::int32_t index = 0;
676
+ for (const auto& member : Reflect<T>::members)
677
+ {
678
+ if (member.value == value)
679
+ return put(index);
680
+ ++index;
681
+ }
682
+ throw Error("A value which is not a member of " + std::string(Reflect<T>::name) + " cannot be held");
683
+ }
684
+
685
+ static ArrowSchema schema(std::string_view name, std::int64_t flags = ARROW_FLAG_NULLABLE)
686
+ {
687
+ const ArrowSchema names = make_schema("u", "");
688
+ return make_schema("i", name, {}, flags, {}, &names);
689
+ }
690
+
691
+ ArrowArray finish()
692
+ {
693
+ Variable names;
694
+ for (const auto& member : Reflect<T>::members)
695
+ names.put(member.name.data(), member.name.size());
696
+ const ArrowArray dictionary = names.finish();
697
+ std::vector<Buffer> buffers;
698
+ buffers.push_back(std::move(validity.bytes));
699
+ buffers.push_back(std::move(data));
700
+ return make_array(validity.length, validity.zeros, std::move(buffers), {}, &dictionary);
701
+ }
702
+ };
703
+
704
+ // As its T, or null
705
+ template <class T>
706
+ struct Column<std::optional<T>>
707
+ {
708
+ Column<T> inner;
709
+
710
+ static void discover(const std::optional<T>& value, Writer& writer)
711
+ {
712
+ if (value)
713
+ Column<T>::discover(*value, writer);
714
+ }
715
+
716
+ void append(const std::optional<T>& value, Writer& writer)
717
+ {
718
+ if (value)
719
+ inner.append(*value, writer);
720
+ else
721
+ inner.append_null();
722
+ }
723
+
724
+ void append_null()
725
+ {
726
+ inner.append_null();
727
+ }
728
+
729
+ static ArrowSchema schema(std::string_view name, std::int64_t flags = ARROW_FLAG_NULLABLE)
730
+ {
731
+ return Column<T>::schema(name, flags);
732
+ }
733
+
734
+ ArrowArray finish()
735
+ {
736
+ return inner.finish();
737
+ }
738
+ };
739
+
740
+ // A list: of a tuple's elements, or of a set's in ascending order, which is that it is held in
741
+ template <class C, class E>
742
+ struct List
743
+ {
744
+ Bits validity;
745
+ Buffer offsets;
746
+ Column<E> elements;
747
+ std::int64_t count = 0;
748
+
749
+ List()
750
+ {
751
+ push(offsets, std::int32_t{0});
752
+ }
753
+
754
+ static void discover(const C& value, Writer& writer)
755
+ {
756
+ if constexpr (shares<E>)
757
+ for (const E& element : value)
758
+ Column<E>::discover(element, writer);
759
+ }
760
+
761
+ void append(const C& value, Writer& writer)
762
+ {
763
+ for (const E& element : value)
764
+ elements.append(element, writer);
765
+ count += static_cast<std::int64_t>(value.size());
766
+ end(true);
767
+ }
768
+
769
+ void append_null()
770
+ {
771
+ end(false);
772
+ }
773
+
774
+ static ArrowSchema schema(std::string_view name, std::int64_t flags = ARROW_FLAG_NULLABLE)
775
+ {
776
+ std::vector<ArrowSchema> children;
777
+ children.push_back(Column<E>::schema("item"));
778
+ return make_schema("+l", name, std::move(children), flags);
779
+ }
780
+
781
+ ArrowArray finish()
782
+ {
783
+ std::vector<Buffer> buffers;
784
+ buffers.push_back(std::move(validity.bytes));
785
+ buffers.push_back(std::move(offsets));
786
+ std::vector<ArrowArray> children;
787
+ children.push_back(elements.finish());
788
+ return make_array(validity.length, validity.zeros, std::move(buffers), std::move(children));
789
+ }
790
+
791
+ protected:
792
+ void end(bool valid)
793
+ {
794
+ if (count > 0x7FFF'FFFF)
795
+ throw Error("A column of tuples or frozensets cannot hold more than 2147483647 elements");
796
+ validity.append(valid);
797
+ push(offsets, static_cast<std::int32_t>(count));
798
+ }
799
+ };
800
+
801
+ template <class E>
802
+ struct Column<std::vector<E>> : List<std::vector<E>, E>
803
+ {
804
+ };
805
+
806
+ template <class E>
807
+ struct Column<std::set<E>> : List<std::set<E>, E>
808
+ {
809
+ };
810
+
811
+ // The numbers of an array: as the tuple of them is, but for being added to their column at once
812
+ template <class E>
813
+ struct Numbers : Column<std::vector<E>>
814
+ {
815
+ void append(std::span<const E> values, Writer&)
816
+ {
817
+ if constexpr (std::same_as<std::vector<E>, Bytes>)
818
+ this->put(values.data(), values.size());
819
+ else
820
+ {
821
+ this->elements.put(values);
822
+ this->count += static_cast<std::int64_t>(values.size());
823
+ this->end(true);
824
+ }
825
+ }
826
+ };
827
+
828
+ // An array of one dimension: its numbers
829
+ template <class E, bool W>
830
+ struct Column<Array<E, 1, W>> : Numbers<E>
831
+ {
832
+ static void discover(const Array<E, 1, W>&, Writer&)
833
+ {
834
+ }
835
+
836
+ void append(const Array<E, 1, W>& value, Writer& writer)
837
+ {
838
+ Numbers<E>::append(*value, writer);
839
+ }
840
+ };
841
+
842
+ // An array of more dimensions: a struct of its numbers and its shape, as arrow.variable_shape_tensor is stored
843
+ template <class E, std::size_t N, bool W>
844
+ struct Column<Array<E, N, W>>
845
+ {
846
+ Bits validity;
847
+ Numbers<E> data;
848
+ Bits shapes;
849
+ Fixed<std::int32_t> sizes;
850
+
851
+ static void discover(const Array<E, N, W>&, Writer&)
852
+ {
853
+ }
854
+
855
+ void append(const Array<E, N, W>& value, Writer& writer)
856
+ {
857
+ validity.append(true);
858
+ data.append(*value, writer);
859
+ shapes.append(true);
860
+ for (const std::size_t size : value.shape())
861
+ {
862
+ if (size > 0x7FFF'FFFF)
863
+ throw Error("A column of arrays cannot hold one of more than 2147483647 in a dimension");
864
+ sizes.put(static_cast<std::int32_t>(size));
865
+ }
866
+ }
867
+
868
+ void append_null()
869
+ {
870
+ validity.append(false);
871
+ data.append_null();
872
+ shapes.append(false);
873
+ for (std::size_t i = 0; i != N; ++i)
874
+ sizes.append_null();
875
+ }
876
+
877
+ static ArrowSchema schema(std::string_view name, std::int64_t flags = ARROW_FLAG_NULLABLE)
878
+ {
879
+ std::vector<ArrowSchema> item;
880
+ item.push_back(Column<std::int32_t>::schema("item"));
881
+ std::vector<ArrowSchema> children;
882
+ children.push_back(Numbers<E>::schema("data"));
883
+ children.push_back(make_schema("+w:" + std::to_string(N), "shape", std::move(item)));
884
+ return make_schema("+s", name, std::move(children), flags);
885
+ }
886
+
887
+ ArrowArray finish()
888
+ {
889
+ std::vector<Buffer> held;
890
+ held.push_back(std::move(shapes.bytes));
891
+ std::vector<ArrowArray> item;
892
+ item.push_back(sizes.finish());
893
+ std::vector<ArrowArray> children;
894
+ children.push_back(data.finish());
895
+ children.push_back(make_array(shapes.length, shapes.zeros, std::move(held), std::move(item)));
896
+ std::vector<Buffer> buffers;
897
+ buffers.push_back(std::move(validity.bytes));
898
+ return make_array(validity.length, validity.zeros, std::move(buffers), std::move(children));
899
+ }
900
+ };
901
+
902
+ // A map, in ascending order of key
903
+ template <class K, class V>
904
+ struct Column<std::map<K, V>>
905
+ {
906
+ Bits validity;
907
+ Buffer offsets;
908
+ Column<K> keys;
909
+ Column<V> values;
910
+ std::int64_t count = 0;
911
+
912
+ Column()
913
+ {
914
+ push(offsets, std::int32_t{0});
915
+ }
916
+
917
+ static void discover(const std::map<K, V>& value, Writer& writer)
918
+ {
919
+ if constexpr (shares<V>)
920
+ for (const auto& entry : value)
921
+ Column<V>::discover(entry.second, writer);
922
+ }
923
+
924
+ void append(const std::map<K, V>& value, Writer& writer)
925
+ {
926
+ for (const auto& [key, element] : value)
927
+ {
928
+ keys.append(key, writer);
929
+ values.append(element, writer);
930
+ }
931
+ count += static_cast<std::int64_t>(value.size());
932
+ end(true);
933
+ }
934
+
935
+ void append_null()
936
+ {
937
+ end(false);
938
+ }
939
+
940
+ static ArrowSchema schema(std::string_view name, std::int64_t flags = ARROW_FLAG_NULLABLE)
941
+ {
942
+ // Its entries, and their keys, are never null
943
+ std::vector<ArrowSchema> entry;
944
+ entry.push_back(Column<K>::schema("key", 0));
945
+ entry.push_back(Column<V>::schema("value"));
946
+ std::vector<ArrowSchema> children;
947
+ children.push_back(make_schema("+s", "entries", std::move(entry), 0));
948
+ return make_schema("+m", name, std::move(children), flags | ARROW_FLAG_MAP_KEYS_SORTED);
949
+ }
950
+
951
+ ArrowArray finish()
952
+ {
953
+ std::vector<ArrowArray> entry;
954
+ entry.push_back(keys.finish());
955
+ entry.push_back(values.finish());
956
+ std::vector<Buffer> none;
957
+ none.emplace_back();
958
+ std::vector<ArrowArray> children;
959
+ children.push_back(make_array(count, 0, std::move(none), std::move(entry)));
960
+
961
+ std::vector<Buffer> buffers;
962
+ buffers.push_back(std::move(validity.bytes));
963
+ buffers.push_back(std::move(offsets));
964
+ return make_array(validity.length, validity.zeros, std::move(buffers), std::move(children));
965
+ }
966
+
967
+ private:
968
+ void end(bool valid)
969
+ {
970
+ if (count > 0x7FFF'FFFF)
971
+ throw Error("A column of Mappings cannot hold more than 2147483647 entries");
972
+ validity.append(valid);
973
+ push(offsets, static_cast<std::int32_t>(count));
974
+ }
975
+ };
976
+
977
+ // A struct: of a class's fields, of a tuple's elements, or of a union's members
978
+ template <class... Ms>
979
+ struct StructColumn
980
+ {
981
+ Bits validity;
982
+ std::tuple<Column<Ms>...> columns;
983
+
984
+ void append_null()
985
+ {
986
+ validity.append(false);
987
+ std::apply([](Column<Ms>&... column) { (column.append_null(), ...); }, columns);
988
+ }
989
+
990
+ template <class Names>
991
+ static ArrowSchema schema(std::string_view name, std::int64_t flags, const Names& names,
992
+ std::string metadata = {})
993
+ {
994
+ std::vector<ArrowSchema> children;
995
+ [[maybe_unused]] std::size_t index = 0;
996
+ (children.push_back(Column<Ms>::schema(names[index++])), ...);
997
+ return make_schema("+s", name, std::move(children), flags, std::move(metadata));
998
+ }
999
+
1000
+ ArrowArray finish()
1001
+ {
1002
+ std::vector<ArrowArray> children;
1003
+ std::apply([&](Column<Ms>&... column) { (children.push_back(column.finish()), ...); }, columns);
1004
+ std::vector<Buffer> buffers;
1005
+ buffers.push_back(std::move(validity.bytes));
1006
+ return make_array(validity.length, validity.zeros, std::move(buffers), std::move(children));
1007
+ }
1008
+ };
1009
+
1010
+ // A struct of a field for each element, named by its position
1011
+ template <class... Ts>
1012
+ struct Column<std::tuple<Ts...>> : StructColumn<Ts...>
1013
+ {
1014
+ static void discover(const std::tuple<Ts...>& value, Writer& writer)
1015
+ {
1016
+ std::apply([&](const Ts&... element) { (Column<Ts>::discover(element, writer), ...); }, value);
1017
+ }
1018
+
1019
+ void append(const std::tuple<Ts...>& value, Writer& writer)
1020
+ {
1021
+ this->validity.append(true);
1022
+ std::apply(
1023
+ [&](Column<Ts>&... column)
1024
+ { std::apply([&](const Ts&... element) { (column.append(element, writer), ...); }, value); },
1025
+ this->columns);
1026
+ }
1027
+
1028
+ static ArrowSchema schema(std::string_view name, std::int64_t flags = ARROW_FLAG_NULLABLE)
1029
+ {
1030
+ std::array<std::string, sizeof...(Ts)> names;
1031
+ for (std::size_t i = 0; i != names.size(); ++i)
1032
+ names[i] = std::to_string(i);
1033
+ return StructColumn<Ts...>::schema(name, flags, names);
1034
+ }
1035
+ };
1036
+
1037
+ // A struct of a field for each member, of which that of the member held is not null
1038
+ template <class... Ts>
1039
+ struct Union : StructColumn<Ts...>
1040
+ {
1041
+ static constexpr std::array<std::string_view, sizeof...(Ts)> names{member_name<Ts>()...};
1042
+
1043
+ static void discover(const std::variant<Ts...>& value, Writer& writer)
1044
+ {
1045
+ if constexpr ((shares<Ts> || ...))
1046
+ std::visit(
1047
+ [&](const auto& member)
1048
+ { Column<std::remove_cvref_t<decltype(member)>>::discover(member, writer); },
1049
+ value);
1050
+ }
1051
+
1052
+ void append(const std::variant<Ts...>& value, Writer& writer)
1053
+ {
1054
+ if (value.valueless_by_exception())
1055
+ throw Error("A variant without a value cannot be held");
1056
+ this->validity.append(true);
1057
+ std::size_t index = 0;
1058
+ std::apply(
1059
+ [&](Column<Ts>&... column)
1060
+ {
1061
+ ((index++ == value.index() ? column.append(std::get<Ts>(value), writer) : column.append_null()),
1062
+ ...);
1063
+ },
1064
+ this->columns);
1065
+ }
1066
+
1067
+ static ArrowSchema schema(std::string_view name, std::int64_t flags = ARROW_FLAG_NULLABLE)
1068
+ {
1069
+ return StructColumn<Ts...>::schema(name, flags, names);
1070
+ }
1071
+ };
1072
+
1073
+ template <class... Ts>
1074
+ struct Column<std::variant<Ts...>> : Union<Ts...>
1075
+ {
1076
+ };
1077
+
1078
+ template <class... Ts>
1079
+ struct Column<DiscriminatedUnion<Ts...>> : Union<Ts...>
1080
+ {
1081
+ static void discover(const DiscriminatedUnion<Ts...>& value, Writer& writer)
1082
+ {
1083
+ Union<Ts...>::discover(value.as_variant(), writer);
1084
+ }
1085
+
1086
+ void append(const DiscriminatedUnion<Ts...>& value, Writer& writer)
1087
+ {
1088
+ Union<Ts...>::append(value.as_variant(), writer);
1089
+ }
1090
+ };
1091
+
1092
+ // A struct of a field for each of the class's fields. Defined for each number of fields by Fields
1093
+ template <class T, class Fields>
1094
+ struct ClassColumn;
1095
+
1096
+ template <class T, class... Ms>
1097
+ struct ClassColumn<T, std::tuple<Field<T, Ms>...>> : StructColumn<Ms...>
1098
+ {
1099
+ static void discover(const T& value, Writer& writer)
1100
+ {
1101
+ if constexpr (shares<T>)
1102
+ std::apply(
1103
+ [&](const Field<T, Ms>&... field)
1104
+ { (Column<Ms>::discover(value.*field.member, writer), ...); },
1105
+ Reflect<T>::fields);
1106
+ }
1107
+
1108
+ void append(const T& value, Writer& writer)
1109
+ {
1110
+ this->validity.append(true);
1111
+ std::apply(
1112
+ [&](Column<Ms>&... column)
1113
+ {
1114
+ std::apply([&](const Field<T, Ms>&... field)
1115
+ { (column.append(value.*field.member, writer), ...); },
1116
+ Reflect<T>::fields);
1117
+ },
1118
+ this->columns);
1119
+ }
1120
+
1121
+ static ArrowSchema schema(std::string_view name, std::int64_t flags = ARROW_FLAG_NULLABLE,
1122
+ std::string metadata = {})
1123
+ {
1124
+ const auto names = std::apply(
1125
+ [](const Field<T, Ms>&... field) { return std::array<std::string_view, sizeof...(Ms)>{field.name...}; },
1126
+ Reflect<T>::fields);
1127
+ return StructColumn<Ms...>::schema(name, flags, names, std::move(metadata));
1128
+ }
1129
+ };
1130
+
1131
+ template <dataclass_native::Struct T>
1132
+ struct Column<T> : ClassColumn<T, std::remove_const_t<decltype(Reflect<T>::fields)>>
1133
+ {
1134
+ };
1135
+
1136
+ // An object which is not frozen: as its T
1137
+ template <class T>
1138
+ struct Column<Mutable<T>> : Column<T>
1139
+ {
1140
+ static void discover(const Mutable<T>& value, Writer& writer)
1141
+ {
1142
+ if (value)
1143
+ Column<T>::discover(*value, writer);
1144
+ }
1145
+
1146
+ void append(const Mutable<T>& value, Writer& writer)
1147
+ {
1148
+ if (!value)
1149
+ throw Error("An empty Mutable cannot be held");
1150
+ Column<T>::append(*value, writer);
1151
+ }
1152
+ };
1153
+
1154
+ template <class T>
1155
+ struct SharedObjects : SharedBase
1156
+ {
1157
+ std::vector<const T*> objects;
1158
+ std::unordered_map<const T*, std::uint32_t> rows;
1159
+
1160
+ SharedObjects() : SharedBase(Reflect<T>::tag)
1161
+ {
1162
+ }
1163
+
1164
+ Table build(Writer& writer) override
1165
+ {
1166
+ Column<T> column;
1167
+ for (const T* object : objects)
1168
+ column.append(*object, writer);
1169
+ return {Column<T>::schema("", ARROW_FLAG_NULLABLE, metadata(tag_key, tag)), column.finish()};
1170
+ }
1171
+ };
1172
+
1173
+ // As the row of its T, in the table of T's class
1174
+ template <class T>
1175
+ struct Column<Shared<T>> : Fixed<std::uint32_t>
1176
+ {
1177
+ SharedObjects<T>* table = nullptr;
1178
+
1179
+ static void discover(const Shared<T>& value, Writer& writer)
1180
+ {
1181
+ if (!value)
1182
+ throw Error("An empty Shared cannot be held");
1183
+ auto& table = writer.table<SharedObjects<T>>(Reflect<T>::tag);
1184
+ const auto row = static_cast<std::uint32_t>(table.objects.size());
1185
+ if (table.rows.emplace(value.get(), row).second)
1186
+ {
1187
+ // Its row is taken before what it shares is found, as that may be of its own class
1188
+ table.objects.push_back(value.get());
1189
+ Column<T>::discover(*value, writer);
1190
+ }
1191
+ }
1192
+
1193
+ void append(const Shared<T>& value, Writer& writer)
1194
+ {
1195
+ if (!table)
1196
+ table = &writer.table<SharedObjects<T>>(Reflect<T>::tag);
1197
+ put(table->rows.at(value.get()));
1198
+ }
1199
+
1200
+ static ArrowSchema schema(std::string_view name, std::int64_t flags = ARROW_FLAG_NULLABLE)
1201
+ {
1202
+ return make_schema("I", name, {}, flags, metadata(shared_key, Reflect<T>::tag));
1203
+ }
1204
+ };
1205
+
1206
+ template <class... Ss>
1207
+ void add_tables(Writer& writer, Types<Ss...>)
1208
+ {
1209
+ (writer.tables.push_back(std::make_unique<SharedObjects<Ss>>()), ...);
1210
+ }
1211
+
1212
+ // --- Reading
1213
+
1214
+ [[noreturn]] inline void unsupported()
1215
+ {
1216
+ throw Unsupported("The table is not laid out as it is read");
1217
+ }
1218
+
1219
+ [[noreturn]] inline void invalid()
1220
+ {
1221
+ throw Error("The table does not hold the objects read");
1222
+ }
1223
+
1224
+ inline bool has_format(const ArrowSchema& schema, std::string_view format)
1225
+ {
1226
+ return std::string_view(schema.format) == format;
1227
+ }
1228
+
1229
+ // An array which a view reads, having checked that it has the buffers and children of its format
1230
+ inline const ArrowArray& checked(const ArrowSchema& schema, const ArrowArray& array, std::int64_t buffers,
1231
+ std::int64_t children)
1232
+ {
1233
+ if (array.n_buffers != buffers || array.n_children != children || schema.n_children != children)
1234
+ unsupported();
1235
+ return array;
1236
+ }
1237
+
1238
+ template <class P>
1239
+ P value_at(const ArrowArray& array, int buffer, std::int64_t index)
1240
+ {
1241
+ P value;
1242
+ std::memcpy(&value, static_cast<const std::uint8_t*>(array.buffers[buffer]) + index * sizeof(P), sizeof(P));
1243
+ return value;
1244
+ }
1245
+
1246
+ inline bool bit_at(const void* bits, std::int64_t index)
1247
+ {
1248
+ return (static_cast<const std::uint8_t*>(bits)[index / 8] >> (index % 8)) & 1;
1249
+ }
1250
+
1251
+ // Whether the value at an index of an array, which is from the array's offset, is null
1252
+ inline bool null_at(const ArrowArray& array, std::int64_t index)
1253
+ {
1254
+ return array.null_count != 0 && array.buffers[0] && !bit_at(array.buffers[0], array.offset + index);
1255
+ }
1256
+
1257
+ // Strings, or binary, of either size of offset
1258
+ struct Text
1259
+ {
1260
+ const ArrowArray* array = nullptr;
1261
+ bool large = false;
1262
+
1263
+ Text() = default;
1264
+
1265
+ Text(const ArrowSchema& schema, const ArrowArray& array, std::string_view small, std::string_view big)
1266
+ : array(&checked(schema, array, 3, 0)), large(has_format(schema, big))
1267
+ {
1268
+ if (!large && !has_format(schema, small))
1269
+ unsupported();
1270
+ }
1271
+
1272
+ std::string_view get(std::int64_t index) const
1273
+ {
1274
+ index += array->offset;
1275
+ const std::int64_t start =
1276
+ large ? value_at<std::int64_t>(*array, 1, index) : value_at<std::int32_t>(*array, 1, index);
1277
+ const std::int64_t end = large ? value_at<std::int64_t>(*array, 1, index + 1)
1278
+ : value_at<std::int32_t>(*array, 1, index + 1);
1279
+ return {static_cast<const char*>(array->buffers[2]) + start, static_cast<std::size_t>(end - start)};
1280
+ }
1281
+ };
1282
+
1283
+ struct Reader;
1284
+
1285
+ struct ReadBase
1286
+ {
1287
+ std::string_view tag;
1288
+ virtual ~ReadBase() = default;
1289
+ };
1290
+
1291
+ struct Reader
1292
+ {
1293
+ std::span<const SharedTable> shared;
1294
+ std::vector<std::unique_ptr<ReadBase>> tables;
1295
+
1296
+ template <class S>
1297
+ S& table(std::string_view tag)
1298
+ {
1299
+ for (auto& table : tables)
1300
+ if (table->tag == tag)
1301
+ return static_cast<S&>(*table);
1302
+ for (const SharedTable& given : shared)
1303
+ if (given.tag == tag)
1304
+ {
1305
+ tables.push_back(std::make_unique<S>(*given.schema, *given.array));
1306
+ return static_cast<S&>(*tables.back());
1307
+ }
1308
+ invalid();
1309
+ }
1310
+ };
1311
+
1312
+ // Reads the column of a type: null() is whether a value is null, and get() the value, at an index which is
1313
+ // from the array's offset. Constructing one throws Unsupported if the column is not laid out as it reads
1314
+ template <class T>
1315
+ struct View;
1316
+
1317
+ // The value at an index, which may not be null: but for an optional, of which null() is never true
1318
+ template <class T>
1319
+ T element(const View<T>& view, std::int64_t index, Reader& reader)
1320
+ {
1321
+ if (view.null(index))
1322
+ invalid();
1323
+ return view.get(index, reader);
1324
+ }
1325
+
1326
+ struct Slots
1327
+ {
1328
+ const ArrowArray* array = nullptr;
1329
+
1330
+ bool null(std::int64_t index) const
1331
+ {
1332
+ return null_at(*array, index);
1333
+ }
1334
+ };
1335
+
1336
+ template <class T>
1337
+ requires std::integral<T> || std::floating_point<T>
1338
+ struct View<T> : Slots
1339
+ {
1340
+ View(const ArrowSchema& schema, const ArrowArray& array) : Slots{&checked(schema, array, 2, 0)}
1341
+ {
1342
+ if (!has_format(schema, format_of<T>()))
1343
+ unsupported();
1344
+ }
1345
+
1346
+ T get(std::int64_t index, Reader&) const
1347
+ {
1348
+ return value_at<T>(*array, 1, array->offset + index);
1349
+ }
1350
+ };
1351
+
1352
+ template <>
1353
+ struct View<bool> : Slots
1354
+ {
1355
+ View(const ArrowSchema& schema, const ArrowArray& array) : Slots{&checked(schema, array, 2, 0)}
1356
+ {
1357
+ if (!has_format(schema, "b"))
1358
+ unsupported();
1359
+ }
1360
+
1361
+ bool get(std::int64_t index, Reader&) const
1362
+ {
1363
+ return bit_at(array->buffers[1], array->offset + index);
1364
+ }
1365
+ };
1366
+
1367
+ template <>
1368
+ struct View<std::string> : Slots
1369
+ {
1370
+ Text text;
1371
+
1372
+ View(const ArrowSchema& schema, const ArrowArray& array) : Slots{&array}, text(schema, array, "u", "U")
1373
+ {
1374
+ }
1375
+
1376
+ std::string get(std::int64_t index, Reader&) const
1377
+ {
1378
+ return std::string(text.get(index));
1379
+ }
1380
+ };
1381
+
1382
+ template <>
1383
+ struct View<Bytes> : Slots
1384
+ {
1385
+ Text text;
1386
+
1387
+ View(const ArrowSchema& schema, const ArrowArray& array) : Slots{&array}, text(schema, array, "z", "Z")
1388
+ {
1389
+ }
1390
+
1391
+ Bytes get(std::int64_t index, Reader&) const
1392
+ {
1393
+ const std::string_view bytes = text.get(index);
1394
+ return {bytes.begin(), bytes.end()};
1395
+ }
1396
+ };
1397
+
1398
+ // A decimal128 of any number of digits, but of its own places
1399
+ template <int Places>
1400
+ struct View<Decimal<Places>> : Slots
1401
+ {
1402
+ View(const ArrowSchema& schema, const ArrowArray& array) : Slots{&checked(schema, array, 2, 0)}
1403
+ {
1404
+ const std::string_view format = schema.format;
1405
+ const std::size_t comma = format.find(',');
1406
+ if (!format.starts_with("d:") || comma == format.npos ||
1407
+ format.substr(comma + 1) != std::to_string(Places))
1408
+ unsupported();
1409
+ }
1410
+
1411
+ Decimal<Places> get(std::int64_t index, Reader&) const
1412
+ {
1413
+ const auto words = value_at<std::array<std::int64_t, 2>>(*array, 1, array->offset + index);
1414
+ const Decimal<Places> value{words[0]};
1415
+ if (words[1] != (words[0] < 0 ? -1 : 0) || !value.fits())
1416
+ invalid();
1417
+ return value;
1418
+ }
1419
+ };
1420
+
1421
+ // Whether or not the column is named as arrow.uuid
1422
+ template <>
1423
+ struct View<Uuid> : Slots
1424
+ {
1425
+ View(const ArrowSchema& schema, const ArrowArray& array) : Slots{&checked(schema, array, 2, 0)}
1426
+ {
1427
+ if (!has_format(schema, Column<Uuid>::format))
1428
+ unsupported();
1429
+ }
1430
+
1431
+ Uuid get(std::int64_t index, Reader&) const
1432
+ {
1433
+ return value_at<Uuid>(*array, 1, array->offset + index);
1434
+ }
1435
+ };
1436
+
1437
+ template <class T, class P>
1438
+ struct TemporalView : Slots
1439
+ {
1440
+ TemporalView(const ArrowSchema& schema, const ArrowArray& array) : Slots{&checked(schema, array, 2, 0)}
1441
+ {
1442
+ if (!has_format(schema, Column<T>::format))
1443
+ unsupported();
1444
+ }
1445
+
1446
+ P number(std::int64_t index) const
1447
+ {
1448
+ return value_at<P>(*array, 1, array->offset + index);
1449
+ }
1450
+ };
1451
+
1452
+ template <>
1453
+ struct View<Date> : TemporalView<Date, std::int32_t>
1454
+ {
1455
+ using TemporalView::TemporalView;
1456
+
1457
+ Date get(std::int64_t index, Reader&) const
1458
+ {
1459
+ return Date{std::chrono::days{number(index)}};
1460
+ }
1461
+ };
1462
+
1463
+ template <>
1464
+ struct View<TimeOfDay> : TemporalView<TimeOfDay, std::int64_t>
1465
+ {
1466
+ using TemporalView::TemporalView;
1467
+
1468
+ TimeOfDay get(std::int64_t index, Reader&) const
1469
+ {
1470
+ const std::int64_t microseconds = number(index);
1471
+ if (microseconds < 0 || microseconds >= day_microseconds)
1472
+ invalid();
1473
+ return {std::chrono::microseconds{microseconds}};
1474
+ }
1475
+ };
1476
+
1477
+ template <>
1478
+ struct View<DateTime> : TemporalView<DateTime, std::int64_t>
1479
+ {
1480
+ using TemporalView::TemporalView;
1481
+
1482
+ DateTime get(std::int64_t index, Reader&) const
1483
+ {
1484
+ return DateTime{std::chrono::microseconds{number(index)}};
1485
+ }
1486
+ };
1487
+
1488
+ template <>
1489
+ struct View<NaiveDateTime> : TemporalView<NaiveDateTime, std::int64_t>
1490
+ {
1491
+ using TemporalView::TemporalView;
1492
+
1493
+ NaiveDateTime get(std::int64_t index, Reader&) const
1494
+ {
1495
+ return NaiveDateTime{std::chrono::microseconds{number(index)}};
1496
+ }
1497
+ };
1498
+
1499
+ template <>
1500
+ struct View<TimeDelta> : TemporalView<TimeDelta, std::int64_t>
1501
+ {
1502
+ using TemporalView::TemporalView;
1503
+
1504
+ TimeDelta get(std::int64_t index, Reader&) const
1505
+ {
1506
+ return TimeDelta{number(index)};
1507
+ }
1508
+ };
1509
+
1510
+ // Names, in a dictionary or not
1511
+ template <Enum T>
1512
+ struct View<T> : Slots
1513
+ {
1514
+ Text names;
1515
+ // Of each name in the dictionary, the member it is: if it is one
1516
+ std::vector<std::optional<T>> members;
1517
+ bool dictionary;
1518
+
1519
+ View(const ArrowSchema& schema, const ArrowArray& array)
1520
+ : Slots{&array}, dictionary(schema.dictionary != nullptr)
1521
+ {
1522
+ if (!dictionary)
1523
+ {
1524
+ names = Text(schema, array, "u", "U");
1525
+ return;
1526
+ }
1527
+ if (!has_format(schema, "i") || !array.dictionary)
1528
+ unsupported();
1529
+ checked(schema, array, 2, 0);
1530
+ names = Text(*schema.dictionary, *array.dictionary, "u", "U");
1531
+ for (std::int64_t i = 0; i != array.dictionary->length; ++i)
1532
+ members.push_back(null_at(*array.dictionary, i) ? std::nullopt : member(names.get(i)));
1533
+ }
1534
+
1535
+ T get(std::int64_t index, Reader&) const
1536
+ {
1537
+ if (!dictionary)
1538
+ return required(member(names.get(index)));
1539
+ const auto position = value_at<std::int32_t>(*array, 1, array->offset + index);
1540
+ if (position < 0 || static_cast<std::size_t>(position) >= members.size())
1541
+ invalid();
1542
+ return required(members[static_cast<std::size_t>(position)]);
1543
+ }
1544
+
1545
+ private:
1546
+ static std::optional<T> member(std::string_view name)
1547
+ {
1548
+ for (const auto& member : Reflect<T>::members)
1549
+ if (member.name == name)
1550
+ return member.value;
1551
+ return std::nullopt;
1552
+ }
1553
+
1554
+ static T required(std::optional<T> member)
1555
+ {
1556
+ if (!member)
1557
+ invalid();
1558
+ return *member;
1559
+ }
1560
+ };
1561
+
1562
+ template <class T>
1563
+ struct View<std::optional<T>>
1564
+ {
1565
+ View<T> inner;
1566
+
1567
+ View(const ArrowSchema& schema, const ArrowArray& array) : inner(schema, array)
1568
+ {
1569
+ }
1570
+
1571
+ bool null(std::int64_t) const
1572
+ {
1573
+ return false;
1574
+ }
1575
+
1576
+ std::optional<T> get(std::int64_t index, Reader& reader) const
1577
+ {
1578
+ if (inner.null(index))
1579
+ return std::nullopt;
1580
+ return inner.get(index, reader);
1581
+ }
1582
+ };
1583
+
1584
+ // The elements of a list, or the entries of a map, at an index: from first to before last, of their array
1585
+ inline std::pair<std::int64_t, std::int64_t> range_at(const ArrowArray& array, std::int64_t index)
1586
+ {
1587
+ index += array.offset;
1588
+ return {value_at<std::int32_t>(array, 1, index), value_at<std::int32_t>(array, 1, index + 1)};
1589
+ }
1590
+
1591
+ template <class C, class E>
1592
+ struct ListView : Slots
1593
+ {
1594
+ View<E> elements;
1595
+
1596
+ ListView(const ArrowSchema& schema, const ArrowArray& array)
1597
+ : Slots{&checked(schema, array, 2, 1)}, elements(child(schema), *array.children[0])
1598
+ {
1599
+ }
1600
+
1601
+ C get(std::int64_t index, Reader& reader) const
1602
+ {
1603
+ C value;
1604
+ for (auto [at, end] = range_at(*array, index); at != end; ++at)
1605
+ value.insert(value.end(), element(elements, at, reader));
1606
+ return value;
1607
+ }
1608
+
1609
+ private:
1610
+ static const ArrowSchema& child(const ArrowSchema& schema)
1611
+ {
1612
+ if (!has_format(schema, "+l") || schema.n_children != 1)
1613
+ unsupported();
1614
+ return *schema.children[0];
1615
+ }
1616
+ };
1617
+
1618
+ template <class E>
1619
+ struct View<std::vector<E>> : ListView<std::vector<E>, E>
1620
+ {
1621
+ using ListView<std::vector<E>, E>::ListView;
1622
+ };
1623
+
1624
+ template <class E>
1625
+ struct View<std::set<E>> : ListView<std::set<E>, E>
1626
+ {
1627
+ using ListView<std::set<E>, E>::ListView;
1628
+ };
1629
+
1630
+ // The numbers of an array
1631
+ template <class E>
1632
+ struct NumbersView : View<std::vector<E>>
1633
+ {
1634
+ using View<std::vector<E>>::View;
1635
+
1636
+ std::vector<E> get(std::int64_t index, Reader& reader) const
1637
+ {
1638
+ if constexpr (!std::same_as<std::vector<E>, Bytes>)
1639
+ {
1640
+ // At once, if none is null
1641
+ const ArrowArray& numbers = *this->elements.array;
1642
+ if (const auto [at, end] = range_at(*this->array, index); numbers.null_count == 0 && at < end)
1643
+ {
1644
+ std::vector<E> values(static_cast<std::size_t>(end - at));
1645
+ std::memcpy(values.data(),
1646
+ static_cast<const std::uint8_t*>(numbers.buffers[1]) +
1647
+ (numbers.offset + at) * sizeof(E),
1648
+ values.size() * sizeof(E));
1649
+ return values;
1650
+ }
1651
+ }
1652
+ return View<std::vector<E>>::get(index, reader);
1653
+ }
1654
+ };
1655
+
1656
+ template <class E, bool W>
1657
+ struct View<Array<E, 1, W>> : NumbersView<E>
1658
+ {
1659
+ using NumbersView<E>::NumbersView;
1660
+
1661
+ Array<E, 1, W> get(std::int64_t index, Reader& reader) const
1662
+ {
1663
+ return NumbersView<E>::get(index, reader);
1664
+ }
1665
+ };
1666
+
1667
+ // An array of more dimensions: the struct of its numbers and its shape
1668
+ template <class E, std::size_t N, bool W>
1669
+ struct View<Array<E, N, W>> : Slots
1670
+ {
1671
+ NumbersView<E> data;
1672
+ const ArrowArray* shapes;
1673
+ View<std::int32_t> sizes;
1674
+
1675
+ View(const ArrowSchema& schema, const ArrowArray& array)
1676
+ : Slots{&laid_out(schema, array)}, data(*schema.children[0], *array.children[0]),
1677
+ shapes(array.children[1]), sizes(*schema.children[1]->children[0], *shapes->children[0])
1678
+ {
1679
+ }
1680
+
1681
+ Array<E, N, W> get(std::int64_t index, Reader& reader) const
1682
+ {
1683
+ index += array->offset;
1684
+ if (data.null(index) || null_at(*shapes, index))
1685
+ invalid();
1686
+ typename Array<E, N, W>::Shape shape;
1687
+ const std::int64_t first = (shapes->offset + index) * static_cast<std::int64_t>(N);
1688
+ for (std::size_t i = 0; i != N; ++i)
1689
+ {
1690
+ const std::int32_t size = element(sizes, first + static_cast<std::int64_t>(i), reader);
1691
+ if (size < 0)
1692
+ invalid();
1693
+ shape[i] = static_cast<std::size_t>(size);
1694
+ }
1695
+ std::vector<E> values = data.get(index, reader);
1696
+ if (!Array<E, N, W>::fits(shape, values.size()))
1697
+ invalid();
1698
+ return {std::move(values), shape};
1699
+ }
1700
+
1701
+ private:
1702
+ static const ArrowArray& laid_out(const ArrowSchema& schema, const ArrowArray& array)
1703
+ {
1704
+ if (!has_format(schema, "+s"))
1705
+ unsupported();
1706
+ checked(schema, array, 1, 2);
1707
+ const ArrowSchema& shape = *schema.children[1];
1708
+ if (!named(*schema.children[0], "data") || !named(shape, "shape") ||
1709
+ !has_format(shape, "+w:" + std::to_string(N)))
1710
+ unsupported();
1711
+ checked(shape, *array.children[1], 1, 1);
1712
+ return array;
1713
+ }
1714
+
1715
+ static bool named(const ArrowSchema& schema, std::string_view name)
1716
+ {
1717
+ return schema.name && name == schema.name;
1718
+ }
1719
+ };
1720
+
1721
+ template <class K, class V>
1722
+ struct View<std::map<K, V>> : Slots
1723
+ {
1724
+ const ArrowArray* entries;
1725
+ View<K> keys;
1726
+ View<V> values;
1727
+
1728
+ View(const ArrowSchema& schema, const ArrowArray& array)
1729
+ : Slots{&checked(schema, array, 2, 1)}, entries(array.children[0]),
1730
+ keys(*entry(schema).children[0], *checked(entry(schema), *entries, 1, 2).children[0]),
1731
+ values(*entry(schema).children[1], *entries->children[1])
1732
+ {
1733
+ }
1734
+
1735
+ std::map<K, V> get(std::int64_t index, Reader& reader) const
1736
+ {
1737
+ std::map<K, V> value;
1738
+ for (auto [at, end] = range_at(*array, index); at != end; ++at)
1739
+ {
1740
+ const std::int64_t entry = entries->offset + at;
1741
+ K key = element(keys, entry, reader);
1742
+ if (!value.emplace(std::move(key), element(values, entry, reader)).second)
1743
+ invalid();
1744
+ }
1745
+ return value;
1746
+ }
1747
+
1748
+ private:
1749
+ static const ArrowSchema& entry(const ArrowSchema& schema)
1750
+ {
1751
+ if (!has_format(schema, "+m") || schema.n_children != 1 || !has_format(*schema.children[0], "+s"))
1752
+ unsupported();
1753
+ return *schema.children[0];
1754
+ }
1755
+ };
1756
+
1757
+ // The views of a struct's fields, each of which is the field of a name: if the struct has it
1758
+ template <class... Ms>
1759
+ struct StructView : Slots
1760
+ {
1761
+ std::tuple<std::optional<View<Ms>>...> views;
1762
+
1763
+ template <class Names>
1764
+ StructView(const ArrowSchema& schema, const ArrowArray& array, const Names& names) : Slots{&array}
1765
+ {
1766
+ if (!has_format(schema, "+s") || array.n_buffers != 1 || array.n_children != schema.n_children)
1767
+ unsupported();
1768
+ [[maybe_unused]] std::size_t index = 0;
1769
+ std::apply(
1770
+ [&](std::optional<View<Ms>>&... view)
1771
+ {
1772
+ ((find(schema, array, names[index++], view)), ...);
1773
+ },
1774
+ views);
1775
+ }
1776
+
1777
+ private:
1778
+ template <class M>
1779
+ static void find(const ArrowSchema& schema, const ArrowArray& array, std::string_view name,
1780
+ std::optional<View<M>>& view)
1781
+ {
1782
+ for (std::int64_t child = 0; child != schema.n_children; ++child)
1783
+ if (schema.children[child]->name && name == schema.children[child]->name)
1784
+ {
1785
+ view.emplace(*schema.children[child], *array.children[child]);
1786
+ return;
1787
+ }
1788
+ }
1789
+ };
1790
+
1791
+ template <class... Ts>
1792
+ struct View<std::tuple<Ts...>> : StructView<Ts...>
1793
+ {
1794
+ View(const ArrowSchema& schema, const ArrowArray& array) : StructView<Ts...>(schema, array, names())
1795
+ {
1796
+ if (!std::apply([](const auto&... view) { return (view.has_value() && ...); }, this->views))
1797
+ invalid();
1798
+ }
1799
+
1800
+ std::tuple<Ts...> get(std::int64_t index, Reader& reader) const
1801
+ {
1802
+ index += this->array->offset;
1803
+ return std::apply(
1804
+ [&](const std::optional<View<Ts>>&... view)
1805
+ {
1806
+ // In braces, so in order
1807
+ return std::tuple<Ts...>{element(*view, index, reader)...};
1808
+ },
1809
+ this->views);
1810
+ }
1811
+
1812
+ private:
1813
+ static std::array<std::string, sizeof...(Ts)> names()
1814
+ {
1815
+ std::array<std::string, sizeof...(Ts)> names;
1816
+ for (std::size_t i = 0; i != names.size(); ++i)
1817
+ names[i] = std::to_string(i);
1818
+ return names;
1819
+ }
1820
+ };
1821
+
1822
+ template <class... Ts>
1823
+ struct UnionView : StructView<Ts...>
1824
+ {
1825
+ UnionView(const ArrowSchema& schema, const ArrowArray& array)
1826
+ : StructView<Ts...>(schema, array, Union<Ts...>::names)
1827
+ {
1828
+ }
1829
+
1830
+ std::variant<Ts...> get(std::int64_t index, Reader& reader) const
1831
+ {
1832
+ index += this->array->offset;
1833
+ // The one member which is not null
1834
+ std::optional<std::variant<Ts...>> value;
1835
+ std::apply(
1836
+ [&](const std::optional<View<Ts>>&... view)
1837
+ {
1838
+ ((view && !view->null(index)
1839
+ ? (value ? invalid() : void(value.emplace(std::in_place_type<Ts>, view->get(index, reader))))
1840
+ : void()),
1841
+ ...);
1842
+ },
1843
+ this->views);
1844
+ if (!value)
1845
+ invalid();
1846
+ return std::move(*value);
1847
+ }
1848
+ };
1849
+
1850
+ template <class... Ts>
1851
+ struct View<std::variant<Ts...>> : UnionView<Ts...>
1852
+ {
1853
+ using UnionView<Ts...>::UnionView;
1854
+ };
1855
+
1856
+ template <class... Ts>
1857
+ struct View<DiscriminatedUnion<Ts...>> : UnionView<Ts...>
1858
+ {
1859
+ using UnionView<Ts...>::UnionView;
1860
+
1861
+ DiscriminatedUnion<Ts...> get(std::int64_t index, Reader& reader) const
1862
+ {
1863
+ return DiscriminatedUnion<Ts...>(UnionView<Ts...>::get(index, reader));
1864
+ }
1865
+ };
1866
+
1867
+ template <class T, class Fields>
1868
+ struct ClassView;
1869
+
1870
+ template <class T, class... Ms>
1871
+ struct ClassView<T, std::tuple<Field<T, Ms>...>> : StructView<Ms...>
1872
+ {
1873
+ ClassView(const ArrowSchema& schema, const ArrowArray& array) : StructView<Ms...>(schema, array, names())
1874
+ {
1875
+ // A field which has no column takes its default, if it has one
1876
+ std::apply(
1877
+ [&](const std::optional<View<Ms>>&... view)
1878
+ {
1879
+ std::apply([&](const Field<T, Ms>&... field)
1880
+ { ((view || field.has_default ? void() : invalid()), ...); },
1881
+ Reflect<T>::fields);
1882
+ },
1883
+ this->views);
1884
+ }
1885
+
1886
+ T get(std::int64_t index, Reader& reader) const
1887
+ {
1888
+ index += this->array->offset;
1889
+ T value{};
1890
+ std::apply(
1891
+ [&](const std::optional<View<Ms>>&... view)
1892
+ {
1893
+ std::apply(
1894
+ [&](const Field<T, Ms>&... field)
1895
+ { ((view ? void(value.*field.member = element(*view, index, reader)) : void()), ...); },
1896
+ Reflect<T>::fields);
1897
+ },
1898
+ this->views);
1899
+ return value;
1900
+ }
1901
+
1902
+ private:
1903
+ static std::array<std::string_view, sizeof...(Ms)> names()
1904
+ {
1905
+ return std::apply(
1906
+ [](const Field<T, Ms>&... field) { return std::array<std::string_view, sizeof...(Ms)>{field.name...}; },
1907
+ Reflect<T>::fields);
1908
+ }
1909
+ };
1910
+
1911
+ template <dataclass_native::Struct T>
1912
+ struct View<T> : ClassView<T, std::remove_const_t<decltype(Reflect<T>::fields)>>
1913
+ {
1914
+ using ClassView<T, std::remove_const_t<decltype(Reflect<T>::fields)>>::ClassView;
1915
+ };
1916
+
1917
+ template <class T>
1918
+ struct View<Mutable<T>> : View<T>
1919
+ {
1920
+ using View<T>::View;
1921
+
1922
+ Mutable<T> get(std::int64_t index, Reader& reader) const
1923
+ {
1924
+ return Mutable<T>(View<T>::get(index, reader));
1925
+ }
1926
+ };
1927
+
1928
+ // The table of a class's shared objects, and those of them which have been read
1929
+ template <class T>
1930
+ struct SharedRows : ReadBase
1931
+ {
1932
+ View<T> view;
1933
+ std::vector<std::shared_ptr<const T>> objects;
1934
+ std::vector<bool> reading;
1935
+
1936
+ SharedRows(const ArrowSchema& schema, const ArrowArray& array)
1937
+ : view(schema, array), objects(static_cast<std::size_t>(array.length)),
1938
+ reading(static_cast<std::size_t>(array.length))
1939
+ {
1940
+ tag = Reflect<T>::tag;
1941
+ }
1942
+ };
1943
+
1944
+ template <class T>
1945
+ struct View<Shared<T>> : Slots
1946
+ {
1947
+ View(const ArrowSchema& schema, const ArrowArray& array) : Slots{&checked(schema, array, 2, 0)}
1948
+ {
1949
+ if (!has_format(schema, "I"))
1950
+ unsupported();
1951
+ }
1952
+
1953
+ Shared<T> get(std::int64_t index, Reader& reader) const
1954
+ {
1955
+ const auto row = value_at<std::uint32_t>(*array, 1, array->offset + index);
1956
+ auto& table = reader.table<SharedRows<T>>(Reflect<T>::tag);
1957
+ if (row >= table.objects.size())
1958
+ invalid();
1959
+ if (!table.objects[row])
1960
+ {
1961
+ // An object which shares itself, which no object held could
1962
+ if (table.reading[row])
1963
+ invalid();
1964
+ table.reading[row] = true;
1965
+ table.objects[row] = std::make_shared<const T>(element(table.view, row, reader));
1966
+ }
1967
+ return Shared<T>(table.objects[row]);
1968
+ }
1969
+ };
1970
+ } // namespace detail
1971
+
1972
+ // The objects as tables: a row for each, and the tables of the objects which they share
1973
+ template <class T>
1974
+ Tables to_arrow(std::span<const T* const> objects)
1975
+ {
1976
+ static_assert(Reflect<T>::arrow, "A class which holds itself cannot be held in an Arrow table");
1977
+ detail::Writer writer;
1978
+ detail::add_tables(writer, typename Reflect<T>::shared{});
1979
+ // First, so that each shared object has its row
1980
+ for (const T* object : objects)
1981
+ detail::Column<T>::discover(*object, writer);
1982
+
1983
+ detail::Column<T> column;
1984
+ for (const T* object : objects)
1985
+ column.append(*object, writer);
1986
+
1987
+ Tables tables;
1988
+ const std::string_view tag = Reflect<T>::tag;
1989
+ tables.root = {detail::Column<T>::schema("", ARROW_FLAG_NULLABLE, detail::metadata(tag_key, tag)),
1990
+ column.finish()};
1991
+ for (auto& table : writer.tables)
1992
+ tables.shared.emplace_back(table->tag, table->build(writer));
1993
+ return tables;
1994
+ }
1995
+
1996
+ template <class T>
1997
+ Tables to_arrow(std::span<const T> objects)
1998
+ {
1999
+ std::vector<const T*> pointers;
2000
+ pointers.reserve(objects.size());
2001
+ for (const T& object : objects)
2002
+ pointers.push_back(&object);
2003
+ return to_arrow<T>(std::span<const T* const>(pointers));
2004
+ }
2005
+
2006
+ // The objects which a table holds, given the tables of the objects which they share. Throws Unsupported if a
2007
+ // table is not laid out as this reads it, and otherwise Error if they do not hold objects of the class.
2008
+ // The tables must be valid Arrow: that is not checked
2009
+ template <class T>
2010
+ std::vector<T> from_arrow(const ArrowSchema& schema, const ArrowArray& array,
2011
+ std::span<const SharedTable> shared = {})
2012
+ {
2013
+ static_assert(Reflect<T>::arrow, "A class which holds itself cannot be held in an Arrow table");
2014
+ detail::Reader reader{shared, {}};
2015
+ const detail::View<T> view(schema, array);
2016
+ std::vector<T> objects;
2017
+ objects.reserve(static_cast<std::size_t>(array.length));
2018
+ for (std::int64_t row = 0; row != array.length; ++row)
2019
+ objects.push_back(detail::element(view, row, reader));
2020
+ return objects;
2021
+ }
2022
+ } // namespace dataclass_native::arrow