-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ES|QL] Add support for command settings (#175114)
## Summary Sync up with elastic/elasticsearch#103949 grammar changes. Command definitions now have a new `mode` list of argument, with related `ESQLCommandMode` at AST level. Validation now accepts settings and validate the specific ENRICH one (error message is taken from the ES codebase). Autocomplete doesn't trigger by default on settings, but it only trigger when user starts to type a setting with the `[` trigger char: ![enrich_modes](https://github.com/elastic/kibana/assets/924948/8882361d-2fc7-44ab-bc8e-3994fc3e733d) Note that multiple settings are supported, but shadowing will trigger a warning. `ccq.mode` value name and descriptions have been taken from the linked ES PR. ### Checklist - [x] [Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html) was added for features that require explanation or tutorials - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios --------- Co-authored-by: Stratoula Kalafateli <efstratia.kalafateli@elastic.co>
- Loading branch information
Showing
28 changed files
with
2,161 additions
and
1,298 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
const { join } = require('path'); | ||
const { ESLint } = require('eslint'); | ||
const partition = require('lodash/partition'); | ||
const { readdirSync, readFileSync, writeFileSync } = require('fs'); | ||
const ora = require('ora'); | ||
const log = ora('Updating ES|QL AST walker from antlr grammar').start(); | ||
/* | ||
* This script will read from the parser file generated by the "build:antlr4ts:esql" task | ||
* and extract all quoted/unquoted tokens to update their ids | ||
* into the "ast_helper" file. | ||
* This prevents the bundle size to increase by ~500 kb ("esql_parser" size). | ||
* This script is run at the end of "build:antlr4ts:esql" task, so no need to call it manually. | ||
*/ | ||
async function execute(folder) { | ||
const generatedAntlrFolder = join(__dirname, '..', 'src', folder, 'antlr'); | ||
|
||
const generatedAntlrFolderContents = readdirSync(generatedAntlrFolder); | ||
|
||
const tokenRegex = /public static readonly (?<name>[A-Z_]*(UN)*QUOTED_[A-Z_]+) = (?<value>\d+);/; | ||
const lexerFile = generatedAntlrFolderContents.find((file) => file === 'esql_parser.ts'); | ||
const lexerFileRows = readFileSync(join(generatedAntlrFolder, lexerFile), 'utf8') | ||
.toString() | ||
.split('\n'); | ||
const tokenList = []; | ||
for (const row of lexerFileRows) { | ||
const match = row.match(tokenRegex); | ||
if (match?.groups) { | ||
tokenList.push(match.groups); | ||
} | ||
} | ||
const [unquotedList, quotedList] = partition(tokenList, ({ name }) => /UNQUOTED/.test(name)); | ||
|
||
// now all quote/unquoted tokens are registered | ||
// dump them into the ast_helper file | ||
const astHelperFileFolder = join(__dirname, '..', 'src', folder, 'lib', 'ast'); | ||
const astHelperFilename = 'ast_helpers.ts'; | ||
|
||
try { | ||
const astHelperContentRows = readFileSync(join(astHelperFileFolder, astHelperFilename), 'utf8') | ||
.toString() | ||
.split('\n'); | ||
|
||
const startAutoGeneratedComment = astHelperContentRows.findIndex( | ||
(row) => row === '/* SCRIPT_MARKER_START */' | ||
); | ||
const endAutoGeneratedComment = | ||
astHelperContentRows.findIndex((row) => row === '/* SCRIPT_MARKER_END */') + 1; | ||
|
||
const newFunctionsContent = ` | ||
/* SCRIPT_MARKER_START */ | ||
function getQuotedText(ctx: ParserRuleContext) { | ||
return [ | ||
${quotedList.map(({ name, value }) => `${value} /* esql_parser.${name} */`).join(', ')} | ||
] | ||
.map((keyCode) => ctx.tryGetToken(keyCode, 0)) | ||
.filter(nonNullable)[0]; | ||
} | ||
function getUnquotedText(ctx: ParserRuleContext) { | ||
return [ | ||
${unquotedList.map(({ name, value }) => `${value} /* esql_parser.${name} */`).join(', ')} | ||
] | ||
.map((keyCode) => ctx.tryGetToken(keyCode, 0)) | ||
.filter(nonNullable)[0]; | ||
} | ||
/* SCRIPT_MARKER_END */ | ||
`; | ||
|
||
const fileContent = astHelperContentRows | ||
.slice(0, startAutoGeneratedComment) | ||
.concat(newFunctionsContent.split('\n'), astHelperContentRows.slice(endAutoGeneratedComment)); | ||
|
||
const fileContentString = fileContent.join('\n'); | ||
|
||
const eslint = new ESLint({ | ||
fix: true, | ||
overrideConfig: { | ||
parser: '@typescript-eslint/parser', | ||
parserOptions: { | ||
sourceType: 'module', | ||
ecmaVersion: 2018, | ||
}, | ||
rules: { | ||
'@kbn/imports/no_unresolvable_imports': 'off', | ||
'prettier/prettier': [ | ||
'error', | ||
{ | ||
parser: 'typescript', | ||
}, | ||
], | ||
}, | ||
}, | ||
}); | ||
|
||
const results = await eslint.lintText(fileContentString); | ||
|
||
if (results.some(({ messages }) => messages.length > 0)) { | ||
const formatter = await eslint.loadFormatter('stylish'); | ||
const resultText = formatter.format(results); | ||
process.exitCode = 1; | ||
return log.fail(resultText); | ||
} | ||
|
||
const filePath = join(astHelperFileFolder, astHelperFilename); | ||
writeFileSync(filePath, results[0].output || '', { encoding: 'utf8' }); | ||
} catch (err) { | ||
return log.fail(err.message); | ||
} | ||
|
||
log.succeed('Updated ES|QL helper from antlr grammar successfully'); | ||
} | ||
|
||
execute('esql'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.