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] Open bracket parsing bug #196241

Merged
merged 8 commits into from
Oct 18, 2024
Merged
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* []';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test worked by accident in the past. The parser threw an error here, not because of the query syntax error, but because of an internal error in the parser that crashed parsing.

const typeErrorCausingQuery = 'from []';

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

Expand Down