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
5 changes: 5 additions & 0 deletions cpp/src/arrow/compute/kernels/vector_replace.cc
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,11 @@ Status CheckReplaceMaskInputs(const DataType& value_type, int64_t arr_length,
template <typename Type>
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];
Expand Down
30 changes: 30 additions & 0 deletions cpp/src/arrow/compute/kernels/vector_replace_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<TypeParam>::ArrayType;
auto ty = this->type();
Expand Down
17 changes: 17 additions & 0 deletions python/pyarrow/tests/test_compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, ''])
Expand Down