From 8b5209699c62634c47eebf87a943dc6e3f54006c Mon Sep 17 00:00:00 2001 From: Chetan Sahney Date: Wed, 25 Nov 2026 15:19:24 +0530 Subject: [PATCH 1/2] GH-55780: [C++] Raise instead of crashing in array_filter and array_take with mixed scalar and array arguments --- .../kernels/vector_selection_internal.cc | 43 ++++++++++++++++++- .../compute/kernels/vector_selection_test.cc | 43 +++++++++++++++++++ 2 files changed, 84 insertions(+), 2 deletions(-) diff --git a/cpp/src/arrow/compute/kernels/vector_selection_internal.cc b/cpp/src/arrow/compute/kernels/vector_selection_internal.cc index ab58d9350547..5837a2c7b72a 100644 --- a/cpp/src/arrow/compute/kernels/vector_selection_internal.cc +++ b/cpp/src/arrow/compute/kernels/vector_selection_internal.cc @@ -48,13 +48,52 @@ using internal::CheckIndexBounds; namespace compute::internal { +namespace { + +// The selection kernels read both arguments as arrays, but scalar arguments are +// only promoted to arrays when all arguments are scalars. Reject a mix of +// scalars and arrays before a kernel is called. +class SelectionFunction : public VectorFunction { + public: + using VectorFunction::VectorFunction; + + Result Execute(const std::vector& args, const FunctionOptions* options, + ExecContext* ctx) const override { + RETURN_NOT_OK(CheckArgumentKinds(args)); + return VectorFunction::Execute(args, options, ctx); + } + + Result Execute(const ExecBatch& batch, const FunctionOptions* options, + ExecContext* ctx) const override { + RETURN_NOT_OK(CheckArgumentKinds(batch.values)); + return VectorFunction::Execute(batch, options, ctx); + } + + private: + Status CheckArgumentKinds(const std::vector& args) const { + bool any_scalar = false; + bool all_scalar = true; + for (const Datum& arg : args) { + any_scalar |= arg.is_scalar(); + all_scalar &= arg.is_scalar(); + } + if (any_scalar && !all_scalar) { + return Status::TypeError("Function '", name(), + "' cannot mix scalar and array arguments"); + } + return Status::OK(); + } +}; + +} // namespace + void RegisterSelectionFunction(const std::string& name, FunctionDoc doc, VectorKernel base_kernel, std::vector&& kernels, const FunctionOptions* default_options, FunctionRegistry* registry) { - auto func = std::make_shared(name, Arity::Binary(), std::move(doc), - default_options); + auto func = std::make_shared(name, Arity::Binary(), std::move(doc), + default_options); for (auto&& kernel_data : kernels) { base_kernel.signature = KernelSignature::Make( {std::move(kernel_data.value_type), std::move(kernel_data.selection_type)}, diff --git a/cpp/src/arrow/compute/kernels/vector_selection_test.cc b/cpp/src/arrow/compute/kernels/vector_selection_test.cc index 5e38ac15306f..503d16568c71 100644 --- a/cpp/src/arrow/compute/kernels/vector_selection_test.cc +++ b/cpp/src/arrow/compute/kernels/vector_selection_test.cc @@ -634,6 +634,29 @@ TEST_F(TestFilterKernel, NoValidityBitmapButUnknownNullCount) { AssertArraysEqual(*expected, *result); } +// GH-55780 +TEST_F(TestFilterKernel, MixedScalarAndArrayArguments) { + const auto message = ::testing::HasSubstr( + "Function 'array_filter' cannot mix scalar and array arguments"); + auto scalar_values = ScalarFromJSON(int32(), "1"); + for (const std::string func_name : {"filter", "array_filter"}) { + ARROW_SCOPED_TRACE(func_name); + EXPECT_RAISES_WITH_MESSAGE_THAT( + TypeError, message, + CallFunction(func_name, {scalar_values, ArrayFromJSON(boolean(), "[true]")})); + } + EXPECT_RAISES_WITH_MESSAGE_THAT( + TypeError, message, + CallFunction("array_filter", + {ArrayFromJSON(int32(), "[1]"), ScalarFromJSON(boolean(), "true")})); + + // With all arguments scalar, the values are promoted to an array of length 1 + ASSERT_OK_AND_ASSIGN( + Datum result, + CallFunction("array_filter", {scalar_values, ScalarFromJSON(boolean(), "true")})); + AssertDatumsEqual(ArrayFromJSON(int32(), "[1]"), result); +} + template class TestFilterKernelWithString : public TestFilterKernel { protected: @@ -1566,6 +1589,26 @@ TEST_F(TestTakeKernel, InvalidIndexType) { .Value(&chunked_arr)); } +// GH-55780 +TEST_F(TestTakeKernel, MixedScalarAndArrayArguments) { + const auto message = + ::testing::HasSubstr("Function 'array_take' cannot mix scalar and array arguments"); + EXPECT_RAISES_WITH_MESSAGE_THAT( + TypeError, message, + CallFunction("array_take", + {ArrayFromJSON(int32(), "[1]"), ScalarFromJSON(int32(), "0")})); + EXPECT_RAISES_WITH_MESSAGE_THAT( + TypeError, message, + CallFunction("array_take", + {ScalarFromJSON(int32(), "1"), ArrayFromJSON(int32(), "[0]")})); + + // With all arguments scalar, the values are promoted to an array of length 1 + ASSERT_OK_AND_ASSIGN(Datum result, + CallFunction("array_take", {ScalarFromJSON(int32(), "1"), + ScalarFromJSON(int32(), "0")})); + AssertDatumsEqual(ArrayFromJSON(int32(), "[1]"), result); +} + TEST_F(TestTakeKernel, TakeXCCEmptyIndices) { auto expected = std::vector{"[]"}; auto values = ArrayFromJSON(int8(), {"[1, 3, 3, 7]"}); From bd9a129b7044301270d1f77bd70b1f83a1bc469d Mon Sep 17 00:00:00 2001 From: Chetan Sahney Date: Thu, 26 Nov 2026 13:20:19 +0530 Subject: [PATCH 2/2] GH-55780: [Python] Test array_filter and array_take with mixed scalar and array arguments --- python/pyarrow/tests/test_compute.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/python/pyarrow/tests/test_compute.py b/python/pyarrow/tests/test_compute.py index 0e0260808d6d..54a87d7ee60a 100644 --- a/python/pyarrow/tests/test_compute.py +++ b/python/pyarrow/tests/test_compute.py @@ -1559,6 +1559,19 @@ def test_take_null_type(): assert len(table.take(indices).column(0)) == 4 +def test_array_take_mixed_scalar_and_array_arguments(): + # GH-55780: mixing scalar and array arguments crashed + message = "Function 'array_take' cannot mix scalar and array arguments" + with pytest.raises(TypeError, match=message): + pc.array_take(pa.array([1]), pa.scalar(0)) + with pytest.raises(TypeError, match=message): + pc.array_take(pa.scalar(1), pa.array([0])) + + # With all arguments scalar, the values are promoted to an array of length 1 + result = pc.array_take(pa.scalar(1), pa.scalar(0)) + assert result.equals(pa.array([1])) + + @pytest.mark.parametrize(('ty', 'values'), all_array_types) def test_drop_null(ty, values): arr = pa.array(values, type=ty) @@ -1751,6 +1764,21 @@ def test_filter_errors(): table.filter(filt) +def test_array_filter_mixed_scalar_and_array_arguments(): + # GH-55780: mixing scalar and array arguments crashed + message = "Function 'array_filter' cannot mix scalar and array arguments" + with pytest.raises(TypeError, match=message): + pc.array_filter(pa.scalar(1), pa.array([True])) + with pytest.raises(TypeError, match=message): + pc.filter(pa.scalar(1), pa.array([True])) + with pytest.raises(TypeError, match=message): + pc.array_filter(pa.array([1]), pa.scalar(True)) + + # With all arguments scalar, the values are promoted to an array of length 1 + result = pc.array_filter(pa.scalar(1), pa.scalar(True)) + assert result.equals(pa.array([1])) + + def test_filter_null_type(): # ARROW-10027 arr = pa.array([None] * 10)