Skip to content

Commit 1db9686

Browse files
[8.x] [ES|QL] Fixes the wrong source validation in case of unknown patterns (elastic#218352) (elastic#218462)
# Backport This will backport the following commits from `main` to `8.x`: - [[ES|QL] Fixes the wrong source validation in case of unknown patterns (elastic#218352)](elastic#218352) <!--- Backport version: 9.6.6 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sorenlouv/backport) <!--BACKPORT [{"author":{"name":"Drew Tate","email":"[email protected]"},"sourceCommit":{"committedDate":"2025-04-16T15:00:53Z","message":"[ES|QL] Fixes the wrong source validation in case of unknown patterns (elastic#218352)\n\n## Summary\n\nCloses https://github.com/elastic/kibana/issues/191556","sha":"eec1a9156a7cdb898b0c2cbfd7ed3495109c0ef9","branchLabelMapping":{"^v9.1.0$":"main","^v8.19.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","Feature:ES|QL","Team:ESQL","backport:version","v9.1.0","v8.19.0"],"title":"[ES|QL] Fixes the wrong source validation in case of unknown patterns","number":218352,"url":"https://github.com/elastic/kibana/pull/218352","mergeCommit":{"message":"[ES|QL] Fixes the wrong source validation in case of unknown patterns (elastic#218352)\n\n## Summary\n\nCloses https://github.com/elastic/kibana/issues/191556","sha":"eec1a9156a7cdb898b0c2cbfd7ed3495109c0ef9"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.1.0","branchLabelMappingKey":"^v9.1.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/218352","number":218352,"mergeCommit":{"message":"[ES|QL] Fixes the wrong source validation in case of unknown patterns (elastic#218352)\n\n## Summary\n\nCloses https://github.com/elastic/kibana/issues/191556","sha":"eec1a9156a7cdb898b0c2cbfd7ed3495109c0ef9"}},{"branch":"8.x","label":"v8.19.0","branchLabelMappingKey":"^v8.19.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}] BACKPORT--> Co-authored-by: Drew Tate <[email protected]>
1 parent 54d58ac commit 1db9686

File tree

4 files changed

+132
-22
lines changed

4 files changed

+132
-22
lines changed

src/platform/packages/shared/kbn-esql-validation-autocomplete/src/validation/__tests__/test_suites/validation.command.from.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ export const validationFromCommandTestSuite = (setup: helpers.Setup) => {
5858
await expectErrors('fRoM *,-.*', []);
5959
await expectErrors('fRoM .secret_index', []);
6060
await expectErrors('from my-index', []);
61+
62+
await expectErrors('FROM index, missingIndex*', []);
63+
await expectErrors('FROM index, lol*catz', []);
64+
await expectErrors('FROM index*, lol*catz', []);
65+
await expectErrors('FROM missingIndex*, index', []);
66+
await expectErrors('FROM missingIndex*, missingIndex2*, index', []);
6167
});
6268

6369
test('errors on trailing comma', async () => {
@@ -95,6 +101,17 @@ export const validationFromCommandTestSuite = (setup: helpers.Setup) => {
95101
await expectErrors(`FROM indexes*`, ['Unknown index [indexes*]']);
96102
await expectErrors('from numberField', ['Unknown index [numberField]']);
97103
await expectErrors('FROM policy', ['Unknown index [policy]']);
104+
105+
await expectErrors('FROM index, missingIndex', ['Unknown index [missingIndex]']);
106+
await expectErrors('FROM missingIndex, index', ['Unknown index [missingIndex]']);
107+
await expectErrors('FROM *missingIndex, missingIndex2, index', [
108+
'Unknown index [missingIndex2]',
109+
]);
110+
await expectErrors('FROM missingIndex*', ['Unknown index [missingIndex*]']);
111+
await expectErrors('FROM *missingIndex, missing*Index2', [
112+
'Unknown index [*missingIndex]',
113+
'Unknown index [missing*Index2]',
114+
]);
98115
});
99116
});
100117

src/platform/packages/shared/kbn-esql-validation-autocomplete/src/validation/commands/metrics/index.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
*/
99

1010
import { ESQLAstTimeseriesCommand, ESQLMessage } from '@kbn/esql-ast';
11-
import { ESQLFunction, ESQLSource } from '@kbn/esql-ast/src/types';
11+
import { ESQLFunction } from '@kbn/esql-ast/src/types';
1212
import {
1313
isAggFunction,
1414
isFunctionOperatorParam,
1515
isMaybeAggFunction,
1616
} from '../../../shared/helpers';
1717
import { ReferenceMaps } from '../../types';
1818
import { isFunctionItem, isLiteralItem } from '../../../..';
19-
import { validateSource } from '../../validation';
19+
import { validateSources } from '../../validation';
2020

