Skip to content

Commit

Permalink
Add nanosecond & microsecond to DatetimeProperties (#11911)
Browse files Browse the repository at this point in the history
This PR:

- [x] Implemented `extract_milli_second`, `extract_micro_second` and `extract_nano_second` in libcudf.
- [x] Added `nanosecond` and `microsecond` in `DatetimeProperties` & `DatetimeIndex`.
- [x] Updated docs
- [x] Added & modified tests

Authors:
  - GALI PREM SAGAR (https://github.com/galipremsagar)

Approvers:
  - David Wendt (https://github.com/davidwendt)
  - Matthew Roeschke (https://github.com/mroeschke)
  - Nghia Truong (https://github.com/ttnghia)
  - MithunR (https://github.com/mythrocks)
  - https://github.com/nvdbaranec
  - Bradley Dice (https://github.com/bdice)

URL: #11911
  • Loading branch information
galipremsagar authored Oct 17, 2022
1 parent 9f8b936 commit edc058f
Show file tree
Hide file tree
Showing 11 changed files with 377 additions and 36 deletions.
71 changes: 61 additions & 10 deletions cpp/include/cudf/datetime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace datetime {
*/

/**
* @brief Extracts year from any date time type and returns an int16_t
* @brief Extracts year from any datetime type and returns an int16_t
* cudf::column.
*
* @param column cudf::column_view of the input datetime values
Expand All @@ -50,7 +50,7 @@ std::unique_ptr<cudf::column> extract_year(
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
* @brief Extracts month from any date time type and returns an int16_t
* @brief Extracts month from any datetime type and returns an int16_t
* cudf::column.
*
* @param column cudf::column_view of the input datetime values
Expand All @@ -64,7 +64,7 @@ std::unique_ptr<cudf::column> extract_month(
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
* @brief Extracts day from any date time type and returns an int16_t
* @brief Extracts day from any datetime type and returns an int16_t
* cudf::column.
*
* @param column cudf::column_view of the input datetime values
Expand All @@ -78,7 +78,7 @@ std::unique_ptr<cudf::column> extract_day(
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
* @brief Extracts day from any date time type and returns an int16_t
* @brief Extracts day from any datetime type and returns an int16_t
* cudf::column.
*
* @param column cudf::column_view of the input datetime values
Expand All @@ -92,7 +92,7 @@ std::unique_ptr<cudf::column> extract_weekday(
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
* @brief Extracts hour from any date time type and returns an int16_t
* @brief Extracts hour from any datetime type and returns an int16_t
* cudf::column.
*
* @param column cudf::column_view of the input datetime values
Expand All @@ -106,7 +106,7 @@ std::unique_ptr<cudf::column> extract_hour(
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
* @brief Extracts minute from any date time type and returns an int16_t
* @brief Extracts minute from any datetime type and returns an int16_t
* cudf::column.
*
* @param column cudf::column_view of the input datetime values
Expand All @@ -120,7 +120,7 @@ std::unique_ptr<cudf::column> extract_minute(
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
* @brief Extracts second from any date time type and returns an int16_t
* @brief Extracts second from any datetime type and returns an int16_t
* cudf::column.
*
* @param column cudf::column_view of the input datetime values
Expand All @@ -133,6 +133,57 @@ std::unique_ptr<cudf::column> extract_second(
cudf::column_view const& column,
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
* @brief Extracts millisecond fraction from any datetime type and returns an int16_t
* cudf::column.
*
* A millisecond fraction is only the 3 digits that make up the millisecond portion of a duration.
* For example, the millisecond fraction of 1.234567890 seconds is 234.
*
* @param column cudf::column_view of the input datetime values
* @param mr Device memory resource used to allocate device memory of the returned column
*
* @returns cudf::column of the extracted int16_t milliseconds
* @throw cudf::logic_error if input column datatype is not TIMESTAMP
*/
std::unique_ptr<cudf::column> extract_millisecond_fraction(
cudf::column_view const& column,
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
* @brief Extracts microsecond fraction from any datetime type and returns an int16_t
* cudf::column.
*
* A microsecond fraction is only the 3 digits that make up the microsecond portion of a duration.
* For example, the microsecond fraction of 1.234567890 seconds is 567.
*
* @param column cudf::column_view of the input datetime values
* @param mr Device memory resource used to allocate device memory of the returned column
*
* @returns cudf::column of the extracted int16_t microseconds
* @throw cudf::logic_error if input column datatype is not TIMESTAMP
*/
std::unique_ptr<cudf::column> extract_microsecond_fraction(
cudf::column_view const& column,
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
* @brief Extracts nanosecond fraction from any datetime type and returns an int16_t
* cudf::column.
*
* A nanosecond fraction is only the 3 digits that make up the nanosecond portion of a duration.
* For example, the nanosecond fraction of 1.234567890 seconds is 890.
*
* @param column cudf::column_view of the input datetime values
* @param mr Device memory resource used to allocate device memory of the returned column
*
* @returns cudf::column of the extracted int16_t nanoseconds
* @throw cudf::logic_error if input column datatype is not TIMESTAMP
*/
std::unique_ptr<cudf::column> extract_nanosecond_fraction(
cudf::column_view const& column,
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/** @} */ // end of group
/**
* @addtogroup datetime_compute
Expand All @@ -141,7 +192,7 @@ std::unique_ptr<cudf::column> extract_second(
*/

/**
* @brief Computes the last day of the month in date time type and returns a TIMESTAMP_DAYS
* @brief Computes the last day of the month in datetime type and returns a TIMESTAMP_DAYS
* cudf::column.
*
* @param column cudf::column_view of the input datetime values
Expand Down Expand Up @@ -169,7 +220,7 @@ std::unique_ptr<cudf::column> day_of_year(
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
* @brief Adds or subtracts a number of months from the date time type and returns a
* @brief Adds or subtracts a number of months from the datetime type and returns a
* timestamp column that is of the same type as the input `timestamps` column.
*
* For a given row, if the `timestamps` or the `months` column value is null,
Expand Down Expand Up @@ -204,7 +255,7 @@ std::unique_ptr<cudf::column> add_calendrical_months(
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
* @brief Adds or subtracts a number of months from the date time type and returns a
* @brief Adds or subtracts a number of months from the datetime type and returns a
* timestamp column that is of the same type as the input `timestamps` column.
*
* For a given row, if the `timestamps` value is null, the output for that row is null.
Expand Down
33 changes: 33 additions & 0 deletions cpp/include/cudf/detail/datetime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,39 @@ std::unique_ptr<cudf::column> extract_second(
rmm::cuda_stream_view stream = cudf::default_stream_value,
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
* @copydoc cudf::extract_millisecond_fraction(cudf::column_view const&,
* rmm::mr::device_memory_resource *)
*
* @param stream CUDA stream used for device memory operations and kernel launches.
*/
std::unique_ptr<cudf::column> extract_millisecond_fraction(
cudf::column_view const& column,
rmm::cuda_stream_view stream = cudf::default_stream_value,
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
* @copydoc cudf::extract_microsecond_fraction(cudf::column_view const&,
* rmm::mr::device_memory_resource *)
*
* @param stream CUDA stream used for device memory operations and kernel launches.
*/
std::unique_ptr<cudf::column> extract_microsecond_fraction(
cudf::column_view const& column,
rmm::cuda_stream_view stream = cudf::default_stream_value,
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
* @copydoc cudf::extract_nanosecond_fraction(cudf::column_view const&,
* rmm::mr::device_memory_resource *)
*
* @param stream CUDA stream used for device memory operations and kernel launches.
*/
std::unique_ptr<cudf::column> extract_nanosecond_fraction(
cudf::column_view const& column,
rmm::cuda_stream_view stream = cudf::default_stream_value,
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
* @copydoc cudf::last_day_of_month(cudf::column_view const&, rmm::mr::device_memory_resource *)
*
Expand Down
76 changes: 70 additions & 6 deletions cpp/src/datetime/datetime_ops.cu
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,22 @@ struct extract_component_operator {

if (time_since_midnight.count() < 0) { time_since_midnight += days(1); }

auto hrs_ = duration_cast<hours>(time_since_midnight);
auto mins_ = duration_cast<minutes>(time_since_midnight - hrs_);
auto secs_ = duration_cast<seconds>(time_since_midnight - hrs_ - mins_);
auto const hrs_ = [&] { return duration_cast<hours>(time_since_midnight); };
auto const mins_ = [&] { return duration_cast<minutes>(time_since_midnight) - hrs_(); };
auto const secs_ = [&] {
return duration_cast<seconds>(time_since_midnight) - hrs_() - mins_();
};
auto const millisecs_ = [&] {
return duration_cast<milliseconds>(time_since_midnight) - hrs_() - mins_() - secs_();
};
auto const microsecs_ = [&] {
return duration_cast<microseconds>(time_since_midnight) - hrs_() - mins_() - secs_() -
millisecs_();
};
auto const nanosecs_ = [&] {
return duration_cast<nanoseconds>(time_since_midnight) - hrs_() - mins_() - secs_() -
millisecs_() - microsecs_();
};

switch (Component) {
case datetime_component::YEAR:
Expand All @@ -89,9 +102,12 @@ struct extract_component_operator {
return static_cast<unsigned>(year_month_day(days_since_epoch).day());
case datetime_component::WEEKDAY:
return year_month_weekday(days_since_epoch).weekday().iso_encoding();
case datetime_component::HOUR: return hrs_.count();
case datetime_component::MINUTE: return mins_.count();
case datetime_component::SECOND: return secs_.count();
case datetime_component::HOUR: return hrs_().count();
case datetime_component::MINUTE: return mins_().count();
case datetime_component::SECOND: return secs_().count();
case datetime_component::MILLISECOND: return millisecs_().count();
case datetime_component::MICROSECOND: return microsecs_().count();
case datetime_component::NANOSECOND: return nanosecs_().count();
default: return 0;
}
}
Expand Down Expand Up @@ -495,6 +511,33 @@ std::unique_ptr<column> extract_second(column_view const& column,
cudf::type_id::INT16>(column, stream, mr);
}

std::unique_ptr<column> extract_millisecond_fraction(column_view const& column,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
return detail::apply_datetime_op<
detail::extract_component_operator<detail::datetime_component::MILLISECOND>,
cudf::type_id::INT16>(column, stream, mr);
}

std::unique_ptr<column> extract_microsecond_fraction(column_view const& column,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
return detail::apply_datetime_op<
detail::extract_component_operator<detail::datetime_component::MICROSECOND>,
cudf::type_id::INT16>(column, stream, mr);
}

std::unique_ptr<column> extract_nanosecond_fraction(column_view const& column,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
return detail::apply_datetime_op<
detail::extract_component_operator<detail::datetime_component::NANOSECOND>,
cudf::type_id::INT16>(column, stream, mr);
}

std::unique_ptr<column> last_day_of_month(column_view const& column,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
Expand Down Expand Up @@ -607,6 +650,27 @@ std::unique_ptr<column> extract_second(column_view const& column,
return detail::extract_second(column, cudf::default_stream_value, mr);
}

std::unique_ptr<column> extract_millisecond_fraction(column_view const& column,
rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::extract_millisecond_fraction(column, cudf::default_stream_value, mr);
}

std::unique_ptr<column> extract_microsecond_fraction(column_view const& column,
rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::extract_microsecond_fraction(column, cudf::default_stream_value, mr);
}

std::unique_ptr<column> extract_nanosecond_fraction(column_view const& column,
rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::extract_nanosecond_fraction(column, cudf::default_stream_value, mr);
}

std::unique_ptr<column> last_day_of_month(column_view const& column,
rmm::mr::device_memory_resource* mr)
{
Expand Down
Loading

0 comments on commit edc058f

Please sign in to comment.