From 6e8d07bf40f809d1572394c117349a76ca62fb65 Mon Sep 17 00:00:00 2001 From: Rok Date: Tue, 8 Feb 2022 21:54:39 +0100 Subject: [PATCH] Adding is_dst --- cpp/src/arrow/compute/api_scalar.cc | 1 + cpp/src/arrow/compute/api_scalar.h | 12 +++++ .../compute/kernels/scalar_temporal_test.cc | 11 ++++ .../compute/kernels/scalar_temporal_unary.cc | 51 ++++++++++++++++++- docs/source/cpp/compute.rst | 2 + 5 files changed, 76 insertions(+), 1 deletion(-) diff --git a/cpp/src/arrow/compute/api_scalar.cc b/cpp/src/arrow/compute/api_scalar.cc index 3a4d89e8e31..cead4ec4f61 100644 --- a/cpp/src/arrow/compute/api_scalar.cc +++ b/cpp/src/arrow/compute/api_scalar.cc @@ -773,6 +773,7 @@ SCALAR_EAGER_UNARY(Day, "day") SCALAR_EAGER_UNARY(DayOfYear, "day_of_year") SCALAR_EAGER_UNARY(Hour, "hour") SCALAR_EAGER_UNARY(YearMonthDay, "year_month_day") +SCALAR_EAGER_UNARY(IsDaylightSavings, "is_dst") SCALAR_EAGER_UNARY(ISOCalendar, "iso_calendar") SCALAR_EAGER_UNARY(ISOWeek, "iso_week") SCALAR_EAGER_UNARY(ISOYear, "iso_year") diff --git a/cpp/src/arrow/compute/api_scalar.h b/cpp/src/arrow/compute/api_scalar.h index 96e3d491e45..c13b45b2798 100644 --- a/cpp/src/arrow/compute/api_scalar.h +++ b/cpp/src/arrow/compute/api_scalar.h @@ -1374,6 +1374,18 @@ ARROW_EXPORT Result AssumeTimezone(const Datum& values, AssumeTimezoneOptions options, ExecContext* ctx = NULLPTR); +/// \brief IsDaylightSavings extracts if currently observing daylight savings for each +/// element of `values` +/// +/// \param[in] values input to extract daylight savings indicator from +/// \param[in] ctx the function execution context, optional +/// \return the resulting datum +/// +/// \since 8.0.0 +/// \note API not yet finalized +ARROW_EXPORT Result IsDaylightSavings(const Datum& values, + ExecContext* ctx = NULLPTR); + /// \brief Finds either the FIRST, LAST, or ALL items with a key that matches the given /// query key in a map. /// diff --git a/cpp/src/arrow/compute/kernels/scalar_temporal_test.cc b/cpp/src/arrow/compute/kernels/scalar_temporal_test.cc index f4fa2403c1f..120b7c0f60a 100644 --- a/cpp/src/arrow/compute/kernels/scalar_temporal_test.cc +++ b/cpp/src/arrow/compute/kernels/scalar_temporal_test.cc @@ -475,6 +475,8 @@ TEST_F(ScalarTemporalTest, TestTemporalComponentExtractionWithDifferentUnits) { year_month_day); CheckScalarUnary("day_of_week", unit, times_seconds_precision, int64(), day_of_week); CheckScalarUnary("day_of_year", unit, times_seconds_precision, int64(), day_of_year); + ASSERT_RAISES(Invalid, + IsDaylightSavings(ArrayFromJSON(unit, times_seconds_precision))); CheckScalarUnary("iso_year", unit, times_seconds_precision, int64(), iso_year); CheckScalarUnary("iso_week", unit, times_seconds_precision, int64(), iso_week); CheckScalarUnary("us_week", unit, times_seconds_precision, int64(), us_week); @@ -570,6 +572,9 @@ TEST_F(ScalarTemporalTest, TestZoned1) { auto day_of_week = "[2, 1, 5, 1, 1, 0, 6, 2, 3, 5, 6, 5, 5, 5, 6, 5, null]"; auto day_of_year = "[365, 60, 365, 137, 365, 364, 363, 364, 365, 2, 3, 365, 365, 362, 363, 365, null]"; + std::string is_dst = + "[false, false, false, false, false, false, false, false, false, false, false, " + "false, false, false, false, false, null]"; auto iso_year = "[1970, 2000, 1898, 2033, 2020, 2020, 2019, 2009, 2009, 2009, 2009, 2005, 2005, " "2008, 2008, 2011, null]"; @@ -604,6 +609,7 @@ TEST_F(ScalarTemporalTest, TestZoned1) { CheckScalarUnary("year_month_day", ArrayFromJSON(unit, times), year_month_day); CheckScalarUnary("day_of_week", unit, times, int64(), day_of_week); CheckScalarUnary("day_of_year", unit, times, int64(), day_of_year); + CheckScalarUnary("is_dst", unit, times, boolean(), is_dst); CheckScalarUnary("iso_year", unit, times, int64(), iso_year); CheckScalarUnary("iso_week", unit, times, int64(), iso_week); CheckScalarUnary("us_week", unit, times, int64(), us_week); @@ -644,6 +650,9 @@ TEST_F(ScalarTemporalTest, TestZoned2) { auto day_of_week = "[3, 2, 6, 2, 2, 1, 0, 3, 4, 6, 0, 6, 5, 6, 0, 6, null]"; auto day_of_year = "[1, 61, 1, 138, 1, 365, 364, 365, 1, 3, 4, 1, 365, 363, 364, 1, null]"; + std::string is_dst = + "[false, true, false, false, true, true, true, true, true, true, true, true, " + "true, true, true, true, null]"; auto iso_year = "[1970, 2000, 1898, 2033, 2020, 2020, 2020, 2009, 2009, 2009, 2010, 2005, 2005, " "2008, 2009, 2011, null]"; @@ -679,6 +688,7 @@ TEST_F(ScalarTemporalTest, TestZoned2) { year_month_day); CheckScalarUnary("day_of_week", unit, times_seconds_precision, int64(), day_of_week); CheckScalarUnary("day_of_year", unit, times_seconds_precision, int64(), day_of_year); + CheckScalarUnary("is_dst", unit, times_seconds_precision, boolean(), is_dst); CheckScalarUnary("iso_year", unit, times_seconds_precision, int64(), iso_year); CheckScalarUnary("iso_week", unit, times_seconds_precision, int64(), iso_week); CheckScalarUnary("us_week", unit, times_seconds_precision, int64(), us_week); @@ -710,6 +720,7 @@ TEST_F(ScalarTemporalTest, TestNonexistentTimezone) { ASSERT_RAISES(Invalid, YearMonthDay(timestamp_array)); ASSERT_RAISES(Invalid, DayOfWeek(timestamp_array)); ASSERT_RAISES(Invalid, DayOfYear(timestamp_array)); + ASSERT_RAISES(Invalid, IsDaylightSavings(timestamp_array)); ASSERT_RAISES(Invalid, ISOYear(timestamp_array)); ASSERT_RAISES(Invalid, Week(timestamp_array)); ASSERT_RAISES(Invalid, ISOCalendar(timestamp_array)); diff --git a/cpp/src/arrow/compute/kernels/scalar_temporal_unary.cc b/cpp/src/arrow/compute/kernels/scalar_temporal_unary.cc index 0bb170a790c..8bf82cc037a 100644 --- a/cpp/src/arrow/compute/kernels/scalar_temporal_unary.cc +++ b/cpp/src/arrow/compute/kernels/scalar_temporal_unary.cc @@ -48,7 +48,6 @@ using arrow_vendored::date::Monday; using arrow_vendored::date::months; using arrow_vendored::date::round; using arrow_vendored::date::Sunday; -using arrow_vendored::date::sys_days; using arrow_vendored::date::sys_time; using arrow_vendored::date::trunc; using arrow_vendored::date::weekday; @@ -133,6 +132,26 @@ struct AssumeTimezoneExtractor } }; +template