-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1550c90
commit 188d668
Showing
16 changed files
with
428 additions
and
61 deletions.
There are no files selected for viewing
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
167 changes: 167 additions & 0 deletions
167
src/platform/packages/shared/kbn-esql-ast/src/parser/__tests__/stats.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,167 @@ | ||
/* | ||
* 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
import { EsqlQuery } from '../../query'; | ||
|
||
describe('STATS', () => { | ||
describe('correctly formatted', () => { | ||
it('a simple single aggregate expression', () => { | ||
const src = ` | ||
FROM employees | ||
| STATS 123 | ||
| WHERE still_hired == true | ||
`; | ||
const query = EsqlQuery.fromSrc(src); | ||
|
||
expect(query.errors.length).toBe(0); | ||
expect(query.ast.commands).toMatchObject([ | ||
{}, | ||
{ | ||
type: 'command', | ||
name: 'stats', | ||
args: [ | ||
{ | ||
type: 'field', | ||
column: { | ||
type: 'column', | ||
args: [ | ||
{ | ||
type: 'identifier', | ||
name: '123', | ||
}, | ||
], | ||
}, | ||
value: { | ||
type: 'literal', | ||
value: 123, | ||
}, | ||
}, | ||
], | ||
}, | ||
{}, | ||
]); | ||
}); | ||
|
||
it('aggregation function with escaped values', () => { | ||
const src = ` | ||
FROM employees | ||
| STATS 123, agg("salary") | ||
| WHERE still_hired == true | ||
`; | ||
const query = EsqlQuery.fromSrc(src); | ||
|
||
expect(query.errors.length).toBe(0); | ||
expect(query.ast.commands).toMatchObject([ | ||
{}, | ||
{ | ||
type: 'command', | ||
name: 'stats', | ||
args: [ | ||
{ | ||
type: 'field', | ||
column: { | ||
type: 'column', | ||
args: [ | ||
{ | ||
type: 'identifier', | ||
name: '123', | ||
}, | ||
], | ||
}, | ||
value: { | ||
type: 'literal', | ||
value: 123, | ||
}, | ||
}, | ||
{ | ||
type: 'field', | ||
column: { | ||
type: 'column', | ||
args: [ | ||
{ | ||
type: 'identifier', | ||
name: 'agg("salary")', | ||
}, | ||
], | ||
}, | ||
value: { | ||
type: 'function', | ||
name: 'agg', | ||
}, | ||
}, | ||
], | ||
}, | ||
{}, | ||
]); | ||
}); | ||
|
||
it('field column name defined', () => { | ||
const src = ` | ||
FROM employees | ||
| STATS my_field = agg("salary") | ||
| WHERE still_hired == true | ||
`; | ||
const query = EsqlQuery.fromSrc(src); | ||
|
||
expect(query.errors.length).toBe(0); | ||
expect(query.ast.commands).toMatchObject([ | ||
{}, | ||
{ | ||
type: 'command', | ||
name: 'stats', | ||
args: [ | ||
{ | ||
type: 'field', | ||
column: { | ||
type: 'column', | ||
args: [ | ||
{ | ||
type: 'identifier', | ||
name: 'my_field', | ||
}, | ||
], | ||
}, | ||
value: { | ||
type: 'function', | ||
name: 'agg', | ||
}, | ||
}, | ||
], | ||
}, | ||
{}, | ||
]); | ||
}); | ||
|
||
it('parses BY clause', () => { | ||
const src = ` | ||
FROM employees | ||
| STATS my_field = agg("salary") BY department | ||
| WHERE still_hired == true | ||
`; | ||
const query = EsqlQuery.fromSrc(src); | ||
|
||
expect(query.errors.length).toBe(0); | ||
expect(query.ast.commands).toMatchObject([ | ||
{}, | ||
{ | ||
type: 'command', | ||
name: 'stats', | ||
args: [ | ||
{}, | ||
{ | ||
type: 'option', | ||
name: 'by', | ||
}, | ||
], | ||
}, | ||
{}, | ||
]); | ||
}); | ||
}); | ||
}); |
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
42 changes: 42 additions & 0 deletions
42
src/platform/packages/shared/kbn-esql-ast/src/parser/factories/stats.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,42 @@ | ||
/* | ||
* 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
import { AggFieldContext, StatsCommandContext } from '../../antlr/esql_parser'; | ||
import { ESQLCommand, ESQLField } from '../../types'; | ||
import { createCommand } from '../factories'; | ||
import { createField, visitByOption } from '../walkers'; | ||
|
||
const createAggField = (ctx: AggFieldContext, src: string): ESQLField => { | ||
const fieldCtx = ctx.field(); | ||
const field = createField(fieldCtx, src); | ||
|
||
return field; | ||
}; | ||
|
||
export const createStatsCommand = (ctx: StatsCommandContext, src: string): ESQLCommand<'stats'> => { | ||
const command = createCommand('stats', ctx); | ||
|
||
if (ctx._stats) { | ||
const fields = ctx.aggFields(); | ||
|
||
for (const fieldCtx of fields.aggField_list()) { | ||
const node = createAggField(fieldCtx, src); | ||
|
||
command.args.push(node); | ||
} | ||
} | ||
|
||
if (ctx._grouping) { | ||
const options = visitByOption(ctx, ctx.fields()); | ||
|
||
command.args.push(...options); | ||
} | ||
|
||
return command; | ||
}; |
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.