Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 4.3k
ARROW-1565: [C++] Implement TopK/BottomK #11019
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
3be07c35af55121084be3ed28db26134952c8919211b9c824af1aa59e8f17c70891d296212a80a7ac55a2ccd37e3160109a998e0a64f9d887bbcc8725a15ae83aafbbFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -111,6 +111,9 @@ static auto kSortOptionsType = | ||
| GetFunctionOptionsType<SortOptions>(DataMember("sort_keys", &SortOptions::sort_keys)); | ||
| static auto kPartitionNthOptionsType = GetFunctionOptionsType<PartitionNthOptions>( | ||
| DataMember("pivot", &PartitionNthOptions::pivot)); | ||
| static auto kSelectKOptionsType = GetFunctionOptionsType<SelectKOptions>( | ||
| DataMember("k", &SelectKOptions::k), | ||
| DataMember("sort_keys", &SelectKOptions::sort_keys)); | ||
| } // namespace | ||
| } // namespace internal | ||
| @@ -140,6 +143,29 @@ PartitionNthOptions::PartitionNthOptions(int64_t pivot) | ||
| : FunctionOptions(internal::kPartitionNthOptionsType), pivot(pivot) {} | ||
| constexpr char PartitionNthOptions::kTypeName[]; | ||
| SelectKOptions::SelectKOptions(int64_t k, std::vector<SortKey> sort_keys) | ||
| : FunctionOptions(internal::kSelectKOptionsType), | ||
| k(k), | ||
| sort_keys(std::move(sort_keys)) {} | ||
| bool SelectKOptions::is_top_k() const { | ||
| for (const auto& k : sort_keys) { | ||
| if (k.order != SortOrder::Descending) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
| bool SelectKOptions::is_bottom_k() const { | ||
| for (const auto& k : sort_keys) { | ||
| if (k.order != SortOrder::Ascending) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
| constexpr char SelectKOptions::kTypeName[]; | ||
aocsa marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| namespace internal { | ||
| void RegisterVectorOptions(FunctionRegistry* registry) { | ||
| DCHECK_OK(registry->AddFunctionOptionsType(kFilterOptionsType)); | ||
| @@ -148,6 +174,7 @@ void RegisterVectorOptions(FunctionRegistry* registry) { | ||
| DCHECK_OK(registry->AddFunctionOptionsType(kArraySortOptionsType)); | ||
| DCHECK_OK(registry->AddFunctionOptionsType(kSortOptionsType)); | ||
| DCHECK_OK(registry->AddFunctionOptionsType(kPartitionNthOptionsType)); | ||
| DCHECK_OK(registry->AddFunctionOptionsType(kSelectKOptionsType)); | ||
| } | ||
| } // namespace internal | ||
| @@ -162,6 +189,13 @@ Result<std::shared_ptr<Array>> NthToIndices(const Array& values, int64_t n, | ||
| return result.make_array(); | ||
| } | ||
| Result<std::shared_ptr<Array>> SelectKUnstable(const Datum& datum, SelectKOptions options, | ||
aocsa marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| ExecContext* ctx) { | ||
| ARROW_ASSIGN_OR_RAISE(Datum result, | ||
| CallFunction("select_k_unstable", {datum}, &options, ctx)); | ||
| return result.make_array(); | ||
| } | ||
| Result<Datum> ReplaceWithMask(const Datum& values, const Datum& mask, | ||
| const Datum& replacements, ExecContext* ctx) { | ||
| return CallFunction("replace_with_mask", {values, mask, replacements}, ctx); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -120,6 +120,46 @@ class ARROW_EXPORT SortOptions : public FunctionOptions { | ||
| std::vector<SortKey> sort_keys; | ||
| }; | ||
| /// \brief SelectK options | ||
| class ARROW_EXPORT SelectKOptions : public FunctionOptions { | ||
| public: | ||
| explicit SelectKOptions(int64_t k = -1, std::vector<SortKey> sort_keys = {}); | ||
| constexpr static char const kTypeName[] = "SelectKOptions"; | ||
| static SelectKOptions Defaults() { return SelectKOptions{-1, {}}; } | ||
| static SelectKOptions TopKDefault(int64_t k, std::vector<std::string> key_names = {}) { | ||
| std::vector<SortKey> keys; | ||
| for (const auto& name : key_names) { | ||
| keys.emplace_back(SortKey(name, SortOrder::Descending)); | ||
aocsa marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| if (key_names.empty()) { | ||
| keys.emplace_back(SortKey("not-used", SortOrder::Descending)); | ||
| } | ||
| return SelectKOptions{k, keys}; | ||
| } | ||
| static SelectKOptions BottomKDefault(int64_t k, | ||
| std::vector<std::string> key_names = {}) { | ||
| std::vector<SortKey> keys; | ||
| for (const auto& name : key_names) { | ||
| keys.emplace_back(SortKey(name, SortOrder::Ascending)); | ||
| } | ||
| if (key_names.empty()) { | ||
| keys.emplace_back(SortKey("not-used", SortOrder::Ascending)); | ||
| } | ||
| return SelectKOptions{k, keys}; | ||
| } | ||
| bool is_top_k() const; | ||
| bool is_bottom_k() const; | ||
| /// The number of `k` elements to keep. | ||
| int64_t k; | ||
| /// Column key(s) to order by and how to order by these sort keys. | ||
| std::vector<SortKey> sort_keys; | ||
| }; | ||
| /// \brief Partitioning options for NthToIndices | ||
| class ARROW_EXPORT PartitionNthOptions : public FunctionOptions { | ||
| public: | ||
| @@ -252,6 +292,21 @@ ARROW_EXPORT | ||
| Result<std::shared_ptr<Array>> NthToIndices(const Array& values, int64_t n, | ||
| ExecContext* ctx = NULLPTR); | ||
| /// \brief Returns the first k elements ordered by `options.keys`. | ||
| /// | ||
| /// Return a sorted array with its elements rearranged in such | ||
| /// a way that the value of the element in k-th position (options.k) is in the position it | ||
| /// would be in a sorted datum ordered by `options.keys`. Null like values will be not | ||
| /// part of the output. Output is not guaranteed to be stable. | ||
| /// | ||
| /// \param[in] datum datum to be partitioned | ||
| /// \param[in] options options | ||
| /// \param[in] ctx the function execution context, optional | ||
| /// \return a datum with the same schema as the input | ||
| ARROW_EXPORT | ||
aocsa marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| Result<std::shared_ptr<Array>> SelectKUnstable(const Datum& datum, SelectKOptions options, | ||
| ExecContext* ctx = NULLPTR); | ||
aocsa marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| /// \brief Returns the indices that would sort an array in the | ||
| /// specified order. | ||
| /// | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.