-
Notifications
You must be signed in to change notification settings - Fork 25.6k
ES|QL: Add initial grammar and planning for RRF (snapshot) #123396
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
33eb3fe
Add initial grammar and planning for RRF
ioanatia 9b00645
Move logic from analyzer to parser
ioanatia 406b3ea
Fix references and serialization
ioanatia f08abb7
Add csv test
ioanatia f8c2654
Group by index and id - collect the values for the rest of the columns
ioanatia c6cbfd0
Add statement parser and analyzer tests
ioanatia 587ef71
Update docs/changelog/123396.yaml
ioanatia ee50a80
[CI] Auto commit changes from spotless
281da1d
Add javadoc
ioanatia 0a45f00
Make integration test more resilient
ioanatia e401c23
Merge branch 'main' into esql_rrf
ioanatia 40168dc
Address review feedback
ioanatia 4b3a8d7
[CI] Auto commit changes from spotless
d56d280
Merge branch 'main' into esql_rrf
ioanatia b6b1a84
Fix forbidden APIs check
ioanatia d1de323
Fix EsqlNodeSubclassTests
ioanatia 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| pr: 123396 | ||
| summary: Add initial grammar and planning for RRF (snapshot) | ||
| area: ES|QL | ||
| type: feature | ||
| issues: [] |
79 changes: 79 additions & 0 deletions
79
...n/esql/compute/src/main/java/org/elasticsearch/compute/operator/RrfScoreEvalOperator.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| package org.elasticsearch.compute.operator; | ||
|
|
||
| import org.apache.lucene.util.BytesRef; | ||
| import org.elasticsearch.compute.data.Block; | ||
| import org.elasticsearch.compute.data.BytesRefBlock; | ||
| import org.elasticsearch.compute.data.DoubleVector; | ||
| import org.elasticsearch.compute.data.Page; | ||
|
|
||
| import java.util.HashMap; | ||
|
|
||
| /** | ||
| * Updates the score column with new scores using the RRF formula. | ||
| * Receives the position of the score and fork columns. | ||
| * The new score we assign to each row is equal to {@code 1 / (rank_constant + row_number)}. | ||
| * We use the fork discriminator column to determine the {@code row_number} for each row. | ||
| */ | ||
| public class RrfScoreEvalOperator extends AbstractPageMappingOperator { | ||
|
|
||
| public record Factory(int forkPosition, int scorePosition) implements OperatorFactory { | ||
| @Override | ||
| public Operator get(DriverContext driverContext) { | ||
| return new RrfScoreEvalOperator(forkPosition, scorePosition); | ||
| } | ||
|
|
||
| @Override | ||
| public String describe() { | ||
| return "RrfScoreEvalOperator"; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| private final int scorePosition; | ||
| private final int forkPosition; | ||
|
|
||
| private HashMap<String, Integer> counters = new HashMap<>(); | ||
|
|
||
| public RrfScoreEvalOperator(int forkPosition, int scorePosition) { | ||
| this.scorePosition = scorePosition; | ||
| this.forkPosition = forkPosition; | ||
| } | ||
|
|
||
| @Override | ||
| protected Page process(Page page) { | ||
| BytesRefBlock forkBlock = (BytesRefBlock) page.getBlock(forkPosition); | ||
|
|
||
| DoubleVector.Builder scores = forkBlock.blockFactory().newDoubleVectorBuilder(forkBlock.getPositionCount()); | ||
|
|
||
| for (int i = 0; i < page.getPositionCount(); i++) { | ||
| String fork = forkBlock.getBytesRef(i, new BytesRef()).utf8ToString(); | ||
|
|
||
| int rank = counters.getOrDefault(fork, 1); | ||
| counters.put(fork, rank + 1); | ||
| scores.appendDouble(1.0 / (60 + rank)); | ||
| } | ||
|
|
||
| Block scoreBlock = scores.build().asBlock(); | ||
| page = page.appendBlock(scoreBlock); | ||
|
|
||
| int[] projections = new int[page.getBlockCount() - 1]; | ||
|
|
||
| for (int i = 0; i < page.getBlockCount() - 1; i++) { | ||
| projections[i] = i == scorePosition ? page.getBlockCount() - 1 : i; | ||
| } | ||
|
|
||
| return page.projectBlocks(projections); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "RrfScoreEvalOperator"; | ||
| } | ||
| } | ||
111 changes: 111 additions & 0 deletions
111
x-pack/plugin/esql/qa/testFixtures/src/main/resources/rrf.csv-spec
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| // | ||
| // CSV spec for RRF command | ||
| // | ||
|
|
||
| simpleRrf | ||
| required_capability: fork | ||
| required_capability: rrf | ||
| required_capability: match_operator_colon | ||
|
|
||
| FROM employees METADATA _id, _index, _score | ||
| | FORK ( WHERE emp_no:10001 ) | ||
| ( WHERE emp_no:10002 ) | ||
| | RRF | ||
| | EVAL _score = round(_score, 4) | ||
|
Member
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. Nice trick! ❤️ |
||
| | KEEP _score, _fork, emp_no | ||
ioanatia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| | SORT _score, _fork, emp_no | ||
| ; | ||
|
|
||
| _score:double | _fork:keyword | emp_no:integer | ||
| 0.0164 | fork1 | 10001 | ||
| 0.0164 | fork2 | 10002 | ||
| ; | ||
|
|
||
| rrfWithMatchAndScore | ||
| required_capability: fork | ||
| required_capability: rrf | ||
| required_capability: match_operator_colon | ||
|
|
||
| FROM books METADATA _id, _index, _score | ||
| | FORK ( WHERE title:"Tolkien" | SORT _score, _id DESC | LIMIT 3 ) | ||
| ( WHERE author:"Tolkien" | SORT _score, _id DESC | LIMIT 3 ) | ||
| | RRF | ||
| | EVAL _fork = mv_sort(_fork) | ||
| | EVAL _score = round(_score, 5) | ||
| | KEEP _score, _fork, _id | ||
| ; | ||
|
|
||
| _score:double | _fork:keyword | _id:keyword | ||
| 0.03279 | [fork1, fork2] | 4 | ||
| 0.01613 | fork1 | 56 | ||
| 0.01613 | fork2 | 60 | ||
| 0.01587 | fork2 | 1 | ||
| 0.01587 | fork1 | 26 | ||
| ; | ||
|
|
||
| rrfWithDisjunctionAndPostFilter | ||
| required_capability: fork | ||
| required_capability: rrf | ||
| required_capability: match_operator_colon | ||
|
|
||
| FROM books METADATA _id, _index, _score | ||
| | FORK ( WHERE title:"Tolkien" OR author:"Tolkien" | SORT _score, _id DESC | LIMIT 3 ) | ||
| ( WHERE author:"Tolkien" | SORT _score, _id DESC | LIMIT 3 ) | ||
| | RRF | ||
| | EVAL _fork = mv_sort(_fork) | ||
| | EVAL _score = round(_score, 5) | ||
| | KEEP _score, _fork, _id | ||
| | WHERE _score > 0.014 | ||
| ; | ||
|
|
||
| _score:double | _fork:keyword | _id:keyword | ||
| 0.03252 | [fork1, fork2] | 60 | ||
| 0.032 | [fork1, fork2] | 1 | ||
| 0.01639 | fork2 | 4 | ||
| 0.01587 | fork1 | 40 | ||
| ; | ||
|
|
||
| rrfWithStats | ||
| required_capability: fork | ||
| required_capability: rrf | ||
| required_capability: match_operator_colon | ||
|
|
||
| FROM books METADATA _id, _index, _score | ||
| | FORK ( WHERE title:"Tolkien" | SORT _score, _id DESC | LIMIT 3 ) | ||
| ( WHERE author:"Tolkien" | SORT _score, _id DESC | LIMIT 3 ) | ||
| ( WHERE author:"Ursula K. Le Guin" AND title:"short stories" | SORT _score, _id DESC | LIMIT 3) | ||
| | RRF | ||
| | STATS count_fork=COUNT(*) BY _fork | ||
| ; | ||
|
|
||
| count_fork:long | _fork:keyword | ||
| 3 | fork1 | ||
| 3 | fork2 | ||
| 1 | fork3 | ||
| ; | ||
|
|
||
| rrfWithMultipleForkBranches | ||
| required_capability: fork | ||
| required_capability: rrf | ||
| required_capability: match_operator_colon | ||
|
|
||
| FROM books METADATA _id, _index, _score | ||
| | FORK (WHERE author:"Keith Faulkner" AND qstr("author:Rory or author:Beverlie") | SORT _score, _id DESC | LIMIT 3) | ||
| (WHERE author:"Ursula K. Le Guin" | SORT _score, _id DESC | LIMIT 3) | ||
| (WHERE title:"Tolkien" AND author:"Tolkien" AND year > 2000 AND mv_count(author) == 1 | SORT _score, _id DESC | LIMIT 3) | ||
| (WHERE match(author, "Keith Faulkner") AND match(author, "Rory Tyger") | SORT _score, _id DESC | LIMIT 3) | ||
| | RRF | ||
| | EVAL _fork = mv_sort(_fork) | ||
| | EVAL _score = round(_score, 4) | ||
| | EVAL title = trim(substring(title, 1, 20)) | ||
| | KEEP _score, author, title, _fork | ||
| ; | ||
|
|
||
| _score:double | author:keyword | title:keyword | _fork:keyword | ||
| 0.0328 | [Keith Faulkner, Rory Tyger] | Pop! Went Another Ba | [fork1, fork4] | ||
| 0.0164 | J.R.R. Tolkien | Letters of J R R Tol | fork3 | ||
| 0.0164 | Ursula K. Le Guin | The wind's twelve qu | fork2 | ||
| 0.0161 | [Beverlie Manson, Keith Faulkner] | Rainbow's End: A Mag | fork1 | ||
| 0.0161 | Ursula K. Le Guin | The Word For World i | fork2 | ||
| 0.0159 | Ursula K. Le Guin | The Dispossessed | fork2 | ||
| ; | ||
ioanatia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -65,6 +65,7 @@ import ChangePoint, | |
| Metrics, | ||
| MvExpand, | ||
| Project, | ||
| Rrf, | ||
| Rename, | ||
| Show, | ||
| UnknownCommand; | ||
|
|
||
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.
minor: this is currently configurable in _search, so we probably need to expose it as an option in the future here too
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.
You are right, we need to make the rank constant configurable.
This is added as a separate feature in the meta issue #123391
It will require a syntax change for RRF, so I'd like to keep it separate for now.