2121
/**
2222
* Validates the TIMESERIES source command:
@@ -58,16 +58,6 @@ const findNestedAggFunctionInAggFunction = (agg: ESQLFunction): ESQLFunction | u
5858
}
5959
};
6060

61-
function validateSources(sources: ESQLSource[], references: ReferenceMaps): ESQLMessage[] {
62-
const messages: ESQLMessage[] = [];
63-
64-
for (const source of sources) {
65-
messages.push(...validateSource(source, references));
66-
}
67-
68-
return messages;
69-
}
70-
7161
/**
7262
* Looks for first nested aggregate function in another aggregate a function,
7363
* recursively.

src/platform/packages/shared/kbn-esql-validation-autocomplete/src/validation/esql_validation_meta_tests.json

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9662,6 +9662,31 @@
96629662
"error": [],
96639663
"warning": []
96649664
},
9665+
{
9666+
"query": "FROM index, missingIndex*",
9667+
"error": [],
9668+
"warning": []
9669+
},
9670+
{
9671+
"query": "FROM index, lol*catz",
9672+
"error": [],
9673+
"warning": []
9674+
},
9675+
{
9676+
"query": "FROM index*, lol*catz",
9677+
"error": [],
9678+
"warning": []
9679+
},
9680+
{
9681+
"query": "FROM missingIndex*, index",
9682+
"error": [],
9683+
"warning": []
9684+
},
9685+
{
9686+
"query": "FROM missingIndex*, missingIndex2*, index",
9687+
"error": [],
9688+
"warning": []
9689+
},
96659690
{
96669691
"query": "from index,",
96679692
"error": [
@@ -9741,6 +9766,42 @@
97419766
],
97429767
"warning": []
97439768
},
9769+
{
9770+
"query": "FROM index, missingIndex",
9771+
"error": [
9772+
"Unknown index [missingIndex]"
9773+
],
9774+
"warning": []
9775+
},
9776+
{
9777+
"query": "FROM missingIndex, index",
9778+
"error": [
9779+
"Unknown index [missingIndex]"
9780+
],
9781+
"warning": []
9782+
},
9783+
{
9784+
"query": "FROM *missingIndex, missingIndex2, index",
9785+
"error": [
9786+
"Unknown index [missingIndex2]"
9787+
],
9788+
"warning": []
9789+
},
9790+
{
9791+
"query": "FROM missingIndex*",
9792+
"error": [
9793+
"Unknown index [missingIndex*]"
9794+
],
9795+
"warning": []
9796+
},
9797+
{
9798+
"query": "FROM *missingIndex, missing*Index2",
9799+
"error": [
9800+
"Unknown index [*missingIndex]",
9801+
"Unknown index [missing*Index2]"
9802+
],
9803+
"warning": []
9804+
},
97449805
{
97459806
"query": "from index metadata _id",
97469807
"error": [],

src/platform/packages/shared/kbn-esql-validation-autocomplete/src/validation/validation.ts

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
areFieldAndVariableTypesCompatible,
2525
getColumnExists,
2626
getCommandDefinition,
27+
hasWildcard,
2728
isColumnItem,
2829
isFunctionItem,
2930
isOptionItem,
@@ -282,11 +283,12 @@ function validateCommand(
282283
locations: arg.location,
283284
})
284285
);
285-
} else if (isSourceItem(arg)) {
286-
messages.push(...validateSource(arg, references));
287286
}
288287
}
289288
}
289+
290+
const sources = command.args.filter((arg) => isSourceItem(arg)) as ESQLSource[];
291+
messages.push(...validateSources(sources, references));
290292
}
291293
}
292294

@@ -386,24 +388,64 @@ function validateUnsupportedTypeFields(fields: Map<string, ESQLRealField>, ast:
386388
return messages;
387389
}
388390

389-
export function validateSource(source: ESQLSource, { sources }: ReferenceMaps) {
391+
export function validateSources(
392+
sources: ESQLSource[],
393+
{ sources: availableSources }: ReferenceMaps
394+
) {
390395
const messages: ESQLMessage[] = [];
391-
if (source.incomplete) {
392-
return messages;
396+
397+
const knownIndexNames = [];
398+
const knownIndexPatterns = [];
399+
const unknownIndexNames = [];
400+
const unknownIndexPatterns = [];
401+
402+
for (const source of sources) {
403+
if (source.incomplete) {
404+
return messages;
405+
}
406+
407+
if (source.sourceType === 'index') {
408+
const index = source.index;
409+
const sourceName = source.cluster ? source.name : index?.valueUnquoted;
410+
if (!sourceName) continue;
411+
412+
if (sourceExists(sourceName, availableSources) && !hasWildcard(sourceName)) {
413+
knownIndexNames.push(source);
414+
}
415+
if (sourceExists(sourceName, availableSources) && hasWildcard(sourceName)) {
416+
knownIndexPatterns.push(source);
417+
}
418+
if (!sourceExists(sourceName, availableSources) && !hasWildcard(sourceName)) {
419+
unknownIndexNames.push(source);
420+
}
421+
if (!sourceExists(sourceName, availableSources) && hasWildcard(sourceName)) {
422+
unknownIndexPatterns.push(source);
423+
}
424+
}
393425
}
394426

395-
if (source.sourceType === 'index') {
396-
const index = source.index;
397-
const indexName = source.cluster ? source.name : index?.valueUnquoted;
398-
if (indexName && !sourceExists(indexName, sources)) {
427+
unknownIndexNames.forEach((source) => {
428+
messages.push(
429+
getMessageFromId({
430+
messageId: 'unknownIndex',
431+
values: { name: source.name },
432+
locations: source.location,
433+
})
434+
);
435+
});
436+
437+
if (knownIndexNames.length + unknownIndexNames.length + knownIndexPatterns.length === 0) {
438+
// only if there are no known index names, no known index patterns, and no unknown
439+
// index names do we worry about creating errors for unknown index patterns
440+
unknownIndexPatterns.forEach((source) => {
399441
messages.push(
400442
getMessageFromId({
401443
messageId: 'unknownIndex',
402444
values: { name: source.name },
403445
locations: source.location,
404446
})
405447
);
406-
}
448+
});
407449
}
408450

409451
return messages;

0 commit comments

Comments
 (0)