-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ES|QL] Function AST node subtypes (#189268)
## Summary Closes #189259 - Introduces `subtype` property for *function* AST node types. This allows to discriminate between real functions an various expression types. ### 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#kibana-release-notes-process)
- Loading branch information
1 parent
5817a9c
commit 6c093b6
Showing
6 changed files
with
329 additions
and
27 deletions.
There are no files selected for viewing
227 changes: 227 additions & 0 deletions
227
packages/kbn-esql-ast/src/__tests__/ast.function.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,227 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { getAstAndSyntaxErrors as parse } from '../ast_parser'; | ||
import { Walker } from '../walker'; | ||
|
||
describe('function AST nodes', () => { | ||
describe('"variadic-call"', () => { | ||
it('function call with a single argument', () => { | ||
const query = 'ROW fn(1)'; | ||
const { ast, errors } = parse(query); | ||
|
||
expect(errors.length).toBe(0); | ||
expect(ast).toMatchObject([ | ||
{ | ||
type: 'command', | ||
name: 'row', | ||
args: [ | ||
{ | ||
type: 'function', | ||
name: 'fn', | ||
args: [ | ||
{ | ||
type: 'literal', | ||
value: 1, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
]); | ||
}); | ||
|
||
it('function call with multiple argument', () => { | ||
const query = 'ROW fn(1, 2, 3)'; | ||
const { ast, errors } = parse(query); | ||
|
||
expect(errors.length).toBe(0); | ||
expect(ast).toMatchObject([ | ||
{ | ||
type: 'command', | ||
name: 'row', | ||
args: [ | ||
{ | ||
type: 'function', | ||
name: 'fn', | ||
args: [ | ||
{ | ||
type: 'literal', | ||
value: 1, | ||
}, | ||
{ | ||
type: 'literal', | ||
value: 2, | ||
}, | ||
{ | ||
type: 'literal', | ||
value: 3, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
]); | ||
}); | ||
}); | ||
|
||
describe('"unary-expression"', () => { | ||
it('logical NOT', () => { | ||
const query = 'FROM a | STATS NOT b'; | ||
const { ast, errors } = parse(query); | ||
const fn = Walker.findFunction(ast, ({ name }) => name === 'not'); | ||
|
||
expect(errors.length).toBe(0); | ||
expect(fn).toMatchObject({ | ||
type: 'function', | ||
subtype: 'unary-expression', | ||
name: 'not', | ||
args: [expect.any(Object)], | ||
}); | ||
}); | ||
|
||
// Currently arithmetic unary expressions, like "-x", are transformed to | ||
// binary expressions: "-1 * x". Enable this test once unary expressions | ||
// are supported. | ||
it.skip('arithmetic', () => { | ||
const query = 'FROM a | STATS -a'; | ||
const { ast, errors } = parse(query); | ||
const fn = Walker.findFunction(ast, ({ name }) => name === '*'); | ||
|
||
expect(errors.length).toBe(0); | ||
expect(fn).toMatchObject({ | ||
type: 'function', | ||
subtype: 'unary-expression', | ||
name: '-', | ||
args: [expect.any(Object)], | ||
}); | ||
}); | ||
}); | ||
|
||
describe('"postfix-unary-expression"', () => { | ||
it('IS [NOT] NULL', () => { | ||
const query = 'FROM a | STATS a IS NOT NULL'; | ||
const { ast, errors } = parse(query); | ||
const fn = Walker.findFunction(ast, ({ name }) => name === 'is not null'); | ||
|
||
expect(errors.length).toBe(0); | ||
expect(fn).toMatchObject({ | ||
type: 'function', | ||
subtype: 'postfix-unary-expression', | ||
name: 'is not null', | ||
args: [expect.any(Object)], | ||
}); | ||
}); | ||
}); | ||
|
||
describe('"binary-expression"', () => { | ||
it('arithmetic and logical operations', () => { | ||
const ops = ['+', '-', '*', '/', '%', 'and', 'or', '>', '>=', '<', '<=', '==', '!=']; | ||
|
||
for (const op of ops) { | ||
const query = `ROW 1 ${op} 2`; | ||
const { ast, errors } = parse(query); | ||
|
||
expect(errors.length).toBe(0); | ||
expect(ast).toMatchObject([ | ||
{ | ||
type: 'command', | ||
name: 'row', | ||
args: [ | ||
{ | ||
type: 'function', | ||
subtype: 'binary-expression', | ||
name: op, | ||
args: [ | ||
{ | ||
type: 'literal', | ||
value: 1, | ||
}, | ||
{ | ||
type: 'literal', | ||
value: 2, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
]); | ||
} | ||
}); | ||
|
||
it('logical IN', () => { | ||
const query = 'FROM a | STATS a IN (1, 2, 3)'; | ||
const { ast, errors } = parse(query); | ||
const fn = Walker.findFunction(ast, ({ name }) => name === 'in'); | ||
|
||
expect(errors.length).toBe(0); | ||
expect(fn).toMatchObject({ | ||
type: 'function', | ||
subtype: 'binary-expression', | ||
name: 'in', | ||
args: [expect.any(Object), expect.any(Object)], | ||
}); | ||
}); | ||
|
||
it('logical NOT IN', () => { | ||
const query = 'FROM a | STATS a NOT IN (1, 2, 3)'; | ||
const { ast, errors } = parse(query); | ||
const fn = Walker.findFunction(ast, ({ name }) => name === 'not_in'); | ||
|
||
expect(errors.length).toBe(0); | ||
expect(fn).toMatchObject({ | ||
type: 'function', | ||
subtype: 'binary-expression', | ||
name: 'not_in', | ||
args: [expect.any(Object), expect.any(Object)], | ||
}); | ||
}); | ||
|
||
it('regex expression', () => { | ||
const query = 'FROM a | STATS a LIKE "adsf"'; | ||
const { ast, errors } = parse(query); | ||
const fn = Walker.findFunction(ast, ({ name }) => name === 'like'); | ||
|
||
expect(errors.length).toBe(0); | ||
expect(fn).toMatchObject({ | ||
type: 'function', | ||
subtype: 'binary-expression', | ||
name: 'like', | ||
args: [expect.any(Object), expect.any(Object)], | ||
}); | ||
}); | ||
|
||
it('assignment in ENRICH .. WITH clause', () => { | ||
const query = 'FROM a | ENRICH b ON c WITH d = e'; | ||
const { ast, errors } = parse(query); | ||
const fn = Walker.findFunction(ast, ({ name }) => name === '='); | ||
|
||
expect(errors.length).toBe(0); | ||
expect(fn).toMatchObject({ | ||
type: 'function', | ||
subtype: 'binary-expression', | ||
name: '=', | ||
args: [expect.any(Object), expect.any(Object)], | ||
}); | ||
}); | ||
|
||
it('assignment in STATS', () => { | ||
const query = 'FROM a | STATS b = c'; | ||
const { ast, errors } = parse(query); | ||
const fn = Walker.findFunction(ast, ({ name }) => name === '='); | ||
|
||
expect(errors.length).toBe(0); | ||
expect(fn).toMatchObject({ | ||
type: 'function', | ||
subtype: 'binary-expression', | ||
name: '=', | ||
args: [expect.any(Object), expect.any(Object)], | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.