sparrow-ipc 0.2.0
Loading...
Searching...
No Matches
deserialize_variable_size_binary_array.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <span>
4#include <unordered_set>
5
6#include <sparrow/arrow_interface/arrow_array_schema_proxy.hpp>
7#include <sparrow/variable_size_binary_array.hpp>
8
9#include "Message_generated.h"
13
14namespace sparrow_ipc
15{
16 template <typename T>
18 const org::apache::arrow::flatbuf::RecordBatch& record_batch,
19 std::span<const uint8_t> body,
20 std::string_view name,
21 const std::optional<std::vector<sparrow::metadata_pair>>& metadata,
22 bool nullable,
23 size_t& buffer_index
24 )
25 {
26 const std::string_view format = data_type_to_format(sparrow::detail::get_data_type_from_array<T>::get());
27
28 // Set up flags based on nullable
29 std::optional<std::unordered_set<sparrow::ArrowFlag>> flags;
30 if (nullable)
31 {
32 flags = std::unordered_set<sparrow::ArrowFlag>{sparrow::ArrowFlag::NULLABLE};
33 }
34
35 ArrowSchema schema = make_non_owning_arrow_schema(
36 format,
37 name.data(),
38 metadata,
39 flags,
40 0,
41 nullptr,
42 nullptr
43 );
44
45 const auto compression = record_batch.compression();
46 std::vector<arrow_array_private_data::optionally_owned_buffer> buffers;
47
48 auto validity_buffer_span = utils::get_buffer(record_batch, body, buffer_index);
49 auto offset_buffer_span = utils::get_buffer(record_batch, body, buffer_index);
50 auto data_buffer_span = utils::get_buffer(record_batch, body, buffer_index);
51
52 if (compression)
53 {
54 buffers.push_back(utils::get_decompressed_buffer(validity_buffer_span, compression));
55 buffers.push_back(utils::get_decompressed_buffer(offset_buffer_span, compression));
56 buffers.push_back(utils::get_decompressed_buffer(data_buffer_span, compression));
57 }
58 else
59 {
60 buffers.push_back(validity_buffer_span);
61 buffers.push_back(offset_buffer_span);
62 buffers.push_back(data_buffer_span);
63 }
64
65 // TODO bitmap_ptr is not used anymore... Leave it for now, and remove later if no need confirmed
66 const auto [bitmap_ptr, null_count] = utils::get_bitmap_pointer_and_null_count(validity_buffer_span, record_batch.length());
67
69 record_batch.length(),
70 null_count,
71 0,
72 0,
73 nullptr,
74 nullptr,
75 std::move(buffers)
76 );
77
78 sparrow::arrow_proxy ap{std::move(array), std::move(schema)};
79 return T{std::move(ap)};
80 }
81}
std::span< const uint8_t > get_buffer(const org::apache::arrow::flatbuf::RecordBatch &record_batch, std::span< const uint8_t > body, size_t &buffer_index)
Extracts a buffer from a RecordBatch's body.
std::pair< std::uint8_t *, int64_t > get_bitmap_pointer_and_null_count(std::span< const uint8_t > validity_buffer_span, const int64_t length)
Extracts bitmap pointer and null count from a validity buffer span.
std::variant< std::vector< std::uint8_t >, std::span< const std::uint8_t > > get_decompressed_buffer(std::span< const uint8_t > buffer_span, const org::apache::arrow::flatbuf::BodyCompression *compression)
Retrieves a decompressed buffer or a view of the original buffer.
ArrowSchema make_non_owning_arrow_schema(std::string_view format, const char *name, std::optional< M > metadata, std::optional< std::unordered_set< sparrow::ArrowFlag > > flags, size_t children_count, ArrowSchema **children, ArrowSchema *dictionary)
T deserialize_non_owning_variable_size_binary(const org::apache::arrow::flatbuf::RecordBatch &record_batch, std::span< const uint8_t > body, std::string_view name, const std::optional< std::vector< sparrow::metadata_pair > > &metadata, bool nullable, size_t &buffer_index)
ArrowArray make_arrow_array(int64_t length, int64_t null_count, int64_t offset, size_t children_count, ArrowArray **children, ArrowArray *dictionary, Arg &&private_data_arg)