diff --git a/cpp/src/arrow/compute/kernels/vector_replace.cc b/cpp/src/arrow/compute/kernels/vector_replace.cc index a27638a7bcd..5d494f6267f 100644 --- a/cpp/src/arrow/compute/kernels/vector_replace.cc +++ b/cpp/src/arrow/compute/kernels/vector_replace.cc @@ -359,6 +359,11 @@ Status CheckReplaceMaskInputs(const DataType& value_type, int64_t arr_length, template struct ReplaceMask { static Status Exec(KernelContext* ctx, const ExecSpan& batch, ExecResult* out) { + // Scalar values are only promoted to an array when all arguments are scalars + if (batch[0].is_scalar()) { + return Status::Invalid( + "Values must be an array when mask or replacements is an array, not a scalar"); + } const ArraySpan& arr = batch[0].array; const ExecValue& mask = batch[1]; const ExecValue& replacements = batch[2]; diff --git a/cpp/src/arrow/compute/kernels/vector_replace_test.cc b/cpp/src/arrow/compute/kernels/vector_replace_test.cc index 65cb9126b15..1ddc94a9523 100644 --- a/cpp/src/arrow/compute/kernels/vector_replace_test.cc +++ b/cpp/src/arrow/compute/kernels/vector_replace_test.cc @@ -478,6 +478,22 @@ TYPED_TEST(TestReplaceNumeric, ReplaceWithMaskErrors) { this->AssertRaises(ReplaceWithMask, this->array("[1, 2]"), ChunkedArrayFromJSON(boolean(), {"[true]", "[false]"}), this->array({"[0]"}))); + + // GH-55761: scalar values with an array mask or replacements + const auto scalar_values_message = ::testing::HasSubstr( + "Values must be an array when mask or replacements is an array, not a scalar"); + EXPECT_RAISES_WITH_MESSAGE_THAT( + Invalid, scalar_values_message, + this->AssertRaises(ReplaceWithMask, this->scalar("1"), this->mask("[true]"), + this->array("[2]"))); + EXPECT_RAISES_WITH_MESSAGE_THAT( + Invalid, scalar_values_message, + this->AssertRaises(ReplaceWithMask, this->scalar("1"), this->mask("[true]"), + this->scalar("2"))); + EXPECT_RAISES_WITH_MESSAGE_THAT( + Invalid, scalar_values_message, + this->AssertRaises(ReplaceWithMask, this->scalar("1"), this->mask_scalar(true), + this->array("[2]"))); } TEST_F(TestReplaceBoolean, ReplaceWithMask) { @@ -1096,6 +1112,20 @@ TYPED_TEST(TestReplaceBinary, ReplaceWithMask) { } } +// GH-55761 +TYPED_TEST(TestReplaceBinary, ReplaceWithMaskScalarValues) { + EXPECT_RAISES_WITH_MESSAGE_THAT( + Invalid, + ::testing::HasSubstr( + "Values must be an array when mask or replacements is an array, not a scalar"), + this->AssertRaises(ReplaceWithMask, this->scalar(R"("foo")"), this->mask("[true]"), + this->array(R"(["bar"])"))); + + // With all arguments scalar, the values are promoted to an array of length 1 + this->Assert(ReplaceWithMask, this->scalar(R"("foo")"), this->mask_scalar(true), + this->scalar(R"("bar")"), this->array(R"(["bar"])")); +} + TYPED_TEST(TestReplaceBinary, ReplaceWithMaskRandom) { using ArrayType = typename TypeTraits::ArrayType; auto ty = this->type(); diff --git a/python/pyarrow/tests/test_compute.py b/python/pyarrow/tests/test_compute.py index 0e0260808d6..ba12c8d87e0 100644 --- a/python/pyarrow/tests/test_compute.py +++ b/python/pyarrow/tests/test_compute.py @@ -1412,6 +1412,23 @@ def test_replace_with_mask_error_mask_length_mismatch(): pc.replace_with_mask(arr, mask, replacements) +def test_replace_with_mask_scalar_values_with_array_arguments(): + # GH-55761: scalar values with an array mask or replacements crashed + message = "Values must be an array" + with pytest.raises(pa.ArrowInvalid, match=message): + pc.replace_with_mask(pa.scalar(1), pa.array([True]), pa.array([2])) + with pytest.raises(pa.ArrowInvalid, match=message): + pc.replace_with_mask(pa.scalar(1), pa.array([True]), pa.scalar(2)) + with pytest.raises(pa.ArrowInvalid, match=message): + pc.replace_with_mask(pa.scalar(1), True, pa.array([2])) + with pytest.raises(pa.ArrowInvalid, match=message): + pc.replace_with_mask(pa.scalar("a"), pa.array([True]), pa.array(["b"])) + + # With all arguments scalar, the values are promoted to an array of length 1 + result = pc.replace_with_mask(pa.scalar(1), True, pa.scalar(2)) + assert result.equals(pa.array([2])) + + def test_binary_join(): ar_list = pa.array([['foo', 'bar'], None, []]) expected = pa.array(['foo-bar', None, ''])