-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Fix KQL usage in in STATS .. BY #128371
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
carlosdelest
merged 13 commits into
elastic:main
from
carlosdelest:bugfix/esql-full-text-functions-stats-using-by
May 27, 2025
Merged
Fix KQL usage in in STATS .. BY #128371
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6b94c48
Rewrite the query in LuceneQueryEvaluator
carlosdelest 36c2e40
Add testing and capability
carlosdelest 5bd76d8
Update docs/changelog/128371.yaml
carlosdelest d30603f
Fix capabilities in tests
carlosdelest a9dd22d
Merge remote-tracking branch 'carlosdelest/bugfix/esql-full-text-func…
carlosdelest 737403a
Delete docs/changelog/128371.yaml
carlosdelest 71817bb
Merge branch 'main' into bugfix/esql-full-text-functions-stats-using-by
carlosdelest 12fb456
Merge branch 'main' into bugfix/esql-full-text-functions-stats-using-by
carlosdelest d09d90f
[CI] Auto commit changes from spotless
elasticsearchmachine 94194f4
Add comments to rewriting
carlosdelest 2fe7f68
Merge remote-tracking branch 'carlosdelest/bugfix/esql-full-text-func…
carlosdelest d9ff06b
Merge remote-tracking branch 'origin/main' into bugfix/esql-full-text…
carlosdelest 8cd60cc
Fix merge
carlosdelest 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
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 |
---|---|---|
|
@@ -841,3 +841,22 @@ r:double | author: text | |
4.670000076293945 | Walter Scheps | ||
4.559999942779541 | J.R.R. Tolkien | ||
; | ||
|
||
testMatchInStatsWithGroupingBy | ||
required_capability: match_function | ||
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. Added tests for other functions as well that were missing |
||
required_capability: full_text_functions_in_stats_where | ||
FROM airports | ||
| STATS c = COUNT(*) where match(country, "United States") BY scalerank | ||
| SORT scalerank desc | ||
; | ||
|
||
c: long | scalerank: long | ||
0 | 9 | ||
44 | 8 | ||
10 | 7 | ||
28 | 6 | ||
10 | 5 | ||
12 | 4 | ||
10 | 3 | ||
15 | 2 | ||
; |
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
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
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.
Queries should be rewritten prior to actual execution in the
LuceneQueryEvaluator
. Some queries like KNN or KQL depend on rewriting for being executed.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.
Question, why not always rewrite the query whenever we create a ShardConfig instance?
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.
Rewriting is only needed in this case, where the query is not pushed down to Lucene. Doing the rewriting on the
ShardContexts.toQuery()
method is not necessary for cases when the query is pushed down, and it's clearer that way for debugging as we don't have the rewritten query being pushed down.We could do the rewriting on the
FullTextFunction.toEvaluator()
method, but I think it's cleaner to have just the query and let theLuceneQueryEvaluator
deal with that internal detail instead of forcing the client to do that.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.
This change fixes the issue we have with KQL.
What I don't understand is why the rewrite does not already happen when we do:
elasticsearch/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/fulltext/FullTextFunction.java
Line 345 in 74d025e
which calls
toQuery
which should already handle the rewrite?elasticsearch/server/src/main/java/org/elasticsearch/index/query/SearchExecutionContext.java
Lines 557 to 565 in 74d025e
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.
Good question. The problem lies in the rewriting target.
toQuery()
rewrites theQueryBuilder
. This means that in KQL it generates aCaseInsensitiveTermQuery
, which is the right thing to do.However, CaseInsensitiveTermQuery
is not a query we can create a
Weight` on. It needs to be rewritten using the rewrite strategy by actually rewriting the query itself.The
LuceneOperator
do this implicitly when creating a Lucene slice. But, the LuceneQueryEvaluator does not do that - it creates aWeight
directly on theQuery
itself. In case the query has not been rewritten, it fails asCaseInsensitiveTermQuery
needs to be rewritten first.I will add some comments to explain this in more detail.