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
43 changes: 41 additions & 2 deletions cpp/src/arrow/compute/kernels/vector_selection_internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<Datum> Execute(const std::vector<Datum>& args, const FunctionOptions* options,
ExecContext* ctx) const override {
RETURN_NOT_OK(CheckArgumentKinds(args));
return VectorFunction::Execute(args, options, ctx);
}

Result<Datum> 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<Datum>& 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<SelectionKernelData>&& kernels,
const FunctionOptions* default_options,
FunctionRegistry* registry) {
auto func = std::make_shared<VectorFunction>(name, Arity::Binary(), std::move(doc),
default_options);
auto func = std::make_shared<SelectionFunction>(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)},
Expand Down
43 changes: 43 additions & 0 deletions cpp/src/arrow/compute/kernels/vector_selection_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename TypeClass>
class TestFilterKernelWithString : public TestFilterKernel {
protected:
Expand Down Expand Up @@ -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<std::string>{"[]"};
auto values = ArrayFromJSON(int8(), {"[1, 3, 3, 7]"});
Expand Down
28 changes: 28 additions & 0 deletions python/pyarrow/tests/test_compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down