-
Notifications
You must be signed in to change notification settings - Fork 968
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
fix(search_family): Support multiple fields in SORTBY option in the FT.AGGREGATE command. SECOND PR #4232
Open
BagritsevichStepan
wants to merge
2
commits into
dragonflydb:main
Choose a base branch
from
BagritsevichStepan:df.search/ft-aggregate-support-multiple-fields-in-sortby-options
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
fix(search_family): Support multiple fields in SORTBY option in the FT.AGGREGATE command. SECOND PR #4232
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
#pragma once | ||
|
||
#include <absl/container/flat_hash_map.h> | ||
#include <absl/container/flat_hash_set.h> | ||
#include <absl/types/span.h> | ||
|
||
#include <string> | ||
|
@@ -19,10 +20,16 @@ namespace dfly::aggregate { | |
using Value = ::dfly::search::SortableValue; | ||
using DocValues = absl::flat_hash_map<std::string, Value>; // documents sent through the pipeline | ||
|
||
// TODO: Replace DocValues with compact linear search map instead of hash map | ||
struct PipelineResult { | ||
// Values to be passed to the next step | ||
// TODO: Replace DocValues with compact linear search map instead of hash map | ||
std::vector<DocValues> values; | ||
|
||
using PipelineResult = io::Result<std::vector<DocValues>, facade::ErrorReply>; | ||
using PipelineStep = std::function<PipelineResult(std::vector<DocValues>)>; // Group, Sort, etc. | ||
// Fields from values to be printed | ||
absl::flat_hash_set<std::string> fields_to_print; | ||
}; | ||
|
||
using PipelineStep = std::function<PipelineResult(PipelineResult)>; // Group, Sort, etc. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider modifying current values instead of doing move |
||
|
||
// Iterator over Span<DocValues> that yields doc[field] or monostate if not present. | ||
// Extra clumsy for STL compatibility! | ||
|
@@ -66,6 +73,25 @@ struct Reducer { | |
Func func; | ||
}; | ||
|
||
struct SortParams { | ||
enum class SortOrder { ASC, DESC }; | ||
|
||
constexpr static int64_t kSortAll = -1; | ||
|
||
bool SortAll() const { | ||
return max == kSortAll; | ||
} | ||
|
||
/* Fields to sort by. If multiple fields are provided, sorting works hierarchically: | ||
- First, the i-th field is compared. | ||
- If the i-th field values are equal, the (i + 1)-th field is compared, and so on. */ | ||
absl::InlinedVector<std::pair<std::string, SortOrder>, 1> fields; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest using a bigger default capacity |
||
|
||
/* Max number of elements to include in the sorted result. | ||
If set, only the first [max] elements are fully sorted using partial_sort. */ | ||
int64_t max = kSortAll; | ||
}; | ||
|
||
enum class ReducerFunc { COUNT, COUNT_DISTINCT, SUM, AVG, MAX, MIN }; | ||
|
||
// Find reducer function by uppercase name (COUNT, MAX, etc...), empty functor if not found | ||
|
@@ -76,12 +102,14 @@ PipelineStep MakeGroupStep(absl::Span<const std::string_view> fields, | |
std::vector<Reducer> reducers); | ||
|
||
// Make `SORTBY field [DESC]` step | ||
PipelineStep MakeSortStep(std::string_view field, bool descending = false); | ||
PipelineStep MakeSortStep(SortParams sort_params); | ||
|
||
// Make `LIMIT offset num` step | ||
PipelineStep MakeLimitStep(size_t offset, size_t num); | ||
|
||
// Process values with given steps | ||
PipelineResult Process(std::vector<DocValues> values, absl::Span<const PipelineStep> steps); | ||
PipelineResult Process(std::vector<DocValues> values, | ||
absl::Span<const std::string_view> fields_to_print, | ||
absl::Span<const PipelineStep> steps); | ||
|
||
} // namespace dfly::aggregate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PipelineResult contains more than result.values but you use only this field, maybe you don't need to change the type of parameter