Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-PLACET committed Jan 23, 2025
1 parent 8017bb0 commit 24af55e
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ namespace sparrow
case data_type::TIMESTAMP_MILLISECONDS:
case data_type::TIMESTAMP_MICROSECONDS:
case data_type::TIMESTAMP_NANOSECONDS:
case data_type::DURATION_SECONDS:
case data_type::DURATION_MILLISECONDS:
case data_type::DURATION_MICROSECONDS:
case data_type::DURATION_NANOSECONDS:
case data_type::DECIMAL32:
case data_type::DECIMAL64:
case data_type::DECIMAL128:
Expand Down
9 changes: 9 additions & 0 deletions include/sparrow/layout/dispatch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,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/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"
Expand Down Expand Up @@ -128,6 +129,14 @@ namespace sparrow
return func(unwrap_array<decimal_256_array>(ar));
case data_type::FIXED_WIDTH_BINARY:
return func(unwrap_array<fixed_width_binary_array>(ar));
case data_type::TIMESTAMP_SECONDS:
return func(unwrap_array<timestamp_array<timestamp<std::chrono::seconds>>>(ar));

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

View check run for this annotation

Codecov / codecov/patch

include/sparrow/layout/dispatch.hpp#L133

Added line #L133 was not covered by tests
case data_type::TIMESTAMP_MILLISECONDS:
return func(unwrap_array<timestamp_array<timestamp<std::chrono::milliseconds>>>(ar));

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

View check run for this annotation

Codecov / codecov/patch

include/sparrow/layout/dispatch.hpp#L135

