diff --git a/packages/kbn-esql-validation-autocomplete/scripts/generate_function_definitions.ts b/packages/kbn-esql-validation-autocomplete/scripts/generate_function_definitions.ts index d50d7cc903fce..aa92c7bd024d5 100644 --- a/packages/kbn-esql-validation-autocomplete/scripts/generate_function_definitions.ts +++ b/packages/kbn-esql-validation-autocomplete/scripts/generate_function_definitions.ts @@ -145,6 +145,39 @@ const dateDiffOptions = [ 'ns', ]; +const dateExtractOptions = [ + 'ALIGNED_DAY_OF_WEEK_IN_MONTH', + 'ALIGNED_DAY_OF_WEEK_IN_YEAR', + 'ALIGNED_WEEK_OF_MONTH', + 'ALIGNED_WEEK_OF_YEAR', + 'AMPM_OF_DAY', + 'CLOCK_HOUR_OF_AMPM', + 'CLOCK_HOUR_OF_DAY', + 'DAY_OF_MONTH', + 'DAY_OF_WEEK', + 'DAY_OF_YEAR', + 'EPOCH_DAY', + 'ERA', + 'HOUR_OF_AMPM', + 'HOUR_OF_DAY', + 'INSTANT_SECONDS', + 'MICRO_OF_DAY', + 'MICRO_OF_SECOND', + 'MILLI_OF_DAY', + 'MILLI_OF_SECOND', + 'MINUTE_OF_DAY', + 'MINUTE_OF_HOUR', + 'MONTH_OF_YEAR', + 'NANO_OF_DAY', + 'NANO_OF_SECOND', + 'OFFSET_SECONDS', + 'PROLEPTIC_MONTH', + 'SECOND_OF_DAY', + 'SECOND_OF_MINUTE', + 'YEAR', + 'YEAR_OF_ERA', +]; + /** * Enrichments for function definitions * @@ -168,8 +201,7 @@ const functionEnrichments: Record> date_extract: { signatures: [ { - // override the first param as type chrono_literal - params: [{ type: 'chrono_literal' }], + params: [{ literalOptions: dateExtractOptions }], }, ], }, diff --git a/packages/kbn-esql-validation-autocomplete/scripts/generate_function_validation_tests.ts b/packages/kbn-esql-validation-autocomplete/scripts/generate_function_validation_tests.ts index 0ace35433b67e..8d6394fe96af6 100644 --- a/packages/kbn-esql-validation-autocomplete/scripts/generate_function_validation_tests.ts +++ b/packages/kbn-esql-validation-autocomplete/scripts/generate_function_validation_tests.ts @@ -15,7 +15,7 @@ import { statsAggregationFunctionDefinitions } from '../src/definitions/aggs'; import { evalFunctionDefinitions } from '../src/definitions/functions'; import { groupingFunctionDefinitions } from '../src/definitions/grouping'; import { getFunctionSignatures } from '../src/definitions/helpers'; -import { timeUnits, chronoLiterals } from '../src/definitions/literals'; +import { timeUnits } from '../src/definitions/literals'; import { nonNullable } from '../src/shared/helpers'; import { SupportedFieldType, @@ -1045,14 +1045,10 @@ function getFieldName( } const literals = { - chrono_literal: chronoLiterals[0].name, time_literal: timeUnits[0], }; -function getLiteralType(typeString: 'chrono_literal' | 'time_literal') { - if (typeString === 'chrono_literal') { - return literals[typeString]; - } +function getLiteralType(typeString: 'time_literal') { return `1 ${literals[typeString]}`; } @@ -1116,7 +1112,7 @@ function getFieldMapping( } if (/literal$/.test(typeString) && useLiterals) { return { - name: getLiteralType(typeString as 'chrono_literal' | 'time_literal'), + name: getLiteralType(typeString as 'time_literal'), type, ...rest, }; diff --git a/packages/kbn-esql-validation-autocomplete/src/autocomplete/autocomplete.test.ts b/packages/kbn-esql-validation-autocomplete/src/autocomplete/autocomplete.test.ts index 6f0562d7f118e..57e66a825c37c 100644 --- a/packages/kbn-esql-validation-autocomplete/src/autocomplete/autocomplete.test.ts +++ b/packages/kbn-esql-validation-autocomplete/src/autocomplete/autocomplete.test.ts @@ -10,7 +10,7 @@ import { suggest } from './autocomplete'; import { evalFunctionDefinitions } from '../definitions/functions'; import { builtinFunctions } from '../definitions/builtin'; import { statsAggregationFunctionDefinitions } from '../definitions/aggs'; -import { chronoLiterals, timeUnitsToSuggest } from '../definitions/literals'; +import { timeUnitsToSuggest } from '../definitions/literals'; import { commandDefinitions } from '../definitions/commands'; import { getUnitDuration, TRIGGER_SUGGESTION_COMMAND } from './factories'; import { camelCase, partition } from 'lodash'; @@ -207,9 +207,6 @@ function getLiteralsByType(_type: string | string[]) { // return only singular return timeUnitsToSuggest.map(({ name }) => `1 ${name}`).filter((s) => !/s$/.test(s)); } - if (type.includes('chrono_literal')) { - return chronoLiterals.map(({ name }) => name); - } return []; } diff --git a/packages/kbn-esql-validation-autocomplete/src/autocomplete/factories.ts b/packages/kbn-esql-validation-autocomplete/src/autocomplete/factories.ts index 53e65c2f95aba..a5f21d426c5fc 100644 --- a/packages/kbn-esql-validation-autocomplete/src/autocomplete/factories.ts +++ b/packages/kbn-esql-validation-autocomplete/src/autocomplete/factories.ts @@ -12,7 +12,7 @@ import { groupingFunctionDefinitions } from '../definitions/grouping'; import { statsAggregationFunctionDefinitions } from '../definitions/aggs'; import { evalFunctionDefinitions } from '../definitions/functions'; import { getFunctionSignatures, getCommandSignature } from '../definitions/helpers'; -import { chronoLiterals, timeUnitsToSuggest } from '../definitions/literals'; +import { timeUnitsToSuggest } from '../definitions/literals'; import { FunctionDefinition, CommandDefinition, @@ -336,9 +336,6 @@ export function getCompatibleLiterals(commandName: string, types: string[], name if (types.includes('time_literal_unit')) { suggestions.push(...buildConstantsDefinitions(timeUnitsToSuggest.map(({ name }) => name))); // i.e. year, month, ... } - if (types.includes('chrono_literal')) { - suggestions.push(...buildConstantsDefinitions(chronoLiterals.map(({ name }) => name))); // i.e. EPOC_DAY, ... - } if (types.includes('string')) { if (names) { const index = types.indexOf('string'); diff --git a/packages/kbn-esql-validation-autocomplete/src/definitions/functions.ts b/packages/kbn-esql-validation-autocomplete/src/definitions/functions.ts index ceb5789e0dd39..87c9f82360b44 100644 --- a/packages/kbn-esql-validation-autocomplete/src/definitions/functions.ts +++ b/packages/kbn-esql-validation-autocomplete/src/definitions/functions.ts @@ -668,8 +668,40 @@ const dateExtractDefinition: FunctionDefinition = { params: [ { name: 'datePart', - type: 'chrono_literal', + type: 'string', optional: false, + literalOptions: [ + 'ALIGNED_DAY_OF_WEEK_IN_MONTH', + 'ALIGNED_DAY_OF_WEEK_IN_YEAR', + 'ALIGNED_WEEK_OF_MONTH', + 'ALIGNED_WEEK_OF_YEAR', + 'AMPM_OF_DAY', + 'CLOCK_HOUR_OF_AMPM', + 'CLOCK_HOUR_OF_DAY', + 'DAY_OF_MONTH', + 'DAY_OF_WEEK', + 'DAY_OF_YEAR', + 'EPOCH_DAY', + 'ERA', + 'HOUR_OF_AMPM', + 'HOUR_OF_DAY', + 'INSTANT_SECONDS', + 'MICRO_OF_DAY', + 'MICRO_OF_SECOND', + 'MILLI_OF_DAY', + 'MILLI_OF_SECOND', + 'MINUTE_OF_DAY', + 'MINUTE_OF_HOUR', + 'MONTH_OF_YEAR', + 'NANO_OF_DAY', + 'NANO_OF_SECOND', + 'OFFSET_SECONDS', + 'PROLEPTIC_MONTH', + 'SECOND_OF_DAY', + 'SECOND_OF_MINUTE', + 'YEAR', + 'YEAR_OF_ERA', + ], }, { name: 'date', diff --git a/packages/kbn-esql-validation-autocomplete/src/shared/helpers.ts b/packages/kbn-esql-validation-autocomplete/src/shared/helpers.ts index 32bd23e393431..6f2da35031997 100644 --- a/packages/kbn-esql-validation-autocomplete/src/shared/helpers.ts +++ b/packages/kbn-esql-validation-autocomplete/src/shared/helpers.ts @@ -24,7 +24,7 @@ import { commandDefinitions } from '../definitions/commands'; import { evalFunctionDefinitions } from '../definitions/functions'; import { groupingFunctionDefinitions } from '../definitions/grouping'; import { getFunctionSignatures } from '../definitions/helpers'; -import { timeUnits, chronoLiterals } from '../definitions/literals'; +import { timeUnits } from '../definitions/literals'; import { byOption, metadataOption, @@ -233,9 +233,6 @@ function compareLiteralType(argType: string, item: ESQLLiteral) { return false; } - if (argType === 'chrono_literal') { - return chronoLiterals.some(({ name }) => name === item.text); - } // date-type parameters accept string literals because of ES auto-casting return ['string', 'date'].includes(argType); } diff --git a/packages/kbn-esql-validation-autocomplete/src/validation/esql_validation_meta_tests.json b/packages/kbn-esql-validation-autocomplete/src/validation/esql_validation_meta_tests.json index c49f5c7af82df..7f70b266458f7 100644 --- a/packages/kbn-esql-validation-autocomplete/src/validation/esql_validation_meta_tests.json +++ b/packages/kbn-esql-validation-autocomplete/src/validation/esql_validation_meta_tests.json @@ -11006,6 +11006,13 @@ "error": [], "warning": [] }, + { + "query": "from a_index | eval date_extract(\"SOME_RANDOM_STRING\", now())", + "error": [], + "warning": [ + "Invalid option [\"SOME_RANDOM_STRING\"] for date_extract. Supported options: [\"ALIGNED_DAY_OF_WEEK_IN_MONTH\", \"ALIGNED_DAY_OF_WEEK_IN_YEAR\", \"ALIGNED_WEEK_OF_MONTH\", \"ALIGNED_WEEK_OF_YEAR\", \"AMPM_OF_DAY\", \"CLOCK_HOUR_OF_AMPM\", \"CLOCK_HOUR_OF_DAY\", \"DAY_OF_MONTH\", \"DAY_OF_WEEK\", \"DAY_OF_YEAR\", \"EPOCH_DAY\", \"ERA\", \"HOUR_OF_AMPM\", \"HOUR_OF_DAY\", \"INSTANT_SECONDS\", \"MICRO_OF_DAY\", \"MICRO_OF_SECOND\", \"MILLI_OF_DAY\", \"MILLI_OF_SECOND\", \"MINUTE_OF_DAY\", \"MINUTE_OF_HOUR\", \"MONTH_OF_YEAR\", \"NANO_OF_DAY\", \"NANO_OF_SECOND\", \"OFFSET_SECONDS\", \"PROLEPTIC_MONTH\", \"SECOND_OF_DAY\", \"SECOND_OF_MINUTE\", \"YEAR\", \"YEAR_OF_ERA\"]." + ] + }, { "query": "from a_index | eval var = date_extract(\"ALIGNED_DAY_OF_WEEK_IN_MONTH\", dateField)", "error": [], @@ -11024,7 +11031,6 @@ { "query": "from a_index | eval date_extract(stringField, stringField)", "error": [ - "Argument of [date_extract] must be [chrono_literal], found value [stringField] type [string]", "Argument of [date_extract] must be [date], found value [stringField] type [string]" ], "warning": [] @@ -11044,7 +11050,7 @@ { "query": "row var = date_extract(true, true)", "error": [ - "Argument of [date_extract] must be [chrono_literal], found value [true] type [boolean]", + "Argument of [date_extract] must be [string], found value [true] type [boolean]", "Argument of [date_extract] must be [date], found value [true] type [boolean]" ], "warning": [] @@ -11057,7 +11063,7 @@ { "query": "from a_index | eval date_extract(booleanField, booleanField)", "error": [ - "Argument of [date_extract] must be [chrono_literal], found value [booleanField] type [boolean]", + "Argument of [date_extract] must be [string], found value [booleanField] type [boolean]", "Argument of [date_extract] must be [date], found value [booleanField] type [boolean]" ], "warning": [] diff --git a/packages/kbn-esql-validation-autocomplete/src/validation/validation.test.ts b/packages/kbn-esql-validation-autocomplete/src/validation/validation.test.ts index 14d9c50d5c88c..062e0b9ae78f1 100644 --- a/packages/kbn-esql-validation-autocomplete/src/validation/validation.test.ts +++ b/packages/kbn-esql-validation-autocomplete/src/validation/validation.test.ts @@ -12,7 +12,7 @@ import { ignoreErrorsMap, validateQuery } from './validation'; import { evalFunctionDefinitions } from '../definitions/functions'; import { getFunctionSignatures } from '../definitions/helpers'; import { FunctionDefinition, SupportedFieldType, supportedFieldTypes } from '../definitions/types'; -import { chronoLiterals, timeUnits, timeUnitsToSuggest } from '../definitions/literals'; +import { timeUnits, timeUnitsToSuggest } from '../definitions/literals'; import { statsAggregationFunctionDefinitions } from '../definitions/aggs'; import capitalize from 'lodash/capitalize'; import { camelCase } from 'lodash'; @@ -58,13 +58,9 @@ const nestedFunctions = { }; const literals = { - chrono_literal: chronoLiterals[0].name, time_literal: timeUnitsToSuggest[0].name, }; -function getLiteralType(typeString: 'chrono_literal' | 'time_literal') { - if (typeString === 'chrono_literal') { - return literals[typeString]; - } +function getLiteralType(typeString: 'time_literal') { return `1 ${literals[typeString]}`; } @@ -144,7 +140,7 @@ function getFieldMapping( } if (/literal$/.test(typeString) && useLiterals) { return { - name: getLiteralType(typeString as 'chrono_literal' | 'time_literal'), + name: getLiteralType(typeString as 'time_literal'), type, ...rest, }; @@ -2610,6 +2606,13 @@ describe('validation logic', () => { describe('date_extract', () => { testErrorsAndWarnings('row var = date_extract("ALIGNED_DAY_OF_WEEK_IN_MONTH", now())', []); testErrorsAndWarnings('row date_extract("ALIGNED_DAY_OF_WEEK_IN_MONTH", now())', []); + testErrorsAndWarnings( + 'from a_index | eval date_extract("SOME_RANDOM_STRING", now())', + [], + [ + 'Invalid option ["SOME_RANDOM_STRING"] for date_extract. Supported options: ["ALIGNED_DAY_OF_WEEK_IN_MONTH", "ALIGNED_DAY_OF_WEEK_IN_YEAR", "ALIGNED_WEEK_OF_MONTH", "ALIGNED_WEEK_OF_YEAR", "AMPM_OF_DAY", "CLOCK_HOUR_OF_AMPM", "CLOCK_HOUR_OF_DAY", "DAY_OF_MONTH", "DAY_OF_WEEK", "DAY_OF_YEAR", "EPOCH_DAY", "ERA", "HOUR_OF_AMPM", "HOUR_OF_DAY", "INSTANT_SECONDS", "MICRO_OF_DAY", "MICRO_OF_SECOND", "MILLI_OF_DAY", "MILLI_OF_SECOND", "MINUTE_OF_DAY", "MINUTE_OF_HOUR", "MONTH_OF_YEAR", "NANO_OF_DAY", "NANO_OF_SECOND", "OFFSET_SECONDS", "PROLEPTIC_MONTH", "SECOND_OF_DAY", "SECOND_OF_MINUTE", "YEAR", "YEAR_OF_ERA"].', + ] + ); testErrorsAndWarnings( 'from a_index | eval var = date_extract("ALIGNED_DAY_OF_WEEK_IN_MONTH", dateField)', @@ -2627,7 +2630,6 @@ describe('validation logic', () => { ); testErrorsAndWarnings('from a_index | eval date_extract(stringField, stringField)', [ - 'Argument of [date_extract] must be [chrono_literal], found value [stringField] type [string]', 'Argument of [date_extract] must be [date], found value [stringField] type [string]', ]); @@ -2642,7 +2644,7 @@ describe('validation logic', () => { ); testErrorsAndWarnings('row var = date_extract(true, true)', [ - 'Argument of [date_extract] must be [chrono_literal], found value [true] type [boolean]', + 'Argument of [date_extract] must be [string], found value [true] type [boolean]', 'Argument of [date_extract] must be [date], found value [true] type [boolean]', ]); @@ -2652,7 +2654,7 @@ describe('validation logic', () => { ); testErrorsAndWarnings('from a_index | eval date_extract(booleanField, booleanField)', [ - 'Argument of [date_extract] must be [chrono_literal], found value [booleanField] type [boolean]', + 'Argument of [date_extract] must be [string], found value [booleanField] type [boolean]', 'Argument of [date_extract] must be [date], found value [booleanField] type [boolean]', ]); testErrorsAndWarnings('from a_index | eval date_extract(null, null)', []);