-
Notifications
You must be signed in to change notification settings - Fork 911
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
Manually build ES pagination query for default sorter #4271
Merged
rodrigozhou
merged 1 commit into
temporalio:master
from
rodrigozhou:optimize-pagination-es
May 11, 2023
Merged
Manually build ES pagination query for default sorter #4271
rodrigozhou
merged 1 commit into
temporalio:master
from
rodrigozhou:optimize-pagination-es
May 11, 2023
Conversation
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
rodrigozhou
commented
May 3, 2023
Comment on lines
671
to
676
for k := 0; k < len(defaultSorterFields); k++ { | ||
bq := elastic.NewBoolQuery() | ||
for i := 0; i <= k; i++ { | ||
tp, err := saTypeMap.GetType(defaultSorterFields[i].name) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
isNull := false | ||
switch tp { | ||
case enumspb.INDEXED_VALUE_TYPE_INT, | ||
enumspb.INDEXED_VALUE_TYPE_BOOL, | ||
enumspb.INDEXED_VALUE_TYPE_DATETIME: | ||
isNull = searchAfter[i] == math.MaxInt64 || searchAfter[i] == math.MinInt64 | ||
case enumspb.INDEXED_VALUE_TYPE_DOUBLE: | ||
isNull = searchAfter[i] == "Infinity" || searchAfter[i] == "-Infinity" | ||
case enumspb.INDEXED_VALUE_TYPE_KEYWORD: | ||
isNull = searchAfter[i] == nil | ||
default: | ||
return serviceerror.NewInternal(fmt.Sprintf( | ||
"Invalid field type in default sorter: cannot order by %q", | ||
defaultSorterFields[i].name, | ||
)) | ||
} | ||
|
||
if i == len(defaultSorterFields)-1 && isNull { | ||
return serviceerror.NewInternal(fmt.Sprintf( | ||
"Last field of default sorter cannot be a nullable field: %q has null values", | ||
defaultSorterFields[i].name, | ||
)) | ||
} | ||
|
||
lastValue := searchAfter[i] | ||
if tp == enumspb.INDEXED_VALUE_TYPE_DATETIME && !isNull { | ||
ts, ok := searchAfter[i].(int64) | ||
if !ok { | ||
return fmt.Errorf("Invalid page token: expected unix timestamp, got %v", searchAfter[i]) | ||
} | ||
lastValue = time.Unix(0, ts).Format(time.RFC3339Nano) | ||
} | ||
|
||
if isNull { | ||
bq.MustNot(elastic.NewExistsQuery(defaultSorterFields[i].name)) | ||
} else if i < k { | ||
bq.Filter(elastic.NewTermQuery(defaultSorterFields[i].name, lastValue)) | ||
} else if defaultSorterFields[i].desc { | ||
bq.Filter(elastic.NewRangeQuery(defaultSorterFields[i].name).Lt(lastValue)) | ||
} else { | ||
bq.Filter(elastic.NewRangeQuery(defaultSorterFields[i].name).Gt(lastValue)) | ||
} | ||
} | ||
shouldQueries = append(shouldQueries, bq) | ||
} |
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.
I wrote a generic page token query just in case the default sorter is changed.
ebb5dbf
to
d004216
Compare
alexshtin
approved these changes
May 10, 2023
d004216
to
056ab3d
Compare
056ab3d
to
624149b
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What changed?
Manually build pagination query in ES when using default sorter.
Why?
In cases of deep pagination, manually building the pagination query works faster than using
search_after
.How did you test it?
Existing tests.
Potential risks
No.
Is hotfix candidate?
No.