edponce commented on a change in pull request #11023:
URL: https://github.com/apache/arrow/pull/11023#discussion_r739611163



##########
File path: cpp/src/arrow/compute/kernels/scalar_string.cc
##########
@@ -537,6 +536,341 @@ struct FixedSizeBinaryTransformExecWithState
   }
 };
 
+template <typename Type1, typename Type2>
+struct StringBinaryTransformBase {
+  using ViewType2 = typename GetViewType<Type2>::T;
+  using ArrayType1 = typename TypeTraits<Type1>::ArrayType;
+  using ArrayType2 = typename TypeTraits<Type2>::ArrayType;
+
+  virtual ~StringBinaryTransformBase() = default;
+
+  virtual Status PreExec(KernelContext* ctx, const ExecBatch& batch, Datum* 
out) {
+    return Status::OK();
+  }
+
+  virtual Status InvalidStatus() {
+    return Status::Invalid("Invalid UTF8 sequence in input");
+  }
+
+  // Return the maximum total size of the output in codeunits (i.e. bytes)
+  // given input characteristics for different input shapes.
+  //
+  // Scalar-Scalar
+  virtual int64_t MaxCodeunits(const int64_t input1_ncodeunits, const 
ViewType2) {
+    return input1_ncodeunits;
+  }
+
+  // Scalar-Array
+  virtual int64_t MaxCodeunits(const int64_t input1_ncodeunits, const 
ArrayType2&) {
+    return input1_ncodeunits;
+  }
+
+  // Array-Scalar
+  virtual int64_t MaxCodeunits(const ArrayType1& input1, const ViewType2) {
+    return input1.total_values_length();
+  }
+
+  // Array-Array
+  virtual int64_t MaxCodeunits(const ArrayType1& input1, const ArrayType2&) {
+    return input1.total_values_length();
+  }
+
+  // Not all combinations of input shapes are meaningful to string binary
+  // transforms, so these flags serve as control toggles for enabling/disabling
+  // the corresponding ones. These flags should be set in the PreExec() method.
+  //
+  // This is an example of a StringTransform that disables argument shapes with
+  // mixed scalar/array.
+  //
+  // template <typename Type1, typename Type2>
+  // struct MyStringTransform : public StringBinaryTransformBase<Type1, Type2> 
{
+  //   Status PreExec(KernelContext* ctx, const ExecBatch& batch, Datum* out) 
override {
+  //     EnableScalarArray = false;
+  //     EnableArrayScalar = false;
+  //     return StringBinaryTransformBase::PreExec(ctx, batch, out);
+  //   }
+  //   ...
+  // };
+  bool EnableScalarScalar = true;
+  bool EnableScalarArray = true;
+  bool EnableArrayScalar = true;
+  bool EnableArrayArray = true;
+
+  // Tracks status of transform in StringBinaryTransformExecBase.
+  // The purpose of this transform status is to provide a means to 
report/detect
+  // errors in functions that do not provide a mechanism to return a Status
+  // value but can still detect errors. This status is checked automatically
+  // after MaxCodeunits() and Transform() operations.
+  Status st = Status::OK();
+};
+
+/// Kernel exec generator for binary (two parameters) string transforms.
+/// The first parameter is expected to always be a Binary/StringType while the
+/// second parameter is generic. Types of template parameter StringTransform
+/// need to define a transform method with the following signature:
+///
+/// int64_t Transform(const uint8_t* input, const int64_t 
input_string_ncodeunits,
+///                   const ViewType2 value2, uint8_t* output);
+template <typename Type1, typename Type2, typename StringTransform>
+struct StringBinaryTransformExecBase {
+  using offset_type = typename Type1::offset_type;
+  using ViewType2 = typename GetViewType<Type2>::T;
+  using ArrayType1 = typename TypeTraits<Type1>::ArrayType;
+  using ArrayType2 = typename TypeTraits<Type2>::ArrayType;
+
+  static Status Execute(KernelContext* ctx, StringTransform* transform,
+                        const ExecBatch& batch, Datum* out) {
+    if (batch[0].is_scalar()) {
+      if (batch[1].is_scalar()) {
+        if (transform->EnableScalarScalar) {
+          return ExecScalarScalar(ctx, transform, batch[0].scalar(), 
batch[1].scalar(),
+                                  out);
+        }
+      } else if (batch[1].is_array()) {
+        if (transform->EnableScalarArray) {
+          return ExecScalarArray(ctx, transform, batch[0].scalar(), 
batch[1].array(),
+                                 out);
+        }
+      }
+    } else if (batch[0].is_array()) {
+      if (batch[1].is_array()) {
+        if (transform->EnableArrayArray) {
+          return ExecArrayArray(ctx, transform, batch[0].array(), 
batch[1].array(), out);
+        }
+      } else if (batch[1].is_scalar()) {
+        if (transform->EnableArrayScalar) {
+          return ExecArrayScalar(ctx, transform, batch[0].array(), 
batch[1].scalar(),
+                                 out);
+        }
+      }
+    }
+    return Status::Invalid("Invalid ExecBatch kind for binary string 
transform");
+  }
+
+  static Status ExecScalarScalar(KernelContext* ctx, StringTransform* 
transform,
+                                 const std::shared_ptr<Scalar>& scalar1,
+                                 const std::shared_ptr<Scalar>& scalar2, 
Datum* out) {
+    if (!scalar1->is_valid || !scalar2->is_valid) {
+      return Status::OK();
+    }
+    const auto& binary_scalar1 = checked_cast<const 
BaseBinaryScalar&>(*scalar1);
+    const auto input_string = binary_scalar1.value->data();
+    const auto input_ncodeunits = binary_scalar1.value->size();
+    const auto value2 = UnboxScalar<Type2>::Unbox(*scalar2);
+
+    // Calculate max number of output codeunits
+    const auto max_output_ncodeunits = 
transform->MaxCodeunits(input_ncodeunits, value2);

Review comment:
       The output size depends on the transform and the input encoding 
(binary/ASCII/UTF8). Also, the `MaxCodeunits()` does not needs to calculate the 
exact output size because [a resizing operation is performed at end kernel 
exec](https://github.com/apache/arrow/blob/master/cpp/src/arrow/compute/kernels/scalar_string.cc#L422),
 but needs to allocate enough space, not less.
   
   Binary/ASCII transforms that do not change the size (uppercase, title, 
capitalize, etc.), [use the default 
`MaxCodeunits()`](https://github.com/apache/arrow/blob/master/cpp/src/arrow/compute/kernels/scalar_string.cc#L330).
 On the other hand, the [default `MaxCodeunits()` for UTF8 
transform](https://github.com/apache/arrow/blob/master/cpp/src/arrow/compute/kernels/scalar_string.cc#L555)
 allocates more for the output.
   
   Some transforms will have different estimates for the output size (as is the 
case in this PR). This is the first "binary string transform" implemented as 
such and so I decided to generalize the machinery in order to support other 
ones.
   
   But most importantly is to note that [many string transforms implement their 
own `kernel 
exec`](https://github.com/apache/arrow/blob/master/cpp/src/arrow/compute/kernels/scalar_string.cc#L1010)
 and do not use `MaxCodeunits()`. Hopefully, as the variety of patterns in 
string transforms stabilizes, we can use consistent `kernel execs` and perform 
similarly.




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