Added line #L135 was not covered by tests
case data_type::TIMESTAMP_MICROSECONDS:
return func(unwrap_array<timestamp_array<timestamp<std::chrono::microseconds>>>(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::TIMESTAMP_NANOSECONDS:
return func(unwrap_array<timestamp_array<timestamp<std::chrono::nanoseconds>>>(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
default:
throw std::invalid_argument("array type not supported");
}
Expand Down
1 change: 0 additions & 1 deletion include/sparrow/layout/primitive_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,6 @@ namespace sparrow
primitive_array<T>::primitive_array(arrow_proxy proxy)
: base_type(std::move(proxy))
{
SPARROW_ASSERT_TRUE(this->get_arrow_proxy().data_type() == arrow_traits<T>::type_id);
}

template <primitive_type T>
Expand Down
52 changes: 8 additions & 44 deletions include/sparrow/layout/temporal/timestamp_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include "sparrow/layout/layout_utils.hpp"
#include "sparrow/layout/primitive_array.hpp"
#include "sparrow/layout/temporal/timestamp_reference.hpp"
#include "sparrow/types/temporal_types.hpp"
#include "sparrow/utils/mp_utils.hpp"

// tts : timestamp<std::chrono::seconds>
Expand Down Expand Up @@ -248,6 +247,9 @@ namespace sparrow

static values_layout create_values_layout(arrow_proxy& proxy);

static const date::time_zone* get_timezone(const arrow_proxy& proxy);


values_layout m_values_layout;
const date::time_zone* m_timezone;

Expand All @@ -267,7 +269,8 @@ namespace sparrow
return values_layout{std::move(arr_proxy)};
}

const date::time_zone* get_timezone(const arrow_proxy& proxy)
template <typename T>
const date::time_zone* timestamp_array<T>::get_timezone(const arrow_proxy& proxy)
{
const std::string_view timezone_string = proxy.format().substr(4);
return date::locate_zone(timezone_string);
Expand Down Expand Up @@ -463,48 +466,9 @@ namespace sparrow
{
SPARROW_ASSERT_TRUE(i < this->size());
const auto& val = m_values_layout.value(i);
if constexpr (std::is_same_v<T, std::chrono::year_month_day>)
{
return T{std::chrono::sys_days(std::chrono::days(val))};
}
else if constexpr (std::is_same_v<T, std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds>>)
{
return T{std::chrono::milliseconds(val)};
}
else if constexpr (mpl::is_type_instance_of_v<T, std::chrono::hh_mm_ss>)
{
using duration = typename T::precision;
return T{duration(val)};
}
else if constexpr (mpl::is_type_instance_of_v<T, timestamp>)
{
using duration = T::duration;
const auto sys_time = std::chrono::sys_time<duration>{duration{val}};
return T{m_timezone, sys_time};
}
// else if constexpr (mpl::is_type_instance_of_v<T, std::chrono::zoned_time>)
// {
// using duration = T::duration;
// const auto sys_time = std::chrono::sys_time<duration>{duration{val}};
// return T{m_timezone, sys_time};
// }
else if constexpr (mpl::is_type_instance_of_v<T, std::chrono::duration>)
{
return T{val};
}
else if constexpr (std::is_same_v<T, day_time_interval>)
{
return {};
}
else if constexpr (std::is_same_v<T, month_day_nanoseconds_interval>)
{
return {};
}
else
{
static_assert(mpl::dependent_false<T, T>::value, "Invalid type");
mpl::unreachable();
}
using time_duration = typename T::duration;
const auto sys_time = std::chrono::sys_time<time_duration>{time_duration{val}};
return T{m_timezone, sys_time};
}

template <typename T>
Expand Down
38 changes: 35 additions & 3 deletions include/sparrow/types/data_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#pragma once

#include <chrono>
#include <concepts>

#include "sparrow/types/data_type.hpp"
Expand Down Expand Up @@ -110,13 +111,44 @@ namespace sparrow
using const_reference = decimal<int256_t>;
};

template <>
struct arrow_traits<std::chrono::seconds>
{
static constexpr data_type type_id = data_type::DURATION_SECONDS;
using value_type = std::chrono::seconds;
using const_reference = std::chrono::seconds;
};

template <>
struct arrow_traits<std::chrono::milliseconds>
{
static constexpr data_type type_id = data_type::DURATION_MILLISECONDS;
using value_type = std::chrono::milliseconds;
using const_reference = std::chrono::milliseconds;
};

template <>
struct arrow_traits<std::chrono::microseconds>
{
static constexpr data_type type_id = data_type::DURATION_MICROSECONDS;
using value_type = std::chrono::microseconds;
using const_reference = std::chrono::microseconds;
};

template <>
struct arrow_traits<std::chrono::nanoseconds>
{
static constexpr data_type type_id = data_type::DURATION_NANOSECONDS;
using value_type = std::chrono::nanoseconds;
using const_reference = std::chrono::nanoseconds;
};

template <>
struct arrow_traits<timestamp<std::chrono::seconds>>
{
static constexpr data_type type_id = data_type::TIMESTAMP_SECONDS;
using value_type = timestamp<std::chrono::milliseconds>;
using const_reference = timestamp<std::chrono::milliseconds>;
using value_type = timestamp<std::chrono::seconds>;
using const_reference = timestamp<std::chrono::seconds>;
};

template <>
Expand All @@ -142,6 +174,7 @@ namespace sparrow
using value_type = timestamp<std::chrono::nanoseconds>;
using const_reference = timestamp<std::chrono::nanoseconds>;
};

namespace detail
{
template <class T>
Expand Down Expand Up @@ -197,5 +230,4 @@ namespace sparrow
}
} constexpr has_arrow_traits;
}

}
24 changes: 21 additions & 3 deletions include/sparrow/types/data_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ namespace sparrow
TIMESTAMP_MILLISECONDS,
TIMESTAMP_MICROSECONDS,
TIMESTAMP_NANOSECONDS,
DURATION_SECONDS,
DURATION_MILLISECONDS,
DURATION_MICROSECONDS,
DURATION_NANOSECONDS,
};

