Skip to content

Commit

Permalink
[ES|QL] Fix wilcard complex scenarios (elastic#178938)
Browse files Browse the repository at this point in the history
Improves the wildcard validation logic to handle multiple occurrencies
of wildcards.

Delete any items that are not applicable to this PR.

- [x] [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

(cherry picked from commit ad272c2)
  • Loading branch information
dej611 committed Mar 21, 2024
1 parent 371b0a8 commit e5fcf9d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
21 changes: 19 additions & 2 deletions packages/kbn-monaco/src/esql/lib/ast/shared/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
Expand All @@ -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';
}
Expand All @@ -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
Expand Down
14 changes: 14 additions & 0 deletions packages/kbn-monaco/src/esql/lib/ast/validation/validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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*]']);
Expand Down Expand Up @@ -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', []);
Expand Down

0 comments on commit e5fcf9d

Please sign in to comment.