[KQL] Use cache and other performance improvements (#93319) (#93973)

* [KQL] Use cache and other performance improvements

* Fix test

* Fix jest tests

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Lukas Olson 2021-03-08 12:32:01 -07:00 committed by GitHub
parent 2d60be9340
commit f4fae28bda
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 846 additions and 196 deletions

File diff suppressed because it is too large Load diff

View file

@ -29,7 +29,7 @@ OrQuery
= &{ return errorOnLuceneSyntax; } LuceneQuery
/ head:AndQuery tail:(Or query:AndQuery { return query; })+ {
const nodes = [head, ...tail];
const cursor = nodes.find(node => node.type === 'cursor');
const cursor = parseCursor && nodes.find(node => node.type === 'cursor');
if (cursor) return cursor;
return buildFunctionNode('or', nodes);
}
@ -38,7 +38,7 @@ OrQuery
AndQuery
= head:NotQuery tail:(And query:NotQuery { return query; })+ {
const nodes = [head, ...tail];
const cursor = nodes.find(node => node.type === 'cursor');
const cursor = parseCursor && nodes.find(node => node.type === 'cursor');
if (cursor) return cursor;
return buildFunctionNode('and', nodes);
}
@ -143,7 +143,7 @@ ListOfValues
OrListOfValues
= head:AndListOfValues tail:(Or partial:AndListOfValues { return partial; })+ {
const nodes = [head, ...tail];
const cursor = nodes.find(node => node.type === 'cursor');
const cursor = parseCursor && nodes.find(node => node.type === 'cursor');
if (cursor) {
return {
...cursor,
@ -157,7 +157,7 @@ OrListOfValues
AndListOfValues
= head:NotListOfValues tail:(And partial:NotListOfValues { return partial; })+ {
const nodes = [head, ...tail];
const cursor = nodes.find(node => node.type === 'cursor');
const cursor = parseCursor && nodes.find(node => node.type === 'cursor');
if (cursor) {
return {
...cursor,
@ -213,7 +213,7 @@ Literal "literal"
= QuotedString / UnquotedLiteral
QuotedString
= '"' prefix:QuotedCharacter* cursor:Cursor suffix:QuotedCharacter* '"' {
= &{ return parseCursor; } '"' prefix:QuotedCharacter* cursor:Cursor suffix:QuotedCharacter* '"' {
const { start, end } = location();
return {
type: 'cursor',
@ -234,7 +234,7 @@ QuotedCharacter
/ !Cursor char:[^"] { return char; }
UnquotedLiteral
= prefix:UnquotedCharacter* cursor:Cursor suffix:UnquotedCharacter* {
= &{ return parseCursor; } prefix:UnquotedCharacter* cursor:Cursor suffix:UnquotedCharacter* {
const { start, end } = location();
return {
type: 'cursor',
@ -266,7 +266,7 @@ Wildcard
= '*' { return wildcardSymbol; }
OptionalSpace
= prefix:Space* cursor:Cursor suffix:Space* {
= &{ return parseCursor; } prefix:Space* cursor:Cursor suffix:Space* {
const { start, end } = location();
return {
type: 'cursor',

View file

@ -66,17 +66,13 @@ describe('kql syntax errors', () => {
it('should throw an error for unescaped quotes in a quoted string', () => {
expect(() => {
fromKueryExpression('foo:"ba "r"');
}).toThrow(
'Expected AND, OR, end of input, whitespace but "r" found.\n' + 'foo:"ba "r"\n' + '---------^'
);
}).toThrow('Expected AND, OR, end of input but "r" found.\n' + 'foo:"ba "r"\n' + '---------^');
});
it('should throw an error for unescaped special characters in literals', () => {
expect(() => {
fromKueryExpression('foo:ba:r');
}).toThrow(
'Expected AND, OR, end of input, whitespace but ":" found.\n' + 'foo:ba:r\n' + '------^'
);
}).toThrow('Expected AND, OR, end of input but ":" found.\n' + 'foo:ba:r\n' + '------^');
});
it('should throw an error for range queries missing a value', () => {

View file

@ -12,6 +12,7 @@ module.exports = {
dest: 'src/plugins/data/common/es_query/kuery/ast/_generated_/kuery.js',
options: {
allowedStartRules: ['start', 'Literal'],
cache: true,
},
},
timelion_chain: {

View file

@ -286,9 +286,8 @@ export default function ({ getService }: FtrProviderContext) {
expect(resp.body).to.eql({
error: 'Bad Request',
message:
'KQLSyntaxError: Expected AND, OR, end of input, ' +
'whitespace but "<" found.\ndashboard.attributes.title:foo' +
'<invalid\n------------------------------^: Bad Request',
'KQLSyntaxError: Expected AND, OR, end of input but "<" found.\ndashboard.' +
'attributes.title:foo<invalid\n------------------------------^: Bad Request',
statusCode: 400,
});
}));