From 062c0581aeb20bf44682761a03dcb80a2937c404 Mon Sep 17 00:00:00 2001 From: vadimkibana Date: Wed, 30 Oct 2024 21:01:41 +0100 Subject: [PATCH 1/2] support esql paramters in function call names --- 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 | 16 ++- 5 files changed, 249 insertions(+), 61 deletions(-) diff --git a/packages/kbn-esql-ast/src/builder/builder.ts b/packages/kbn-esql-ast/src/builder/builder.ts index 26b64a6312ee4..1abb56893a64d 100644 --- a/packages/kbn-esql-ast/src/builder/builder.ts +++ b/packages/kbn-esql-ast/src/builder/builder.ts @@ -16,10 +16,14 @@ import { ESQLCommand, ESQLCommandOption, ESQLDecimalLiteral, + ESQLIdentifier, ESQLInlineCast, ESQLIntegerLiteral, ESQLList, ESQLLocation, + ESQLNamedParamLiteral, + ESQLParam, + ESQLPositionalParamLiteral, ESQLSource, } from '../types'; import { AstNodeParserFields, AstNodeTemplate, PartialFields } from './types'; @@ -173,4 +177,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 0fffb3a970e4c..005f88eaf66b2 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: ESQLAstItem, diff --git a/packages/kbn-esql-ast/src/parser/walkers.ts b/packages/kbn-esql-ast/src/parser/walkers.ts index df10161f68bf8..b0b84b1e97702 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 0df75ee2e8f24..756b1d2ddf0dd 100644 --- a/packages/kbn-esql-ast/src/types.ts +++ b/packages/kbn-esql-ast/src/types.ts @@ -47,7 +47,7 @@ export type ESQLAstNodeWithChildren = ESQLAstNodeWithArgs | ESQLList; * of the nodes which are plain arrays, all nodes will be *proper* and we can * remove this type. */ -export type ESQLProperNode = ESQLAstExpression | ESQLAstCommand; +export type ESQLProperNode = ESQLAstExpression | ESQLAstCommand | ESQLIdentifier; export interface ESQLLocation { min: number; @@ -132,6 +132,11 @@ export interface ESQLFunction< */ subtype?: Subtype; + /** + * A node representing the function or operator being called. + */ + operator?: ESQLIdentifier | ESQLParamLiteral; + args: ESQLAstItem[]; } @@ -363,6 +368,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 +385,11 @@ export interface ESQLPositionalParamLiteral extends ESQLParamLiteral<'positional value: number; } +export type ESQLParam = + | ESQLUnnamedParamLiteral + | ESQLNamedParamLiteral + | ESQLPositionalParamLiteral; + export interface ESQLMessage { type: 'error' | 'warning'; text: string; From ac4778cfe107f375cbfcda507b594e0b3797344c Mon Sep 17 00:00:00 2001 From: vadimkibana Date: Thu, 31 Oct 2024 11:48:11 +0100 Subject: [PATCH 2/2] fix type error --- packages/kbn-esql-ast/src/types.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/kbn-esql-ast/src/types.ts b/packages/kbn-esql-ast/src/types.ts index 756b1d2ddf0dd..c21636dc9d3b2 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 @@ -47,7 +48,7 @@ export type ESQLAstNodeWithChildren = ESQLAstNodeWithArgs | ESQLList; * of the nodes which are plain arrays, all nodes will be *proper* and we can * remove this type. */ -export type ESQLProperNode = ESQLAstExpression | ESQLAstCommand | ESQLIdentifier; +export type ESQLProperNode = ESQLAstExpression | ESQLAstCommand; export interface ESQLLocation { min: number;