From 113b69ec194e9500813c5c945be75702dd73f05c Mon Sep 17 00:00:00 2001 From: Drew Tate Date: Thu, 20 Jun 2024 08:28:07 -0600 Subject: [PATCH 1/2] remove chrono literals --- .../scripts/generate_function_definitions.ts | 36 +++++++++++++++++-- .../generate_function_validation_tests.ts | 10 ++---- .../src/autocomplete/autocomplete.test.ts | 5 +-- .../src/autocomplete/factories.ts | 5 +-- .../src/definitions/functions.ts | 34 +++++++++++++++++- .../src/definitions/literals.ts | 33 ----------------- .../src/shared/helpers.ts | 5 +-- .../src/validation/validation.test.ts | 10 ++---- 8 files changed, 76 insertions(+), 62 deletions(-) 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 15333c62300fa..02cd737b86472 100644 --- a/packages/kbn-esql-validation-autocomplete/scripts/generate_function_definitions.ts +++ b/packages/kbn-esql-validation-autocomplete/scripts/generate_function_definitions.ts @@ -174,6 +174,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 * @@ -197,8 +230,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 b7cc5c4481e66..673a0256c9efe 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 { chronoLiterals, timeLiterals } from '../src/definitions/literals'; +import { timeLiterals } 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: timeLiterals[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]}`; } @@ -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 efff60c382d34..c35671aebd98d 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, timeLiterals } from '../definitions/literals'; +import { timeLiterals } from '../definitions/literals'; import { commandDefinitions } from '../definitions/commands'; import { getUnitDuration, TRIGGER_SUGGESTION_COMMAND } from './factories'; import { camelCase, partition } from 'lodash'; @@ -186,9 +186,6 @@ function getLiteralsByType(_type: string | string[]) { // return only singular return timeLiterals.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 4b2e74729db78..94f84280ca367 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, timeLiterals } from '../definitions/literals'; +import { timeLiterals } from '../definitions/literals'; import { FunctionDefinition, CommandDefinition, @@ -326,9 +326,6 @@ export function getCompatibleLiterals(commandName: string, types: string[], name if (types.includes('time_literal_unit')) { suggestions.push(...buildConstantsDefinitions(timeLiterals.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 77f4195f15a82..f39fbbc9bb85f 100644 --- a/packages/kbn-esql-validation-autocomplete/src/definitions/functions.ts +++ b/packages/kbn-esql-validation-autocomplete/src/definitions/functions.ts @@ -652,8 +652,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/definitions/literals.ts b/packages/kbn-esql-validation-autocomplete/src/definitions/literals.ts index 9369414378385..267398471a3cd 100644 --- a/packages/kbn-esql-validation-autocomplete/src/definitions/literals.ts +++ b/packages/kbn-esql-validation-autocomplete/src/definitions/literals.ts @@ -155,36 +155,3 @@ export const timeLiterals: Literals[] = [ ), }, ]; - -export const chronoLiterals: Literals[] = [ - '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', -].map((name) => ({ name: `"${name}"`, description: '' })); diff --git a/packages/kbn-esql-validation-autocomplete/src/shared/helpers.ts b/packages/kbn-esql-validation-autocomplete/src/shared/helpers.ts index 348a9b45e5d12..aba236728d6f3 100644 --- a/packages/kbn-esql-validation-autocomplete/src/shared/helpers.ts +++ b/packages/kbn-esql-validation-autocomplete/src/shared/helpers.ts @@ -23,7 +23,7 @@ import { commandDefinitions } from '../definitions/commands'; import { evalFunctionDefinitions } from '../definitions/functions'; import { groupingFunctionDefinitions } from '../definitions/grouping'; import { getFunctionSignatures } from '../definitions/helpers'; -import { chronoLiterals, timeLiterals } from '../definitions/literals'; +import { timeLiterals } from '../definitions/literals'; import { byOption, metadataOption, @@ -228,9 +228,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/validation.test.ts b/packages/kbn-esql-validation-autocomplete/src/validation/validation.test.ts index 939cd5507ae27..695ab345213a0 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, timeLiterals } from '../definitions/literals'; +import { timeLiterals } from '../definitions/literals'; import { statsAggregationFunctionDefinitions } from '../definitions/aggs'; import capitalize from 'lodash/capitalize'; import { camelCase } from 'lodash'; @@ -57,13 +57,9 @@ const nestedFunctions = { }; const literals = { - chrono_literal: chronoLiterals[0].name, time_literal: timeLiterals[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]}`; } @@ -143,7 +139,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, }; From 7b5c359d4d2c11696ee945aa3db2861dd514cb1a Mon Sep 17 00:00:00 2001 From: Drew Tate Date: Tue, 9 Jul 2024 15:07:49 -0600 Subject: [PATCH 2/2] update tests --- .../src/validation/esql_validation_meta_tests.json | 12 +++++++++--- .../src/validation/validation.test.ts | 12 +++++++++--- 2 files changed, 18 insertions(+), 6 deletions(-) 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 bc7181d31c765..062e0b9ae78f1 100644 --- a/packages/kbn-esql-validation-autocomplete/src/validation/validation.test.ts +++ b/packages/kbn-esql-validation-autocomplete/src/validation/validation.test.ts @@ -2606,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)', @@ -2623,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]', ]); @@ -2638,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]', ]); @@ -2648,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)', []);