-
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
base: main
Are you sure you want to change the base?
Fix KQL usage in in STATS .. BY #128371
Conversation
Hi @carlosdelest, I've created a changelog YAML for you. |
…tions-stats-using-by' into bugfix/esql-full-text-functions-stats-using-by
@@ -184,7 +184,8 @@ private class ShardState { | |||
private final List<SegmentState> perSegmentState; | |||
|
|||
ShardState(ShardConfig config) throws IOException { | |||
weight = config.searcher.createWeight(config.query, scoreMode(), 1.0f); | |||
Query rewritten = config.searcher.rewrite(config.query); |
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 the LuceneQueryEvaluator
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:
Line 345 in 74d025e
shardConfigs[i++] = new ShardConfig(shardContext.toQuery(queryBuilder()), shardContext.searcher()); |
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
public ParsedQuery toQuery(QueryBuilder queryBuilder) { | |
reset(); | |
try { | |
Query query = Rewriteable.rewrite(queryBuilder, this, true).toQuery(this); | |
if (query == null) { | |
query = Queries.newMatchNoDocsQuery("No query left after rewrite."); | |
} | |
return new ParsedQuery(query, copyNamedQueries()); | |
} catch (QueryShardException | ParsingException e) { |
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 the QueryBuilder
. This means that in KQL it generates a CaseInsensitiveTermQuery
, which is the right thing to do.
However, CaseInsensitiveTermQueryis 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 a Weight
directly on the Query
itself. In case the query has not been rewritten, it fails as CaseInsensitiveTermQuery
needs to be rewritten first.
I will add some comments to explain this in more detail.
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
Added tests for other functions as well that were missing
Pinging @elastic/es-analytical-engine (Team:Analytics) |
Pinging @elastic/kibana-esql (ES|QL-ui) |
@elasticmachine run elasticsearch-ci/rest-compatibility |
KQL can now be used in STATS .. BY
This fixes the LuceneQueryEvaluator not doing a rewrite of the query to evaluate. As KQL function depends on rewriting for being executed, this meant no hits when it was being used directly as a filter in a STATS .. BY clause.