|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <optional> |
| 4 | +#include <unordered_set> |
| 5 | +#include <vector> |
| 6 | + |
| 7 | +#include <sparrow/arrow_interface/arrow_array_schema_proxy.hpp> |
| 8 | + |
| 9 | +#include "Message_generated.h" |
| 10 | +#include "sparrow_ipc/arrow_interface/arrow_array.hpp" |
| 11 | +#include "sparrow_ipc/arrow_interface/arrow_schema.hpp" |
| 12 | +#include "sparrow_ipc/deserialize_utils.hpp" |
| 13 | + |
| 14 | +namespace sparrow_ipc::detail |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @brief Generic implementation for deserializing non-owning arrays with simple layout. |
| 18 | + * |
| 19 | + * This function provides the common deserialization logic for array types that have |
| 20 | + * a validity buffer and a single data buffer (e.g., primitive_array, interval_array). |
| 21 | + * |
| 22 | + * @tparam ArrayType The array type template (e.g., sparrow::primitive_array) |
| 23 | + * @tparam T The element type |
| 24 | + * |
| 25 | + * @param record_batch The FlatBuffer RecordBatch containing metadata |
| 26 | + * @param body The raw buffer data |
| 27 | + * @param name The array column name |
| 28 | + * @param metadata Optional metadata pairs |
| 29 | + * @param nullable Whether the array is nullable |
| 30 | + * @param buffer_index The current buffer index (incremented by this function) |
| 31 | + * |
| 32 | + * @return The deserialized array of type ArrayType<T> |
| 33 | + */ |
| 34 | + template <template<typename...> class ArrayType, typename T> |
| 35 | + [[nodiscard]] ArrayType<T> deserialize_non_owning_simple_array( |
| 36 | + const org::apache::arrow::flatbuf::RecordBatch& record_batch, |
| 37 | + std::span<const uint8_t> body, |
| 38 | + std::string_view name, |
| 39 | + const std::optional<std::vector<sparrow::metadata_pair>>& metadata, |
| 40 | + bool nullable, |
| 41 | + size_t& buffer_index |
| 42 | + ) |
| 43 | + { |
| 44 | + const std::string_view format = data_type_to_format( |
| 45 | + sparrow::detail::get_data_type_from_array<ArrayType<T>>::get() |
| 46 | + ); |
| 47 | + |
| 48 | + // Set up flags based on nullable |
| 49 | + std::optional<std::unordered_set<sparrow::ArrowFlag>> flags; |
| 50 | + if (nullable) |
| 51 | + { |
| 52 | + flags = std::unordered_set<sparrow::ArrowFlag>{sparrow::ArrowFlag::NULLABLE}; |
| 53 | + } |
| 54 | + |
| 55 | + ArrowSchema schema = make_non_owning_arrow_schema( |
| 56 | + format, |
| 57 | + name.data(), |
| 58 | + metadata, |
| 59 | + flags, |
| 60 | + 0, |
| 61 | + nullptr, |
| 62 | + nullptr |
| 63 | + ); |
| 64 | + |
| 65 | + const auto compression = record_batch.compression(); |
| 66 | + std::vector<arrow_array_private_data::optionally_owned_buffer> buffers; |
| 67 | + |
| 68 | + auto validity_buffer_span = utils::get_buffer(record_batch, body, buffer_index); |
| 69 | + auto data_buffer_span = utils::get_buffer(record_batch, body, buffer_index); |
| 70 | + |
| 71 | + if (compression) |
| 72 | + { |
| 73 | + buffers.push_back(utils::get_decompressed_buffer(validity_buffer_span, compression)); |
| 74 | + buffers.push_back(utils::get_decompressed_buffer(data_buffer_span, compression)); |
| 75 | + } |
| 76 | + else |
| 77 | + { |
| 78 | + buffers.emplace_back(validity_buffer_span); |
| 79 | + buffers.emplace_back(data_buffer_span); |
| 80 | + } |
| 81 | + |
| 82 | + // TODO bitmap_ptr is not used anymore... Leave it for now, and remove later if no need confirmed |
| 83 | + const auto [bitmap_ptr, null_count] = utils::get_bitmap_pointer_and_null_count(validity_buffer_span, record_batch.length()); |
| 84 | + |
| 85 | + ArrowArray array = make_arrow_array<arrow_array_private_data>( |
| 86 | + record_batch.length(), |
| 87 | + null_count, |
| 88 | + 0, |
| 89 | + 0, |
| 90 | + nullptr, |
| 91 | + nullptr, |
| 92 | + std::move(buffers) |
| 93 | + ); |
| 94 | + |
| 95 | + sparrow::arrow_proxy ap{std::move(array), std::move(schema)}; |
| 96 | + return ArrayType<T>{std::move(ap)}; |
| 97 | + } |
| 98 | +} |
0 commit comments