felipecrv commented on code in PR #41827:
URL: https://github.com/apache/arrow/pull/41827#discussion_r1623650513


##########
cpp/src/arrow/compute/kernels/scalar_cast_string.cc:
##########
@@ -510,6 +510,60 @@ void AddBinaryToFixedSizeBinaryCast(CastFunction* func) {
   AddBinaryToFixedSizeBinaryCast<FixedSizeBinaryType>(func);
 }
 
+// ----------------------------------------------------------------------
+// Union to String
+
+template <typename O>
+struct UnionToStringCastFunctor {
+  using BuilderType = typename TypeTraits<O>::BuilderType;
+
+  static Status Exec(KernelContext* ctx, const ExecSpan& batch, ExecResult* 
out) {
+    const ArraySpan& input = batch[0].array;
+    const auto& union_type = checked_cast<const UnionType&>(*input.type);
+    const auto type_ids = input.GetValues<int8_t>(1);
+    const auto& offsets = input.GetValues<int32_t>(2);
+
+    BuilderType builder(input.type->GetSharedPtr(), ctx->memory_pool());
+    RETURN_NOT_OK(builder.Reserve(input.length));
+
+    for (int64_t i = 0; i < input.length; ++i) {

Review Comment:
   A string formatter for unions should allocate a vector of string formatters 
that can do virtual dispatching (and deal with nesting themselves as well). 
`StringFormatter<T>` performs static dispatch which allows loop-specialization 
for the non-nested types. But for nested types we will need to setup a vector 
of `VirtualStringFormatter` (which is actually a tree) so that all the 
"switching on the type" happens at construction time (beginning of the loop) 
and invocations inside the loop are following the same function pointers from 
the vtables.
   
   ```cpp
   // in header
   class VirtualStringFormatter {
     virtual ... = 0;
   };
   
   Result<std::unique_ptr<VirtualStringFormatter>> MakeFormatter(
     const std::shared_ptr<DataType>& type);
   
   // in an anon namespace of the .cpp
   // one sub-class per `Type::type`
   class <T>StringFormatter : public VirtualStringFormatter {
   }
   // you can use templates to cover most cases delegating to StringFormatter<T>
   
   class UnionStringFormatter : ...
   ```
   
   This hierarchy would be similar to the builder class hierarchy.



-- 
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]

Reply via email to