Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add date array #342

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ set(SPARROW_HEADERS
${SPARROW_INCLUDE_DIR}/sparrow/layout/primitive_array.hpp
${SPARROW_INCLUDE_DIR}/sparrow/layout/struct_layout/struct_array.hpp
${SPARROW_INCLUDE_DIR}/sparrow/layout/struct_layout/struct_value.hpp
${SPARROW_INCLUDE_DIR}/sparrow/layout/temporal/date_array.hpp
${SPARROW_INCLUDE_DIR}/sparrow/layout/temporal/duration_array.hpp
${SPARROW_INCLUDE_DIR}/sparrow/layout/temporal/timestamp_array.hpp
${SPARROW_INCLUDE_DIR}/sparrow/layout/temporal/timestamp_concepts.hpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ namespace sparrow
case data_type::HALF_FLOAT:
case data_type::FLOAT:
case data_type::DOUBLE:
case data_type::DATE_DAYS:
case data_type::DATE_MILLISECONDS:
case data_type::TIMESTAMP_SECONDS:
case data_type::TIMESTAMP_MILLISECONDS:
case data_type::TIMESTAMP_MICROSECONDS:
Expand Down
19 changes: 19 additions & 0 deletions include/sparrow/builder/builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <sparrow/layout/list_layout/list_array.hpp>
#include <sparrow/layout/primitive_array.hpp>
#include <sparrow/layout/struct_layout/struct_array.hpp>
#include <sparrow/layout/temporal/date_array.hpp>
#include <sparrow/layout/union_array.hpp>
#include <sparrow/layout/variable_size_binary_layout/variable_size_binary_array.hpp>
#include <sparrow/utils/ranges.hpp>
Expand Down Expand Up @@ -120,6 +121,12 @@ namespace sparrow
&& std::is_scalar_v<ensured_range_value_t<T>>;

template <typename T>
concept translates_to_date_layout = std::ranges::input_range<T>
&& mpl::any_of(
date_types_t{},
mpl::predicate::same_as<ensured_range_value_t<T>>{}
);
template <typename T>
concept translates_to_duration_layout = std::ranges::input_range<T>
&& mpl::any_of(
duration_types_t{},
Expand Down Expand Up @@ -197,6 +204,18 @@ namespace sparrow
}
};

template <translates_to_date_layout T, class OPTION_FLAGS>
struct builder<T, dont_enforce_layout, OPTION_FLAGS>
{
using type = sparrow::date_array<ensured_range_value_t<T>>;

template <class U>
static type create(U&& t)
{
return type(std::forward<U>(t));
}
};

