Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ES|QL] Fix wilcard complex scenarios #178938

Merged
merged 4 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 17 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 @@ -398,7 +398,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 @@ -407,6 +407,17 @@ 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') {
// replace 2 ore more consecutive wildcards with a single one
const setOfChars = name.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 @@ -415,6 +426,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 @@ -425,7 +440,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
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,50 @@
"query": "from index*",
"error": false
},
{
"query": "from in*ex*",
"error": false
},
{
"query": "from *n*ex",
"error": false
},
{
"query": "from *n*ex*",
"error": false
},
{
"query": "from i*d*x*",
"error": false
},
{
"query": "from i*d*x",
"error": false
},
{
"query": "from i***x*",
"error": false
},
{
"query": "from i****",
"error": false
},
{
"query": "from i**",
"error": false
},
{
"query": "from index**",
"error": false
},
{
"query": "from *ex",
"error": false
},
{
"query": "from *ex*",
"error": false
},
{
"query": "from in*ex",
"error": false
Expand Down Expand Up @@ -2438,6 +2478,18 @@
"query": "from index | drop s*",
"error": false
},
{
"query": "from index | drop s**Field",
"error": false
},
{
"query": "from index | drop *Field*",
"error": false
},
{
"query": "from index | drop s*F*d",
"error": false
},
{
"query": "from index | drop *Field",
"error": false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,17 @@ describe('validation logic', () => {
]);
testErrorsAndWarnings(`from ind*, other*`, []);
testErrorsAndWarnings(`from index*`, []);
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 @@ -768,6 +778,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