Skip to content

Commit

Permalink
Add std::formatters for sparrow arrays and types
Browse files Browse the repository at this point in the history
Fix

Fix

Try fix

WIP

address comments
  • Loading branch information
Alex-PLACET committed Dec 17, 2024
1 parent 1998e0a commit d25de9a
Show file tree
Hide file tree
Showing 37 changed files with 2,371 additions and 637 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/qemu.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ jobs:
;;
alpine*)
apk update
apk add git cmake make doctest-dev date-dev tzdata g++ samurai ccache
apk add git cmake make doctest-dev date-dev tzdata g++ samurai ccache linux-headers musl-dev
;;
esac
Expand Down
21 changes: 21 additions & 0 deletions include/sparrow/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,24 @@ namespace sparrow
return std::make_pair(proxy.extract_array(), proxy.extract_schema());
}
}

#if defined(__cpp_lib_format)

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

auto format(const sparrow::array& ar, std::format_context& ctx) const
{
return ar.visit([&ctx](const auto& layout)
{
return std::format_to(ctx.out(), "{}", layout);
});
}
};

#endif
114 changes: 113 additions & 1 deletion include/sparrow/arrow_array_schema_proxy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@

#pragma once

#include <cstdint>
#include <iterator>
#include <optional>
#include <ranges>
#include <string>
#include <string_view>

#include "sparrow/arrow_interface/arrow_array/private_data.hpp"
Expand Down Expand Up @@ -480,5 +484,113 @@ namespace sparrow
const auto it = bitmap.insert(sparrow::next(bitmap.cbegin(), index), range.begin(), range.end());
return static_cast<size_t>(std::distance(bitmap.begin(), it));
}

}

#if defined(__cpp_lib_format)

template <>
struct std::formatter<sparrow::buffer_view<uint8_t>>
{
private:

char delimiter = ' ';
static constexpr std::string_view opening = "[";
static constexpr std::string_view closing = "]";

public:

constexpr auto parse(std::format_parse_context& ctx)
{
auto it = ctx.begin();
auto end = ctx.end();

// Parse optional delimiter
if (it != end && *it != '}')
{
delimiter = *it++;
}

if (it != end && *it != '}')
{
throw std::format_error("Invalid format specifier for range");
}

return it;
}

auto format(const sparrow::buffer_view<uint8_t>& range, std::format_context& ctx) const
{
auto out = ctx.out();

// Write opening bracket
out = std::ranges::copy(opening, out).out;

// Write range elements
bool first = true;
for (const auto& elem : range)
{
if (!first)
{
*out++ = delimiter;
}
out = std::format_to(out, "{}", elem);
first = false;
}

// Write closing bracket
out = std::ranges::copy(closing, out).out;

return out;
}
};

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

auto format(const sparrow::arrow_proxy& obj, std::format_context& ctx) const
{
std::string buffers_description_str;
for (size_t i = 0; i < obj.n_buffers(); ++i)
{
std::format_to(
std::back_inserter(buffers_description_str),
"<{}[{} b]{}",
"uint8_t",
obj.buffers()[i].size() * sizeof(uint8_t),
obj.buffers()[i]
);
}

std::string children_str;
for (const auto& child : obj.children())
{
std::format_to(std::back_inserter(children_str), "{}\n", child);
}

const std::string dictionary_str = obj.dictionary() ? std::format("{}", *obj.dictionary()) : "nullptr";

return std::format_to(
ctx.out(),
"arrow_proxy\n- format: {}\n- name; {}\n- metadata: {}\n- data_type: {}\n- null_count:{}\n- length: {}\n- offset: {}\n- n_buffers: {}\n- buffers:\n{}\n- n_children: {}\n-children: {}\n- dictionary: {}",
obj.format(),
obj.name().value_or(""),
obj.metadata().value_or(""),
obj.data_type(),
obj.null_count(),
obj.length(),
obj.offset(),
obj.n_buffers(),
buffers_description_str,
obj.n_children(),
children_str,
dictionary_str
);
}
};

#endif
48 changes: 47 additions & 1 deletion include/sparrow/arrow_interface/arrow_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

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

