Skip to content

Commit

Permalink
rebase and fix
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanMabille committed Dec 18, 2024
1 parent 9d92aa2 commit cd52b2d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 53 deletions.
54 changes: 17 additions & 37 deletions include/sparrow/types/data_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,43 +499,6 @@ namespace sparrow
}
}

/// @returns The number of bytes required to store the provided primitive data type.
template<std::integral T>
constexpr size_t primitive_bytes_count(data_type data_type, T size)
{
SPARROW_ASSERT_TRUE(data_type_is_primitive(data_type));
constexpr double bit_per_byte = 8.;
switch (data_type)
{
case data_type::BOOL:
return static_cast<std::size_t>(std::ceil(static_cast<double>(size) / bit_per_byte));
case data_type::UINT8:
// TODO: Replace static_cast<std::size_t> by the 32 bit fix check function
case data_type::INT8:
return static_cast<std::size_t>(size);
case data_type::UINT16:
return (sizeof(std::uint16_t) / sizeof(std::uint8_t)) * static_cast<std::size_t>(size);
case data_type::INT16:
return (sizeof(std::int16_t) / sizeof(std::uint8_t)) * static_cast<std::size_t>(size);
case data_type::UINT32:
return (sizeof(std::uint32_t) / sizeof(std::uint8_t)) * static_cast<std::size_t>(size);
case data_type::INT32:
return (sizeof(std::int32_t) / sizeof(std::uint8_t)) * static_cast<std::size_t>(size);
case data_type::UINT64:
return (sizeof(std::uint64_t) / sizeof(std::uint8_t)) * static_cast<std::size_t>(size);
case data_type::INT64:
return (sizeof(std::int64_t) / sizeof(std::uint8_t)) * static_cast<std::size_t>(size);
case data_type::HALF_FLOAT:
return (sizeof(float16_t) / sizeof(std::uint8_t)) * static_cast<std::size_t>(size);
case data_type::FLOAT:
return (sizeof(float32_t) / sizeof(std::uint8_t)) * static_cast<std::size_t>(size);
case data_type::DOUBLE:
return (sizeof(float64_t) / sizeof(std::uint8_t)) * static_cast<std::size_t>(size);
default:
throw std::runtime_error("Unsupported data type");
}
}

class list_value;
class struct_value;

Expand Down Expand Up @@ -785,8 +748,12 @@ namespace std
return "double";
case STRING:
return "String";
case LARGE_STRING:
return "Large string";
case BINARY:
return "Binary";
case LARGE_BINARY:
return "Large binary";
case TIMESTAMP:
return "Timestamp";
case LIST:
Expand Down Expand Up @@ -845,6 +812,19 @@ namespace std
}
};

template <>
struct formatter<std::byte>
{
constexpr auto parse(std::format_parse_context& ctx)
{
return ctx.begin(); // Simple implementation
}

auto format(const std::byte& b, std::format_context& ctx) const
{
return std::format_to(ctx.out(), "{}", static_cast<int>(b));
}
};
}

#endif
28 changes: 28 additions & 0 deletions include/sparrow/utils/vector_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
#include <ranges>
#include <vector>

#if defined(__cpp_lib_format)
# include <format>
#endif

namespace sparrow
{
/**
Expand Down Expand Up @@ -68,3 +72,27 @@ namespace sparrow
return std::lexicographical_compare_three_way(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}
}

#if defined(__cpp_lib_format)
template <class T>
struct std::formatter<sparrow::vector_view<T>>
{
constexpr auto parse(std::format_parse_context& ctx)
{
return ctx.begin(); // Simple implementation
}

auto format(const sparrow::vector_view<T>& vec, std::format_context& ctx) const
{
std::format_to(ctx.out(), "<");
if (!vec.empty())
{
for (std::size_t i = 0; i < vec.size() - 1; ++i)
{
std::format_to(ctx.out(), "{}, ", vec[i]);
}
}
return std::format_to(ctx.out(), "{}>", vec.back());
}
};
#endif
40 changes: 25 additions & 15 deletions test/test_binary_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,16 @@ namespace sparrow
word4
};
std::vector<std::size_t> where_nulls{2,3};
binary_array array(words, std::move(where_nulls));
const binary_array array(words, std::move(where_nulls));

REQUIRE_EQ(array.size(), words.size());

// check nulls
CHECK_EQ(array[0].has_value(), true);
CHECK_EQ(array[1].has_value(), true);
CHECK_EQ(array[2].has_value(), false);
CHECK_EQ(array[3].has_value(), false);
CHECK_EQ(array[4].has_value(), true);
CHECK(array[0].has_value());
CHECK(array[1].has_value());
CHECK_FALSE(array[2].has_value());
CHECK_FALSE(array[3].has_value());
CHECK(array[4].has_value());

// check values
CHECK_EQ(array[0].value(), word0);
Expand Down Expand Up @@ -291,15 +291,15 @@ namespace sparrow
const auto array_bitmap = array.bitmap();

layout_type::const_bitmap_iterator citer = array_bitmap.begin();
CHECK_EQ(*citer, true);
CHECK_EQ(*(++citer), false);
CHECK_EQ(*(++citer), true);
CHECK_EQ(*(++citer), true);
CHECK_EQ(*(++citer), false);
CHECK_EQ(*(++citer), true);
CHECK_EQ(*(++citer), true);
CHECK_EQ(*(++citer), true);
CHECK_EQ(*(++citer), true);
CHECK(*citer);
CHECK_FALSE(*(++citer));
CHECK(*(++citer));
CHECK(*(++citer));
CHECK_FALSE(*(++citer));
CHECK(*(++citer));
CHECK(*(++citer));
CHECK(*(++citer));
CHECK(*(++citer));
}
}

Expand Down Expand Up @@ -408,6 +408,16 @@ namespace sparrow
CHECK_EQ(it->get(), words[m_offset + 8]);
}
}
#if defined(__cpp_lib_format)
TEST_CASE_FIXTURE(binary_array_fixture, "formatting")
{
const layout_type array(std::move(m_arrow_proxy));
const std::string formatted = std::format("{}", array);
constexpr std::string_view
expected = "Binary [name=test | size=9] <<1, 1, 255, 0>, null, <2, 3>, <3, 5, 255>, null, <8, 13>, <13, 21, 251, 8>, <21, 34, 248>, <34, 55>>";
CHECK_EQ(formatted, expected);
}
#endif
}
}

2 changes: 1 addition & 1 deletion test/test_string_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ namespace sparrow
}

#if defined(__cpp_lib_format)
TEST_CASE_FIXTURE(variable_size_binary_fixture, "formatting")
TEST_CASE_FIXTURE(string_array_fixture, "formatting")
{
const layout_type array(std::move(m_arrow_proxy));
const std::string formatted = std::format("{}", array);
Expand Down

0 comments on commit cd52b2d

Please sign in to comment.