Revert "[ES|QL] Add improved support for Elasticsearch sub-types in AST for both validation and autocomplete (#188600)"

This reverts commit d9282a6e6e.
This commit is contained in:
Jonathan Budzenski 2024-07-31 18:37:05 -05:00
parent d9e1223f28
commit 8a60cd4e68
37 changed files with 14365 additions and 30691 deletions

View file

@ -25,7 +25,6 @@ import {
} from '../src/definitions/types';
import { FUNCTION_DESCRIBE_BLOCK_NAME } from '../src/validation/function_describe_block_name';
import { getMaxMinNumberOfParams } from '../src/validation/helpers';
import { ESQL_NUMBER_TYPES, isNumericType, isStringType } from '../src/shared/esql_types';
export const fieldNameFromType = (type: SupportedFieldType) => `${camelCase(type)}Field`;
@ -142,8 +141,8 @@ function generateImplicitDateCastingTestsForFunction(
const allSignaturesWithDateParams = definition.signatures.filter((signature) =>
signature.params.some(
(param, i) =>
(param.type === 'date' || param.type === 'date_period') &&
!definition.signatures.some((def) => isStringType(getParamAtPosition(def, i)?.type)) // don't count parameters that already accept a string
param.type === 'date' &&
!definition.signatures.some((def) => getParamAtPosition(def, i)?.type === 'string') // don't count parameters that already accept a string
)
);
@ -301,8 +300,8 @@ function generateWhereCommandTestsForEvalFunction(
// TODO: not sure why there's this constraint...
const supportedFunction = signatures.some(
({ returnType, params }) =>
[...ESQL_NUMBER_TYPES, 'string'].includes(returnType) &&
params.every(({ type }) => [...ESQL_NUMBER_TYPES, 'string'].includes(type))
['number', 'string'].includes(returnType) &&
params.every(({ type }) => ['number', 'string'].includes(type))
);
if (!supportedFunction) {
@ -312,12 +311,12 @@ function generateWhereCommandTestsForEvalFunction(
const supportedSignatures = signatures.filter(({ returnType }) =>
// TODO — not sure why the tests have this limitation... seems like any type
// that can be part of a boolean expression should be allowed in a where clause
[...ESQL_NUMBER_TYPES, 'string'].includes(returnType)
['number', 'string'].includes(returnType)
);
for (const { params, returnType, ...restSign } of supportedSignatures) {
const correctMapping = getFieldMapping(params);
testCases.set(
`from a_index | where ${!isNumericType(returnType) ? 'length(' : ''}${
`from a_index | where ${returnType !== 'number' ? 'length(' : ''}${
// hijacking a bit this function to produce a function call
getFunctionSignatures(
{
@ -327,7 +326,7 @@ function generateWhereCommandTestsForEvalFunction(
},
{ withTypes: false }
)[0].declaration
}${!isNumericType(returnType) ? ')' : ''} > 0`,
}${returnType !== 'number' ? ')' : ''} > 0`,
[]
);
@ -338,7 +337,7 @@ function generateWhereCommandTestsForEvalFunction(
supportedTypesAndFieldNames
);
testCases.set(
`from a_index | where ${!isNumericType(returnType) ? 'length(' : ''}${
`from a_index | where ${returnType !== 'number' ? 'length(' : ''}${
// hijacking a bit this function to produce a function call
getFunctionSignatures(
{
@ -348,7 +347,7 @@ function generateWhereCommandTestsForEvalFunction(
},
{ withTypes: false }
)[0].declaration
}${!isNumericType(returnType) ? ')' : ''} > 0`,
}${returnType !== 'number' ? ')' : ''} > 0`,
expectedErrors
);
}
@ -358,7 +357,7 @@ function generateWhereCommandTestsForAggFunction(
{ name, alias, signatures, ...defRest }: FunctionDefinition,
testCases: Map<string, string[]>
) {
// statsSignatures.some(({ returnType, params }) => [...ESQL_NUMBER_TYPES].includes(returnType))
// statsSignatures.some(({ returnType, params }) => ['number'].includes(returnType))
for (const { params, ...signRest } of signatures) {
const fieldMapping = getFieldMapping(params);
@ -543,7 +542,7 @@ function generateEvalCommandTestsForEvalFunction(
signatureWithGreatestNumberOfParams.params
).concat({
name: 'extraArg',
type: 'integer',
type: 'number',
});
// get the expected args from the first signature in case of errors
@ -661,7 +660,7 @@ function generateStatsCommandTestsForAggFunction(
testCases.set(`from a_index | stats var = ${correctSignature}`, []);
testCases.set(`from a_index | stats ${correctSignature}`, []);
if (isNumericType(signRest.returnType)) {
if (signRest.returnType === 'number') {
testCases.set(`from a_index | stats var = round(${correctSignature})`, []);
testCases.set(`from a_index | stats round(${correctSignature})`, []);
testCases.set(
@ -714,8 +713,8 @@ function generateStatsCommandTestsForAggFunction(
}
// test only numeric functions for now
if (isNumericType(params[0].type)) {
const nestedBuiltin = 'doubleField / 2';
if (params[0].type === 'number') {
const nestedBuiltin = 'numberField / 2';
const fieldMappingWithNestedBuiltinFunctions = getFieldMapping(params);
fieldMappingWithNestedBuiltinFunctions[0].name = nestedBuiltin;
@ -727,16 +726,16 @@ function generateStatsCommandTestsForAggFunction(
},
{ withTypes: false }
)[0].declaration;
// from a_index | STATS aggFn( doubleField / 2 )
// from a_index | STATS aggFn( numberField / 2 )
testCases.set(`from a_index | stats ${fnSignatureWithBuiltinString}`, []);
testCases.set(`from a_index | stats var0 = ${fnSignatureWithBuiltinString}`, []);
testCases.set(`from a_index | stats avg(doubleField), ${fnSignatureWithBuiltinString}`, []);
testCases.set(`from a_index | stats avg(numberField), ${fnSignatureWithBuiltinString}`, []);
testCases.set(
`from a_index | stats avg(doubleField), var0 = ${fnSignatureWithBuiltinString}`,
`from a_index | stats avg(numberField), var0 = ${fnSignatureWithBuiltinString}`,
[]
);
const nestedEvalAndBuiltin = 'round(doubleField / 2)';
const nestedEvalAndBuiltin = 'round(numberField / 2)';
const fieldMappingWithNestedEvalAndBuiltinFunctions = getFieldMapping(params);
fieldMappingWithNestedBuiltinFunctions[0].name = nestedEvalAndBuiltin;
@ -748,18 +747,18 @@ function generateStatsCommandTestsForAggFunction(
},
{ withTypes: false }
)[0].declaration;
// from a_index | STATS aggFn( round(doubleField / 2) )
// from a_index | STATS aggFn( round(numberField / 2) )
testCases.set(`from a_index | stats ${fnSignatureWithEvalAndBuiltinString}`, []);
testCases.set(`from a_index | stats var0 = ${fnSignatureWithEvalAndBuiltinString}`, []);
testCases.set(
`from a_index | stats avg(doubleField), ${fnSignatureWithEvalAndBuiltinString}`,
`from a_index | stats avg(numberField), ${fnSignatureWithEvalAndBuiltinString}`,
[]
);
testCases.set(
`from a_index | stats avg(doubleField), var0 = ${fnSignatureWithEvalAndBuiltinString}`,
`from a_index | stats avg(numberField), var0 = ${fnSignatureWithEvalAndBuiltinString}`,
[]
);
// from a_index | STATS aggFn(round(doubleField / 2) ) BY round(doubleField / 2)
// from a_index | STATS aggFn(round(numberField / 2) ) BY round(numberField / 2)
testCases.set(
`from a_index | stats ${fnSignatureWithEvalAndBuiltinString} by ${nestedEvalAndBuiltin}`,
[]
@ -769,19 +768,19 @@ function generateStatsCommandTestsForAggFunction(
[]
);
testCases.set(
`from a_index | stats avg(doubleField), ${fnSignatureWithEvalAndBuiltinString} by ${nestedEvalAndBuiltin}, ipField`,
`from a_index | stats avg(numberField), ${fnSignatureWithEvalAndBuiltinString} by ${nestedEvalAndBuiltin}, ipField`,
[]
);
testCases.set(
`from a_index | stats avg(doubleField), var0 = ${fnSignatureWithEvalAndBuiltinString} by var1 = ${nestedEvalAndBuiltin}, ipField`,
`from a_index | stats avg(numberField), var0 = ${fnSignatureWithEvalAndBuiltinString} by var1 = ${nestedEvalAndBuiltin}, ipField`,
[]
);
testCases.set(
`from a_index | stats avg(doubleField), ${fnSignatureWithEvalAndBuiltinString} by ${nestedEvalAndBuiltin}, ${nestedBuiltin}`,
`from a_index | stats avg(numberField), ${fnSignatureWithEvalAndBuiltinString} by ${nestedEvalAndBuiltin}, ${nestedBuiltin}`,
[]
);
testCases.set(
`from a_index | stats avg(doubleField), var0 = ${fnSignatureWithEvalAndBuiltinString} by var1 = ${nestedEvalAndBuiltin}, ${nestedBuiltin}`,
`from a_index | stats avg(numberField), var0 = ${fnSignatureWithEvalAndBuiltinString} by var1 = ${nestedEvalAndBuiltin}, ${nestedBuiltin}`,
[]
);
}
@ -799,7 +798,7 @@ function generateStatsCommandTestsForAggFunction(
.filter(({ constantOnly }) => !constantOnly)
.map(
(_) =>
`Aggregate function's parameters must be an attribute, literal or a non-aggregation function; found [avg(doubleField)] of type [double]`
`Aggregate function's parameters must be an attribute, literal or a non-aggregation function; found [avg(numberField)] of type [number]`
);
testCases.set(
`from a_index | stats var = ${
@ -966,17 +965,9 @@ function generateSortCommandTestsForAggFunction(
const generateSortCommandTestsForGroupingFunction = generateSortCommandTestsForAggFunction;
const fieldTypesToConstants: Record<SupportedFieldType, string> = {
text: '"a"',
keyword: '"a"',
double: '5.5',
integer: '5',
long: '5',
unsigned_long: '5',
counter_integer: '5',
counter_long: '5',
counter_double: '5.5',
date: 'to_datetime("2021-01-01T00:00:00Z")',
date_period: 'to_date_period("2021-01-01/2021-01-02")',
string: '"a"',
number: '5',
date: 'now()',
boolean: 'true',
version: 'to_version("1.0.0")',
ip: 'to_ip("127.0.0.1")',
@ -1012,8 +1003,8 @@ function prepareNestedFunction(fnSignature: FunctionDefinition): string {
}
const toAvgSignature = statsAggregationFunctionDefinitions.find(({ name }) => name === 'avg')!;
const toInteger = evalFunctionDefinitions.find(({ name }) => name === 'to_integer')!;
const toDoubleSignature = evalFunctionDefinitions.find(({ name }) => name === 'to_double')!;
const toStringSignature = evalFunctionDefinitions.find(({ name }) => name === 'to_string')!;
const toDateSignature = evalFunctionDefinitions.find(({ name }) => name === 'to_datetime')!;
const toBooleanSignature = evalFunctionDefinitions.find(({ name }) => name === 'to_boolean')!;
@ -1028,12 +1019,10 @@ const toCartesianShapeSignature = evalFunctionDefinitions.find(
)!;
const toVersionSignature = evalFunctionDefinitions.find(({ name }) => name === 'to_version')!;
// We don't have full list for long, unsigned_long, etc.
const nestedFunctions: Record<SupportedFieldType, string> = {
double: prepareNestedFunction(toDoubleSignature),
integer: prepareNestedFunction(toInteger),
text: prepareNestedFunction(toStringSignature),
keyword: prepareNestedFunction(toStringSignature),
number: prepareNestedFunction(toInteger),
string: prepareNestedFunction(toStringSignature),
date: prepareNestedFunction(toDateSignature),
boolean: prepareNestedFunction(toBooleanSignature),
ip: prepareNestedFunction(toIpSignature),
version: prepareNestedFunction(toVersionSignature),
@ -1041,8 +1030,6 @@ const nestedFunctions: Record<SupportedFieldType, string> = {
geo_shape: prepareNestedFunction(toGeoShapeSignature),
cartesian_point: prepareNestedFunction(toCartesianPointSignature),
cartesian_shape: prepareNestedFunction(toCartesianShapeSignature),
// @ts-expect-error
datetime: prepareNestedFunction(toDateSignature),
};
function getFieldName(
@ -1099,7 +1086,6 @@ function getFieldMapping(
number: '5',
date: 'now()',
};
return params.map(({ name: _name, type, constantOnly, literalOptions, ...rest }) => {
const typeString: string = type;
if (isSupportedFieldType(typeString)) {
@ -1138,7 +1124,7 @@ function getFieldMapping(
...rest,
};
}
return { name: 'textField', type, ...rest };
return { name: 'stringField', type, ...rest };
});
}
@ -1239,12 +1225,8 @@ function generateIncorrectlyTypedParameters(
}
const fieldName = wrongFieldMapping[i].name;
if (
fieldName === 'doubleField' &&
signatures.every(
(signature) =>
getParamAtPosition(signature, i)?.type !== 'keyword' ||
getParamAtPosition(signature, i)?.type !== 'text'
)
fieldName === 'numberField' &&
signatures.every((signature) => getParamAtPosition(signature, i)?.type !== 'string')
) {
return;
}