Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cpp/src/arrow/compute/kernels/scalar_string_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,11 @@ void MakeUnaryStringBatchKernel(
ARROW_DCHECK_OK(registry->AddFunction(std::move(func)));
}

template <template <typename> class ExecFunctor>
template <template <typename> class ExecFunctor, typename FunctionType = ScalarFunction>
void MakeUnaryStringBatchKernelWithState(
std::string name, FunctionRegistry* registry, FunctionDoc doc,
MemAllocation::type mem_allocation = MemAllocation::PREALLOCATE) {
auto func = std::make_shared<ScalarFunction>(name, Arity::Unary(), std::move(doc));
auto func = std::make_shared<FunctionType>(name, Arity::Unary(), std::move(doc));
{
using t32 = ExecFunctor<StringType>;
ScalarKernel kernel{{utf8()}, utf8(), t32::Exec, t32::State::Init};
Expand Down
12 changes: 12 additions & 0 deletions cpp/src/arrow/compute/kernels/scalar_string_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2471,6 +2471,18 @@ TYPED_TEST(TestStringKernels, TrimUTF8) {
"[\"zȺz矢ba\", null, \"\", \"zȺz\"]", &options);
this->CheckUnary("utf8_rtrim", "[\"azȺz矢ba\", null, \"bab\", \"zȺz\"]", this->type(),
"[\"azȺz矢\", null, \"\", \"zȺz\"]", &options);
this->CheckUnary("utf8_trim",
ArrayFromJSON(dictionary(int64(), this->type()),
R"(["azȺz矢ba", null, "bab", "zȺz"])"),
this->type(), R"(["zȺz矢", null, "", "zȺz"])", &options);
this->CheckUnary("utf8_ltrim",
ArrayFromJSON(dictionary(int64(), this->type()),
R"(["azȺz矢ba", null, "bab", "zȺz"])"),
this->type(), R"(["zȺz矢ba", null, "", "zȺz"])", &options);
this->CheckUnary("utf8_rtrim",
ArrayFromJSON(dictionary(int64(), this->type()),
R"(["azȺz矢ba", null, "bab", "zȺz"])"),
this->type(), R"(["azȺz矢", null, "", "zȺz"])", &options);

options = TrimOptions{"ȺA"};
this->CheckUnary("utf8_trim", "[\"ȺȺfoo矢ȺAȺ\", null, \"barȺAȺ\", \"ȺAȺfooȺAȺ矢barA\"]",
Expand Down
28 changes: 25 additions & 3 deletions cpp/src/arrow/compute/kernels/scalar_string_utf8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -881,10 +881,32 @@ const FunctionDoc utf8_rtrim_whitespace_doc(

#endif // ARROW_WITH_UTF8PROC

// Trim functions additionally dispatch dictionary-encoded inputs to the kernel
// for their value type (the dictionary is then decoded via Cast() before Exec
// runs), matching CompareFunction/ScalarCTypeToInt64Function elsewhere.
struct UnaryStringWithDictionaryFunction : public ScalarFunction {
using ScalarFunction::ScalarFunction;

Result<const Kernel*> DispatchBest(std::vector<TypeHolder>* types) const override {
RETURN_NOT_OK(CheckArity(types->size()));

using arrow::compute::detail::DispatchExactImpl;
if (auto kernel = DispatchExactImpl(this, *types)) return kernel;

EnsureDictionaryDecoded(types);

if (auto kernel = DispatchExactImpl(this, *types)) return kernel;
return arrow::compute::detail::NoMatchingKernel(this, *types);
}
};

void AddUtf8StringTrim(FunctionRegistry* registry) {
MakeUnaryStringBatchKernelWithState<UTF8Trim>("utf8_trim", registry, utf8_trim_doc);
MakeUnaryStringBatchKernelWithState<UTF8LTrim>("utf8_ltrim", registry, utf8_ltrim_doc);
MakeUnaryStringBatchKernelWithState<UTF8RTrim>("utf8_rtrim", registry, utf8_rtrim_doc);
MakeUnaryStringBatchKernelWithState<UTF8Trim, UnaryStringWithDictionaryFunction>(
"utf8_trim", registry, utf8_trim_doc);
MakeUnaryStringBatchKernelWithState<UTF8LTrim, UnaryStringWithDictionaryFunction>(
"utf8_ltrim", registry, utf8_ltrim_doc);
MakeUnaryStringBatchKernelWithState<UTF8RTrim, UnaryStringWithDictionaryFunction>(
"utf8_rtrim", registry, utf8_rtrim_doc);
#ifdef ARROW_WITH_UTF8PROC
MakeUnaryStringBatchKernel<UTF8TrimWhitespace>("utf8_trim_whitespace", registry,
utf8_trim_whitespace_doc);
Expand Down
Loading