Skip to content

Commit 733eae0

Browse files
authored
Add time array (#346)
1 parent cceb32e commit 733eae0

File tree

12 files changed

+859
-15
lines changed

12 files changed

+859
-15
lines changed

CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,12 @@ set(SPARROW_HEADERS
203203
${SPARROW_INCLUDE_DIR}/sparrow/layout/struct_layout/struct_value.hpp
204204
${SPARROW_INCLUDE_DIR}/sparrow/layout/temporal/date_array.hpp
205205
${SPARROW_INCLUDE_DIR}/sparrow/layout/temporal/duration_array.hpp
206+
${SPARROW_INCLUDE_DIR}/sparrow/layout/temporal/interval_types.hpp
207+
${SPARROW_INCLUDE_DIR}/sparrow/layout/temporal/time_array.hpp
208+
${SPARROW_INCLUDE_DIR}/sparrow/layout/temporal/time_types.hpp
206209
${SPARROW_INCLUDE_DIR}/sparrow/layout/temporal/timestamp_array.hpp
207210
${SPARROW_INCLUDE_DIR}/sparrow/layout/temporal/timestamp_concepts.hpp
208211
${SPARROW_INCLUDE_DIR}/sparrow/layout/temporal/timestamp_reference.hpp
209-
${SPARROW_INCLUDE_DIR}/sparrow/layout/temporal/interval_types.hpp
210212
${SPARROW_INCLUDE_DIR}/sparrow/layout/trivial_copyable_data_access.hpp
211213
${SPARROW_INCLUDE_DIR}/sparrow/layout/variable_size_binary_layout/variable_size_binary_array.hpp
212214
${SPARROW_INCLUDE_DIR}/sparrow/layout/variable_size_binary_layout/variable_size_binary_iterator.hpp

include/sparrow/arrow_interface/arrow_array_schema_info_utils.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ namespace sparrow
5050
case data_type::TIMESTAMP_MILLISECONDS:
5151
case data_type::TIMESTAMP_MICROSECONDS:
5252
case data_type::TIMESTAMP_NANOSECONDS:
53+
case data_type::TIME_SECONDS:
54+
case data_type::TIME_MILLISECONDS:
55+
case data_type::TIME_MICROSECONDS:
56+
case data_type::TIME_NANOSECONDS:
5357
case data_type::DURATION_SECONDS:
5458
case data_type::DURATION_MILLISECONDS:
5559
case data_type::DURATION_MICROSECONDS:

include/sparrow/builder/builder.hpp

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,21 @@
2222
#include <utility>
2323
#include <vector>
2424

25-
#include <sparrow/array.hpp>
26-
#include <sparrow/builder/builder_utils.hpp>
27-
#include <sparrow/builder/nested_eq.hpp>
28-
#include <sparrow/builder/nested_less.hpp>
29-
#include <sparrow/layout/dictionary_encoded_array.hpp>
30-
#include <sparrow/layout/fixed_width_binary_array.hpp>
31-
#include <sparrow/layout/list_layout/list_array.hpp>
32-
#include <sparrow/layout/primitive_array.hpp>
33-
#include <sparrow/layout/struct_layout/struct_array.hpp>
34-
#include <sparrow/layout/temporal/date_array.hpp>
35-
#include <sparrow/layout/union_array.hpp>
36-
#include <sparrow/layout/variable_size_binary_layout/variable_size_binary_array.hpp>
37-
#include <sparrow/utils/ranges.hpp>
38-
25+
#include "sparrow/array.hpp"
26+
#include "sparrow/builder/builder_utils.hpp"
27+
#include "sparrow/builder/nested_eq.hpp"
28+
#include "sparrow/builder/nested_less.hpp"
29+
#include "sparrow/layout/dictionary_encoded_array.hpp"
30+
#include "sparrow/layout/fixed_width_binary_array.hpp"
31+
#include "sparrow/layout/list_layout/list_array.hpp"
32+
#include "sparrow/layout/primitive_array.hpp"
33+
#include "sparrow/layout/struct_layout/struct_array.hpp"
34+
#include "sparrow/layout/temporal/date_array.hpp"
3935
#include "sparrow/layout/temporal/interval_array.hpp"
36+
#include "sparrow/layout/temporal/time_array.hpp"
37+
#include "sparrow/layout/union_array.hpp"
38+
#include "sparrow/layout/variable_size_binary_layout/variable_size_binary_array.hpp"
39+
#include "sparrow/utils/ranges.hpp"
4040

4141
namespace sparrow
4242
{
@@ -144,6 +144,13 @@ namespace sparrow
144144
mpl::predicate::same_as<ensured_range_value_t<T>>{}
145145
);
146146

147+
template <typename T>
148+
concept translates_to_time_layout = std::ranges::input_range<T>
149+
&& mpl::any_of(
150+
time_types_t{},
151+
mpl::predicate::same_as<ensured_range_value_t<T>>{}
152+
);
153+
147154
template <class T>
148155
concept translate_to_variable_sized_list_layout = std::ranges::input_range<T>
149156
&& std::ranges::input_range<ensured_range_value_t<T>>
@@ -264,6 +271,18 @@ namespace sparrow
264271
}
265272
};
266273