// helper function to check if a string is all digits
Expand Down Expand Up @@ -268,7 +272,7 @@ namespace sparrow
// TODO: add propper timestamp support below
else if (format.starts_with("t"))
{
else if (format.starts_with("tss:"))
if (format.starts_with("tss:"))
{
return data_type::TIMESTAMP_SECONDS;
}

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

View check run for this annotation

Codecov / codecov/patch

include/sparrow/types/data_type.hpp#L278

Added line #L278 was not covered by tests
Expand Down Expand Up @@ -470,7 +474,6 @@ namespace sparrow
return "tsu:";
case data_type::TIMESTAMP_NANOSECONDS:
return "tsn:";
return "tDm";
case data_type::LIST:
return "+l";
case data_type::LARGE_LIST:
Expand Down Expand Up @@ -544,7 +547,14 @@ namespace sparrow
float64_t,
std::string,
std::vector<byte_t>,
sparrow::timestamp,
timestamp<std::chrono::seconds>,
timestamp<std::chrono::milliseconds>,
timestamp<std::chrono::microseconds>,
timestamp<std::chrono::nanoseconds>,
std::chrono::seconds,
std::chrono::milliseconds,
std::chrono::microseconds,
std::chrono::nanoseconds,
// TODO: add missing fundamental types here
list_value,
struct_value,
Expand Down Expand Up @@ -783,6 +793,14 @@ namespace std
return "Timestamp microseconds";

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

View check run for this annotation

Codecov / codecov/patch

include/sparrow/types/data_type.hpp#L793

Added line #L793 was not covered by tests
case TIMESTAMP_NANOSECONDS:
return "Timestamp nanoseconds";

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

View check run for this annotation

Codecov / codecov/patch

include/sparrow/types/data_type.hpp#L795

Added line #L795 was not covered by tests
case DURATION_SECONDS:
return "Seconds";

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

View check run for this annotation

Codecov / codecov/patch

include/sparrow/types/data_type.hpp#L797

Added line #L797 was not covered by tests
case DURATION_MILLISECONDS:
return "Milliseconds";

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

View check run for this annotation

Codecov / codecov/patch

include/sparrow/types/data_type.hpp#L799

Added line #L799 was not covered by tests
case DURATION_MICROSECONDS:
return "Microseconds";

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

View check run for this annotation

Codecov / codecov/patch

include/sparrow/types/data_type.hpp#L801

Added line #L801 was not covered by tests
case DURATION_NANOSECONDS:
return "Nanoseconds";

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

View check run for this annotation

Codecov / codecov/patch

include/sparrow/types/data_type.hpp#L803

Added line #L803 was not covered by tests
case LIST:
return "List";
case LARGE_LIST:
Expand Down
14 changes: 10 additions & 4 deletions src/array_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,21 @@ namespace sparrow
case data_type::SPARSE_UNION:
return detail::make_wrapper_ptr<sparse_union_array>(std::move(proxy));
case data_type::TIMESTAMP_SECONDS:
return detail::make_wrapper_ptr<timestamp_array<std::chrono::seconds>>(std::move(proxy));
return detail::make_wrapper_ptr<timestamp_array<timestamp<std::chrono::seconds>>>(

Check warning on line 125 in src/array_factory.cpp

View check run for this annotation

Codecov / codecov/patch

src/array_factory.cpp#L125

Added line #L125 was not covered by tests
std::move(proxy)
);
case data_type::TIMESTAMP_MILLISECONDS:
return detail::make_wrapper_ptr<timestamp_array<std::chrono::milliseconds>>(std::move(proxy)
return detail::make_wrapper_ptr<timestamp_array<timestamp<std::chrono::milliseconds>>>(

Check warning on line 129 in src/array_factory.cpp

View check run for this annotation

Codecov / codecov/patch

src/array_factory.cpp#L129

Added line #L129 was not covered by tests
std::move(proxy)
);
case data_type::TIMESTAMP_MICROSECONDS:
return detail::make_wrapper_ptr<timestamp_array<std::chrono::microseconds>>(std::move(proxy)
return detail::make_wrapper_ptr<timestamp_array<timestamp<std::chrono::microseconds>>>(

Check warning on line 133 in src/array_factory.cpp

View check run for this annotation

Codecov / codecov/patch

src/array_factory.cpp#L133

Added line #L133 was not covered by tests
std::move(proxy)
);
case data_type::TIMESTAMP_NANOSECONDS:
return detail::make_wrapper_ptr<timestamp_array<std::chrono::nanoseconds>>(std::move(proxy));
return detail::make_wrapper_ptr<timestamp_array<timestamp<std::chrono::nanoseconds>>>(

Check warning on line 137 in src/array_factory.cpp

View check run for this annotation

Codecov / codecov/patch

src/array_factory.cpp#L137

Added line #L137 was not covered by tests
std::move(proxy)
);
case data_type::MAP:
case data_type::DECIMAL32:
return detail::make_wrapper_ptr<decimal_32_array>(std::move(proxy));
Expand Down
4 changes: 4 additions & 0 deletions src/arrow_interface/arrow_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ namespace sparrow
case data_type::TIMESTAMP_MILLISECONDS:
case data_type::TIMESTAMP_MICROSECONDS:
case data_type::TIMESTAMP_NANOSECONDS:
case data_type::DURATION_SECONDS:
case data_type::DURATION_MILLISECONDS:
case data_type::DURATION_MICROSECONDS:
case data_type::DURATION_NANOSECONDS:
return {make_valid_buffer(), make_buffer(1, size * 8)};
case data_type::DECIMAL32:
return {make_valid_buffer(), make_buffer(1, size * 4)};
Expand Down
61 changes: 4 additions & 57 deletions test/test_timestamp_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,63 +28,10 @@ namespace sparrow
template <typename T>
T make_value(size_t i)
{
if constexpr (std::is_same_v<T, std::chrono::year_month_day>)
{
return std::chrono::year_month_day{
std::chrono::year{static_cast<int>(1980 + i)},
std::chrono::month{static_cast<unsigned int>(i + 1)},
std::chrono::day{static_cast<unsigned int>(i + 1)}
};
}
else if constexpr (std::is_same_v<T, std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds>>)
{
return std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds>{
std::chrono::milliseconds{static_cast<int>(i)}
};
}
else if constexpr (mpl::is_type_instance_of_v<T, std::chrono::hh_mm_ss>)
{
using duration = typename T::precision;
return T(duration(static_cast<int>(i)));
}
else if constexpr (mpl::is_type_instance_of_v<T, timestamp>)
{
using duration = T::duration;
const duration duration_v{static_cast<int>(i)};
const date::sys_time<duration> sys_time{duration_v};
return T(new_york, sys_time);
}
// else if constexpr (mpl::is_type_instance_of_v<T, date::zoned_time>)
// {
// using duration = T::duration;
// const duration duration_v{static_cast<int>(i)};
// const date::sys_time<duration> sys_time{duration_v};
// return T(new_york, sys_time);
// }
else if constexpr (mpl::is_type_instance_of_v<T, std::chrono::duration>)
{
return T(static_cast<int>(i));
}
else if constexpr (std::is_same_v<T, day_time_interval>)
{
return T(
std::chrono::days{static_cast<int>(i)},
std::chrono::duration<int32_t, std::milli>{static_cast<int>(i)}
);
}
else if constexpr (std::is_same_v<T, month_day_nanoseconds_interval>)
{
return T(
std::chrono::months{static_cast<int>(i)},
std::chrono::days{static_cast<int>(i)},
std::chrono::duration<int64_t, std::nano>{static_cast<int>(i)}
);
}
else
{
static_assert(mpl::dependent_false<T, T>::value, "Invalid type");
mpl::unreachable();
}
using duration = T::duration;
const duration duration_v{static_cast<int>(i)};
const date::sys_time<duration> sys_time{duration_v};
return T(new_york, sys_time);
}

template <typename T>
Expand Down

0 comments on commit 24af55e

Please sign in to comment.