Skip to content

Commit

Permalink
Add name and metadata arguments to convenient constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-PLACET committed Dec 18, 2024
1 parent 3b9e4da commit 38a4a30
Show file tree
Hide file tree
Showing 20 changed files with 707 additions and 378 deletions.
8 changes: 4 additions & 4 deletions include/sparrow/arrow_array_schema_proxy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,22 +107,22 @@ namespace sparrow
* @param data_type The data type to set.
*/
void SPARROW_API set_data_type(enum data_type data_type);
[[nodiscard]] SPARROW_API std::optional<const std::string_view> name() const;
[[nodiscard]] SPARROW_API std::optional<std::string_view> name() const;

/**
* Set the name of the `ArrowSchema`.
* @exception `arrow_proxy_exception` If the `ArrowSchema` was not created with sparrow.
* @param name The name to set.
*/
SPARROW_API void set_name(std::optional<const std::string_view> name);
[[nodiscard]] SPARROW_API std::optional<const std::string_view> metadata() const;
SPARROW_API void set_name(std::optional<std::string_view> name);
[[nodiscard]] SPARROW_API std::optional<std::string_view> metadata() const;

/**
* Set the metadata of the `ArrowSchema`.
* @exception `arrow_proxy_exception` If the `ArrowSchema` was not created with sparrow.
* @param metadata The metadata to set.
*/
SPARROW_API void set_metadata(std::optional<const std::string_view> metadata);
SPARROW_API void set_metadata(std::optional<std::string_view> metadata);
[[nodiscard]] SPARROW_API std::vector<ArrowFlag> flags() const;

