rok commented on a change in pull request #10457:
URL: https://github.com/apache/arrow/pull/10457#discussion_r689611393
##########
File path: cpp/src/arrow/compute/kernels/scalar_temporal.cc
##########
@@ -63,79 +71,132 @@ const std::string& GetInputTimezone(const ArrayData&
array) {
return checked_cast<const TimestampType&>(*array.type).timezone();
}
-template <typename T>
-Status TemporalComponentExtractCheckTimezone(const T& input) {
- const auto& timezone = GetInputTimezone(input);
- if (!timezone.empty()) {
- return Status::NotImplemented(
- "Cannot extract components from timestamp with specific timezone: ",
timezone);
- }
- return Status::OK();
-}
-
-template <typename Op, typename OutType>
+template <
+ template <typename Duration, typename TimePointConverter, typename
DaysConverter>
+ class Op,
+ typename Duration, typename OutType>
struct TemporalComponentExtract {
- using OutValue = typename internal::GetOutputType<OutType>::T;
-
static Status Exec(KernelContext* ctx, const ExecBatch& batch, Datum* out) {
- RETURN_NOT_OK(TemporalComponentExtractCheckTimezone(batch.values[0]));
- return ScalarUnaryNotNull<OutType, TimestampType, Op>::Exec(ctx, batch,
out);
+ const auto& timezone = GetInputTimezone(batch.values[0]);
+ if (timezone.empty()) {
+ using ExecTemplate = Op<Duration,
std::function<sys_time<Duration>(int64_t)>,
+ std::function<sys_days(sys_days)>>;
+ auto op = ExecTemplate([](int64_t t) { return
sys_time<Duration>(Duration{t}); },
+ [](sys_days d) { return d; });
+ applicator::ScalarUnaryNotNullStateful<OutType, TimestampType,
ExecTemplate> kernel{
+ op};
+ return kernel.Exec(ctx, batch, out);
+ } else {
+ const time_zone* tz;
+ try {
+ tz = locate_zone(timezone);
+ } catch (const std::runtime_error& ex) {
+ return Status::Invalid(ex.what());
+ }
+ using ExecTemplate = Op<Duration,
std::function<local_time<Duration>(int64_t)>,
+ std::function<local_days(sys_days)>>;
+ auto op = ExecTemplate(
+ [tz](int64_t t) { return
tz->to_local(sys_time<Duration>(Duration{t})); },
+ [](sys_days d) { return local_days(year_month_day(d)); });
+ applicator::ScalarUnaryNotNullStateful<OutType, TimestampType,
ExecTemplate> kernel{
+ op};
+ return kernel.Exec(ctx, batch, out);
+ }
}
};
-template <typename Op, typename OutType>
-struct DayOfWeekExec {
- using OutValue = typename internal::GetOutputType<OutType>::T;
-
+template <
+ template <typename Duration, typename TimePointConverter, typename
DaysConverter>
+ class Op,
+ typename Duration, typename OutType>
+struct TemporalComponentExtractDayOfWeek {
static Status Exec(KernelContext* ctx, const ExecBatch& batch, Datum* out) {
+ const auto& timezone = GetInputTimezone(batch.values[0]);
const DayOfWeekOptions& options = DayOfWeekState::Get(ctx);
Review comment:
Indeed. This will be useful for the `Strftime` and `localize` kernels as
well.
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]