[8.13] [ES|QL] Fix wilcard complex scenarios (#178938) (#179150)

# Backport

This will backport the following commits from `main` to `8.13`:
- [[ES|QL] Fix wilcard complex scenarios
(#178938)](https://github.com/elastic/kibana/pull/178938)

<!--- Backport version: 8.9.8 -->

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

<!--BACKPORT [{"author":{"name":"Marco
Liberati","email":"dej611@users.noreply.github.com"},"sourceCommit":{"committedDate":"2024-03-21T09:19:57Z","message":"[ES|QL]
Fix wilcard complex scenarios (#178938)\n\n## Summary\r\n\r\nImproves
the wildcard validation logic to handle multiple occurrencies\r\nof
wildcards.\r\n\r\n\r\n### Checklist\r\n\r\nDelete any items that are not
applicable to this PR.\r\n\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","sha":"ad272c2acf90598c12873d9cd731c793951f9dd2","branchLabelMapping":{"^v8.14.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","Team:Visualizations","Feature:ES|QL","v8.14.0","v8.13.1"],"number":178938,"url":"https://github.com/elastic/kibana/pull/178938","mergeCommit":{"message":"[ES|QL]
Fix wilcard complex scenarios (#178938)\n\n## Summary\r\n\r\nImproves
the wildcard validation logic to handle multiple occurrencies\r\nof
wildcards.\r\n\r\n\r\n### Checklist\r\n\r\nDelete any items that are not
applicable to this PR.\r\n\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","sha":"ad272c2acf90598c12873d9cd731c793951f9dd2"}},"sourceBranch":"main","suggestedTargetBranches":["8.13"],"targetPullRequestStates":[{"branch":"main","label":"v8.14.0","labelRegex":"^v8.14.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/178938","number":178938,"mergeCommit":{"message":"[ES|QL]
Fix wilcard complex scenarios (#178938)\n\n## Summary\r\n\r\nImproves
the wildcard validation logic to handle multiple occurrencies\r\nof
wildcards.\r\n\r\n\r\n### Checklist\r\n\r\nDelete any items that are not
applicable to this PR.\r\n\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","sha":"ad272c2acf90598c12873d9cd731c793951f9dd2"}},{"branch":"8.13","label":"v8.13.1","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->
This commit is contained in:
Marco Liberati 2024-03-22 10:29:31 +01:00 committed by GitHub
parent 0621fe6be4
commit bd79cf4a15
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 33 additions and 2 deletions

View file

@ -399,7 +399,7 @@ function fuzzySearch(fuzzyName: string, resources: IterableIterator<string>) {
}
}
function getMatcher(name: string, position: 'start' | 'end' | 'middle') {
function getMatcher(name: string, position: 'start' | 'end' | 'middle' | 'multiple-within') {
if (position === 'start') {
const prefix = name.substring(1);
return (resource: string) => resource.endsWith(prefix);
@ -408,6 +408,19 @@ function getMatcher(name: string, position: 'start' | 'end' | 'middle') {
const prefix = name.substring(0, name.length - 1);
return (resource: string) => resource.startsWith(prefix);
}
if (position === 'multiple-within') {
// make sure to remove the * at the beginning of the name if present
const safeName = name.startsWith('*') ? name.slice(1) : name;
// replace 2 ore more consecutive wildcards with a single one
const setOfChars = safeName.replace(/\*{2+}/g, '*').split('*');
return (resource: string) => {
let index = -1;
return setOfChars.every((char) => {
index = resource.indexOf(char, index + 1);
return index !== -1;
});
};
}
const [prefix, postFix] = name.split('*');
return (resource: string) => resource.startsWith(prefix) && resource.endsWith(postFix);
}
@ -416,6 +429,10 @@ function getWildcardPosition(name: string) {
if (!hasWildcard(name)) {
return 'none';
}
const wildCardCount = name.match(/\*/g)!.length;
if (wildCardCount > 1) {
return 'multiple-within';
}
if (name.startsWith('*')) {
return 'start';
}
@ -426,7 +443,7 @@ function getWildcardPosition(name: string) {
}
export function hasWildcard(name: string) {
return name.includes('*');
return /\*/.test(name);
}
export function isVariable(
column: ESQLRealField | ESQLVariable | undefined

View file

@ -394,7 +394,18 @@ describe('validation logic', () => {
]);
testErrorsAndWarnings(`from ind*, other*`, []);
testErrorsAndWarnings(`from index*`, []);
testErrorsAndWarnings(`from *a_i*dex*`, []);
testErrorsAndWarnings(`from in*ex*`, []);
testErrorsAndWarnings(`from *n*ex`, []);
testErrorsAndWarnings(`from *n*ex*`, []);
testErrorsAndWarnings(`from i*d*x*`, []);
testErrorsAndWarnings(`from i*d*x`, []);
testErrorsAndWarnings(`from i***x*`, []);
testErrorsAndWarnings(`from i****`, []);
testErrorsAndWarnings(`from i**`, []);
testErrorsAndWarnings(`from index**`, []);
testErrorsAndWarnings(`from *ex`, []);
testErrorsAndWarnings(`from *ex*`, []);
testErrorsAndWarnings(`from in*ex`, []);
testErrorsAndWarnings(`from ind*ex`, []);
testErrorsAndWarnings(`from indexes*`, ['Unknown index [indexes*]']);
@ -730,6 +741,9 @@ describe('validation logic', () => {
]);
testErrorsAndWarnings('from index | drop `any#Char$Field`', []);
testErrorsAndWarnings('from index | drop s*', []);
testErrorsAndWarnings('from index | drop s**Field', []);
testErrorsAndWarnings('from index | drop *Field*', []);
testErrorsAndWarnings('from index | drop s*F*d', []);
testErrorsAndWarnings('from index | drop *Field', []);
testErrorsAndWarnings('from index | drop s*Field', []);
testErrorsAndWarnings('from index | drop string*Field', []);