Skip to content

Commit

Permalink
[ES|QL] Open bracket parsing bug (elastic#196241)
Browse files Browse the repository at this point in the history
## Summary

Closes elastic#191683

Gracefully handles open square brackets in `FROM` command.


### 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

### For maintainers

- [x] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#_add_your_labels)
  • Loading branch information
vadimkibana authored Oct 18, 2024
1 parent 37b57a3 commit 3849d99
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
16 changes: 16 additions & 0 deletions packages/kbn-esql-ast/src/parser/__tests__/from.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,5 +187,21 @@ describe('FROM', () => {

expect(errors.length > 0).toBe(true);
});

it('when open square bracket "[" is entered', () => {
const text = 'FROM kibana_sample_data_logs [';
const { errors } = parse(text);

expect(errors.length > 0).toBe(true);
expect(errors[0].message.toLowerCase().includes('metadata')).toBe(true);
});

it('when close square bracket "]" is entered', () => {
const text = 'FROM kibana_sample_data_logs []';
const { errors } = parse(text);

expect(errors.length > 0).toBe(true);
expect(errors[0].message.toLowerCase().includes('metadata')).toBe(true);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export class ESQLAstBuilderListener implements ESQLParserListener {
const metadataContext = ctx.metadata();
const metadataContent =
metadataContext?.deprecated_metadata()?.metadataOption() || metadataContext?.metadataOption();
if (metadataContent) {
if (metadataContent && metadataContent.METADATA()) {
const option = createOption(
metadataContent.METADATA().getText().toLowerCase(),
metadataContent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,16 @@ describe('useEsqlIndex', () => {
expect(result.current).toEqual([]);
});

it('returns indices which appear in source before syntax error', async () => {
const typeErrorCausingQuery = 'from auditbeat* [, auditbeat2*';

const { result } = renderHook(() => useEsqlIndex(typeErrorCausingQuery, 'esql'));

expect(result.current).toEqual(['auditbeat*']);
});

it('should return empty array if invalid query is causing a TypeError in ES|QL parser', async () => {
const typeErrorCausingQuery = 'from auditbeat* []';
const typeErrorCausingQuery = 'from []';

const { result } = renderHook(() => useEsqlIndex(typeErrorCausingQuery, 'esql'));

Expand Down

0 comments on commit 3849d99

Please sign in to comment.