rok commented on a change in pull request #10647:
URL: https://github.com/apache/arrow/pull/10647#discussion_r668589540
##########
File path: cpp/src/arrow/compute/kernels/scalar_temporal.cc
##########
@@ -321,6 +322,53 @@ struct Nanosecond {
}
};
+// ----------------------------------------------------------------------
+// Convert timestamps to a string representation with an arbitrary format
+
+template <typename Duration>
+inline std::string get_timestamp(int64_t arg, const StrftimeOptions* options) {
+ auto zt = arrow_vendored::date::zoned_time<Duration>{options->tz,
+
sys_time<Duration>(Duration{arg})};
+ return arrow_vendored::date::format(options->format, zt.get_local_time());
+}
+
+template <typename Duration>
+struct Strftime {
+ static Status Call(KernelContext* ctx, const Scalar& in, Scalar* out) {
+ const StrftimeOptions options = StrftimeState::Get(ctx);
+
+ if (in.is_valid) {
+ const auto& in_val = internal::UnboxScalar<const
TimestampType>::Unbox(in);
+ *checked_cast<StringScalar*>(out) =
+ StringScalar(get_timestamp<Duration>(in_val, &options));
+ } else {
+ out->is_valid = false;
+ }
+ return Status::OK();
+ }
+
+ static Status Call(KernelContext* ctx, const ArrayData& in, ArrayData* out) {
+ const StrftimeOptions options = StrftimeState::Get(ctx);
+
+ std::unique_ptr<ArrayBuilder> array_builder;
+ RETURN_NOT_OK(MakeBuilder(ctx->memory_pool(), utf8(), &array_builder));
+ StringBuilder* string_builder =
checked_cast<StringBuilder*>(array_builder.get());
+ RETURN_NOT_OK(string_builder->Reserve(in.length * 30));
Review comment:
How about a random string size times a fudge factor? :)
```
int expected_string_size = round(get_timestamp<Duration>(0, &options).size()
* 1.1);
RETURN_NOT_OK(string_builder->Reserve(in.length * expected_string_size));
```
--
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]