[ResponseOps] ES|QL query rule executor silently rewrites the saved query via parse+pretty-print#275231
Conversation
There was a problem hiding this comment.
The new token-based appendLimitToQuery approach is sound and matches the existing antlr4 token-channel pattern used in kbn-esql-language/.../lexer_scope.ts, and antlr4 is a runtime dependency so the import is safe. One inline finding: a leftover casing mismatch in a CSV generator test assertion that lines up with the still-failing CI Jest test.
Generated by Claude Reviewer for issue #275231 · 182.3 AIC · ⌖ 10.3 AIC · ⊞ 3.9K
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
…akalexi/kibana into bug/appendLimitToQuery-rewrites-query
|
Pinging @elastic/response-ops (Team:ResponseOps) |
|
We recently did some changes and we include parenthesis at the AST so @bartoval will help here |
This Pr should solve the parens problems elastic/esql-js#130. This fix should be active on Kibana soon. https://github.com/elastic/kibana/pull/275286/changes#diff-7ae45ad102eab3b6d7e7896acd08c427a9b25b346470d7bc6507b6481575d519R143 |
|
Thanks @stratoula and @bartoval! I am wondering what you think about using |
adcoelho
left a comment
There was a problem hiding this comment.
response-ops changes OK, just some tests
Sorry Alexi, I am not sure I follow 😅 (maybe because it is late). Can you elaborate? |
That's my bad, sorry! I wanted to get your thoughts about keeping this PR open with this change to I'm also wondering if change in @bartoval's PR would be able to backported? bc I think we will want to backport a fix for this as well |
Hello @doakalexi . const trimmedQuery = queryString.trimEnd();
return `${trimmedQuery}\n| LIMIT ${limit}`;cause you problems? |
Thanks @bartoval! It looks like |
Does it make sense to have this utils in this package? it seems we already have I see from past commits that you implemented this function and all the consumers (or part of it) to use it. |
I think it makes sense to keep appendLimitToQuery, I think there a couple places that use the function already and it's nice to have a consistent way to append a limit. but I can remove it if you think it's not useful too Thanks for the suggestion, I updated to use \n in this commit, 211df81 |
That works for me; I'll leave the approval to @stratoula . In any case, I am restating my thoughts above-> This existing function https://github.com/bartoval/kibana/blob/main/src/platform/packages/shared/kbn-esql-utils/src/utils/append_to_query/utils.ts#L90 It works in a similar way. Just change your 2 consumers to query = appendToESQLQuery(this.job.query.esql, `| LIMIT ${maxRows}`);query = appendToESQLQuery(this.job.query.esql, `| LIMIT ${alertLimit}`);Then we can delete this file and the test |
stratoula
left a comment
There was a problem hiding this comment.
The appendLimitToQuery is only used in 2 consumers. I will keep it thought to esql-utils:
- it is a common thing to do and I want to change it with the composer api
- I prefer to have one place to add the utils and not have multiple solutions across consumers
Alexi the solution is ok for now. Can you just remove the wrong test I point out?
| ); | ||
| }); | ||
|
|
||
| it('removes trailing block comments before appending the limit', () => { |
There was a problem hiding this comment.
This is wrong, it doesnt remove them. I dont think we need this test, can we remove this please?
There was a problem hiding this comment.
Thanks @stratoula and @bartoval for your help on this! Test removed in this commit, 87df6b0
…akalexi/kibana into bug/appendLimitToQuery-rewrites-query
💛 Build succeeded, but was flaky
Failed CI Steps
Test Failures
Metrics [docs]
History
|
|
Starting backport for target branches: 9.4 |
💚 All backports created successfully
Note: Successful backport PRs will be merged automatically after passing CI. Questions ?Please refer to the Backport tool documentation |
…aved query via parse+pretty-print (#275231) (#276032) # Backport This will backport the following commits from `main` to `9.4`: - [[ResponseOps] ES|QL query rule executor silently rewrites the saved query via parse+pretty-print (#275231)](#275231) <!--- Backport version: 9.6.6 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sorenlouv/backport) <!--BACKPORT [{"author":{"name":"Alexi Doak","email":"109488926+doakalexi@users.noreply.github.com"},"sourceCommit":{"committedDate":"2026-07-02T16:11:59Z","message":"[ResponseOps] ES|QL query rule executor silently rewrites the saved query via parse+pretty-print (#275231)\n\nResolves https://github.com/elastic/kibana/issues/273173\n\n## Summary\n\nWhen we execute an ES|QL rule we call, `appendLimitToQuery`, which\nparses the query, and re-emits it with the limit via BasicPrettyPrinter\nfrom @elastic/esql. Using the pretty printer drops parentheses that\nchange the AST shape, so calculations that depend on grouping with\nsame-precedence binary operators (*// or +/-) are rewritten and evaluate\nto a different value than what the user authored.\n\nThis PR updates `appendLimitToQuery` to find the end of the actual query\nand strip off any comments that would interfere with appending the limit\nand returns the rest of the query as written with the new limit.\n\n\n### Checklist\n\n- [ ] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios\n\n\n### To verify\n\n1. Create a few different ES|QL rules. Use queries that include\nparentheses and order of operations calculations.\n2. Let the rules run\n3. Verify that that results of the rule queries match what is expected.\n\n---------\n\nCo-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>","sha":"89d065fa2b2977d852775627599ae73506d611ad","branchLabelMapping":{"^v9.5.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","Team:ResponseOps","backport:version","v9.5.0","v9.4.4"],"title":"[ResponseOps] ES|QL query rule executor silently rewrites the saved query via parse+pretty-print","number":275231,"url":"https://github.com/elastic/kibana/pull/275231","mergeCommit":{"message":"[ResponseOps] ES|QL query rule executor silently rewrites the saved query via parse+pretty-print (#275231)\n\nResolves https://github.com/elastic/kibana/issues/273173\n\n## Summary\n\nWhen we execute an ES|QL rule we call, `appendLimitToQuery`, which\nparses the query, and re-emits it with the limit via BasicPrettyPrinter\nfrom @elastic/esql. Using the pretty printer drops parentheses that\nchange the AST shape, so calculations that depend on grouping with\nsame-precedence binary operators (*// or +/-) are rewritten and evaluate\nto a different value than what the user authored.\n\nThis PR updates `appendLimitToQuery` to find the end of the actual query\nand strip off any comments that would interfere with appending the limit\nand returns the rest of the query as written with the new limit.\n\n\n### Checklist\n\n- [ ] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios\n\n\n### To verify\n\n1. Create a few different ES|QL rules. Use queries that include\nparentheses and order of operations calculations.\n2. Let the rules run\n3. Verify that that results of the rule queries match what is expected.\n\n---------\n\nCo-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>","sha":"89d065fa2b2977d852775627599ae73506d611ad"}},"sourceBranch":"main","suggestedTargetBranches":["9.4"],"targetPullRequestStates":[{"branch":"main","label":"v9.5.0","branchLabelMappingKey":"^v9.5.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/275231","number":275231,"mergeCommit":{"message":"[ResponseOps] ES|QL query rule executor silently rewrites the saved query via parse+pretty-print (#275231)\n\nResolves https://github.com/elastic/kibana/issues/273173\n\n## Summary\n\nWhen we execute an ES|QL rule we call, `appendLimitToQuery`, which\nparses the query, and re-emits it with the limit via BasicPrettyPrinter\nfrom @elastic/esql. Using the pretty printer drops parentheses that\nchange the AST shape, so calculations that depend on grouping with\nsame-precedence binary operators (*// or +/-) are rewritten and evaluate\nto a different value than what the user authored.\n\nThis PR updates `appendLimitToQuery` to find the end of the actual query\nand strip off any comments that would interfere with appending the limit\nand returns the rest of the query as written with the new limit.\n\n\n### Checklist\n\n- [ ] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios\n\n\n### To verify\n\n1. Create a few different ES|QL rules. Use queries that include\nparentheses and order of operations calculations.\n2. Let the rules run\n3. Verify that that results of the rule queries match what is expected.\n\n---------\n\nCo-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>","sha":"89d065fa2b2977d852775627599ae73506d611ad"}},{"branch":"9.4","label":"v9.4.4","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}] BACKPORT--> Co-authored-by: Alexi Doak <109488926+doakalexi@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
…uery via parse+pretty-print (elastic#275231) Resolves elastic#273173 ## Summary When we execute an ES|QL rule we call, `appendLimitToQuery`, which parses the query, and re-emits it with the limit via BasicPrettyPrinter from @elastic/esql. Using the pretty printer drops parentheses that change the AST shape, so calculations that depend on grouping with same-precedence binary operators (*// or +/-) are rewritten and evaluate to a different value than what the user authored. This PR updates `appendLimitToQuery` to find the end of the actual query and strip off any comments that would interfere with appending the limit and returns the rest of the query as written with the new limit. ### Checklist - [ ] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios ### To verify 1. Create a few different ES|QL rules. Use queries that include parentheses and order of operations calculations. 2. Let the rules run 3. Verify that that results of the rule queries match what is expected. --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Resolves #273173
Summary
When we execute an ES|QL rule we call,
appendLimitToQuery, which parses the query, and re-emits it with the limit via BasicPrettyPrinter from @elastic/esql. Using the pretty printer drops parentheses that change the AST shape, so calculations that depend on grouping with same-precedence binary operators (*// or +/-) are rewritten and evaluate to a different value than what the user authored.This PR updates
appendLimitToQueryto find the end of the actual query and strip off any comments that would interfere with appending the limit and returns the rest of the query as written with the new limit.Checklist
To verify