Skip to content

Commit

Permalink
[8.x] [ES|QL] Fix new variables being suggested in incorrect places (#…
Browse files Browse the repository at this point in the history
…192405) (#193500)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[ES|QL] Fix new variables being suggested in incorrect places
(#192405)](#192405)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Quynh Nguyen
(Quinn)","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-09-19T23:44:40Z","message":"[ES|QL]
Fix new variables being suggested in incorrect places (#192405)\n\n##
Summary\r\n\r\nFixes #191876 and
another bug\r\nwhere var0 was being suggested within the case expression
if it was not\r\npreviously prefixed with `var0 =`.\r\n\r\n\r\n1. No
more suggestions for new variable assignment if cursor is within
a\r\nfunction\r\n\r\nBefore\r\n\r\n\r\nhttps://github.com/user-attachments/assets/01327c3e-f339-44f5-bb69-922318d2ded6\r\n\r\n\r\nAfter\r\n\r\n\r\nhttps://github.com/user-attachments/assets/3eb490d4-46c3-4839-a1d9-06b61a030e97\r\n\r\n\r\n\r\n2.
No more suggestions within
`case()`\r\n\r\nBefore\r\n\r\n\r\nhttps://github.com/user-attachments/assets/36707c65-e2c8-458a-97c2-4b287cada3f8\r\n\r\n\r\nAfter\r\n\r\n\r\nhttps://github.com/user-attachments/assets/bee088cd-2140-4e9e-bf09-22bd278a242d\r\n\r\n\r\n\r\n###
Checklist\r\n\r\nDelete any items that are not applicable to this
PR.\r\n\r\n- [ ] Any text added follows [EUI's
writing\r\nguidelines](https://elastic.github.io/eui/#/guidelines/writing),
uses\r\nsentence case text and includes
[i18n\r\nsupport](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)\r\n-
[
]\r\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\r\nwas
added for features that require explanation or tutorials\r\n- [ ] [Unit
or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n- [ ] [Flaky
Test\r\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1)
was\r\nused on any tests changed\r\n- [ ] Any UI touched in this PR is
usable by keyboard only (learn more\r\nabout [keyboard
accessibility](https://webaim.org/techniques/keyboard/))\r\n- [ ] Any UI
touched in this PR does not create any new axe failures\r\n(run axe in
browser:\r\n[FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/),\r\n[Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US))\r\n-
[ ] If a plugin configuration key changed, check if it needs to
be\r\nallowlisted in the cloud and added to the
[docker\r\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\r\n-
[ ] This renders correctly on smaller devices using a
responsive\r\nlayout. (You can test this [in
your\r\nbrowser](https://www.browserstack.com/guide/responsive-testing-on-local-server))\r\n-
[ ] This was checked for
[cross-browser\r\ncompatibility](https://www.elastic.co/support/matrix#matrix_browsers)\r\n\r\n\r\n###
Risk Matrix\r\n\r\nDelete this section if it is not applicable to this
PR.\r\n\r\nBefore closing this PR, invite QA, stakeholders, and other
developers to\r\nidentify risks that should be tested prior to the
change/feature\r\nrelease.\r\n\r\nWhen forming the risk matrix, consider
some of the following examples\r\nand how they may potentially impact
the change:\r\n\r\n| Risk | Probability | Severity | Mitigation/Notes
|\r\n\r\n|---------------------------|-------------|----------|-------------------------|\r\n|
Multiple Spaces&mdash;unexpected behavior in non-default Kibana
Space.\r\n| Low | High | Integration tests will verify that all features
are still\r\nsupported in non-default Kibana Space and when user
switches between\r\nspaces. |\r\n| Multiple nodes&mdash;Elasticsearch
polling might have race conditions\r\nwhen multiple Kibana nodes are
polling for the same tasks. | High | Low\r\n| Tasks are idempotent, so
executing them multiple times will not result\r\nin logical error, but
will degrade performance. To test for this case we\r\nadd plenty of unit
tests around this logic and document manual testing\r\nprocedure. |\r\n|
Code should gracefully handle cases when feature X or plugin Y
are\r\ndisabled. | Medium | High | Unit tests will verify that any
feature flag\r\nor plugin combination still results in our service
operational. |\r\n| [See more potential
risk\r\nexamples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)
|\r\n\r\n\r\n### For maintainers\r\n\r\n- [ ] This was checked for
breaking API changes and was
[labeled\r\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\r\n\r\n---------\r\n\r\nCo-authored-by:
Elastic Machine
<[email protected]>","sha":"ef5aee1cc252e79aaa833eddeb686dc9492de98c","branchLabelMapping":{"^v9.0.0$":"main","^v8.16.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","v9.0.0","backport:prev-major","Feature:ES|QL","Team:ESQL","v8.16.0","backport:current-major"],"title":"[ES|QL]
Fix new variables being suggested in incorrect
places","number":192405,"url":"https://github.com/elastic/kibana/pull/192405","mergeCommit":{"message":"[ES|QL]
Fix new variables being suggested in incorrect places (#192405)\n\n##
Summary\r\n\r\nFixes #191876 and
another bug\r\nwhere var0 was being suggested within the case expression
if it was not\r\npreviously prefixed with `var0 =`.\r\n\r\n\r\n1. No
more suggestions for new variable assignment if cursor is within
a\r\nfunction\r\n\r\nBefore\r\n\r\n\r\nhttps://github.com/user-attachments/assets/01327c3e-f339-44f5-bb69-922318d2ded6\r\n\r\n\r\nAfter\r\n\r\n\r\nhttps://github.com/user-attachments/assets/3eb490d4-46c3-4839-a1d9-06b61a030e97\r\n\r\n\r\n\r\n2.
No more suggestions within
`case()`\r\n\r\nBefore\r\n\r\n\r\nhttps://github.com/user-attachments/assets/36707c65-e2c8-458a-97c2-4b287cada3f8\r\n\r\n\r\nAfter\r\n\r\n\r\nhttps://github.com/user-attachments/assets/bee088cd-2140-4e9e-bf09-22bd278a242d\r\n\r\n\r\n\r\n###
Checklist\r\n\r\nDelete any items that are not applicable to this
PR.\r\n\r\n- [ ] Any text added follows [EUI's
writing\r\nguidelines](https://elastic.github.io/eui/#/guidelines/writing),
uses\r\nsentence case text and includes
[i18n\r\nsupport](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)\r\n-
[
]\r\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\r\nwas
added for features that require explanation or tutorials\r\n- [ ] [Unit
or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n- [ ] [Flaky
Test\r\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1)
was\r\nused on any tests changed\r\n- [ ] Any UI touched in this PR is
usable by keyboard only (learn more\r\nabout [keyboard
accessibility](https://webaim.org/techniques/keyboard/))\r\n- [ ] Any UI
touched in this PR does not create any new axe failures\r\n(run axe in
browser:\r\n[FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/),\r\n[Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US))\r\n-
[ ] If a plugin configuration key changed, check if it needs to
be\r\nallowlisted in the cloud and added to the
[docker\r\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\r\n-
[ ] This renders correctly on smaller devices using a
responsive\r\nlayout. (You can test this [in
your\r\nbrowser](https://www.browserstack.com/guide/responsive-testing-on-local-server))\r\n-
[ ] This was checked for
[cross-browser\r\ncompatibility](https://www.elastic.co/support/matrix#matrix_browsers)\r\n\r\n\r\n###
Risk Matrix\r\n\r\nDelete this section if it is not applicable to this
PR.\r\n\r\nBefore closing this PR, invite QA, stakeholders, and other
developers to\r\nidentify risks that should be tested prior to the
change/feature\r\nrelease.\r\n\r\nWhen forming the risk matrix, consider
some of the following examples\r\nand how they may potentially impact
the change:\r\n\r\n| Risk | Probability | Severity | Mitigation/Notes
|\r\n\r\n|---------------------------|-------------|----------|-------------------------|\r\n|
Multiple Spaces&mdash;unexpected behavior in non-default Kibana
Space.\r\n| Low | High | Integration tests will verify that all features
are still\r\nsupported in non-default Kibana Space and when user
switches between\r\nspaces. |\r\n| Multiple nodes&mdash;Elasticsearch
polling might have race conditions\r\nwhen multiple Kibana nodes are
polling for the same tasks. | High | Low\r\n| Tasks are idempotent, so
executing them multiple times will not result\r\nin logical error, but
will degrade performance. To test for this case we\r\nadd plenty of unit
tests around this logic and document manual testing\r\nprocedure. |\r\n|
Code should gracefully handle cases when feature X or plugin Y
are\r\ndisabled. | Medium | High | Unit tests will verify that any
feature flag\r\nor plugin combination still results in our service
operational. |\r\n| [See more potential
risk\r\nexamples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)
|\r\n\r\n\r\n### For maintainers\r\n\r\n- [ ] This was checked for
breaking API changes and was
[labeled\r\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\r\n\r\n---------\r\n\r\nCo-authored-by:
Elastic Machine
<[email protected]>","sha":"ef5aee1cc252e79aaa833eddeb686dc9492de98c"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/192405","number":192405,"mergeCommit":{"message":"[ES|QL]
Fix new variables being suggested in incorrect places (#192405)\n\n##
Summary\r\n\r\nFixes #191876 and
another bug\r\nwhere var0 was being suggested within the case expression
if it was not\r\npreviously prefixed with `var0 =`.\r\n\r\n\r\n1. No
more suggestions for new variable assignment if cursor is within
a\r\nfunction\r\n\r\nBefore\r\n\r\n\r\nhttps://github.com/user-attachments/assets/01327c3e-f339-44f5-bb69-922318d2ded6\r\n\r\n\r\nAfter\r\n\r\n\r\nhttps://github.com/user-attachments/assets/3eb490d4-46c3-4839-a1d9-06b61a030e97\r\n\r\n\r\n\r\n2.
No more suggestions within
`case()`\r\n\r\nBefore\r\n\r\n\r\nhttps://github.com/user-attachments/assets/36707c65-e2c8-458a-97c2-4b287cada3f8\r\n\r\n\r\nAfter\r\n\r\n\r\nhttps://github.com/user-attachments/assets/bee088cd-2140-4e9e-bf09-22bd278a242d\r\n\r\n\r\n\r\n###
Checklist\r\n\r\nDelete any items that are not applicable to this
PR.\r\n\r\n- [ ] Any text added follows [EUI's
writing\r\nguidelines](https://elastic.github.io/eui/#/guidelines/writing),
uses\r\nsentence case text and includes
[i18n\r\nsupport](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)\r\n-
[
]\r\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\r\nwas
added for features that require explanation or tutorials\r\n- [ ] [Unit
or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n- [ ] [Flaky
Test\r\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1)
was\r\nused on any tests changed\r\n- [ ] Any UI touched in this PR is
usable by keyboard only (learn more\r\nabout [keyboard
accessibility](https://webaim.org/techniques/keyboard/))\r\n- [ ] Any UI
touched in this PR does not create any new axe failures\r\n(run axe in
browser:\r\n[FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/),\r\n[Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US))\r\n-
[ ] If a plugin configuration key changed, check if it needs to
be\r\nallowlisted in the cloud and added to the
[docker\r\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\r\n-
[ ] This renders correctly on smaller devices using a
responsive\r\nlayout. (You can test this [in
your\r\nbrowser](https://www.browserstack.com/guide/responsive-testing-on-local-server))\r\n-
[ ] This was checked for
[cross-browser\r\ncompatibility](https://www.elastic.co/support/matrix#matrix_browsers)\r\n\r\n\r\n###
Risk Matrix\r\n\r\nDelete this section if it is not applicable to this
PR.\r\n\r\nBefore closing this PR, invite QA, stakeholders, and other
developers to\r\nidentify risks that should be tested prior to the
change/feature\r\nrelease.\r\n\r\nWhen forming the risk matrix, consider
some of the following examples\r\nand how they may potentially impact
the change:\r\n\r\n| Risk | Probability | Severity | Mitigation/Notes
|\r\n\r\n|---------------------------|-------------|----------|-------------------------|\r\n|
Multiple Spaces&mdash;unexpected behavior in non-default Kibana
Space.\r\n| Low | High | Integration tests will verify that all features
are still\r\nsupported in non-default Kibana Space and when user
switches between\r\nspaces. |\r\n| Multiple nodes&mdash;Elasticsearch
polling might have race conditions\r\nwhen multiple Kibana nodes are
polling for the same tasks. | High | Low\r\n| Tasks are idempotent, so
executing them multiple times will not result\r\nin logical error, but
will degrade performance. To test for this case we\r\nadd plenty of unit
tests around this logic and document manual testing\r\nprocedure. |\r\n|
Code should gracefully handle cases when feature X or plugin Y
are\r\ndisabled. | Medium | High | Unit tests will verify that any
feature flag\r\nor plugin combination still results in our service
operational. |\r\n| [See more potential
risk\r\nexamples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)
|\r\n\r\n\r\n### For maintainers\r\n\r\n- [ ] This was checked for
breaking API changes and was
[labeled\r\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\r\n\r\n---------\r\n\r\nCo-authored-by:
Elastic Machine
<[email protected]>","sha":"ef5aee1cc252e79aaa833eddeb686dc9492de98c"}},{"branch":"8.x","label":"v8.16.0","branchLabelMappingKey":"^v8.16.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Quynh Nguyen (Quinn) <[email protected]>
  • Loading branch information
kibanamachine and qn895 authored Sep 20, 2024
1 parent a2e1d7f commit ab6d783
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ describe('autocomplete.suggest', () => {
test('case', async () => {
const { assertSuggestions } = await setup();
const comparisonOperators = ['==', '!=', '>', '<', '>=', '<=']
.map((op) => `${op} `)
.map((op) => `${op}`)
.concat(',');

// case( / ) suggest any field/eval function in this position as first argument
Expand Down Expand Up @@ -626,7 +626,6 @@ describe('autocomplete.suggest', () => {
// Notice no extra space after field name
...getFieldNamesByType('any').map((field) => `${field}`),
...getFunctionSignaturesByReturnType('eval', 'any', { scalar: true }, undefined, []),
'var0 = ',
]);

// case( field > 0, >) suggests fields like normal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ import {
} from '../definitions/types';
import { metadataOption } from '../definitions/options';
import { comparisonFunctions } from '../definitions/builtin';
import { countBracketsUnclosed } from '../shared/helpers';

type GetFieldsByTypeFn = (
type: string | string[],
Expand Down Expand Up @@ -554,8 +555,11 @@ async function getExpressionSuggestionsByType(
const fieldsMap: Map<string, ESQLRealField> = await (argDef ? getFieldsMap() : new Map());
const anyVariables = collectVariables(commands, fieldsMap, innerText);

const previousWord = findPreviousWord(innerText);
// enrich with assignment has some special rules who are handled somewhere else
const canHaveAssignments = ['eval', 'stats', 'row'].includes(command.name);
const canHaveAssignments =
['eval', 'stats', 'row'].includes(command.name) &&
!comparisonFunctions.map((fn) => fn.name).includes(previousWord);

const references = { fields: fieldsMap, variables: anyVariables };

Expand Down Expand Up @@ -1422,10 +1426,9 @@ async function getFunctionArgsSuggestions(
suggestions.push(
...comparisonFunctions.map<SuggestionRawDefinition>(({ name, description }) => ({
label: name,
text: name + ' ',
text: name,
kind: 'Function' as ItemKind,
detail: description,
command: TRIGGER_SUGGESTION_COMMAND,
}))
);
}
Expand Down Expand Up @@ -1786,10 +1789,13 @@ async function getOptionArgsSuggestions(
openSuggestions: true,
}))
);
// Checks if cursor is still within function ()
// by checking if the marker editor/cursor is within an unclosed parenthesis
const canHaveAssignment = countBracketsUnclosed('(', innerText) === 0;

if (option.name === 'by') {
// Add quick snippet for for stats ... by bucket(<>)
if (command.name === 'stats') {
if (command.name === 'stats' && canHaveAssignment) {
suggestions.push({
label: i18n.translate(
'kbn-esql-validation-autocomplete.esql.autocomplete.addDateHistogram',
Expand Down Expand Up @@ -1820,12 +1826,13 @@ async function getOptionArgsSuggestions(
{
functions: true,
fields: false,
}
},
{ ignoreFn: canHaveAssignment ? [] : ['bucket', 'case'] }
))
);
}

if (command.name === 'stats' && isNewExpression) {
if (command.name === 'stats' && isNewExpression && canHaveAssignment) {
suggestions.push(buildNewVarDefinition(findNewVariable(anyVariables)));
}
}
Expand Down

0 comments on commit ab6d783

Please sign in to comment.