From 178b0dc23b0a803e81cc231e40f0b6e03563391d Mon Sep 17 00:00:00 2001 From: Chetan Sahney Date: Fri, 4 Dec 2026 17:31:55 +0530 Subject: [PATCH 1/2] GH-55960: [C++] Raise instead of crashing in if_else with a non-boolean condition and dictionary values --- cpp/src/arrow/compute/kernels/scalar_if_else.cc | 6 +++--- .../arrow/compute/kernels/scalar_if_else_test.cc | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/cpp/src/arrow/compute/kernels/scalar_if_else.cc b/cpp/src/arrow/compute/kernels/scalar_if_else.cc index 331439e00928..acc20203c9dd 100644 --- a/cpp/src/arrow/compute/kernels/scalar_if_else.cc +++ b/cpp/src/arrow/compute/kernels/scalar_if_else.cc @@ -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); diff --git a/cpp/src/arrow/compute/kernels/scalar_if_else_test.cc b/cpp/src/arrow/compute/kernels/scalar_if_else_test.cc index ac3a8bd4706d..0591b9591bf5 100644 --- a/cpp/src/arrow/compute/kernels/scalar_if_else_test.cc +++ b/cpp/src/arrow/compute/kernels/scalar_if_else_test.cc @@ -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]"); From 649487a807cccbc50880042410cd940b3a45d675 Mon Sep 17 00:00:00 2001 From: Chetan Sahney Date: Sat, 5 Dec 2026 01:31:23 +0530 Subject: [PATCH 2/2] GH-55960: [Python] Test if_else with a non-boolean condition and dictionary values --- python/pyarrow/tests/test_compute.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/python/pyarrow/tests/test_compute.py b/python/pyarrow/tests/test_compute.py index 0db8fd2c171f..aeadd841c2cc 100644 --- a/python/pyarrow/tests/test_compute.py +++ b/python/pyarrow/tests/test_compute.py @@ -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())