rok commented on a change in pull request #10176:
URL: https://github.com/apache/arrow/pull/10176#discussion_r639240708



##########
File path: cpp/src/arrow/compute/kernels/scalar_temporal.cc
##########
@@ -0,0 +1,509 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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.
+
+#include "arrow/builder.h"
+#include "arrow/compute/kernels/common.h"
+#include "arrow/util/time.h"
+#include "arrow/vendored/datetime.h"
+
+namespace arrow {
+
+namespace compute {
+namespace internal {
+
+using arrow_vendored::date::days;
+using arrow_vendored::date::floor;
+using arrow_vendored::date::hh_mm_ss;
+using arrow_vendored::date::sys_days;
+using arrow_vendored::date::sys_time;
+using arrow_vendored::date::trunc;
+using arrow_vendored::date::weekday;
+using arrow_vendored::date::weeks;
+using arrow_vendored::date::year_month_day;
+using arrow_vendored::date::years;
+using arrow_vendored::date::literals::dec;
+using arrow_vendored::date::literals::jan;
+using arrow_vendored::date::literals::last;
+using arrow_vendored::date::literals::mon;
+using arrow_vendored::date::literals::thu;
+
+// ----------------------------------------------------------------------
+// Extract year from timestamp
+
+template <typename Duration>
+struct Year {
+  template <typename T, typename Arg>
+  static T Call(KernelContext*, Arg arg, Status*) {
+    return static_cast<T>(static_cast<const int32_t>(
+        
year_month_day(floor<days>(sys_time<Duration>(Duration{arg}))).year()));
+  }
+};
+
+// ----------------------------------------------------------------------
+// Extract month from timestamp
+
+template <typename Duration>
+struct Month {
+  template <typename T, typename Arg>
+  static T Call(KernelContext*, Arg arg, Status*) {
+    return static_cast<T>(static_cast<const uint32_t>(
+        
year_month_day(floor<days>(sys_time<Duration>(Duration{arg}))).month()));
+  }
+};
+
+// ----------------------------------------------------------------------
+// Extract day from timestamp
+
+template <typename Duration>
+struct Day {
+  template <typename T, typename Arg>
+  static T Call(KernelContext*, Arg arg, Status*) {
+    return static_cast<T>(static_cast<const uint32_t>(
+        year_month_day(floor<days>(sys_time<Duration>(Duration{arg}))).day()));
+  }
+};
+
+// ----------------------------------------------------------------------
+// Extract day of week from timestamp
+
+template <typename Duration>
+struct DayOfWeek {
+  template <typename T, typename Arg>
+  static T Call(KernelContext*, Arg arg, Status*) {
+    return static_cast<T>(
+        weekday(year_month_day(floor<days>(sys_time<Duration>(Duration{arg}))))
+            .iso_encoding());
+  }
+};
+
+// ----------------------------------------------------------------------
+// Extract day of year from timestamp
+
+template <typename Duration>
+struct DayOfYear {
+  template <typename T, typename Arg>
+  static T Call(KernelContext*, Arg arg, Status*) {
+    const auto sd = sys_days{floor<days>(Duration{arg})};
+    return static_cast<T>((sd - sys_days(year_month_day(sd).year() / jan / 
0)).count());
+  }
+};
+
+// ----------------------------------------------------------------------
+// Extract ISO Year values from timestamp
+
+template <typename Duration>
+struct ISOYear {
+  template <typename T, typename Arg>
+  static T Call(KernelContext*, Arg arg, Status*) {
+    return static_cast<T>(static_cast<const int32_t>(
+        year_month_day{sys_days{floor<days>(Duration{arg})} + 
days{3}}.year()));
+  }
+};
+
+// ----------------------------------------------------------------------
+// Extract ISO week from timestamp
+
+// Based on
+// 
https://github.com/HowardHinnant/date/blob/6e921e1b1d21e84a5c82416ba7ecd98e33a436d0/include/date/iso_week.h#L1503
+template <typename Duration>
+struct ISOWeek {
+  template <typename T, typename Arg>
+  static T Call(KernelContext*, Arg arg, Status*) {
+    const auto dp = sys_days{floor<days>(Duration{arg})};
+    auto y = year_month_day{dp + days{3}}.year();
+    auto start = sys_days((y - years{1}) / dec / thu[last]) + (mon - thu);
+    if (dp < start) {
+      --y;
+      start = sys_days((y - years{1}) / dec / thu[last]) + (mon - thu);
+    }
+    return static_cast<T>(trunc<weeks>(dp - start).count() + 1);
+  }
+};
+
+// ----------------------------------------------------------------------
+// Extract ISO calendar values from timestamp
+
+const std::shared_ptr<DataType> iso_calendar_type = struct_(
+    {field("iso_year", int64()), field("iso_week", int64()), field("weekday", 
int64())});
+
+template <typename Duration>
+struct ISOCalendar {
+  static Status Call(KernelContext* ctx, const Scalar& in, Scalar* out) {
+    using ScalarType = typename TypeTraits<Int64Type>::ScalarType;
+
+    const auto& in_val = internal::UnboxScalar<const TimestampType>::Unbox(in);
+    const auto dp = sys_days{floor<days>(Duration{in_val})};
+    const auto ymd = year_month_day(dp);
+    auto y = year_month_day{dp + days{3}}.year();
+    auto start = sys_days((y - years{1}) / dec / thu[last]) + (mon - thu);
+    if (dp < start) {
+      --y;
+      start = sys_days((y - years{1}) / dec / thu[last]) + (mon - thu);
+    }
+
+    std::vector<std::shared_ptr<Scalar>> values = {
+        std::make_shared<ScalarType>(
+            static_cast<int64_t>(static_cast<int32_t>(ymd.year()))),
+        std::make_shared<ScalarType>(
+            static_cast<int64_t>(trunc<weeks>(dp - start).count() + 1)),
+        
std::make_shared<ScalarType>(static_cast<int64_t>(weekday(ymd).iso_encoding()))};
+    *checked_cast<StructScalar*>(out) =
+        StructScalar(std::move(values), iso_calendar_type);
+    return Status::OK();
+  }

Review comment:
       Got it. Kernel signature was set to only expect arrays and not scalars.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to