From 0fe335d657ce04aef0c9bd9c7f84219a36b2c649 Mon Sep 17 00:00:00 2001 From: Soham Dutta Date: Sat, 14 Nov 2026 19:55:15 +0000 Subject: [PATCH] [C++][Compute] Support decimal and fixed_size_binary types in search_sorted search_sorted raised NotImplemented for decimal32/64/128/256 and fixed_size_binary inputs, although these types are orderable and already supported by sort_indices. Add them to the type dispatch. The search itself needs no changes since GetViewType and CompareTypeValues already handle them; only the two type_singleton() calls, which don't exist for parametric types, are adjusted. --- .../compute/kernels/vector_search_sorted.cc | 19 ++++++++++++++----- .../kernels/vector_search_sorted_test.cc | 19 +++++++++++++++++++ 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/cpp/src/arrow/compute/kernels/vector_search_sorted.cc b/cpp/src/arrow/compute/kernels/vector_search_sorted.cc index aa4fe40011d5..069e9b60072f 100644 --- a/cpp/src/arrow/compute/kernels/vector_search_sorted.cc +++ b/cpp/src/arrow/compute/kernels/vector_search_sorted.cc @@ -86,7 +86,12 @@ const FunctionDoc search_sorted_doc( VISIT(DoubleType) \ VISIT(BinaryType) \ VISIT(LargeBinaryType) \ - VISIT(BinaryViewType) + VISIT(BinaryViewType) \ + VISIT(FixedSizeBinaryType) \ + VISIT(Decimal32Type) \ + VISIT(Decimal64Type) \ + VISIT(Decimal128Type) \ + VISIT(Decimal256Type) template using SearchValue = typename GetViewType::T; @@ -568,8 +573,13 @@ Status VisitArrayNeedleRuns(const std::shared_ptr& needles_data, Visitor&& visitor) { using ArrayType = typename TypeTraits::ArrayType; - auto physical_type = TypeTraits::type_singleton(); - auto physical_data = ToPhysicalData(needles_data, physical_type); + std::shared_ptr physical_data; + if constexpr (TypeTraits::is_parameter_free) { + physical_data = ToPhysicalData(needles_data, TypeTraits::type_singleton()); + } else { + // Parametric types (decimals, fixed_size_binary) are their own physical type + physical_data = needles_data; + } ArrayType array(physical_data); for (int64_t index = 0; index < array.length(); ++index) { RETURN_NOT_OK(visitor(ReadVisitedNeedle(array, index))); @@ -706,8 +716,7 @@ Result ComputeInsertionIndices(const Datum& sorted_values, const Datum& n SearchSortedOptions::Side side, NullPlacement null_placement, uint64_t insertion_offset, ExecContext* ctx) { - auto physical_type = TypeTraits::type_singleton(); - DCHECK_NE(physical_type->id(), Type::RUN_END_ENCODED); + static_assert(ArrowType::type_id != Type::RUN_END_ENCODED); if (sorted_values.type()->id() == Type::RUN_END_ENCODED) { return ComputeInsertionIndicesWithAccessor SupportedTypeSmokeCases() { R"(["a", "c", "bb", "z"])", "[0, 3, 1, 5]", "[0, 3, 3, 5]"}, {"StringView", utf8_view(), R"(["aa", "bb", "bb", "dd", "ff"])", R"(["a", "c", "bb", "z"])", "[0, 3, 1, 5]", "[0, 3, 3, 5]"}, + {"FixedSizeBinary", fixed_size_binary(2), R"(["aa", "bb", "bb", "dd", "ff"])", + R"(["ab", "cc", "bb", "zz"])", "[1, 3, 1, 5]", "[1, 3, 3, 5]"}, + {"Decimal32", decimal32(5, 2), R"(["-12.50", "-1.00", "-1.00", "0.00", "3.25"])", + R"(["-20.00", "-1.00", "1.00", "5.00"])", "[0, 1, 4, 5]", "[0, 3, 4, 5]"}, + {"Decimal64", decimal64(10, 2), R"(["-12.50", "-1.00", "-1.00", "0.00", "3.25"])", + R"(["-20.00", "-1.00", "1.00", "5.00"])", "[0, 1, 4, 5]", "[0, 3, 4, 5]"}, + {"Decimal128", decimal128(20, 2), R"(["-12.50", "-1.00", "-1.00", "0.00", "3.25"])", + R"(["-20.00", "-1.00", "1.00", "5.00"])", "[0, 1, 4, 5]", "[0, 3, 4, 5]"}, + {"Decimal256", decimal256(40, 2), R"(["-12.50", "-1.00", "-1.00", "0.00", "3.25"])", + R"(["-20.00", "-1.00", "1.00", "5.00"])", "[0, 1, 4, 5]", "[0, 3, 4, 5]"}, }; } @@ -540,6 +550,15 @@ TEST(SearchSorted, RunEndEncodedDate32Values) { CheckSearchSorted(Datum(values), Datum(needles), "[2]", "[2]"); } +TEST(SearchSorted, RunEndEncodedDecimalValues) { + auto values_type = run_end_encoded(int16(), decimal128(10, 2)); + ASSERT_OK_AND_ASSIGN(auto values, + REEFromJSON(values_type, R"([null, "-1.00", "-1.00", "2.50"])")); + auto needles = ArrayFromJSON(decimal128(10, 2), R"(["-1.00", "0.00", null])"); + + CheckSearchSorted(Datum(values), Datum(needles), "[1, 3, null]", "[3, 3, null]"); +} + TEST(SearchSorted, RunEndEncodedNulls) { auto values_type = run_end_encoded(int16(), int32()); ASSERT_OK_AND_ASSIGN(auto ree_values,