Skip to content

Commit

Permalink
Revert "[ES|QL] Add improved support for Elasticsearch sub-types in A…
Browse files Browse the repository at this point in the history
…ST for both validation and autocomplete (elastic#188600)"

This reverts commit d9282a6.
  • Loading branch information
jbudz committed Jul 31, 2024
1 parent d9e1223 commit 8a60cd4
Show file tree
Hide file tree
Showing 37 changed files with 15,269 additions and 31,595 deletions.
2 changes: 1 addition & 1 deletion packages/kbn-esql-ast/src/ast_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export class AstListener implements ESQLParserListener {
const command = createCommand('limit', ctx);
this.ast.push(command);
if (ctx.getToken(esql_parser.INTEGER_LITERAL, 0)) {
const literal = createLiteral('integer', ctx.INTEGER_LITERAL());
const literal = createLiteral('number', ctx.INTEGER_LITERAL());
if (literal) {
command.args.push(literal);
}
Expand Down
23 changes: 7 additions & 16 deletions packages/kbn-esql-ast/src/ast_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ import type {
ESQLCommandMode,
ESQLInlineCast,
ESQLUnknownItem,
ESQLNumericLiteralType,
FunctionSubtype,
ESQLNumericLiteral,
} from './types';

export function nonNullable<T>(v: T): v is NonNullable<T> {
Expand Down Expand Up @@ -89,14 +87,11 @@ export function createList(ctx: ParserRuleContext, values: ESQLLiteral[]): ESQLL
};
}

export function createNumericLiteral(
ctx: DecimalValueContext | IntegerValueContext,
literalType: ESQLNumericLiteralType
): ESQLLiteral {
export function createNumericLiteral(ctx: DecimalValueContext | IntegerValueContext): ESQLLiteral {
const text = ctx.getText();
return {
type: 'literal',
literalType,
literalType: 'number',
text,
name: text,
value: Number(text),
Expand All @@ -105,13 +100,10 @@ export function createNumericLiteral(
};
}

export function createFakeMultiplyLiteral(
ctx: ArithmeticUnaryContext,
literalType: ESQLNumericLiteralType
): ESQLLiteral {
export function createFakeMultiplyLiteral(ctx: ArithmeticUnaryContext): ESQLLiteral {
return {
type: 'literal',
literalType,
literalType: 'number',
text: ctx.getText(),
name: ctx.getText(),
value: ctx.PLUS() ? 1 : -1,
Expand Down Expand Up @@ -166,21 +158,20 @@ export function createLiteral(
location: getPosition(node.symbol),
incomplete: isMissingText(text),
};
if (type === 'decimal' || type === 'integer') {
if (type === 'number') {
return {
...partialLiteral,
literalType: type,
value: Number(text),
paramType: 'number',
} as ESQLNumericLiteral<'decimal'> | ESQLNumericLiteral<'integer'>;
};
} else if (type === 'param') {
throw new Error('Should never happen');
}
return {
...partialLiteral,
literalType: type,
value: text,
} as ESQLLiteral;
};
}

export function createTimeUnit(ctx: QualifiedIntegerLiteralContext): ESQLTimeInterval {
Expand Down
19 changes: 5 additions & 14 deletions packages/kbn-esql-ast/src/ast_walker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ import {
createUnknownItem,
} from './ast_helpers';
import { getPosition } from './ast_position_utils';
import {
import type {
ESQLLiteral,
ESQLColumn,
ESQLFunction,
Expand Down Expand Up @@ -289,7 +289,7 @@ function visitOperatorExpression(
const arg = visitOperatorExpression(ctx.operatorExpression());
// this is a number sign thing
const fn = createFunction('*', ctx, undefined, 'binary-expression');
fn.args.push(createFakeMultiplyLiteral(ctx, 'integer'));
fn.args.push(createFakeMultiplyLiteral(ctx));
if (arg) {
fn.args.push(arg);
}
Expand Down Expand Up @@ -328,21 +328,16 @@ function getConstant(ctx: ConstantContext): ESQLAstItem {
// e.g. 1 year, 15 months
return createTimeUnit(ctx);
}

// Decimal type covers multiple ES|QL types: long, double, etc.
if (ctx instanceof DecimalLiteralContext) {
return createNumericLiteral(ctx.decimalValue(), 'decimal');
return createNumericLiteral(ctx.decimalValue());
}

// Integer type encompasses integer
if (ctx instanceof IntegerLiteralContext) {
return createNumericLiteral(ctx.integerValue(), 'integer');
return createNumericLiteral(ctx.integerValue());
}
if (ctx instanceof BooleanLiteralContext) {
return getBooleanValue(ctx);
}
if (ctx instanceof StringLiteralContext) {
// String literal covers multiple ES|QL types: text and keyword types
return createLiteral('string', ctx.string_().QUOTED_STRING());
}
if (
Expand All @@ -351,18 +346,14 @@ function getConstant(ctx: ConstantContext): ESQLAstItem {
ctx instanceof StringArrayLiteralContext
) {
const values: ESQLLiteral[] = [];

for (const numericValue of ctx.getTypedRuleContexts(NumericValueContext)) {
const isDecimal =
numericValue.decimalValue() !== null && numericValue.decimalValue() !== undefined;
const value = numericValue.decimalValue() || numericValue.integerValue();
values.push(createNumericLiteral(value!, isDecimal ? 'decimal' : 'integer'));
values.push(createNumericLiteral(value!));
}
for (const booleanValue of ctx.getTypedRuleContexts(BooleanValueContext)) {
values.push(getBooleanValue(booleanValue)!);
}
for (const string of ctx.getTypedRuleContexts(StringContext)) {
// String literal covers multiple ES|QL types: text and keyword types
const literal = createLiteral('string', string.QUOTED_STRING());
if (literal) {
values.push(literal);
Expand Down
17 changes: 3 additions & 14 deletions packages/kbn-esql-ast/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,30 +179,19 @@ export interface ESQLList extends ESQLAstBaseItem {
values: ESQLLiteral[];
}

export type ESQLNumericLiteralType = 'decimal' | 'integer';

export type ESQLLiteral =
| ESQLDecimalLiteral
| ESQLIntegerLiteral
| ESQLNumberLiteral
| ESQLBooleanLiteral
| ESQLNullLiteral
| ESQLStringLiteral
| ESQLParamLiteral<string>;

// Exporting here to prevent TypeScript error TS4058
// Return type of exported function has or is using name 'ESQLNumericLiteral' from external module
// @internal
export interface ESQLNumericLiteral<T extends ESQLNumericLiteralType> extends ESQLAstBaseItem {
export interface ESQLNumberLiteral extends ESQLAstBaseItem {
type: 'literal';
literalType: T;
literalType: 'number';
value: number;
}
// We cast anything as decimal (e.g. 32.12) as generic decimal numeric type here
// @internal
export type ESQLDecimalLiteral = ESQLNumericLiteral<'decimal'>;

// @internal
export type ESQLIntegerLiteral = ESQLNumericLiteral<'integer'>;

// @internal
export interface ESQLBooleanLiteral extends ESQLAstBaseItem {
Expand Down
28 changes: 14 additions & 14 deletions packages/kbn-esql-ast/src/walker/walker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ describe('structurally can walk all nodes', () => {
expect(columns).toMatchObject([
{
type: 'literal',
literalType: 'integer',
literalType: 'number',
name: '123',
},
{
Expand Down Expand Up @@ -244,7 +244,7 @@ describe('structurally can walk all nodes', () => {
expect(columns).toMatchObject([
{
type: 'literal',
literalType: 'integer',
literalType: 'number',
name: '1',
},
{
Expand All @@ -264,7 +264,7 @@ describe('structurally can walk all nodes', () => {
},
{
type: 'literal',
literalType: 'decimal',
literalType: 'number',
name: '3.14',
},
]);
Expand All @@ -288,12 +288,12 @@ describe('structurally can walk all nodes', () => {
values: [
{
type: 'literal',
literalType: 'integer',
literalType: 'number',
name: '1',
},
{
type: 'literal',
literalType: 'integer',
literalType: 'number',
name: '2',
},
],
Expand All @@ -318,12 +318,12 @@ describe('structurally can walk all nodes', () => {
values: [
{
type: 'literal',
literalType: 'integer',
literalType: 'number',
name: '1',
},
{
type: 'literal',
literalType: 'integer',
literalType: 'number',
name: '2',
},
],
Expand All @@ -333,7 +333,7 @@ describe('structurally can walk all nodes', () => {
values: [
{
type: 'literal',
literalType: 'decimal',
literalType: 'number',
name: '3.3',
},
],
Expand All @@ -342,17 +342,17 @@ describe('structurally can walk all nodes', () => {
expect(literals).toMatchObject([
{
type: 'literal',
literalType: 'integer',
literalType: 'number',
name: '1',
},
{
type: 'literal',
literalType: 'integer',
literalType: 'number',
name: '2',
},
{
type: 'literal',
literalType: 'decimal',
literalType: 'number',
name: '3.3',
},
]);
Expand Down Expand Up @@ -511,7 +511,7 @@ describe('structurally can walk all nodes', () => {

describe('cast expression', () => {
test('can visit cast expression', () => {
const query = 'FROM index | STATS a = 123::integer';
const query = 'FROM index | STATS a = 123::number';
const { ast } = getAstAndSyntaxErrors(query);

const casts: ESQLInlineCast[] = [];
Expand All @@ -523,10 +523,10 @@ describe('structurally can walk all nodes', () => {
expect(casts).toMatchObject([
{
type: 'inlineCast',
castType: 'integer',
castType: 'number',
value: {
type: 'literal',
literalType: 'integer',
literalType: 'number',
value: 123,
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { join } from 'path';
import _ from 'lodash';
import type { RecursivePartial } from '@kbn/utility-types';
import { FunctionDefinition } from '../src/definitions/types';
import { esqlToKibanaType } from '../src/shared/esql_to_kibana_type';

const aliasTable: Record<string, string[]> = {
to_version: ['to_ver'],
Expand Down Expand Up @@ -239,10 +240,10 @@ function getFunctionDefinition(ESFunctionDefinition: Record<string, any>): Funct
...signature,
params: signature.params.map((param: any) => ({
...param,
type: param.type,
type: esqlToKibanaType(param.type),
description: undefined,
})),
returnType: signature.returnType,
returnType: esqlToKibanaType(signature.returnType),
variadic: undefined, // we don't support variadic property
minParams: signature.variadic
? signature.params.filter((param: any) => !param.optional).length
Expand Down
Loading

0 comments on commit 8a60cd4

Please sign in to comment.