sparrow-extensions 0.1.0
Extension types for the sparrow library
Loading...
Searching...
No Matches
uuid_array.hpp
Go to the documentation of this file.
1// Copyright 2024 Man Group Operations Limited
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#include <cstddef>
18#include <string_view>
19
20#include "sparrow/fixed_width_binary_array.hpp"
21#include "sparrow/utils/contracts.hpp"
22
23namespace sparrow_extensions
24{
42 {
43 public:
44
45 static constexpr size_t UUID_SIZE = 16;
46 static constexpr std::string_view EXTENSION_NAME = "arrow.uuid";
47
48 protected:
49
50 static void init(sparrow::arrow_proxy& proxy)
51 {
52 const size_t element_size = sparrow::num_bytes_for_fixed_sized_binary(proxy.format());
53 SPARROW_ASSERT_TRUE(element_size == UUID_SIZE);
54 // Add UUID extension metadata
55 std::optional<sparrow::key_value_view> metadata = proxy.metadata();
56 std::vector<sparrow::metadata_pair> extension_metadata = metadata.has_value()
57 ? std::vector<sparrow::metadata_pair>(
58 metadata->begin(),
59 metadata->end()
60 )
61 : std::vector<sparrow::metadata_pair>{};
62
63 // Check if extension metadata already exists
64 const bool has_extension_name = std::ranges::find_if(
65 extension_metadata,
66 [](const auto& pair)
67 {
68 return pair.first == "ARROW:extension:name"
69 && pair.second == EXTENSION_NAME;
70 }
71 )
72 != extension_metadata.end();
73 if (!has_extension_name)
74 {
75 extension_metadata.emplace_back("ARROW:extension:name", EXTENSION_NAME);
76 extension_metadata.emplace_back("ARROW:extension:metadata", "");
77 }
78 proxy.set_metadata(std::make_optional(extension_metadata));
79 }
80 };
81
82 using uuid_array = sparrow::fixed_width_binary_array_impl<
83 sparrow::fixed_width_binary_traits::value_type,
84 sparrow::fixed_width_binary_traits::const_reference,
86
87} // namespace sparrow_extensions
88
89namespace sparrow::detail
90{
91 template <>
92 struct get_data_type_from_array<sparrow_extensions::uuid_array>
93 {
94 [[nodiscard]] static constexpr sparrow::data_type get()
95 {
96 return sparrow::data_type::FIXED_WIDTH_BINARY;
97 }
98 };
99}
100
sparrow::fixed_width_binary_array_impl< sparrow::fixed_width_binary_traits::value_type, sparrow::fixed_width_binary_traits::const_reference, uuid_extension > uuid_array
UUID array implementation following Arrow canonical extension specification.
static void init(sparrow::arrow_proxy &proxy)
static constexpr size_t UUID_SIZE
static constexpr std::string_view EXTENSION_NAME