Skip to content
Draft
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
14 changes: 6 additions & 8 deletions cpp/src/arrow/compute/kernels/scalar_if_else.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1494,13 +1494,10 @@ struct CaseWhenFunction : ScalarFunction {
return arrow::compute::detail::NoMatchingKernel(this, *types);
}

static std::shared_ptr<MatchConstraint> DecimalMatchConstraint() {
static std::shared_ptr<MatchConstraint> ValueTypesMatchConstraint() {
static auto constraint =
MatchConstraint::Make([](const std::vector<TypeHolder>& types) -> bool {
DCHECK_GE(types.size(), 2);
DCHECK(std::all_of(types.begin() + 1, types.end(), [](const TypeHolder& type) {
return is_decimal(type.id());
}));
return std::all_of(
types.begin() + 2, types.end(),
[&types](const TypeHolder& type) { return type == types[1]; });
Expand Down Expand Up @@ -2762,6 +2759,9 @@ struct ChooseFunction : ScalarFunction {
void AddCaseWhenKernel(const std::shared_ptr<CaseWhenFunction>& scalar_function,
detail::GetTypeId get_id, ArrayKernelExec exec,
std::shared_ptr<MatchConstraint> constraint = nullptr) {
if (constraint == nullptr) {
constraint = CaseWhenFunction::ValueTypesMatchConstraint();
}
ScalarKernel kernel(
KernelSignature::Make({InputType(Type::STRUCT), InputType(get_id.id)}, LastType,
/*is_varargs=*/true, std::move(constraint)),
Expand Down Expand Up @@ -2939,10 +2939,8 @@ void RegisterScalarIfElse(FunctionRegistry* registry) {
AddPrimitiveCaseWhenKernels(func, {boolean(), null(), float16()});
AddCaseWhenKernel(func, Type::FIXED_SIZE_BINARY,
CaseWhenFunctor<FixedSizeBinaryType>::Exec);
AddCaseWhenKernel(func, Type::DECIMAL128, CaseWhenFunctor<FixedSizeBinaryType>::Exec,
CaseWhenFunction::DecimalMatchConstraint());
AddCaseWhenKernel(func, Type::DECIMAL256, CaseWhenFunctor<FixedSizeBinaryType>::Exec,
CaseWhenFunction::DecimalMatchConstraint());
AddCaseWhenKernel(func, Type::DECIMAL128, CaseWhenFunctor<FixedSizeBinaryType>::Exec);
AddCaseWhenKernel(func, Type::DECIMAL256, CaseWhenFunctor<FixedSizeBinaryType>::Exec);
AddBinaryCaseWhenKernels(func, BaseBinaryTypes());
AddNestedCaseWhenKernels(func);
DCHECK_OK(registry->AddFunction(std::move(func)));
Expand Down
39 changes: 39 additions & 0 deletions cpp/src/arrow/compute/kernels/scalar_if_else_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2726,6 +2726,45 @@ TEST(TestCaseWhen, UnionBoolStringRandom) {
}

TEST(TestCaseWhen, DispatchExact) {
// Matching parameterized types should exact-match.
CheckDispatchExact("case_when",
{struct_({field("", boolean())}), fixed_size_binary(4),
fixed_size_binary(4)});
CheckDispatchExact("case_when",
{struct_({field("", boolean())}), list(int32()), list(int32())});
CheckDispatchExact(
"case_when",
{struct_({field("", boolean())}), fixed_size_list(int32(), 2),
fixed_size_list(int32(), 2)});
CheckDispatchExact(
"case_when",
{struct_({field("", boolean())}), dictionary(int8(), utf8()),
dictionary(int8(), utf8())});

// Mismatched parameterized types should not exact-match.
CheckDispatchExactFails("case_when",
{struct_({field("", boolean())}), fixed_size_binary(4),
fixed_size_binary(5)});
CheckDispatchExactFails("case_when",
{struct_({field("", boolean())}), list(int16()),
list(int32())});
CheckDispatchExactFails(
"case_when",
{struct_({field("", boolean())}), fixed_size_list(int32(), 2),
fixed_size_list(int32(), 3)});
CheckDispatchExactFails(
"case_when",
{struct_({field("", boolean())}), struct_({field("a", int32())}),
struct_({field("a", int64())})});
CheckDispatchExactFails(
"case_when",
{struct_({field("", boolean())}), dictionary(int8(), utf8()),
dictionary(int8(), large_utf8())});
CheckDispatchExactFails(
"case_when",
{struct_({field("", boolean())}), dictionary(int8(), utf8()),
dictionary(int16(), utf8())});

// Decimal types with same (p, s)
CheckDispatchExact("case_when", {struct_({field("", boolean())}), decimal128(20, 3),
decimal128(20, 3)});
Expand Down
Loading