#include "sparrow/arrow_interface/arrow_array/private_data.hpp"
#include "sparrow/c_interface.hpp"
Expand Down Expand Up @@ -150,4 +153,47 @@ namespace sparrow
copy_array(source_array, source_schema, target);
return target;
}
}

};

#if defined(__cpp_lib_format)

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

auto format(const ArrowArray& obj, std::format_context& ctx) const
{
std::string children_str = std::format("{}", static_cast<void*>(obj.children));
for (int i = 0; i < obj.n_children; ++i)
{
children_str += std::format("\n-{}", static_cast<void*>(obj.children[i]));
}

std::string buffer_str = std::format("{}", static_cast<void*>(obj.buffers));
for (int i = 0; i < obj.n_buffers; ++i)
{
buffer_str += std::format("\n\t- {}", obj.buffers[i]);
}

return std::format_to(
ctx.out(),
"ArrowArray - ptr address: {}\n- length: {}\n- null_count: {}\n- offset: {}\n- n_buffers: {}\n- buffers: {}\n- n_children: {}\n- children: {}\n- dictionary: {}\n",
static_cast<const void*>(&obj),
obj.length,
obj.null_count,
obj.offset,
obj.n_buffers,
buffer_str,
obj.n_children,
children_str,
static_cast<const void*>(obj.dictionary)
);
}
};

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,8 @@

#pragma once

#include <algorithm>

#include "sparrow/buffer/buffer_adaptor.hpp"
#include "sparrow/buffer/buffer_view.hpp"
#include "sparrow/c_interface.hpp"
#include "sparrow/types/data_type.hpp"
#include "sparrow/utils/contracts.hpp"

namespace sparrow
{
Expand Down
46 changes: 45 additions & 1 deletion include/sparrow/arrow_interface/arrow_schema.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
#pragma once

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

#include "sparrow/arrow_interface/arrow_schema/private_data.hpp"
#include "sparrow/config/config.hpp"
#include "sparrow/utils/contracts.hpp"


namespace sparrow
{
/**
Expand Down Expand Up @@ -163,3 +166,44 @@ namespace sparrow
return target;
}
}

#if defined(__cpp_lib_format)

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

auto format(const ArrowSchema& obj, std::format_context& ctx) const
{
std::string children_str = std::format("{}", static_cast<void*>(obj.children));
for (int i = 0; i < obj.n_children; ++i)
{
children_str += std::format("\n-{}", static_cast<void*>(obj.children[i]));
}

const std::string format = obj.format ? obj.format : "nullptr";
const std::string name = obj.name ? obj.name : "nullptr";
const std::string metadata = obj.metadata ? obj.metadata : "nullptr";

return std::format_to(
ctx.out(),
"ArrowArray - ptr address: {}\n- format: {}\n- name: {}\n- metadata: {}\n- flags: {}\n- n_children: {}\n- children: {}\n- dictionary: {}\n- release: {}\n- private_data: {}\n",
static_cast<const void*>(&obj),
format,
name,
metadata,
obj.flags,
obj.n_children,
children_str,
static_cast<const void*>(obj.dictionary),
static_cast<const void*>(std::addressof(obj.release)),
obj.private_data
);
}
};

#endif
24 changes: 24 additions & 0 deletions include/sparrow/details/3rdparty/float16_t.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
#include <cmath>
#include <bitset>
#include <type_traits>
#if defined(__cpp_lib_format)
# include <format>
#endif
#include <sstream>

#ifdef _MSC_VER
#pragma warning( push )
Expand Down Expand Up @@ -1025,6 +1029,26 @@ namespace std
template<> inline constexpr bool is_arithmetic_v<numeric::float16_t> = true;
template<> inline constexpr bool is_signed_v<numeric::float16_t> = true;

#if defined(__cpp_lib_format)

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

auto format(const numeric::float16_t& value, std::format_context& ctx) const
{
std::ostringstream oss;
oss << value;
return std::format_to(ctx.out(), "{}", oss.str());
}
};

#endif

}

#endif
Expand Down
Loading

0 comments on commit d25de9a

Please sign in to comment.