Skip to content

Commit

Permalink
[8.x] [ES|QL] open suggestions automatically in sources lists and &#x…
Browse files Browse the repository at this point in the history
…60;ENRICH` (#191312) (#193013)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[ES|QL] open suggestions automatically in sources lists and
`ENRICH`
(#191312)](#191312)

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

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

<!--BACKPORT [{"author":{"name":"Drew
Tate","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-09-16T13:41:38Z","message":"[ES|QL]
open suggestions automatically in sources lists and `ENRICH`
(#191312)\n\n## Summary\r\n\r\nPart of
https://github.com/elastic/kibana/issues/189662\r\n\r\nAlso, a follow-on
to https://github.com/elastic/kibana/issues/187184\r\nwith certain
source names (e.g. `foo$bar`, `my-policy`) and fields in\r\nthe `ENRICH`
command.\r\n\r\nDuring this effort I
discovered\r\nhttps://github.com//issues/191321 and a bug
with the\r\nvalidation of field names in the \"ENRICH ... WITH\" list
(documented\r\n[here](#177699)),
both of which\r\nwill be addressed separately.\r\n\r\n##
Sources\r\n\r\n### General
flow\r\n\r\n\r\nhttps://github.com/user-attachments/assets/4b103621-0e66-4c36-807f-4932f0cb8faf\r\n\r\n###
Works with wild-card
matches\r\n\r\n\r\nhttps://github.com/user-attachments/assets/6b47fffc-e922-4e2d-b6aa-3d9a2fc2236c\r\n\r\n###
`METADATA` field
list\r\n\r\n\r\nhttps://github.com/user-attachments/assets/d3bdf4dc-1d0c-4d56-81d7-af6bc4e25a4a\r\n\r\n##
ENRICH\r\n\r\nAutosuggest now helps you
along\r\n\r\n\r\nhttps://github.com/user-attachments/assets/d627484c-e729-4dc7-9e7b-795395a31d4f\r\n\r\nAlso,
fixed this bug (follow on
to\r\nhttps://github.com//issues/187184
)\r\n\r\n\r\n\r\nhttps://github.com/user-attachments/assets/aa62a0c3-6db5-434a-829a-59f14c5c4c85\r\n\r\n\r\n###
Checklist\r\n\r\n- [x] [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\r\n---------\r\n\r\nCo-authored-by: Elastic Machine
<[email protected]>\r\nCo-authored-by: Stratoula
Kalafateli
<[email protected]>","sha":"e404a3992e220735ae51918c532a1a032e7f7993","branchLabelMapping":{"^v9.0.0$":"main","^v8.16.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:enhancement","v9.0.0","backport:prev-minor","Feature:ES|QL","Team:ESQL"],"title":"[ES|QL]
open suggestions automatically in sources lists and
`ENRICH`","number":191312,"url":"https://github.com/elastic/kibana/pull/191312","mergeCommit":{"message":"[ES|QL]
open suggestions automatically in sources lists and `ENRICH`
(#191312)\n\n## Summary\r\n\r\nPart of
https://github.com/elastic/kibana/issues/189662\r\n\r\nAlso, a follow-on
to https://github.com/elastic/kibana/issues/187184\r\nwith certain
source names (e.g. `foo$bar`, `my-policy`) and fields in\r\nthe `ENRICH`
command.\r\n\r\nDuring this effort I
discovered\r\nhttps://github.com//issues/191321 and a bug
with the\r\nvalidation of field names in the \"ENRICH ... WITH\" list
(documented\r\n[here](#177699)),
both of which\r\nwill be addressed separately.\r\n\r\n##
Sources\r\n\r\n### General
flow\r\n\r\n\r\nhttps://github.com/user-attachments/assets/4b103621-0e66-4c36-807f-4932f0cb8faf\r\n\r\n###
Works with wild-card
matches\r\n\r\n\r\nhttps://github.com/user-attachments/assets/6b47fffc-e922-4e2d-b6aa-3d9a2fc2236c\r\n\r\n###
`METADATA` field
list\r\n\r\n\r\nhttps://github.com/user-attachments/assets/d3bdf4dc-1d0c-4d56-81d7-af6bc4e25a4a\r\n\r\n##
ENRICH\r\n\r\nAutosuggest now helps you
along\r\n\r\n\r\nhttps://github.com/user-attachments/assets/d627484c-e729-4dc7-9e7b-795395a31d4f\r\n\r\nAlso,
fixed this bug (follow on
to\r\nhttps://github.com//issues/187184
)\r\n\r\n\r\n\r\nhttps://github.com/user-attachments/assets/aa62a0c3-6db5-434a-829a-59f14c5c4c85\r\n\r\n\r\n###
Checklist\r\n\r\n- [x] [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\r\n---------\r\n\r\nCo-authored-by: Elastic Machine
<[email protected]>\r\nCo-authored-by: Stratoula
Kalafateli
<[email protected]>","sha":"e404a3992e220735ae51918c532a1a032e7f7993"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/191312","number":191312,"mergeCommit":{"message":"[ES|QL]
open suggestions automatically in sources lists and `ENRICH`
(#191312)\n\n## Summary\r\n\r\nPart of
https://github.com/elastic/kibana/issues/189662\r\n\r\nAlso, a follow-on
to https://github.com/elastic/kibana/issues/187184\r\nwith certain
source names (e.g. `foo$bar`, `my-policy`) and fields in\r\nthe `ENRICH`
command.\r\n\r\nDuring this effort I
discovered\r\nhttps://github.com//issues/191321 and a bug
with the\r\nvalidation of field names in the \"ENRICH ... WITH\" list
(documented\r\n[here](#177699)),
both of which\r\nwill be addressed separately.\r\n\r\n##
Sources\r\n\r\n### General
flow\r\n\r\n\r\nhttps://github.com/user-attachments/assets/4b103621-0e66-4c36-807f-4932f0cb8faf\r\n\r\n###
Works with wild-card
matches\r\n\r\n\r\nhttps://github.com/user-attachments/assets/6b47fffc-e922-4e2d-b6aa-3d9a2fc2236c\r\n\r\n###
`METADATA` field
list\r\n\r\n\r\nhttps://github.com/user-attachments/assets/d3bdf4dc-1d0c-4d56-81d7-af6bc4e25a4a\r\n\r\n##
ENRICH\r\n\r\nAutosuggest now helps you
along\r\n\r\n\r\nhttps://github.com/user-attachments/assets/d627484c-e729-4dc7-9e7b-795395a31d4f\r\n\r\nAlso,
fixed this bug (follow on
to\r\nhttps://github.com//issues/187184
)\r\n\r\n\r\n\r\nhttps://github.com/user-attachments/assets/aa62a0c3-6db5-434a-829a-59f14c5c4c85\r\n\r\n\r\n###
Checklist\r\n\r\n- [x] [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\r\n---------\r\n\r\nCo-authored-by: Elastic Machine
<[email protected]>\r\nCo-authored-by: Stratoula
Kalafateli
<[email protected]>","sha":"e404a3992e220735ae51918c532a1a032e7f7993"}}]}]
BACKPORT-->

Co-authored-by: Drew Tate <[email protected]>
  • Loading branch information
kibanamachine and drewdaemon authored Sep 16, 2024
1 parent 612e578 commit 02f8d09
Show file tree
Hide file tree
Showing 10 changed files with 491 additions and 208 deletions.
2 changes: 1 addition & 1 deletion packages/kbn-esql-ast/src/ast_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,9 @@ export function createSource(
index,
name: text,
sourceType: type,
text,
location: getPosition(ctx.start, ctx.stop),
incomplete: Boolean(ctx.exception || text === ''),
text: ctx?.getText(),
};
}

Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-esql-utils/src/utils/query_parsing_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function getIndexPatternFromESQLQuery(esql?: string) {
const sourceCommand = ast.find(({ name }) => ['from', 'metrics'].includes(name));
const args = (sourceCommand?.args ?? []) as ESQLSource[];
const indices = args.filter((arg) => arg.sourceType === 'index');
return indices?.map((index) => index.text).join(',');
return indices?.map((index) => index.name).join(',');
}

// For ES|QL we consider stats and keep transformational command
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ const visibleIndices = indexes
.map(({ name, suggestedAs }) => suggestedAs || name)
.sort();

const addTrailingSpace = (strings: string[], predicate: (s: string) => boolean = (_s) => true) =>
strings.map((string) => (predicate(string) ? `${string} ` : string));

const metadataFields = [...METADATA_FIELDS].sort();

describe('autocomplete.suggest', () => {
Expand All @@ -37,17 +34,17 @@ describe('autocomplete.suggest', () => {
test('suggests visible indices on space', async () => {
const { assertSuggestions } = await setup();

await assertSuggestions('from /', addTrailingSpace(visibleIndices));
await assertSuggestions('FROM /', addTrailingSpace(visibleIndices));
await assertSuggestions('from /index', addTrailingSpace(visibleIndices));
await assertSuggestions('from /', visibleIndices);
await assertSuggestions('FROM /', visibleIndices);
await assertSuggestions('from /index', visibleIndices);
});

test('suggests visible indices on comma', async () => {
const { assertSuggestions } = await setup();

await assertSuggestions('FROM a,/', addTrailingSpace(visibleIndices));
await assertSuggestions('FROM a, /', addTrailingSpace(visibleIndices));
await assertSuggestions('from *,/', addTrailingSpace(visibleIndices));
await assertSuggestions('FROM a,/', visibleIndices);
await assertSuggestions('FROM a, /', visibleIndices);
await assertSuggestions('from *,/', visibleIndices);
});

test('can suggest integration data sources', async () => {
Expand All @@ -56,10 +53,7 @@ describe('autocomplete.suggest', () => {
.filter(({ hidden }) => !hidden)
.map(({ name, suggestedAs }) => suggestedAs || name)
.sort();
const expectedSuggestions = addTrailingSpace(
visibleDataSources,
(s) => !integrations.find(({ name }) => name === s)
);
const expectedSuggestions = visibleDataSources;
const { assertSuggestions, callbacks } = await setup();
const cb = {
...callbacks,
Expand All @@ -75,7 +69,7 @@ describe('autocomplete.suggest', () => {
});

describe('... METADATA <fields>', () => {
const metadataFieldsSandIndex = metadataFields.filter((field) => field !== '_index');
const metadataFieldsAndIndex = metadataFields.filter((field) => field !== '_index');

test('on <kbd>SPACE</kbd> without comma ",", suggests adding metadata', async () => {
const { assertSuggestions } = await setup();
Expand Down Expand Up @@ -103,8 +97,8 @@ describe('autocomplete.suggest', () => {
test('filters out already used metadata fields', async () => {
const { assertSuggestions } = await setup();

await assertSuggestions('from a, b [metadata _index, /]', metadataFieldsSandIndex);
await assertSuggestions('from a, b metadata _index, /', metadataFieldsSandIndex);
await assertSuggestions('from a, b [metadata _index, /]', metadataFieldsAndIndex);
await assertSuggestions('from a, b metadata _index, /', metadataFieldsAndIndex);
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { groupingFunctionDefinitions } from '../../definitions/grouping';
import * as autocomplete from '../autocomplete';
import type { ESQLCallbacks } from '../../shared/types';
import type { EditorContext, SuggestionRawDefinition } from '../types';
import { TIME_SYSTEM_PARAMS } from '../factories';
import { TIME_SYSTEM_PARAMS, getSafeInsertText } from '../factories';
import { getFunctionSignatures } from '../../definitions/helpers';
import { ESQLRealField } from '../../validation/types';
import {
Expand Down Expand Up @@ -280,10 +280,7 @@ export function createCompletionContext(triggerCharacter?: string) {
export function getPolicyFields(policyName: string) {
return policies
.filter(({ name }) => name === policyName)
.flatMap(({ enrichFields }) =>
// ok, this is a bit of cheating as it's using the same logic as in the helper
enrichFields.map((field) => (/[^a-zA-Z\d_\.@]/.test(field) ? `\`${field}\`` : field))
);
.flatMap(({ enrichFields }) => enrichFields.map((field) => getSafeInsertText(field)));
}

export interface SuggestOptions {
Expand Down
Loading

0 comments on commit 02f8d09

Please sign in to comment.