/**
Expand Down
63 changes: 31 additions & 32 deletions include/sparrow/buffer/dynamic_bitset/dynamic_bitset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

#pragma once

#include "sparrow/buffer/dynamic_bitset/dynamic_bitset_base.hpp"
#include "sparrow/buffer/buffer.hpp"
#include "sparrow/buffer/dynamic_bitset/dynamic_bitset_base.hpp"
#include "sparrow/utils/ranges.hpp"

namespace sparrow
Expand All @@ -38,15 +38,16 @@ namespace sparrow
using value_type = typename base_type::value_type;
using size_type = typename base_type::size_type;

template<std::ranges::input_range R>
requires std::convertible_to<std::ranges::range_value_t<R>, value_type>
template <std::ranges::input_range R>
requires std::convertible_to<std::ranges::range_value_t<R>, value_type>
explicit dynamic_bitset(const R& r)
: dynamic_bitset(std::ranges::size(r), true)
{
std::size_t i = 0;
for(auto value : r)
for (auto value : r)
{
if(!value){
if (!value)
{
this->set(i, false);
}
i++;
Expand Down Expand Up @@ -109,25 +110,24 @@ namespace sparrow
{
}


using validity_bitmap = dynamic_bitset<std::uint8_t>;


namespace detail
{
using validity_bitmap = sparrow::validity_bitmap;
inline validity_bitmap ensure_validity_bitmap_impl(std::size_t size, const validity_bitmap & bitmap)

inline validity_bitmap ensure_validity_bitmap_impl(std::size_t size, const validity_bitmap& bitmap)
{
if(bitmap.size() == 0)
if (bitmap.size() == 0)
{
return validity_bitmap(size, true);
}
return bitmap; // copy
return bitmap; // copy
}
inline validity_bitmap ensure_validity_bitmap_impl(std::size_t size, validity_bitmap && bitmap)

inline validity_bitmap ensure_validity_bitmap_impl(std::size_t size, validity_bitmap&& bitmap)
{
if(bitmap.size() == 0)
if (bitmap.size() == 0)
{
bitmap.resize(size, true);
}
Expand All @@ -136,15 +136,16 @@ namespace sparrow

// range of booleans
template <std::ranges::input_range R>
requires(std::same_as<std::ranges::range_value_t<R>, bool>)
requires(std::same_as<std::ranges::range_value_t<R>, bool>)
validity_bitmap ensure_validity_bitmap_impl(std::size_t size, R&& range)
{
{
SPARROW_ASSERT_TRUE(size == range_size(range) || range_size(range) == 0);
validity_bitmap bitmap(size, true);
std::size_t i = 0;
for(auto value : range)
for (auto value : range)
{
if(!value){
if (!value)
{
bitmap.set(i, false);
}
i++;
Expand All @@ -154,33 +155,31 @@ namespace sparrow

// range of indices / integers (but not booleans)
template <std::ranges::input_range R>
requires(
std::unsigned_integral<std::ranges::range_value_t<R>> &&
!std::same_as<std::ranges::range_value_t<R>, bool> &&
!std::same_as<std::decay_t<R>, validity_bitmap>
)
requires(std::unsigned_integral<std::ranges::range_value_t<R>> && !std::same_as<std::ranges::range_value_t<R>, bool> && !std::same_as<std::decay_t<R>, validity_bitmap>)
validity_bitmap ensure_validity_bitmap_impl(std::size_t size, R&& range_of_indices)
{
{
validity_bitmap bitmap(size, true);
for(auto index : range_of_indices)
for (auto index : range_of_indices)
{
bitmap.set(index, false);
}
return bitmap;
}
} // namespace detail
} // namespace detail

template <class T>
concept validity_bitmap_input =
std::same_as<T, validity_bitmap> ||
std::same_as<T, const validity_bitmap&> ||
(std::ranges::input_range<T> && std::same_as<std::ranges::range_value_t<T>, bool>) ||
(std::ranges::input_range<T> && std::unsigned_integral<std::ranges::range_value_t<T>> );
concept validity_bitmap_input = (std::same_as<T, validity_bitmap> || std::same_as<T, const validity_bitmap&>
|| (std::ranges::input_range<T>
&& std::same_as<std::ranges::range_value_t<T>, bool>)
|| (std::ranges::input_range<T>
&& std::unsigned_integral<std::ranges::range_value_t<T>>) )
&& (not std::same_as<T, std::string> && not std::same_as<T, std::string_view>
&& not std::same_as<T, const char*>);

template <validity_bitmap_input R>
validity_bitmap ensure_validity_bitmap(std::size_t size, R&& validity_input)
{
return detail::ensure_validity_bitmap_impl(size, std::forward<R>(validity_input));
}
} // namespace sparrow

} // namespace sparrow
16 changes: 15 additions & 1 deletion include/sparrow/buffer/dynamic_bitset/dynamic_bitset_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,13 @@ namespace sparrow
requires std::ranges::random_access_range<std::remove_pointer_t<B>>
constexpr auto dynamic_bitset_base<B>::operator[](size_type pos) -> reference
{
if(pos >= size())
{
throw std::out_of_range(
"dynamic_bitset_base::operator[]: index out of range for dynamic_bitset_base of size"
+ std::to_string(size()) + " at index " + std::to_string(pos)
);
}
SPARROW_ASSERT_TRUE(pos < size());
SPARROW_ASSERT_TRUE(data() != nullptr);
return reference(*this, buffer().data()[block_index(pos)], bit_mask(pos));
Expand Down Expand Up @@ -219,7 +226,14 @@ namespace sparrow
requires std::ranges::random_access_range<std::remove_pointer_t<B>>
constexpr void dynamic_bitset_base<B>::set(size_type pos, value_type value)
{
SPARROW_ASSERT_TRUE(pos < size());
if(pos >= size())
{
throw std::out_of_range(
"dynamic_bitset_base::operator[]: index out of range for dynamic_bitset_base of size"
+ std::to_string(size()) + " at index " + std::to_string(pos)
);
}
// SPARROW_ASSERT_TRUE(pos < size());
SPARROW_ASSERT_TRUE(data() != nullptr);
block_type& block = buffer().data()[block_index(pos)];
const bool old_value = block & bit_mask(pos);
Expand Down
30 changes: 15 additions & 15 deletions include/sparrow/layout/array_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ namespace sparrow
using const_iterator = layout_iterator<iterator_types>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;

std::optional<std::string_view> name() const;
std::optional<std::string_view> metadata() const;

bool empty() const;
size_type size() const;

Expand All @@ -127,9 +130,6 @@ namespace sparrow
const_bitmap_range bitmap() const;
const_value_range values() const;

[[nodiscard]] std::optional<std::string_view> name() const;
[[nodiscard]] std::optional<std::string_view> metadata() const;

/**
* Slices the array to keep only the elements between the given \p start and \p end.
* A copy of the \ref array is modified. The data is not modified, only the ArrowArray.offset and
Expand Down Expand Up @@ -192,6 +192,18 @@ namespace sparrow
* array_crtp_base implementation *
**********************************/