274+
template <translates_to_time_layout T, class OPTION_FLAGS>
275+
struct builder<T, dont_enforce_layout, OPTION_FLAGS>
276+
{
277+
using type = sparrow::time_array<ensured_range_value_t<T>>;
278+
279+
template <class U>
280+
static type create(U&& t)
281+
{
282+
return type(std::forward<U>(t));
283+
}
284+
};
285+
267286
template <translate_to_variable_sized_list_layout T, class OPTION_FLAGS>
268287
struct builder<T, dont_enforce_layout, OPTION_FLAGS>
269288
{

include/sparrow/layout/dispatch.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "sparrow/layout/temporal/date_array.hpp"
3030
#include "sparrow/layout/temporal/duration_array.hpp"
3131
#include "sparrow/layout/temporal/interval_array.hpp"
32+
#include "sparrow/layout/temporal/time_array.hpp"
3233
#include "sparrow/layout/temporal/timestamp_array.hpp"
3334
#include "sparrow/layout/union_array.hpp"
3435
#include "sparrow/layout/variable_size_binary_layout/variable_size_binary_array.hpp"
@@ -145,6 +146,14 @@ namespace sparrow
145146
return func(unwrap_array<timestamp_array<timestamp<std::chrono::microseconds>>>(ar));
146147
case data_type::TIMESTAMP_NANOSECONDS:
147148
return func(unwrap_array<timestamp_array<timestamp<std::chrono::nanoseconds>>>(ar));
149+
case data_type::TIME_SECONDS:
150+
return func(unwrap_array<time_seconds_array>(ar));
151+
case data_type::TIME_MILLISECONDS:
152+
return func(unwrap_array<time_milliseconds_array>(ar));
153+
case data_type::TIME_MICROSECONDS:
154+
return func(unwrap_array<time_microseconds_array>(ar));
155+
case data_type::TIME_NANOSECONDS:
156+
return func(unwrap_array<time_nanoseconds_array>(ar));
148157
case data_type::DURATION_SECONDS:
149158
return func(unwrap_array<duration_seconds_array>(ar));
150159
case data_type::DURATION_MILLISECONDS:
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 "sparrow/layout/array_trivial_copyable.hpp"
18+
#include "sparrow/layout/temporal/time_types.hpp"
19+
20+
// tts : std::chrono::seconds
21+
22+
namespace sparrow
23+
{
24+
using time_types_t = mpl::
25+
typelist<chrono::time_seconds, chrono::time_milliseconds, chrono::time_microseconds, chrono::time_nanoseconds>;
26+
27+
static constexpr time_types_t time_types;
28+
template <typename T>
29+
concept time_type = mpl::contains<T>(time_types);
30+
31+
/**
32+
* Array of time values.
33+
*
34+
* As the other arrays in sparrow, \c time_array<T> provides an API as if it was holding
35+
* \c nullable<T> values instead of \c T values.
36+
*
37+
* Internally, the array contains a validity bitmap and a contiguous memory buffer
38+
* holding the values.
39+
*
40+
* @tparam T the type of the values in the array.
41+
* @see https://arrow.apache.org/docs/dev/format/Columnar.html#fixed-size-primitive-layout
42+
*/
43+
template <time_type T>
44+
using time_array = array_trivial_copyable<T>;
45+
46+
using time_seconds_array = time_array<chrono::time_seconds>;
47+
using time_milliseconds_array = time_array<chrono::time_milliseconds>;
48+
using time_microseconds_array = time_array<chrono::time_microseconds>;
49+
using time_nanoseconds_array = time_array<chrono::time_nanoseconds>;
50+
51+
template <class T>
52+
struct is_time_array : std::false_type
53+
{
54+
};
55+
56+
template <class T>
57+
struct is_time_array<time_array<T>> : std::true_type
58+
{
59+
};
60+
61+
/**
62+
* Checks whether T is a time_array type.
63+
*/
64+
template <class T>
65+
constexpr bool is_time_array_v = is_time_array<T>::value;
66+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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 <chrono>
18+
#include <cstdint>
19+
20+
#if defined(__cpp_lib_format)
21+
# include <format>
22+
#endif
23+
24+
namespace sparrow::chrono
25+
{
26+
/**
27+
* A duration representing time elapsed since midnight.
28+
*/
29+
struct time_seconds : public std::chrono::duration<int32_t>
30+
{
31+
time_seconds() = default;
32+
33+
explicit time_seconds(int32_t seconds)
34+
: std::chrono::duration<int32_t>(seconds)
35+
{
36+
}
37+
};
38+
39+
/**
40+
* A duration representing time elapsed since midnight, in milliseconds.
41+
*/
42+
struct time_milliseconds : public std::chrono::duration<int32_t, std::milli>
43+
{
44+
time_milliseconds() = default;
45+
46+
explicit time_milliseconds(int32_t milliseconds)
47+
: std::chrono::duration<int32_t, std::milli>(milliseconds)
48+
{
49+
}
50+
};
51+
52+
/**
53+
* A duration representing time elapsed since midnight, in microseconds.
54+
*/
55+
struct time_microseconds : public std::chrono::duration<int64_t, std::micro>
56+
{
57+
time_microseconds() = default;
58+
59+
explicit time_microseconds(int64_t microseconds)
60+
: std::chrono::duration<int64_t, std::micro>(microseconds)
61+
{
62+
}
63+
};
64+
65+
/**
66+
* A duration representing time elapsed since midnight, in nanoseconds.
67+
*/
68+
struct time_nanoseconds : public std::chrono::duration<int64_t, std::nano>
69+
{
70+
time_nanoseconds() = default;
71+
72+
explicit time_nanoseconds(int64_t nanoseconds)
73+
: std::chrono::duration<int64_t, std::nano>(nanoseconds)
74+
{
75+
}
76+
};
77+
}
78+
79+
#if defined(__cpp_lib_format)
80+
81+
template <typename T>
82+
requires std::same_as<T, sparrow::chrono::time_seconds>
83+
|| std::same_as<T, sparrow::chrono::time_milliseconds>
84+
|| std::same_as<T, sparrow::chrono::time_microseconds>
85+
|| std::same_as<T, sparrow::chrono::time_nanoseconds>
86+
struct std::formatter<T>
87+
{
88+
constexpr auto parse(std::format_parse_context& ctx)
89+
{
90+
return ctx.begin(); // Simple implementation
91+
}
92+
93+
auto format(const T& time, std::format_context& ctx) const
94+
{
95+
return std::format_to(ctx.out(), "{}", time.count());
96+
}
97+
};
98+
99+
#endif

include/sparrow/types/data_traits.hpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "sparrow/layout/temporal/date_array.hpp"
2121
#include "sparrow/layout/temporal/interval_types.hpp"
22+
#include "sparrow/layout/temporal/time_types.hpp"
2223
#include "sparrow/types/data_type.hpp"
2324
#include "sparrow/utils/nullable.hpp"
2425
#include "sparrow/utils/vector_view.hpp"
@@ -181,6 +182,30 @@ namespace sparrow
181182
using const_reference = timestamp<std::chrono::nanoseconds>;
182183
};
183184

185+
template <>
186+
struct arrow_traits<chrono::time_seconds> : common_native_types_traits<chrono::time_seconds>
187+
{
188+
static constexpr data_type type_id = data_type::TIME_SECONDS;
189+
};
190+
191+
template <>
192+
struct arrow_traits<chrono::time_milliseconds> : common_native_types_traits<chrono::time_milliseconds>
193+
{
194+
static constexpr data_type type_id = data_type::TIME_MILLISECONDS;
195+
};
196+
197+
template <>
198+
struct arrow_traits<chrono::time_microseconds> : common_native_types_traits<chrono::time_microseconds>
199+
{
200+
static constexpr data_type type_id = data_type::TIME_MICROSECONDS;
201+
};
202+
203+
template <>
204+
struct arrow_traits<chrono::time_nanoseconds> : common_native_types_traits<chrono::time_nanoseconds>
205+
{
206+
static constexpr data_type type_id = data_type::TIME_NANOSECONDS;
207+
};
208+
184209
template <>
185210
struct arrow_traits<chrono::months> : common_native_types_traits<chrono::months>
186211
{

0 commit comments

Comments
 (0)