Skip to content

Commit dbc1e90

Browse files
felipecrvzagto
andauthored
GH-32105: [C++] Encode and decode Run-End Encoded vectors (#34195)
* Closes #13330 * Closes: #32105 Lead-authored-by: Felipe Oliveira Carvalho <[email protected]> Co-authored-by: Tobias Zagorni <[email protected]> Signed-off-by: Matt Topol <[email protected]>
1 parent afa362e commit dbc1e90

8 files changed

Lines changed: 1028 additions & 0 deletions

File tree

‎cpp/src/arrow/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ if(ARROW_COMPUTE)
479479
compute/kernels/vector_nested.cc
480480
compute/kernels/vector_rank.cc
481481
compute/kernels/vector_replace.cc
482+
compute/kernels/vector_run_end_encode.cc
482483
compute/kernels/vector_select_k.cc
483484
compute/kernels/vector_sort.cc)
484485

‎cpp/src/arrow/compute/api_vector.cc‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ static auto kRankOptionsType = GetFunctionOptionsType<RankOptions>(
148148
DataMember("sort_keys", &RankOptions::sort_keys),
149149
DataMember("null_placement", &RankOptions::null_placement),
150150
DataMember("tiebreaker", &RankOptions::tiebreaker));
151+
static auto kRunEndEncodeOptionsType = GetFunctionOptionsType<RunEndEncodeOptions>(
152+
DataMember("run_end_type", &RunEndEncodeOptions::run_end_type));
151153
} // namespace
152154
} // namespace internal
153155

@@ -209,6 +211,10 @@ RankOptions::RankOptions(std::vector<SortKey> sort_keys, NullPlacement null_plac
209211
tiebreaker(tiebreaker) {}
210212
constexpr char RankOptions::kTypeName[];
211213

214+
RunEndEncodeOptions::RunEndEncodeOptions(std::shared_ptr<DataType> run_end_type)
215+
: FunctionOptions(internal::kRunEndEncodeOptionsType),
216+
run_end_type{std::move(run_end_type)} {}
217+
212218
namespace internal {
213219
void RegisterVectorOptions(FunctionRegistry* registry) {
214220
DCHECK_OK(registry->AddFunctionOptionsType(kFilterOptionsType));
@@ -220,6 +226,7 @@ void RegisterVectorOptions(FunctionRegistry* registry) {
220226
DCHECK_OK(registry->AddFunctionOptionsType(kSelectKOptionsType));
221227
DCHECK_OK(registry->AddFunctionOptionsType(kCumulativeSumOptionsType));
222228
DCHECK_OK(registry->AddFunctionOptionsType(kRankOptionsType));
229+
DCHECK_OK(registry->AddFunctionOptionsType(kRunEndEncodeOptionsType));
223230
}
224231
} // namespace internal
225232

@@ -310,6 +317,15 @@ Result<Datum> DictionaryEncode(const Datum& value, const DictionaryEncodeOptions
310317
return CallFunction("dictionary_encode", {value}, &options, ctx);
311318
}
312319

320+
Result<Datum> RunEndEncode(const Datum& value, const RunEndEncodeOptions& options,
321+
ExecContext* ctx) {
322+
return CallFunction("run_end_encode", {value}, &options, ctx);
323+
}
324+
325+
Result<Datum> RunEndDecode(const Datum& value, ExecContext* ctx) {
326+
return CallFunction("run_end_decode", {value}, ctx);
327+
}
328+
313329
const char kValuesFieldName[] = "values";
314330
const char kCountsFieldName[] = "counts";
315331
const int32_t kValuesFieldIndex = 0;

‎cpp/src/arrow/compute/api_vector.h‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ class ARROW_EXPORT DictionaryEncodeOptions : public FunctionOptions {
8080
NullEncodingBehavior null_encoding_behavior = MASK;
8181
};
8282

83+
/// \brief Options for the run-end encode function
84+
class ARROW_EXPORT RunEndEncodeOptions : public FunctionOptions {
85+
public:
86+
explicit RunEndEncodeOptions(std::shared_ptr<DataType> run_end_type = int32());
87+
static constexpr char const kTypeName[] = "RunEndEncodeOptions";
88+
89+
std::shared_ptr<DataType> run_end_type;
90+
};
91+
8392
class ARROW_EXPORT ArraySortOptions : public FunctionOptions {
8493
public:
8594
explicit ArraySortOptions(SortOrder order = SortOrder::Ascending,
@@ -547,6 +556,36 @@ Result<Datum> DictionaryEncode(
547556
const DictionaryEncodeOptions& options = DictionaryEncodeOptions::Defaults(),
548557
ExecContext* ctx = NULLPTR);
549558

559+
/// \brief Run-end-encode values in an array-like object
560+
///
561+
/// The returned run-end encoded type uses the same value type of the input and
562+
/// run-end type defined in the options.
563+
///
564+
/// \param[in] value array-like input
565+
/// \param[in] options configures encoding behavior
566+
/// \param[in] ctx the function execution context, optional
567+
/// \return result with same shape but run-end encoded
568+
///
569+
/// \since 12.0.0
570+
/// \note API not yet finalized
571+
ARROW_EXPORT
572+
Result<Datum> RunEndEncode(const Datum& value, const RunEndEncodeOptions& options,
573+
ExecContext* ctx = NULLPTR);
574+
575+
/// \brief Decode a Run-End Encoded array to a plain array
576+
///
577+
/// The output data type is the same as the values array type of run-end encoded
578+
/// input.
579+
///
580+
/// \param[in] value run-end-encoded input
581+
/// \param[in] ctx the function execution context, optional
582+
/// \return plain array resulting from decoding the run-end encoded input
583+
///
584+
/// \since 12.0.0
585+
/// \note API not yet finalized
586+
ARROW_EXPORT
587+
Result<Datum> RunEndDecode(const Datum& value, ExecContext* ctx = NULLPTR);
588+
550589
ARROW_EXPORT
551590
Result<Datum> CumulativeSum(
552591
const Datum& values,

‎cpp/src/arrow/compute/kernels/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ add_arrow_compute_test(vector_test
7979
vector_replace_test.cc
8080
vector_selection_test.cc
8181
vector_sort_test.cc
82+
vector_run_end_encode_test.cc
8283
select_k_test.cc
8384
test_util.cc)
8485

0 commit comments

Comments
 (0)