sparrow-ipc 0.2.0
Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <optional>
5#include <string_view>
6
7#include <sparrow/record_batch.hpp>
8
10
11namespace sparrow_ipc::utils
12{
13 // Aligns a value to the next multiple of 8, as required by the Arrow IPC format for message bodies
14 constexpr size_t align_to_8(const size_t n)
15 {
16 return (n + 7) & -8;
17 }
18
31 SPARROW_IPC_API std::optional<std::string_view> get_substr_after_separator(std::string_view str, std::string_view sep);
32
47 SPARROW_IPC_API std::optional<int32_t> parse_to_int32(std::string_view str);
48
62 SPARROW_IPC_API std::optional<int32_t> get_substr_as_int32(std::string_view str, std::string_view sep);
63
80 SPARROW_IPC_API std::optional<std::tuple<int32_t, int32_t, std::optional<int32_t>>> parse_decimal_format(std::string_view format_str);
81
97 SPARROW_IPC_API std::vector<std::string_view> extract_words_after_colon(std::string_view str);
98
114 template <std::ranges::input_range R>
115 requires std::same_as<std::ranges::range_value_t<R>, sparrow::record_batch>
116 bool check_record_batches_consistency(const R& record_batches)
117 {
118 if (record_batches.empty() || record_batches.size() == 1)
119 {
120 return true;
121 }
122 const sparrow::record_batch& first_rb = record_batches[0];
123 const size_t first_rb_nb_columns = first_rb.nb_columns();
124 for (const sparrow::record_batch& rb : record_batches)
125 {
126 const auto rb_nb_columns = rb.nb_columns();
127 if (rb_nb_columns != first_rb_nb_columns)
128 {
129 return false;
130 }
131 for (size_t col_idx = 0; col_idx < rb.nb_columns(); ++col_idx)
132 {
133 const sparrow::array& arr = rb.get_column(col_idx);
134 const sparrow::array& first_arr = first_rb.get_column(col_idx);
135 const auto arr_data_type = arr.data_type();
136 const auto first_arr_data_type = first_arr.data_type();
137 if (arr_data_type != first_arr_data_type)
138 {
139 return false;
140 }
141 }
142 }
143 return true;
144 }
145}
#define SPARROW_IPC_API
Definition config.hpp:12
SPARROW_IPC_API std::optional< int32_t > get_substr_as_int32(std::string_view str, std::string_view sep)
SPARROW_IPC_API std::optional< std::string_view > get_substr_after_separator(std::string_view str, std::string_view sep)
SPARROW_IPC_API std::optional< std::tuple< int32_t, int32_t, std::optional< int32_t > > > parse_decimal_format(std::string_view format_str)
constexpr size_t align_to_8(const size_t n)
Definition utils.hpp:14
bool check_record_batches_consistency(const R &record_batches)
Checks if all record batches in a collection have consistent structure.
Definition utils.hpp:116
SPARROW_IPC_API std::optional< int32_t > parse_to_int32(std::string_view str)
SPARROW_IPC_API std::vector< std::string_view > extract_words_after_colon(std::string_view str)