18#include <initializer_list>
22#include <unordered_map>
29#if defined(__cpp_lib_format)
77 using name_range = std::ranges::ref_view<const std::vector<name_type>>;
78 using column_range = std::ranges::ref_view<const std::vector<array>>;
105 std::ranges::input_range NR,
106 std::ranges::input_range CR,
109 std::convertible_to<std::ranges::range_value_t<NR>, std::string>
110 and std::same_as<std::ranges::range_value_t<CR>,
array>
115 std::optional<std::string_view>
name = std::nullopt,
116 std::optional<METADATA_RANGE> metadata = std::nullopt
140 template <std::ranges::input_range CR, input_metadata_container METADATA_RANGE = std::vector<metadata_pair>>
141 requires std::same_as<std::ranges::range_value_t<CR>,
array>
144 std::optional<std::string_view>
name = std::nullopt,
145 std::optional<METADATA_RANGE> metadata = std::nullopt
450 template <
class AA,
class AS>
451 void init(AA* arr, AS* sch);
466 template <
class U,
class R>
467 [[nodiscard]] std::vector<U> to_vector(R&& range)
const;
495 using metadata_type = std::vector<metadata_pair>;
496 std::optional<name_type> m_name = std::nullopt;
497 std::optional<metadata_type> m_metadata = std::nullopt;
498 std::vector<name_type> m_name_list;
499 std::vector<array> m_array_list;
500 mutable std::unordered_map<name_type, array*> m_array_map;
502 mutable bool m_dirty_map =
true;
528 template <std::ranges::input_range NR, std::ranges::input_range CR, input_metadata_container METADATA_RANGE>
529 requires(std::convertible_to<std::ranges::range_value_t<NR>, std::string>
530 and std::same_as<std::ranges::range_value_t<CR>,
array>)
534 std::optional<std::string_view>
name,
535 std::optional<METADATA_RANGE> metadata
538 , m_metadata(std::move(metadata))
539 , m_name_list(to_vector<name_type>(std::forward<NR>(
names)))
540 , m_array_list(to_vector<array>(std::forward<CR>(
columns)))
542 update_array_map_cache();
547 inline std::vector<record_batch::name_type>
get_names(
const std::vector<array>& array_list)
549 const auto names = array_list
550 | std::views::transform(
553 return ar.
name().value();
556 return {names.begin(), names.end()};
560 template <std::ranges::input_range CR, input_metadata_container METADATA_RANGE>
561 requires std::same_as<std::ranges::range_value_t<CR>, array>
564 , m_metadata(
std::move(metadata))
568 update_array_map_cache();
572 void record_batch::init(
ArrowArray&& arr, AS* sch)
574 partial_init_from_schema(*sch);
575 std::size_t column_size = m_name_list.capacity();
576 for (std::size_t i = 0; i < column_size; ++i)
578 m_name_list.emplace_back(sch->children[i]->name);
579 m_array_list.emplace_back(std::move(*(arr.children[i])), sch->children[i]);
583 update_array_map_cache();
586 template <
class AA,
class AS>
587 void record_batch::init(AA* arr, AS* sch)
589 partial_init_from_schema(*sch);
590 std::size_t column_size = m_name_list.capacity();
591 for (std::size_t i = 0; i < column_size; ++i)
593 m_name_list.emplace_back(sch->children[i]->name);
594 m_array_list.emplace_back(arr->children[i], sch->children[i]);
596 update_array_map_cache();
599 template <
class U,
class R>
600 std::vector<U> record_batch::to_vector(R&& range)
const
603 if constexpr (std::ranges::sized_range<
decltype(range)>)
605 v.reserve(std::ranges::size(range));
607 std::ranges::move(range, std::back_inserter(v));
612#if defined(__cpp_lib_format)
614struct std::formatter<
sparrow::record_batch>
616 constexpr auto parse(std::format_parse_context& ctx)
621 auto format(
const sparrow::record_batch& rb, std::format_context& ctx)
const
623 const auto values_by_columns = rb.
columns()
624 | std::views::transform(
625 [&rb](
const auto& ar)
627 return std::views::iota(0u, rb.
nb_rows())
628 | std::views::transform(
646 os << std::format(
"{}", value);
Dynamically typed array encapsulating an Arrow layout.
SPARROW_API std::optional< std::string_view > name() const
SPARROW_API record_batch(const record_batch &other)
Copy constructor.
SPARROW_API record_batch(ArrowArray &&array, ArrowSchema &&schema)
Constructs a record_batch from the given Arrow C structures, whose ownership is transferred to the re...
SPARROW_API const std::optional< name_type > & name() const
Gets the name of the record batch.
std::initializer_list< std::pair< name_type, array > > initializer_type
SPARROW_API void add_column(array column)
Adds a new column using the array's internal name.
SPARROW_API array & get_column(size_type index)
Gets the column at the specified index.
SPARROW_API array & get_column(const name_type &key)
Gets the column with the specified name.
SPARROW_API const array & get_column(const name_type &key) const
Gets the column with the specified name.
SPARROW_API record_batch(struct_array &&ar)
Constructs a record_batch from a struct_array.
SPARROW_API name_range names() const
Gets a range view of the column names.
record_batch & operator=(record_batch &&)=default
SPARROW_API bool contains_column(const name_type &key) const
Checks if the record batch contains a column with the specified name.
SPARROW_API struct_array extract_struct_array()
Moves the internal columns into a struct_array and empties the record batch.
SPARROW_API column_range columns() const
Gets a range view of the columns.
SPARROW_API record_batch(ArrowArray &&array, ArrowSchema *schema)
Constructs an record_batch from the given Arrow C structures.
constexpr record_batch(NR &&names, CR &&columns, std::optional< std::string_view > name=std::nullopt, std::optional< METADATA_RANGE > metadata=std::nullopt)
Constructs a record_batch from separate name and array ranges.
SPARROW_API const name_type & get_column_name(size_type index) const
Gets the name of the column at the specified index.
SPARROW_API record_batch(ArrowArray *array, ArrowSchema *schema)
Constructs an record_batch from the given Arrow C structures.
SPARROW_API record_batch(ArrowArray &&array, const ArrowSchema *schema)
Constructs an record_batch from the given Arrow C structures.
SPARROW_API void add_column(name_type name, array column)
Adds a new column to the record batch with the specified name.
SPARROW_API size_type nb_rows() const
Gets the number of rows in the record batch.
SPARROW_API const array & get_column(size_type index) const
Gets the column at the specified index.
SPARROW_API record_batch & operator=(const record_batch &other)
Copy assignment operator.
SPARROW_API record_batch(const ArrowArray *array, const ArrowSchema *schema)
Constructs an record_batch from the given Arrow C structures.
SPARROW_API size_type nb_columns() const
Gets the number of columns in the record batch.
record_batch(record_batch &&)=default
SPARROW_API record_batch(initializer_type init)
Constructs a record_batch from initializer list of name-array pairs.
std::ranges::ref_view< const std::vector< array > > column_range
std::ranges::ref_view< const std::vector< name_type > > name_range
std::vector< record_batch::name_type > get_names(const std::vector< array > &array_list)
ArrowArray make_empty_arrow_array()
SPARROW_API bool operator==(const array &lhs, const array &rhs)
Compares the content of two arrays.
std::ostream & operator<<(std::ostream &os, const nullval_t &)
constexpr void to_table_with_columns(OutputIt out, const Headers &headers, const Columns &columns)