[ES|QL] Update function supports and improve signatures (#166282)

## Summary

Built on top of #166157

Functions in the related PR have been added at lexer level so now the
grammar supports them.
A separate task to detach function definitions from the grammar has been
logged [here](https://github.com/elastic/kibana/issues/166242).

Took also the chance here to define better signatures for functions:


![esql_new_functions](2dc710f5-26fd-41fd-b8a6-3bed48033e7d)


### Checklist

Delete any items that are not applicable to this PR.

- [x] Any text added follows [EUI's writing
guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses
sentence case text and includes [i18n
support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)
- [x]
[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)
was added for features that require explanation or tutorials
- [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
This commit is contained in:
Marco Liberati 2023-09-13 12:40:53 +02:00 committed by GitHub
parent 21d3fc4b6e
commit bc718d84f6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 1256 additions and 775 deletions

View file

@ -166,11 +166,17 @@ MATH_FUNCTION
| S U B S T R I N G | S U B S T R I N G
| T R I M | T R I M
| C O N C A T | C O N C A T
| C O A L E S C E
| G R E A T E S T
| L E F T
| N O W
| R I G H T
| S T A R T S UNDERSCORE W I T H | S T A R T S UNDERSCORE W I T H
| D A T E UNDERSCORE F O R M A T | D A T E UNDERSCORE F O R M A T
| D A T E UNDERSCORE T R U N C | D A T E UNDERSCORE T R U N C
| D A T E UNDERSCORE P A R S E | D A T E UNDERSCORE P A R S E
| A U T O UNDERSCORE B U C K E T | A U T O UNDERSCORE B U C K E T
| D A T E UNDERSCORE E X T R A C T
| I S UNDERSCORE F I N I T E | I S UNDERSCORE F I N I T E
| I S UNDERSCORE I N F I N I T E | I S UNDERSCORE I N F I N I T E
| C A S E | C A S E
@ -194,10 +200,12 @@ MATH_FUNCTION
| T O UNDERSCORE D T | T O UNDERSCORE D T
| T O UNDERSCORE D B L | T O UNDERSCORE D B L
| T O UNDERSCORE D O U B L E | T O UNDERSCORE D O U B L E
| T O UNDERSCORE D E G R E E S
| T O UNDERSCORE I N T | T O UNDERSCORE I N T
| T O UNDERSCORE I N T E G E R | T O UNDERSCORE I N T E G E R
| T O UNDERSCORE L O N G
| T O UNDERSCORE I P | T O UNDERSCORE I P
| T O UNDERSCORE L O N G
| T O UNDERSCORE R A D I A N S
| T O UNDERSCORE V E R S I O N | T O UNDERSCORE V E R S I O N
| T O UNDERSCORE U N S I G N E D UNDERSCORE L O N G | T O UNDERSCORE U N S I G N E D UNDERSCORE L O N G
; ;
@ -212,6 +220,20 @@ UNARY_FUNCTION
| P E R C E N T I L E | P E R C E N T I L E
| M E D I A N | M E D I A N
| M E D I A N UNDERSCORE A B S O L U T E UNDERSCORE D E V I A T I O N | M E D I A N UNDERSCORE A B S O L U T E UNDERSCORE D E V I A T I O N
| A C O S
| A S I N
| A T A N
| A T A N '2'
| C E I L
| C O S
| C O S H
| F L O O R
| L T R I M
| S I N
| S I N H
| S Q R T
| T A N
| T A N H
; ;
WHERE_FUNCTIONS WHERE_FUNCTIONS

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load diff

View file

@ -16,6 +16,48 @@ const examplesLabel = i18n.translate('monaco.esql.autocomplete.examplesLabel', {
defaultMessage: 'Examples:', defaultMessage: 'Examples:',
}); });
/** @internal */
export const buildFunctionDocumentation = (
signatures: Array<{
declaration: string;
examples?: string[];
}>
) => `
---
\
***${declarationLabel}***
${signatures
.map(
({ declaration }) => `
\
- \`\`${declaration}\`\`
\
`
)
.join('\n\n')}
---
${
signatures.some((examples) => examples)
? `\
***${examplesLabel}***
\
${signatures
.filter(({ examples }) => examples)
.map(
({ examples }) => `
${examples!
.map(
(i) => `
- \`\`${i}\`\`)
`
)
.join('')}
`
)}`
: ''
}`;
/** @internal **/ /** @internal **/
export const buildDocumentation = (declaration: string, examples?: string[]) => ` export const buildDocumentation = (declaration: string, examples?: string[]) => `
--- ---

View file

@ -14,6 +14,7 @@ import { getParser, ROOT_STATEMENT } from '../antlr_facade';
import { isDynamicAutocompleteItem } from './dymanic_item'; import { isDynamicAutocompleteItem } from './dymanic_item';
import { getDurationItemsWithQuantifier } from './helpers'; import { getDurationItemsWithQuantifier } from './helpers';
import { mathCommandDefinition } from './autocomplete_definitions/functions_commands';
describe('autocomplete_listener', () => { describe('autocomplete_listener', () => {
const getAutocompleteSuggestions = (text: string) => { const getAutocompleteSuggestions = (text: string) => {
@ -177,34 +178,7 @@ describe('autocomplete_listener', () => {
}); });
describe('eval', () => { describe('eval', () => {
const functionSuggestions = [ const functionSuggestions = mathCommandDefinition.map(({ label }) => String(label));
'round',
'abs',
'pow',
'log10',
'concat',
'substring',
'trim',
'starts_with',
'split',
'to_string',
'to_boolean',
'to_datetime',
'to_double',
'to_integer',
'to_long',
'to_unsigned_long',
'to_ip',
'to_version',
'date_format',
'date_trunc',
'date_parse',
'auto_bucket',
'is_finite',
'is_infinite',
'case',
'length',
];
testSuggestions('from a | eval ', ['var0']); testSuggestions('from a | eval ', ['var0']);
testSuggestions('from a | eval a ', ['=']); testSuggestions('from a | eval a ', ['=']);