[ES|QL] fixes getIndexPatternFromESQLQuery for comma separated indices (#169562)

## Summary

Comma separated source indices work as expected with ES|QL query, but
`getIndexPatternFromESQLQuery` does not parses it correctly. This small
PR is an attempt to fix this behavior

### Before

```ts
      const idxPattern7 = getIndexPatternFromESQLQuery('from foo-1, foo-2 | limit 2');
      expect(idxPattern7).toBe('foo-1,');
```

### After
```ts
      const idxPattern7 = getIndexPatternFromESQLQuery('from foo-1, foo-2 | limit 2');
      expect(idxPattern7).toBe('foo-1, foo-2');
```

### Checklist

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

Co-authored-by: Stratoula Kalafateli <efstratia.kalafateli@elastic.co>
This commit is contained in:
Vitalii Dmyterko 2023-10-24 11:13:48 +01:00 committed by GitHub
parent 2b91c31fd0
commit 7112690e8e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 2 deletions

View file

@ -101,6 +101,18 @@ describe('sql query helpers', () => {
const idxPattern5 = getIndexPatternFromESQLQuery('from foo | limit 2');
expect(idxPattern5).toBe('foo');
const idxPattern6 = getIndexPatternFromESQLQuery('from foo-1,foo-2 | limit 2');
expect(idxPattern6).toBe('foo-1,foo-2');
const idxPattern7 = getIndexPatternFromESQLQuery('from foo-1, foo-2 | limit 2');
expect(idxPattern7).toBe('foo-1, foo-2');
const idxPattern8 = getIndexPatternFromESQLQuery('FROM foo-1, foo-2');
expect(idxPattern8).toBe('foo-1, foo-2');
const idxPattern9 = getIndexPatternFromESQLQuery('FROM foo-1, foo-2 [metadata _id]');
expect(idxPattern9).toBe('foo-1, foo-2');
});
});
});

View file

@ -59,10 +59,10 @@ export function getIndexPatternFromESQLQuery(esql?: string): string {
}
const parsedString = esql?.replaceAll('`', '');
// case insensitive match for the index pattern
const regex = new RegExp(/FROM\s+([\w*-.!@$^()~;]+)/, 'i');
const regex = new RegExp(/FROM\s+([\w*-.!@$^()~;\s]+)/, 'i');
const matches = parsedString?.match(regex);
if (matches) {
return matches[1];
return matches[1]?.trim();
}
return '';
}