This is an automated email from the ASF dual-hosted git repository.
pitrou pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow.git
The following commit(s) were added to refs/heads/main by this push:
new e8ce017bc5c GH-54668: [C++][Compute] Implement equality comparison
between Utf8View and other string types (#55171)
e8ce017bc5c is described below
commit e8ce017bc5c543119d5b74663ab996699adff805
Author: Mrigank Bhatnagar <[email protected]>
AuthorDate: Wed Nov 4 20:54:47 2026 +0530
GH-54668: [C++][Compute] Implement equality comparison between Utf8View and
other string types (#55171)
### Rationale for this change
Comparing `Utf8` or `LargeUtf8` with `Utf8View` (and `Binary` /
`LargeBinary` with `BinaryView`) currently fails with:
```text
ArrowNotImplementedError: Function 'equal' has no kernel matching input
types (string, string_view)
```
This PR registers direct comparison kernels between offset-based
string/binary types and their view equivalents so mixed comparisons execute
zero-copy without requiring explicit casting.
### What changes are included in this PR?
1. `cpp/src/arrow/compute/kernels/scalar_compare.cc`:
- Registered bidirectional mixed kernels for `(utf8, utf8_view)`,
`(large_utf8, utf8_view)`, `(binary, binary_view)`, and `(large_binary,
binary_view)` via `applicator::ScalarBinary`.
- Updated `MakeFlippedCompare` to flip argument signatures for
asymmetric kernels (`in_types[0] != in_types[1]`) so flipped functions (`less`,
`less_equal`) dispatch correctly, while preserving existing signatures and
match constraints on symmetric kernels.
- Added a `DispatchExactImpl` check in `DispatchBest` after dictionary
decoding so dictionary-encoded inputs match exact mixed kernels directly.
2. `cpp/src/arrow/compute/kernels/scalar_compare_test.cc`:
- Parameterized existing `TestBinaryViewCompareKernel.ArrayArray` and
`ArrayScalar` fixtures with the mixed type pairs across all comparison
functions.
- Added `CheckDispatchBest` coverage for mixed pairs, along with slice
and dictionary test cases.
### Are these changes tested?
Yes, covered by C++ unit tests in
`cpp/src/arrow/compute/kernels/scalar_compare_test.cc`.
### Are there any user-facing changes?
Yes. Users can now directly compare `Utf8View` and `BinaryView`
columns/scalars against `Utf8`, `LargeUtf8`, `Binary`, and `LargeBinary` across
all comparison functions (`equal`, `not_equal`, `less`, `less_equal`,
`greater`, `greater_equal`).
### Was AI used for this PR?
AI was used to assist with kernel registration and testing under human
direction.
**PR code and description written by:**
- [X] Human
- [X] AI
**Reviewed before submission by:**
- [X] Human
- [x] AI
- [ ] Not reviewed
* GitHub Issue: #54668
Authored-by: Mrigank Bhatnagar <[email protected]>
Signed-off-by: Antoine Pitrou <[email protected]>
---
cpp/src/arrow/compute/kernels/scalar_compare.cc | 30 ++++++++++
.../arrow/compute/kernels/scalar_compare_test.cc | 70 ++++++++++++++++++----
2 files changed, 88 insertions(+), 12 deletions(-)
diff --git a/cpp/src/arrow/compute/kernels/scalar_compare.cc
b/cpp/src/arrow/compute/kernels/scalar_compare.cc
index d844597e5c7..65987c47ef2 100644
--- a/cpp/src/arrow/compute/kernels/scalar_compare.cc
+++ b/cpp/src/arrow/compute/kernels/scalar_compare.cc
@@ -343,6 +343,8 @@ struct CompareFunction : ScalarFunction {
EnsureDictionaryDecoded(types);
ReplaceNullWithOtherType(types);
+ if (auto kernel = DispatchExactImpl(this, *types)) return kernel;
+
if (auto type = CommonNumeric(*types)) {
ReplaceTypes(type, types);
} else if (auto type = CommonTemporal(types->data(), types->size())) {
@@ -430,6 +432,25 @@ std::shared_ptr<ScalarFunction>
MakeCompareFunction(std::string name, FunctionDo
DCHECK_OK(func->AddKernel({ty, ty}, boolean(), std::move(exec)));
}
+ auto exec_base_view =
+ applicator::ScalarBinary<BooleanType, BinaryType, BinaryViewType,
Op>::Exec;
+ auto exec_view_base =
+ applicator::ScalarBinary<BooleanType, BinaryViewType, BinaryType,
Op>::Exec;
+ auto exec_large_view =
+ applicator::ScalarBinary<BooleanType, LargeBinaryType, BinaryViewType,
Op>::Exec;
+ auto exec_view_large =
+ applicator::ScalarBinary<BooleanType, BinaryViewType, LargeBinaryType,
Op>::Exec;
+
+ DCHECK_OK(func->AddKernel({utf8(), utf8_view()}, boolean(), exec_base_view));
+ DCHECK_OK(func->AddKernel({utf8_view(), utf8()}, boolean(), exec_view_base));
+ DCHECK_OK(func->AddKernel({large_utf8(), utf8_view()}, boolean(),
exec_large_view));
+ DCHECK_OK(func->AddKernel({utf8_view(), large_utf8()}, boolean(),
exec_view_large));
+
+ DCHECK_OK(func->AddKernel({binary(), binary_view()}, boolean(),
exec_base_view));
+ DCHECK_OK(func->AddKernel({binary_view(), binary()}, boolean(),
exec_view_base));
+ DCHECK_OK(func->AddKernel({large_binary(), binary_view()}, boolean(),
exec_large_view));
+ DCHECK_OK(func->AddKernel({binary_view(), large_binary()}, boolean(),
exec_view_large));
+
for (const auto id : {Type::DECIMAL128, Type::DECIMAL256}) {
auto exec =
GenerateDecimal<applicator::ScalarBinaryEqualTypes,
WithType0<BooleanType>, Op>(
@@ -470,6 +491,15 @@ std::shared_ptr<ScalarFunction>
MakeFlippedCompare(std::string name,
std::make_shared<CompareFunction>(name, Arity::Binary(), std::move(doc));
for (const ScalarKernel* kernel : func.kernels()) {
ScalarKernel flipped_kernel = *kernel;
+ const auto& in_types = kernel->signature->in_types();
+ if (in_types[0] != in_types[1]) {
+ // Currently, all heterogeneous-type kernels (e.g. utf8 vs utf8_view)
have no
+ // match constraint, so we can safely create a new signature without one.
+ // For symmetric-type kernels (e.g. decimal128 vs decimal128), we
preserve the
+ // original signature which may carry constraints like
DecimalsHaveSameScale.
+ flipped_kernel.signature = KernelSignature::Make({in_types[1],
in_types[0]},
+
kernel->signature->out_type());
+ }
if (kernel->data) {
auto compare_data = checked_cast<const CompareData*>(kernel->data.get());
flipped_kernel.data =
diff --git a/cpp/src/arrow/compute/kernels/scalar_compare_test.cc
b/cpp/src/arrow/compute/kernels/scalar_compare_test.cc
index 2aae5bf2ee9..d389cca377a 100644
--- a/cpp/src/arrow/compute/kernels/scalar_compare_test.cc
+++ b/cpp/src/arrow/compute/kernels/scalar_compare_test.cc
@@ -1021,6 +1021,16 @@ TEST(TestCompareKernel, DispatchBest) {
CheckDispatchBest(name, {binary(), fixed_size_binary(2)}, {binary(),
binary()});
CheckDispatchBest(name, {fixed_size_binary(4), fixed_size_binary(2)},
{fixed_size_binary(4), fixed_size_binary(2)});
+ CheckDispatchBest(name, {utf8(), utf8_view()}, {utf8(), utf8_view()});
+ CheckDispatchBest(name, {utf8_view(), utf8()}, {utf8_view(), utf8()});
+ CheckDispatchBest(name, {large_utf8(), utf8_view()}, {large_utf8(),
utf8_view()});
+ CheckDispatchBest(name, {utf8_view(), large_utf8()}, {utf8_view(),
large_utf8()});
+ CheckDispatchBest(name, {binary(), binary_view()}, {binary(),
binary_view()});
+ CheckDispatchBest(name, {binary_view(), binary()}, {binary_view(),
binary()});
+ CheckDispatchBest(name, {large_binary(), binary_view()},
+ {large_binary(), binary_view()});
+ CheckDispatchBest(name, {binary_view(), large_binary()},
+ {binary_view(), large_binary()});
CheckDispatchBest(name, {decimal128(3, 2), decimal128(6, 3)},
{decimal128(4, 3), decimal128(6, 3)});
@@ -1197,7 +1207,14 @@ TEST_F(TestStringCompareKernel, RandomCompareArrayArray)
{
}
TEST(TestBinaryViewCompareKernel, ArrayArray) {
- const auto cases = std::vector<std::shared_ptr<DataType>>{binary_view(),
utf8_view()};
+ const auto cases =
+ std::vector<std::pair<std::shared_ptr<DataType>,
std::shared_ptr<DataType>>>{
+ {binary_view(), binary_view()}, {utf8_view(), utf8_view()},
+ {utf8(), utf8_view()}, {utf8_view(), utf8()},
+ {large_utf8(), utf8_view()}, {utf8_view(), large_utf8()},
+ {binary(), binary_view()}, {binary_view(), binary()},
+ {large_binary(), binary_view()}, {binary_view(), large_binary()},
+ };
const auto expected = std::vector<std::pair<std::string, std::string>>{
{"equal", "[true, false, false, false, false, false, null]"},
{"not_equal", "[false, true, true, true, true, true, null]"},
@@ -1206,18 +1223,19 @@ TEST(TestBinaryViewCompareKernel, ArrayArray) {
{"less", "[false, true, true, true, true, false, null]"},
{"less_equal", "[true, true, true, true, true, false, null]"}};
- for (const auto& ty : cases) {
- auto lhs =
- ArrayFromJSON(ty, R"(["", "abc", "abcdefghijkl", "abcdefghijklm",
"prefix_same_A",
+ for (const auto& [lhs_type, rhs_type] : cases) {
+ auto lhs = ArrayFromJSON(
+ lhs_type, R"(["", "abc", "abcdefghijkl", "abcdefghijklm",
"prefix_same_A",
"samepref_size", null])");
auto rhs = ArrayFromJSON(
- ty, R"(["", "abd", "abcdefghijklm", "abcdefghijklz", "prefix_same_B",
+ rhs_type, R"(["", "abd", "abcdefghijklm", "abcdefghijklz",
"prefix_same_B",
"samepref", null])");
- CheckScalarBinary("equal", ArrayFromJSON(ty, R"([])"), ArrayFromJSON(ty,
R"([])"),
+ CheckScalarBinary("equal", ArrayFromJSON(lhs_type, R"([])"),
+ ArrayFromJSON(rhs_type, R"([])"),
ArrayFromJSON(boolean(), R"([])"));
- CheckScalarBinary("equal", ArrayFromJSON(ty, R"([null])"),
- ArrayFromJSON(ty, R"([null])"),
+ CheckScalarBinary("equal", ArrayFromJSON(lhs_type, R"([null])"),
+ ArrayFromJSON(rhs_type, R"([null])"),
ArrayFromJSON(boolean(), R"([null])"));
for (const auto& function_and_expected : expected) {
CheckScalarBinary(function_and_expected.first, lhs, rhs,
@@ -1227,10 +1245,19 @@ TEST(TestBinaryViewCompareKernel, ArrayArray) {
}
TEST(TestBinaryViewCompareKernel, ArrayScalar) {
- for (const auto& ty : {binary_view(), utf8_view()}) {
- auto arr = ArrayFromJSON(ty, R"(["", "abc", "abcdefghijklmnop", null])");
- auto scalar = ScalarFromJSON(ty, R"("abc")");
- auto null_scalar = ScalarFromJSON(ty, "null");
+ const auto cases =
+ std::vector<std::pair<std::shared_ptr<DataType>,
std::shared_ptr<DataType>>>{
+ {binary_view(), binary_view()}, {utf8_view(), utf8_view()},
+ {utf8(), utf8_view()}, {utf8_view(), utf8()},
+ {large_utf8(), utf8_view()}, {utf8_view(), large_utf8()},
+ {binary(), binary_view()}, {binary_view(), binary()},
+ {large_binary(), binary_view()}, {binary_view(), large_binary()},
+ };
+
+ for (const auto& [arr_type, scalar_type] : cases) {
+ auto arr = ArrayFromJSON(arr_type, R"(["", "abc", "abcdefghijklmnop",
null])");
+ auto scalar = ScalarFromJSON(scalar_type, R"("abc")");
+ auto null_scalar = ScalarFromJSON(scalar_type, "null");
CheckScalarBinary("equal", arr, scalar,
ArrayFromJSON(boolean(), R"([false, true, false,
null])"));
@@ -1245,6 +1272,25 @@ TEST(TestBinaryViewCompareKernel, ArrayScalar) {
}
}
+TEST(TestBinaryViewCompareKernel, Sliced) {
+ auto str_arr = ArrayFromJSON(utf8(), R"(["a", "b", null, "d"])");
+ auto view_arr = ArrayFromJSON(utf8_view(), R"(["a", "x", "c", null])");
+ CheckScalarBinary("equal", str_arr->Slice(1, 2), view_arr->Slice(1, 2),
+ ArrayFromJSON(boolean(), "[false, null]"));
+ CheckScalarBinary("equal", view_arr->Slice(1, 2), str_arr->Slice(1, 2),
+ ArrayFromJSON(boolean(), "[false, null]"));
+}
+
+TEST(TestBinaryViewCompareKernel, Dictionary) {
+ auto dict_str =
+ ArrayFromJSON(dictionary(int32(), utf8()), R"(["apple", "banana",
null])");
+ auto plain_view = ArrayFromJSON(utf8_view(), R"(["apple", "cherry",
"banana"])");
+ CheckScalarBinary("equal", dict_str, plain_view,
+ ArrayFromJSON(boolean(), "[true, false, null]"));
+ CheckScalarBinary("equal", plain_view, dict_str,
+ ArrayFromJSON(boolean(), "[true, false, null]"));
+}
+
template <typename T>
class TestVarArgsCompare : public ::testing::Test {
protected: