From 3a63f94180aca327faea68dbbd7b01b45ad7c651 Mon Sep 17 00:00:00 2001 From: Drew Tate Date: Wed, 16 Oct 2024 17:46:20 -0600 Subject: [PATCH] Support lists --- .../src/shared/esql_types.ts | 23 +++--------- .../src/shared/helpers.test.ts | 35 ++++++++++++++++++- .../src/shared/helpers.ts | 4 +++ 3 files changed, 42 insertions(+), 20 deletions(-) diff --git a/packages/kbn-esql-validation-autocomplete/src/shared/esql_types.ts b/packages/kbn-esql-validation-autocomplete/src/shared/esql_types.ts index 4df08ac1dfb3e..dbf45437dce92 100644 --- a/packages/kbn-esql-validation-autocomplete/src/shared/esql_types.ts +++ b/packages/kbn-esql-validation-autocomplete/src/shared/esql_types.ts @@ -7,7 +7,7 @@ * License v3.0 only", or the "Server Side Public License, v 1". */ -import { ESQLDecimalLiteral, ESQLLiteral, ESQLNumericLiteralType } from '@kbn/esql-ast/src/types'; +import { ESQLLiteral, ESQLNumericLiteralType } from '@kbn/esql-ast/src/types'; import { FunctionParameterType } from '../definitions/types'; export const ESQL_COMMON_NUMERIC_TYPES = ['double', 'long', 'integer'] as const; @@ -27,15 +27,6 @@ export const ESQL_NUMBER_TYPES = [ export const ESQL_STRING_TYPES = ['keyword', 'text'] as const; export const ESQL_DATE_TYPES = ['datetime', 'date_period'] as const; -/** - * - * @param type - * @returns - */ -export function isStringType(type: unknown) { - return typeof type === 'string' && ['keyword', 'text'].includes(type); -} - export function isNumericType(type: unknown): type is ESQLNumericLiteralType { return ( typeof type === 'string' && @@ -43,20 +34,14 @@ export function isNumericType(type: unknown): type is ESQLNumericLiteralType { ); } -export function isNumericDecimalType(type: unknown): type is ESQLDecimalLiteral { - return ( - typeof type === 'string' && - ESQL_NUMERIC_DECIMAL_TYPES.includes(type as (typeof ESQL_NUMERIC_DECIMAL_TYPES)[number]) - ); -} - /** * Compares two types, taking into account literal types * @TODO strengthen typing here (remove `string`) + * @TODO — clean up time duration and date period */ export const compareTypesWithLiterals = ( - a: ESQLLiteral['literalType'] | FunctionParameterType | 'timeInterval', - b: ESQLLiteral['literalType'] | FunctionParameterType | 'timeInterval' + a: ESQLLiteral['literalType'] | FunctionParameterType | 'timeInterval' | string, + b: ESQLLiteral['literalType'] | FunctionParameterType | 'timeInterval' | string ) => { if (a === b) { return true; diff --git a/packages/kbn-esql-validation-autocomplete/src/shared/helpers.test.ts b/packages/kbn-esql-validation-autocomplete/src/shared/helpers.test.ts index 1d7d3d36be3c5..0078e0fac119c 100644 --- a/packages/kbn-esql-validation-autocomplete/src/shared/helpers.test.ts +++ b/packages/kbn-esql-validation-autocomplete/src/shared/helpers.test.ts @@ -101,6 +101,7 @@ describe('getExpressionType', () => { const cases: Array<{ expression: string; expectedType: SupportedDataType }> = [ { expectedType: 'boolean', expression: '"true"::bool' }, { expectedType: 'boolean', expression: '"false"::boolean' }, + { expectedType: 'boolean', expression: '"false"::BooLEAN' }, { expectedType: 'cartesian_point', expression: '""::cartesian_point' }, { expectedType: 'cartesian_shape', expression: '""::cartesian_shape' }, { expectedType: 'date_nanos', expression: '1::date_nanos' }, @@ -119,6 +120,7 @@ describe('getExpressionType', () => { { expectedType: 'time_duration', expression: '1::time_duration' }, { expectedType: 'unsigned_long', expression: '1::unsigned_long' }, { expectedType: 'version', expression: '"1.2.3"::version' }, + { expectedType: 'version', expression: '"1.2.3"::VERSION' }, ]; test.each(cases)( 'detects a casted literal of type $expectedType ($expression)', @@ -307,5 +309,36 @@ describe('getExpressionType', () => { }); }); - describe('lists', () => {}); + describe('lists', () => { + const cases: Array<{ expression: string; expectedType: SupportedDataType | 'unknown' }> = [ + { + expression: '["foo", "bar"]', + expectedType: 'keyword', + }, + { + expression: '[1, 2]', + expectedType: 'integer', + }, + { + expression: '[1., 2.]', + expectedType: 'double', + }, + { + expression: '[null, null, null]', + expectedType: 'null', + }, + { + expression: '[true, false]', + expectedType: 'boolean', + }, + ]; + + test.each(cases)( + 'reports the type of $expression as $expectedType', + ({ expression, expectedType }) => { + const ast = getASTForExpression(expression); + expect(getExpressionType(ast)).toBe(expectedType); + } + ); + }); }); diff --git a/packages/kbn-esql-validation-autocomplete/src/shared/helpers.ts b/packages/kbn-esql-validation-autocomplete/src/shared/helpers.ts index ebb9c60e2b83a..31c2c01a11404 100644 --- a/packages/kbn-esql-validation-autocomplete/src/shared/helpers.ts +++ b/packages/kbn-esql-validation-autocomplete/src/shared/helpers.ts @@ -805,6 +805,10 @@ export function getExpressionType( return column.type; } + if (root.type === 'list') { + return getExpressionType(root.values[0], fields, variables); + } + if (isFunctionItem(root)) { const fnDefinition = getFunctionDefinition(root.name); if (!fnDefinition) {