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
6 changes: 3 additions & 3 deletions cpp/src/arrow/compute/kernels/scalar_if_else.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1265,9 +1265,9 @@ struct IfElseFunction : ScalarFunction {
// TODO(ARROW-14105): apply implicit casts to dictionary types too
TypeHolder* right_arg = &(*types)[2];
if (is_dictionary(left_arg->id()) && left_arg->type->Equals(*right_arg->type)) {
auto kernel = DispatchExactImpl(this, *types);
DCHECK(kernel);
return kernel;
// No kernel matches if the condition is not boolean
if (auto kernel = DispatchExactImpl(this, *types)) return kernel;
return arrow::compute::detail::NoMatchingKernel(this, *types);
}

internal::EnsureDictionaryDecoded(left_arg, num_args);
Expand Down
15 changes: 15 additions & 0 deletions cpp/src/arrow/compute/kernels/scalar_if_else_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,21 @@ TYPED_TEST(TestIfElseDict, DifferentDictionaries) {
CheckDictionary("if_else", {MakeNullScalar(boolean()), values1, values2});
}

// GH-55960: a non-boolean condition with dictionary values used to crash.
TEST(TestIfElse, DictionaryNonBooleanCondition) {
auto type = dictionary(int32(), utf8());
auto values = DictArrayFromJSON(type, "[0, 1]", R"(["a", "b"])");
EXPECT_RAISES_WITH_MESSAGE_THAT(
NotImplemented,
::testing::HasSubstr("Function 'if_else' has no kernel matching input types"),
CallFunction("if_else", {ArrayFromJSON(int64(), "[1, 0]"), values, values}));
EXPECT_RAISES_WITH_MESSAGE_THAT(
NotImplemented,
::testing::HasSubstr("Function 'if_else' has no kernel matching input types"),
CallFunction("if_else",
{ArrayFromJSON(int64(), "[1, 0]"), values, MakeNullScalar(null())}));
}

// GH-47825: BitmapOp overrides partial leading byte for unaligned chunked execution.
TEST(TestIfElse, ChunkedUnalignedBitmapOp) {
auto cond = ArrayFromJSON(boolean(), "[true, true]");
Expand Down
13 changes: 13 additions & 0 deletions python/pyarrow/tests/test_compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -1266,6 +1266,19 @@ def test_extract_regex_span():
assert struct.tolist() == expected


def test_if_else_dictionary_non_boolean_condition():
# GH-55960: a non-boolean condition with dictionary values crashed
values = pa.array(["a", "b"]).dictionary_encode()
message = "Function 'if_else' has no kernel matching input types"
with pytest.raises(pa.ArrowNotImplementedError, match=message):
pc.if_else(pa.array([1, 0]), values, values)

# A boolean condition still selects from the dictionary values
result = pc.if_else(pa.array([True, False]), values,
pa.array(["c", "d"]).dictionary_encode())
assert result.to_pylist() == ["a", "d"]


def test_replace_with_mask_null_type():
# GH-47447: replace_with_mask crashed for null type arrays
input = pa.array([None], pa.null())
Expand Down