From 750452eb1189b021f7c538532ff17975cb7d78d5 Mon Sep 17 00:00:00 2001 From: Vadim Kibana <82822460+vadimkibana@users.noreply.github.com> Date: Thu, 31 Oct 2024 15:27:54 +0100 Subject: [PATCH] [ES|QL] Support ES|QL paramters in function names (#198486) ## Summary Partially addresses https://github.com/elastic/kibana/issues/198251?reload=1?reload=1 - Improves for `param` node parsing inside `function` call nodes. - Adds the new `identifier` node type. - The `function` AST nodes now have `operator` property, which contains either a `param` or `identifier` node. ### Checklist - [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#_add_your_labels) --- packages/kbn-esql-ast/src/builder/builder.ts | 65 +++++++++++++ .../src/parser/__tests__/function.test.ts | 97 +++++++++++++++++++ packages/kbn-esql-ast/src/parser/factories.ts | 68 ++++++++++++- packages/kbn-esql-ast/src/parser/walkers.ts | 64 +----------- packages/kbn-esql-ast/src/types.ts | 15 +++ 5 files changed, 249 insertions(+), 60 deletions(-) diff --git a/packages/kbn-esql-ast/src/builder/builder.ts b/packages/kbn-esql-ast/src/builder/builder.ts index d033e177bd4b5..894ab99e5b3e8 100644 --- a/packages/kbn-esql-ast/src/builder/builder.ts +++ b/packages/kbn-esql-ast/src/builder/builder.ts @@ -18,10 +18,14 @@ import { ESQLCommand, ESQLCommandOption, ESQLDecimalLiteral, + ESQLIdentifier, ESQLInlineCast, ESQLIntegerLiteral, ESQLList, ESQLLocation, + ESQLNamedParamLiteral, + ESQLParam, + ESQLPositionalParamLiteral, ESQLOrderExpression, ESQLSource, } from '../types'; @@ -190,4 +194,65 @@ export namespace Builder { }; } } + + export const identifier = ( + template: AstNodeTemplate, + fromParser?: Partial + ): ESQLIdentifier => { + return { + ...template, + ...Builder.parserFields(fromParser), + type: 'identifier', + }; + }; + + export namespace param { + export const unnamed = (fromParser?: Partial): ESQLParam => { + const node = { + ...Builder.parserFields(fromParser), + name: '', + value: '', + paramType: 'unnamed', + type: 'literal', + literalType: 'param', + }; + + return node as ESQLParam; + }; + + export const named = ( + template: Omit, 'name' | 'literalType' | 'paramType'>, + fromParser?: Partial + ): ESQLNamedParamLiteral => { + const node: ESQLNamedParamLiteral = { + ...template, + ...Builder.parserFields(fromParser), + name: '', + type: 'literal', + literalType: 'param', + paramType: 'named', + }; + + return node; + }; + + export const positional = ( + template: Omit< + AstNodeTemplate, + 'name' | 'literalType' | 'paramType' + >, + fromParser?: Partial + ): ESQLPositionalParamLiteral => { + const node: ESQLPositionalParamLiteral = { + ...template, + ...Builder.parserFields(fromParser), + name: '', + type: 'literal', + literalType: 'param', + paramType: 'positional', + }; + + return node; + }; + } } diff --git a/packages/kbn-esql-ast/src/parser/__tests__/function.test.ts b/packages/kbn-esql-ast/src/parser/__tests__/function.test.ts index 9d822f78f9333..d05ed36204b17 100644 --- a/packages/kbn-esql-ast/src/parser/__tests__/function.test.ts +++ b/packages/kbn-esql-ast/src/parser/__tests__/function.test.ts @@ -69,6 +69,103 @@ describe('function AST nodes', () => { }, ]); }); + + it('parses out function name as identifier node', () => { + 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', + operator: { + type: 'identifier', + name: 'fn', + }, + }, + ], + }, + ]); + }); + + it('parses out function name as named param', () => { + const query = 'ROW ?insert_here(1, 2, 3)'; + const { ast, errors } = parse(query); + + expect(errors.length).toBe(0); + expect(ast).toMatchObject([ + { + type: 'command', + name: 'row', + args: [ + { + type: 'function', + name: '?insert_here', + operator: { + type: 'literal', + literalType: 'param', + paramType: 'named', + value: 'insert_here', + }, + }, + ], + }, + ]); + }); + + it('parses out function name as unnamed param', () => { + const query = 'ROW ?(1, 2, 3)'; + const { ast, errors } = parse(query); + + expect(errors.length).toBe(0); + expect(ast).toMatchObject([ + { + type: 'command', + name: 'row', + args: [ + { + type: 'function', + name: '?', + operator: { + type: 'literal', + literalType: 'param', + paramType: 'unnamed', + }, + }, + ], + }, + ]); + }); + + it('parses out function name as positional param', () => { + const query = 'ROW ?30035(1, 2, 3)'; + const { ast, errors } = parse(query); + + expect(errors.length).toBe(0); + expect(ast).toMatchObject([ + { + type: 'command', + name: 'row', + args: [ + { + type: 'function', + name: '?30035', + operator: { + type: 'literal', + literalType: 'param', + paramType: 'positional', + value: 30035, + }, + }, + ], + }, + ]); + }); }); describe('"unary-expression"', () => { diff --git a/packages/kbn-esql-ast/src/parser/factories.ts b/packages/kbn-esql-ast/src/parser/factories.ts index 246a62747ee6e..b575447f7e744 100644 --- a/packages/kbn-esql-ast/src/parser/factories.ts +++ b/packages/kbn-esql-ast/src/parser/factories.ts @@ -11,7 +11,13 @@ * In case of changes in the grammar, this script should be updated: esql_update_ast_script.js */ -import type { Token, ParserRuleContext, TerminalNode, RecognitionException } from 'antlr4'; +import type { + Token, + ParserRuleContext, + TerminalNode, + RecognitionException, + ParseTree, +} from 'antlr4'; import { IndexPatternContext, QualifiedNameContext, @@ -21,6 +27,10 @@ import { type IntegerValueContext, type QualifiedIntegerLiteralContext, QualifiedNamePatternContext, + FunctionContext, + IdentifierContext, + InputParamContext, + InputNamedOrPositionalParamContext, } from '../antlr/esql_parser'; import { DOUBLE_TICKS_REGEX, SINGLE_BACKTICK, TICKS_REGEX } from './constants'; import type { @@ -42,6 +52,8 @@ import type { ESQLNumericLiteral, ESQLOrderExpression, InlineCastingType, + ESQLFunctionCallExpression, + ESQLIdentifier, } from '../types'; import { parseIdentifier, getPosition } from './helpers'; import { Builder, type AstNodeParserFields } from '../builder'; @@ -201,6 +213,60 @@ export function createFunction( return node; } +export const createFunctionCall = (ctx: FunctionContext): ESQLFunctionCallExpression => { + const functionExpressionCtx = ctx.functionExpression(); + const functionName = functionExpressionCtx.functionName(); + const node: ESQLFunctionCallExpression = { + type: 'function', + subtype: 'variadic-call', + name: functionName.getText().toLowerCase(), + text: ctx.getText(), + location: getPosition(ctx.start, ctx.stop), + args: [], + incomplete: Boolean(ctx.exception), + }; + + const identifierOrParameter = functionName.identifierOrParameter(); + if (identifierOrParameter) { + const identifier = identifierOrParameter.identifier(); + if (identifier) { + node.operator = createIdentifier(identifier); + } else { + const parameter = identifierOrParameter.parameter(); + if (parameter) { + node.operator = createParam(parameter); + } + } + } + + return node; +}; + +const createIdentifier = (identifier: IdentifierContext): ESQLIdentifier => { + return Builder.identifier( + { name: identifier.getText().toLowerCase() }, + createParserFields(identifier) + ); +}; + +export const createParam = (ctx: ParseTree) => { + if (ctx instanceof InputParamContext) { + return Builder.param.unnamed(createParserFields(ctx)); + } else if (ctx instanceof InputNamedOrPositionalParamContext) { + const text = ctx.getText(); + const value = text.slice(1); + const valueAsNumber = Number(value); + const isPositional = String(valueAsNumber) === value; + const parserFields = createParserFields(ctx); + + if (isPositional) { + return Builder.param.positional({ value: valueAsNumber }, parserFields); + } else { + return Builder.param.named({ value }, parserFields); + } + } +}; + export const createOrderExpression = ( ctx: ParserRuleContext, arg: ESQLColumn, diff --git a/packages/kbn-esql-ast/src/parser/walkers.ts b/packages/kbn-esql-ast/src/parser/walkers.ts index 268c90417078b..60d69a17bb1c7 100644 --- a/packages/kbn-esql-ast/src/parser/walkers.ts +++ b/packages/kbn-esql-ast/src/parser/walkers.ts @@ -60,8 +60,6 @@ import { type ValueExpressionContext, ValueExpressionDefaultContext, InlineCastContext, - InputNamedOrPositionalParamContext, - InputParamContext, IndexPatternContext, InlinestatsCommandContext, } from '../antlr/esql_parser'; @@ -86,8 +84,9 @@ import { createInlineCast, createUnknownItem, createOrderExpression, + createFunctionCall, + createParam, } from './factories'; -import { getPosition } from './helpers'; import { ESQLLiteral, @@ -97,9 +96,6 @@ import { ESQLAstItem, ESQLAstField, ESQLInlineCast, - ESQLUnnamedParamLiteral, - ESQLPositionalParamLiteral, - ESQLNamedParamLiteral, ESQLOrderExpression, } from '../types'; import { firstItem, lastItem } from '../visitor/utils'; @@ -390,50 +386,8 @@ function getConstant(ctx: ConstantContext): ESQLAstItem { const values: ESQLLiteral[] = []; for (const child of ctx.children) { - if (child instanceof InputParamContext) { - const literal: ESQLUnnamedParamLiteral = { - type: 'literal', - literalType: 'param', - paramType: 'unnamed', - text: ctx.getText(), - name: '', - value: '', - location: getPosition(ctx.start, ctx.stop), - incomplete: Boolean(ctx.exception), - }; - values.push(literal); - } else if (child instanceof InputNamedOrPositionalParamContext) { - const text = child.getText(); - const value = text.slice(1); - const valueAsNumber = Number(value); - const isPositional = String(valueAsNumber) === value; - - if (isPositional) { - const literal: ESQLPositionalParamLiteral = { - type: 'literal', - literalType: 'param', - paramType: 'positional', - value: valueAsNumber, - text, - name: '', - location: getPosition(ctx.start, ctx.stop), - incomplete: Boolean(ctx.exception), - }; - values.push(literal); - } else { - const literal: ESQLNamedParamLiteral = { - type: 'literal', - literalType: 'param', - paramType: 'named', - value, - text, - name: '', - location: getPosition(ctx.start, ctx.stop), - incomplete: Boolean(ctx.exception), - }; - values.push(literal); - } - } + const param = createParam(child); + if (param) values.push(param); } return values; @@ -478,15 +432,7 @@ export function visitPrimaryExpression(ctx: PrimaryExpressionContext): ESQLAstIt } if (ctx instanceof FunctionContext) { const functionExpressionCtx = ctx.functionExpression(); - const functionNameContext = functionExpressionCtx.functionName().MATCH() - ? functionExpressionCtx.functionName().MATCH() - : functionExpressionCtx.functionName().identifierOrParameter(); - const fn = createFunction( - functionNameContext.getText().toLowerCase(), - ctx, - undefined, - 'variadic-call' - ); + const fn = createFunctionCall(ctx); const asteriskArg = functionExpressionCtx.ASTERISK() ? createColumnStar(functionExpressionCtx.ASTERISK()!) : undefined; diff --git a/packages/kbn-esql-ast/src/types.ts b/packages/kbn-esql-ast/src/types.ts index eabdefa5a401a..ea76fc3e0b9a4 100644 --- a/packages/kbn-esql-ast/src/types.ts +++ b/packages/kbn-esql-ast/src/types.ts @@ -26,6 +26,7 @@ export type ESQLSingleAstItem = | ESQLTimeInterval | ESQLList | ESQLLiteral + | ESQLIdentifier | ESQLCommandMode | ESQLInlineCast | ESQLOrderExpression @@ -132,6 +133,11 @@ export interface ESQLFunction< */ subtype?: Subtype; + /** + * A node representing the function or operator being called. + */ + operator?: ESQLIdentifier | ESQLParamLiteral; + args: ESQLAstItem[]; } @@ -363,6 +369,10 @@ export interface ESQLNamedParamLiteral extends ESQLParamLiteral<'named'> { value: string; } +export interface ESQLIdentifier extends ESQLAstBaseItem { + type: 'identifier'; +} + export const isESQLNamedParamLiteral = (node: ESQLAstItem): node is ESQLNamedParamLiteral => isESQLAstBaseItem(node) && (node as ESQLNamedParamLiteral).literalType === 'param' && @@ -376,6 +386,11 @@ export interface ESQLPositionalParamLiteral extends ESQLParamLiteral<'positional value: number; } +export type ESQLParam = + | ESQLUnnamedParamLiteral + | ESQLNamedParamLiteral + | ESQLPositionalParamLiteral; + export interface ESQLMessage { type: 'error' | 'warning'; text: string;