template <translates_to_duration_layout T, class OPTION_FLAGS>
struct builder<T, dont_enforce_layout, OPTION_FLAGS>
{
Expand Down
6 changes: 6 additions & 0 deletions include/sparrow/layout/dispatch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
#include "sparrow/layout/primitive_array.hpp"
#include "sparrow/layout/run_end_encoded_layout/run_end_encoded_array.hpp"
#include "sparrow/layout/struct_layout/struct_array.hpp"
#include "sparrow/layout/temporal/date_array.hpp"
#include "sparrow/layout/temporal/duration_array.hpp"
#include "sparrow/layout/temporal/interval_array.hpp"
#include "sparrow/layout/temporal/timestamp_array.hpp"
#include "sparrow/layout/union_array.hpp"
#include "sparrow/layout/variable_size_binary_layout/variable_size_binary_array.hpp"
#include "sparrow/types/data_traits.hpp"
#include "sparrow/types/data_type.hpp"

namespace sparrow
{
Expand Down Expand Up @@ -131,6 +133,10 @@
return func(unwrap_array<decimal_256_array>(ar));
case data_type::FIXED_WIDTH_BINARY:
return func(unwrap_array<fixed_width_binary_array>(ar));
case sparrow::data_type::DATE_DAYS:
return func(unwrap_array<date_days_array>(ar));

Check warning on line 137 in include/sparrow/layout/dispatch.hpp

View check run for this annotation

Codecov / codecov/patch

include/sparrow/layout/dispatch.hpp#L137

Added line #L137 was not covered by tests
case data_type::DATE_MILLISECONDS:
return func(unwrap_array<date_milliseconds_array>(ar));

Check warning on line 139 in include/sparrow/layout/dispatch.hpp

View check run for this annotation

Codecov / codecov/patch

include/sparrow/layout/dispatch.hpp#L139

Added line #L139 was not covered by tests
case data_type::TIMESTAMP_SECONDS:
return func(unwrap_array<timestamp_array<timestamp<std::chrono::seconds>>>(ar));
case data_type::TIMESTAMP_MILLISECONDS:
Expand Down
64 changes: 64 additions & 0 deletions include/sparrow/layout/temporal/date_array.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2024 Man Group Operations Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or mplied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include "sparrow/layout/array_trivial_copyable.hpp"
#include "sparrow/layout/temporal/date_types.hpp"

// tdD : std::chrono::seconds
// tdm : std::chrono::milliseconds

namespace sparrow
{
using date_types_t = mpl::typelist<date_days, date_milliseconds>;

static constexpr date_types_t date_types;
template <typename T>
concept date_type = mpl::contains<T>(date_types);

/**
* Array of std::chrono::duration values.
*
* As the other arrays in sparrow, \c date_array<T> provides an API as if it was holding
* \c nullable<T> values instead of \c T values.
*
* Internally, the array contains a validity bitmap and a contiguous memory buffer
* holding the values.
*
* @tparam T the type of the values in the array.
* @see https://arrow.apache.org/docs/dev/format/Columnar.html#fixed-size-primitive-layout
*/
template <date_type T>
using date_array = array_trivial_copyable<T>;

using date_days_array = date_array<date_days>;
using date_milliseconds_array = date_array<date_milliseconds>;

template <class T>
struct is_date_array : std::false_type
{
};

template <class T>
struct is_date_array<date_array<T>> : std::true_type
{
};

/**
* Checks whether T is a date_array type.
*/
template <class T>
constexpr bool is_date_array_v = is_date_array<T>::value;
}
25 changes: 25 additions & 0 deletions include/sparrow/layout/temporal/date_types.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2024 Man Group Operations Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or mplied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include <chrono>

#include "sparrow/layout/temporal/temporal_types.hpp"

namespace sparrow
{
using date_days = std::chrono::time_point<std::chrono::system_clock, chrono::days>;
using date_milliseconds = std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds>;
}
7 changes: 2 additions & 5 deletions include/sparrow/layout/temporal/interval_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,10 @@
# include <format>
#endif

#include "sparrow/layout/temporal/temporal_types.hpp"

namespace sparrow
{
namespace chrono
{
using days = std::chrono::duration<int32_t, std::ratio<86400>>;
using months = std::chrono::duration<int32_t, std::ratio<2629746>>;
}

// We pack the structures to ensure that they are the same size on all platforms
#pragma pack(push, 1)
Expand Down
23 changes: 23 additions & 0 deletions include/sparrow/layout/temporal/temporal_types.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2024 Man Group Operations Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include <chrono>

namespace sparrow::chrono
{
using days = std::chrono::duration<int32_t, std::ratio<86400>>; // 1 day = 86400 seconds
using months = std::chrono::duration<int32_t, std::ratio<2629746>>; // 1 month = 2629746 seconds
}
13 changes: 13 additions & 0 deletions include/sparrow/types/data_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <chrono>
#include <concepts>

#include "sparrow/layout/temporal/date_array.hpp"
#include "sparrow/layout/temporal/interval_types.hpp"
#include "sparrow/types/data_type.hpp"
#include "sparrow/utils/nullable.hpp"
Expand Down Expand Up @@ -112,6 +113,18 @@ namespace sparrow
using const_reference = decimal<int256_t>;
};

template <>
struct arrow_traits<date_days> : common_native_types_traits<date_days>
{
static constexpr data_type type_id = data_type::DATE_DAYS;
};

template <>
struct arrow_traits<date_milliseconds> : common_native_types_traits<date_milliseconds>
{
static constexpr data_type type_id = data_type::DATE_MILLISECONDS;
};

template <>
struct arrow_traits<std::chrono::seconds> : common_native_types_traits<std::chrono::seconds>
{
Expand Down
23 changes: 22 additions & 1 deletion include/sparrow/types/data_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <chrono>
#include <version>

#include "sparrow/layout/temporal/date_types.hpp"
#include "sparrow/layout/temporal/interval_types.hpp"

#if defined(SPARROW_USE_DATE_POLYFILL)
Expand Down Expand Up @@ -188,6 +189,8 @@
DECIMAL128,
DECIMAL256,
FIXED_WIDTH_BINARY,
DATE_DAYS,
DATE_MILLISECONDS,
TIMESTAMP_SECONDS,
TIMESTAMP_MILLISECONDS,
TIMESTAMP_MICROSECONDS,
Expand Down Expand Up @@ -278,7 +281,15 @@
// TODO: add propper timestamp support below
else if (format.starts_with("t"))
{
if (format.starts_with("tss:"))
if (format == "tdD")
{
return data_type::DATE_DAYS;
}

Check warning on line 287 in include/sparrow/types/data_type.hpp

View check run for this annotation

Codecov / codecov/patch

include/sparrow/types/data_type.hpp#L287

Added line #L287 was not covered by tests
else if (format == "tdm")
{
return data_type::DATE_MILLISECONDS;
}

Check warning on line 291 in include/sparrow/types/data_type.hpp

View check run for this annotation

Codecov / codecov/patch

include/sparrow/types/data_type.hpp#L291

Added line #L291 was not covered by tests
else if (format.starts_with("tss:"))
{
return data_type::TIMESTAMP_SECONDS;
}
Expand Down Expand Up @@ -500,6 +511,10 @@
return "z";
case data_type::LARGE_BINARY:
return "Z";
case data_type::DATE_DAYS:
return "tdD";
case data_type::DATE_MILLISECONDS:
return "tdm";
case data_type::TIMESTAMP_SECONDS:
return "tss:";
case data_type::TIMESTAMP_MILLISECONDS:
Expand Down Expand Up @@ -595,6 +610,8 @@
float64_t,
std::string,
std::vector<byte_t>,
date_days,
date_milliseconds,
timestamp<std::chrono::seconds>,
timestamp<std::chrono::milliseconds>,
timestamp<std::chrono::microseconds>,
Expand Down Expand Up @@ -836,6 +853,10 @@
return "Binary";
case LARGE_BINARY:
return "Large binary";
case DATE_DAYS:
return "Date days";

Check warning on line 857 in include/sparrow/types/data_type.hpp

View check run for this annotation

Codecov / codecov/patch

include/sparrow/types/data_type.hpp#L857

Added line #L857 was not covered by tests
case DATE_MILLISECONDS:
return "Date milliseconds";

Check warning on line 859 in include/sparrow/types/data_type.hpp

View check run for this annotation

Codecov / codecov/patch

include/sparrow/types/data_type.hpp#L859

Added line #L859 was not covered by tests
case TIMESTAMP_SECONDS:
return "Timestamp seconds";
case TIMESTAMP_MILLISECONDS:
Expand Down
5 changes: 5 additions & 0 deletions src/array_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "sparrow/layout/primitive_array.hpp"
#include "sparrow/layout/run_end_encoded_layout/run_end_encoded_array.hpp"
#include "sparrow/layout/struct_layout/struct_array.hpp"
#include "sparrow/layout/temporal/date_array.hpp"
#include "sparrow/layout/temporal/duration_array.hpp"
#include "sparrow/layout/temporal/interval_array.hpp"
#include "sparrow/layout/temporal/timestamp_array.hpp"
Expand Down Expand Up @@ -123,6 +124,10 @@
return detail::make_wrapper_ptr<dense_union_array>(std::move(proxy));
case data_type::SPARSE_UNION:
return detail::make_wrapper_ptr<sparse_union_array>(std::move(proxy));
case data_type::DATE_DAYS:
return detail::make_wrapper_ptr<date_days_array>(std::move(proxy));

Check warning on line 128 in src/array_factory.cpp

View check run for this annotation

Codecov / codecov/patch

src/array_factory.cpp#L128

Added line #L128 was not covered by tests
case data_type::DATE_MILLISECONDS:
return detail::make_wrapper_ptr<date_milliseconds_array>(std::move(proxy));

Check warning on line 130 in src/array_factory.cpp

View check run for this annotation

Codecov / codecov/patch

src/array_factory.cpp#L130

Added line #L130 was not covered by tests
case data_type::TIMESTAMP_SECONDS:
return detail::make_wrapper_ptr<timestamp_seconds_array>(std::move(proxy));
case data_type::TIMESTAMP_MILLISECONDS:
Expand Down
3 changes: 3 additions & 0 deletions src/arrow_interface/arrow_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ namespace sparrow
return {make_buffer(0, size)};
case data_type::DENSE_UNION:
return {make_buffer(0, size), make_buffer(1, size * 4)};
case data_type::DATE_DAYS:
return {make_valid_buffer(), make_buffer(1, size * 4)};
case data_type::DATE_MILLISECONDS:
case data_type::TIMESTAMP_SECONDS:
case data_type::TIMESTAMP_MILLISECONDS:
case data_type::TIMESTAMP_MICROSECONDS:
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ else()
test_dictionary_encoded_array.cpp
test_dispatch.cpp
test_duration_array.cpp
test_date_array.cpp
test_dynamic_bitset_view.cpp
test_dynamic_bitset.cpp
test_fixed_width_binary_array.cpp
Expand Down
Loading
Loading