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



##########
File path: cpp/src/arrow/compute/kernels/scalar_temporal.cc
##########
@@ -0,0 +1,614 @@
+// 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 applicator::ScalarUnaryNotNull;
+using applicator::SimpleUnary;
+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;
+
+// Based on ScalarUnaryNotNullStateful. Adds timezone awareness.
+template <typename OutType, typename Op>
+struct ScalarUnaryStatefulTemporal {
+  using ThisType = ScalarUnaryStatefulTemporal<OutType, Op>;
+  using OutValue = typename GetOutputType<OutType>::T;
+
+  Op op;
+  explicit ScalarUnaryStatefulTemporal(Op op) : op(std::move(op)) {}
+
+  template <typename Type>
+  struct ArrayExec {
+    static Status Exec(const ThisType& functor, KernelContext* ctx, const 
ArrayData& arg0,
+                       Datum* out) {
+      const std::string timezone =
+          std::static_pointer_cast<const TimestampType>(arg0.type)->timezone();
+      Status st = Status::OK();
+      ArrayData* out_arr = out->mutable_array();
+      auto out_data = out_arr->GetMutableValues<OutValue>(1);
+
+      if (timezone.empty()) {
+        VisitArrayValuesInline<Int64Type>(
+            arg0,
+            [&](int64_t v) {
+              *out_data++ = functor.op.template Call<OutValue>(ctx, v, &st);
+            },
+            [&]() {
+              // null
+              ++out_data;
+            });
+      } else {
+        st = Status::Invalid("Timezone aware timestamps not supported. 
Timezone found: ",
+                             timezone);
+      }
+      return st;
+    }
+  };
+
+  Status Scalar(KernelContext* ctx, const Scalar& arg0, Datum* out) {
+    const std::string timezone =
+        std::static_pointer_cast<const TimestampType>(arg0.type)->timezone();
+    Status st = Status::OK();
+    if (timezone.empty()) {
+      if (arg0.is_valid) {
+        int64_t arg0_val = UnboxScalar<Int64Type>::Unbox(arg0);
+        BoxScalar<OutType>::Box(this->op.template Call<OutValue>(ctx, 
arg0_val, &st),
+                                out->scalar().get());
+      }
+    } else {
+      st = Status::Invalid("Timezone aware timestamps not supported. Timezone 
found: ",
+                           timezone);
+    }
+    return st;
+  }
+
+  Status Exec(KernelContext* ctx, const ExecBatch& batch, Datum* out) {
+    if (batch[0].kind() == Datum::ARRAY) {
+      return ArrayExec<OutType>::Exec(*this, ctx, *batch[0].array(), out);
+    } else {
+      return Scalar(ctx, *batch[0].scalar(), out);
+    }
+  }
+};
+
+template <typename OutType, typename Op>
+struct ScalarUnaryTemporal {
+  using OutValue = typename GetOutputType<OutType>::T;
+
+  static Status Exec(KernelContext* ctx, const ExecBatch& batch, Datum* out) {
+    // Seed kernel with dummy state
+    ScalarUnaryStatefulTemporal<OutType, Op> kernel({});
+    return kernel.Exec(ctx, batch, out);
+  }
+};
+
+// ----------------------------------------------------------------------
+// Extract year from timestamp
+
+template <typename Duration>
+struct Year {
+  template <typename T>
+  static T Call(KernelContext*, int64_t 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>
+  static T Call(KernelContext*, int64_t 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>
+  static T Call(KernelContext*, int64_t 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>
+  static T Call(KernelContext*, int64_t 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>
+  static T Call(KernelContext*, int64_t arg, Status*) {
+    const auto sd = sys_days{floor<days>(Duration{arg})};

Review comment:
       `sys_days` is shorthand for `sys_time<days>`. I've made everything 
`sys_time<days>` now.




-- 
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:
us...@infra.apache.org


Reply via email to