Skip to content

Commit

Permalink
Support lists
Browse files Browse the repository at this point in the history
  • Loading branch information
drewdaemon committed Oct 16, 2024
1 parent c6ed42b commit 3a63f94
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 20 deletions.
23 changes: 4 additions & 19 deletions packages/kbn-esql-validation-autocomplete/src/shared/esql_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,36 +27,21 @@ 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' &&
[...ESQL_NUMBER_TYPES, 'decimal'].includes(type as (typeof ESQL_NUMBER_TYPES)[number])
);
}

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand All @@ -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)',
Expand Down Expand Up @@ -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);
}
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 3a63f94

Please sign in to comment.