This is an automated email from the ASF dual-hosted git repository.
zanmato1984 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow.git
The following commit(s) were added to refs/heads/main by this push:
new 05671f6e7b4 GH-55404: [C++] Fix coalesce exact dispatch for mixed
dictionary types (#55405)
05671f6e7b4 is described below
commit 05671f6e7b41a52e62b724529e26c5ef4e398180
Author: Rossi Sun <[email protected]>
AuthorDate: Tue Nov 10 09:44:07 2026 -0800
GH-55404: [C++] Fix coalesce exact dispatch for mixed dictionary types
(#55405)
### Rationale for this change
The `coalesce` dictionary kernel accepts arguments with different index
types, value types, or ordered flags during exact dispatch. Expression binding
therefore skips dictionary decoding and type promotion, and the bound
expression fails with a `TypeError` at execution.
### What changes are included in this PR?
Require full dictionary type equality using `AllTypesAreIdentical()`. Mixed
dictionary arguments follow the existing best-dispatch and cast-insertion path,
while identical dictionary types retain dictionary-encoded execution.
### Are these changes tested?
Added exact/best-dispatch and expression binding/execution tests covering
differences in index types, value types, and ordering, both argument orders,
and variadic calls. Same-type inputs with different dictionary contents verify
that dictionary-encoded execution is preserved.
### Are there any user-facing changes?
Bound `coalesce` expressions with compatible mixed dictionary inputs now
execute successfully, returning decoded values promoted to a common type where
needed.
### Was AI used for this PR?
AI generated the implementation, tests, and PR description. The author
conducted a thorough review with AI assistance.
**PR code and description written by:**
- [ ] Human
- [x] AI
**Reviewed before submission by:**
- [x] Human
- [x] AI
- [ ] Not reviewed
* GitHub Issue: #55404
Authored-by: Rossi Sun <[email protected]>
Signed-off-by: Rossi Sun <[email protected]>
---
cpp/src/arrow/compute/expression_test.cc | 52 ++++++++++++++++++++++
cpp/src/arrow/compute/kernels/scalar_if_else.cc | 3 +-
.../arrow/compute/kernels/scalar_if_else_test.cc | 28 ++++++++++++
3 files changed, 82 insertions(+), 1 deletion(-)
diff --git a/cpp/src/arrow/compute/expression_test.cc
b/cpp/src/arrow/compute/expression_test.cc
index 10c1b348e62..e123411a3c7 100644
--- a/cpp/src/arrow/compute/expression_test.cc
+++ b/cpp/src/arrow/compute/expression_test.cc
@@ -1063,6 +1063,58 @@ TEST(Expression, ExecuteCoalesceOnMixedDecimalTypes) {
AssertDatumsEqual(actual, ArrayFromJSON(decimal128(4, 3), R"(["1.230",
"2.345"])"));
}
+TEST(Expression, CoalesceDictionaryTypes) {
+ auto check = [](const ArrayVector& arguments,
+ const std::shared_ptr<DataType>& output_type,
+ const std::shared_ptr<Array>& expected) {
+ FieldVector fields;
+ std::vector<Expression> args, expected_args;
+ for (size_t i = 0; i < arguments.size(); ++i) {
+ auto name = "arg" + std::to_string(i);
+ fields.push_back(field(name, arguments[i]->type()));
+ args.push_back(field_ref(name));
+ expected_args.push_back(arguments[i]->type()->Equals(output_type)
+ ? args.back()
+ : cast(args.back(), output_type));
+ }
+ Schema input_schema(fields);
+ Expression bound;
+ ExpectBindsTo(call("coalesce", args), call("coalesce", expected_args),
&bound,
+ input_schema);
+ ASSERT_NE(bound.type(), nullptr);
+ EXPECT_TRUE(bound.type()->Equals(output_type));
+ ASSERT_OK_AND_ASSIGN(auto input, StructArray::Make(arguments, fields));
+ ASSERT_OK_AND_ASSIGN(auto actual,
+ ExecuteScalarExpression(bound, input_schema,
Datum(input)));
+ ASSERT_OK(actual.make_array()->ValidateFull());
+ // Also check logical values when the result stays dictionary-encoded.
+ ASSERT_OK_AND_ASSIGN(actual, Cast(actual, expected->type()));
+ AssertDatumsEqual(actual, expected);
+ };
+
+ auto type = dictionary(int8(), int32());
+ auto left = DictArrayFromJSON(type, "[0, null, 1, null, null]", "[10, 30]");
+ auto right = DictArrayFromJSON(type, "[null, 0, 1, null, null]", "[20, 40]");
+ auto third = DictArrayFromJSON(type, "[null, null, null, 0, null]", "[50]");
+ auto expected = ArrayFromJSON(int32(), "[10, 20, 30, null, null]");
+ check({left}, type, ArrayFromJSON(int32(), "[10, null, 30, null, null]"));
+ check({left, right}, type, expected);
+ check({left, right, third}, type, ArrayFromJSON(int32(), "[10, 20, 30, 50,
null]"));
+
+ for (const auto& other : {dictionary(int16(), int32()), dictionary(int8(),
int64()),
+ dictionary(int8(), int32(), /*ordered=*/true)}) {
+ auto output_type = checked_cast<const
DictionaryType&>(*other).value_type();
+ auto mixed_right = DictArrayFromJSON(other, "[null, 0, 1, null, null]",
"[20, 40]");
+ check({left, mixed_right}, output_type,
+ ArrayFromJSON(output_type, "[10, 20, 30, null, null]"));
+ check({mixed_right, left}, output_type,
+ ArrayFromJSON(output_type, "[10, 20, 40, null, null]"));
+ auto mixed_third = DictArrayFromJSON(other, "[null, null, null, 0, null]",
"[50]");
+ check({left, right, mixed_third}, output_type,
+ ArrayFromJSON(output_type, "[10, 20, 30, 50, null]"));
+ }
+}
+
TEST(Expression, BindNestedCall) {
auto expr = add(field_ref("a"),
call("subtract", {call("multiply", {field_ref("b"),
field_ref("c")}),
diff --git a/cpp/src/arrow/compute/kernels/scalar_if_else.cc
b/cpp/src/arrow/compute/kernels/scalar_if_else.cc
index 666c0ae93ab..331439e0092 100644
--- a/cpp/src/arrow/compute/kernels/scalar_if_else.cc
+++ b/cpp/src/arrow/compute/kernels/scalar_if_else.cc
@@ -2816,7 +2816,8 @@ void AddNestedCoalesceKernels(const
std::shared_ptr<ScalarFunction>& scalar_func
AddNestedCoalesceKernel<StructType>(scalar_function);
AddNestedCoalesceKernel<DenseUnionType>(scalar_function);
AddNestedCoalesceKernel<SparseUnionType>(scalar_function);
- AddNestedCoalesceKernel<DictionaryType>(scalar_function);
+ AddCoalesceKernel(scalar_function, Type::DICTIONARY,
+ CoalesceFunctor<DictionaryType>::Exec,
AllTypesAreIdentical());
}
void AddChooseKernel(const std::shared_ptr<ScalarFunction>& scalar_function,
diff --git a/cpp/src/arrow/compute/kernels/scalar_if_else_test.cc
b/cpp/src/arrow/compute/kernels/scalar_if_else_test.cc
index e05a1f081be..ac3a8bd4706 100644
--- a/cpp/src/arrow/compute/kernels/scalar_if_else_test.cc
+++ b/cpp/src/arrow/compute/kernels/scalar_if_else_test.cc
@@ -3743,6 +3743,34 @@ TEST(TestCoalesce, DispatchExact) {
CheckDispatchExactFails("coalesce", {decimal128(3, 2), decimal256(4, 1)});
}
+TEST(TestCoalesce, DictionaryDispatchExact) {
+ auto type = dictionary(int8(), int32());
+ CheckDispatchExact("coalesce", {type});
+ CheckDispatchExact("coalesce", {type, dictionary(int8(), int32())});
+ CheckDispatchExact("coalesce", {type, type, type});
+ for (const auto& other : {dictionary(int16(), int32()), dictionary(int8(),
int64()),
+ dictionary(int8(), int32(), /*ordered=*/true)}) {
+ CheckDispatchExactFails("coalesce", {type, other});
+ CheckDispatchExactFails("coalesce", {other, type});
+ CheckDispatchExactFails("coalesce", {type, type, other});
+ }
+}
+
+TEST(TestCoalesce, DictionaryDispatchBest) {
+ auto type = dictionary(int8(), int32());
+ CheckDispatchBest("coalesce", {type}, {type});
+ CheckDispatchBest("coalesce", {type, dictionary(int8(), int32())}, {type,
type});
+ CheckDispatchBest("coalesce", {type, type, type}, {type, type, type});
+ for (const auto& other : {dictionary(int16(), int32()), dictionary(int8(),
int64()),
+ dictionary(int8(), int32(), /*ordered=*/true)}) {
+ auto value_type = checked_cast<const DictionaryType&>(*other).value_type();
+ CheckDispatchBest("coalesce", {type, other}, {value_type, value_type});
+ CheckDispatchBest("coalesce", {other, type}, {value_type, value_type});
+ CheckDispatchBest("coalesce", {type, type, other},
+ {value_type, value_type, value_type});
+ }
+}
+
template <typename Type>
class TestChooseNumeric : public ::testing::Test {};
template <typename Type>