template <class D>
std::optional<std::string_view> array_crtp_base<D>::name() const
{
return get_arrow_proxy().name();
}

template <class D>
std::optional<std::string_view> array_crtp_base<D>::metadata() const
{
return get_arrow_proxy().metadata();
}

/**
* Checks if the array has no element, i.e. whether begin() == end().
*/
Expand Down Expand Up @@ -373,18 +385,6 @@ namespace sparrow
return const_value_range(this->derived_cast().value_cbegin(), this->derived_cast().value_cend());
}

template <class D>
std::optional<std::string_view> array_crtp_base<D>::name() const
{
return m_proxy.name();
}

template <class D>
std::optional<std::string_view> array_crtp_base<D>::metadata() const
{
return m_proxy.metadata();
}

template <class D>
array_crtp_base<D>::array_crtp_base(arrow_proxy proxy)
: m_proxy(std::move(proxy))
Expand Down
43 changes: 33 additions & 10 deletions include/sparrow/layout/dictionary_encoded_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include "sparrow/utils/functor_index_iterator.hpp"
#include "sparrow/utils/memory.hpp"


namespace sparrow
{
template <class Layout, bool is_const>
Expand Down Expand Up @@ -122,8 +121,11 @@ namespace sparrow
dictionary_encoded_array(self_type&&);
self_type& operator=(self_type&&);

[[nodiscard]] size_type size() const;
[[nodiscard]] bool empty() const;
std::optional<std::string_view> name() const;
std::optional<std::string_view> metadata() const;

size_type size() const;
bool empty() const;

const_reference operator[](size_type i) const;

Expand Down Expand Up @@ -171,8 +173,13 @@ namespace sparrow
private:

template <validity_bitmap_input R = validity_bitmap>
static auto
create_proxy(keys_buffer_type&& keys, array&& values, R&& bitmaps = validity_bitmap{}) -> arrow_proxy;
static auto create_proxy(
keys_buffer_type&& keys,
array&& values,
R&& bitmaps = validity_bitmap{},
std::optional<std::string_view> name = std::nullopt,
std::optional<std::string_view> metadata = std::nullopt
) -> arrow_proxy;

using keys_layout = primitive_array<IT>;
using values_layout = cloning_ptr<array_wrapper>;
Expand Down Expand Up @@ -253,9 +260,13 @@ namespace sparrow

template <std::integral IT>
template <validity_bitmap_input VBI>
auto
dictionary_encoded_array<IT>::create_proxy(keys_buffer_type&& keys, array&& values, VBI&& validity_input)
-> arrow_proxy
auto dictionary_encoded_array<IT>::create_proxy(
keys_buffer_type&& keys,
array&& values,
VBI&& validity_input,
std::optional<std::string_view> name,
std::optional<std::string_view> metadata
) -> arrow_proxy
{
const auto size = keys.size();
validity_bitmap vbitmap = ensure_validity_bitmap(size, std::forward<VBI>(validity_input));
Expand All @@ -266,8 +277,8 @@ namespace sparrow
// create arrow schema and array
ArrowSchema schema = make_arrow_schema(
sparrow::data_type_format_of<IT>(),
std::nullopt, // name
std::nullopt, // metadata
name, // name
metadata, // metadata
std::nullopt, // flags
0, // n_children
nullptr, // children
Expand All @@ -291,6 +302,18 @@ namespace sparrow
return arrow_proxy(std::move(arr), std::move(schema));
}

template <std::integral IT>
std::optional<std::string_view> dictionary_encoded_array<IT>::name() const
{
return m_proxy.name();
}

template <std::integral IT>
std::optional<std::string_view> dictionary_encoded_array<IT>::metadata() const
{
return m_proxy.metadata();
}

template <std::integral IT>
auto dictionary_encoded_array<IT>::size() const -> size_type
{
Expand Down
Loading

0 comments on commit 38a4a30

Please sign in to comment.