diff --git a/include/sparrow/types/data_type.hpp b/include/sparrow/types/data_type.hpp index 56ca3b4a..21d5c032 100644 --- a/include/sparrow/types/data_type.hpp +++ b/include/sparrow/types/data_type.hpp @@ -499,43 +499,6 @@ namespace sparrow } } - /// @returns The number of bytes required to store the provided primitive data type. - template - 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::ceil(static_cast(size) / bit_per_byte)); - case data_type::UINT8: - // TODO: Replace static_cast by the 32 bit fix check function - case data_type::INT8: - return static_cast(size); - case data_type::UINT16: - return (sizeof(std::uint16_t) / sizeof(std::uint8_t)) * static_cast(size); - case data_type::INT16: - return (sizeof(std::int16_t) / sizeof(std::uint8_t)) * static_cast(size); - case data_type::UINT32: - return (sizeof(std::uint32_t) / sizeof(std::uint8_t)) * static_cast(size); - case data_type::INT32: - return (sizeof(std::int32_t) / sizeof(std::uint8_t)) * static_cast(size); - case data_type::UINT64: - return (sizeof(std::uint64_t) / sizeof(std::uint8_t)) * static_cast(size); - case data_type::INT64: - return (sizeof(std::int64_t) / sizeof(std::uint8_t)) * static_cast(size); - case data_type::HALF_FLOAT: - return (sizeof(float16_t) / sizeof(std::uint8_t)) * static_cast(size); - case data_type::FLOAT: - return (sizeof(float32_t) / sizeof(std::uint8_t)) * static_cast(size); - case data_type::DOUBLE: - return (sizeof(float64_t) / sizeof(std::uint8_t)) * static_cast(size); - default: - throw std::runtime_error("Unsupported data type"); - } - } - class list_value; class struct_value; @@ -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: @@ -845,6 +812,19 @@ namespace std } }; + template <> + struct formatter + { + 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(b)); + } + }; } #endif diff --git a/include/sparrow/utils/vector_view.hpp b/include/sparrow/utils/vector_view.hpp index 44914ccb..2659e667 100644 --- a/include/sparrow/utils/vector_view.hpp +++ b/include/sparrow/utils/vector_view.hpp @@ -18,6 +18,10 @@ #include #include +#if defined(__cpp_lib_format) +# include +#endif + namespace sparrow { /** @@ -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 +struct std::formatter> +{ + constexpr auto parse(std::format_parse_context& ctx) + { + return ctx.begin(); // Simple implementation + } + + auto format(const sparrow::vector_view& 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 diff --git a/test/test_binary_array.cpp b/test/test_binary_array.cpp index 6a6ab2e2..345abd01 100644 --- a/test/test_binary_array.cpp +++ b/test/test_binary_array.cpp @@ -92,16 +92,16 @@ namespace sparrow word4 }; std::vector 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); @@ -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)); } } @@ -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 } } diff --git a/test/test_string_array.cpp b/test/test_string_array.cpp index 0ea9b427..ef0989e4 100644 --- a/test/test_string_array.cpp +++ b/test/test_string_array.cpp @@ -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);