-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ES|QL] Improve variable and field name handling #195149
[ES|QL] Improve variable and field name handling #195149
Conversation
…d-and-variable-representations
5cf9522
to
625b765
Compare
|
||
const visitor = new Visitor() | ||
.on('visitLiteralExpression', (ctx) => { | ||
// TODO - add these as variables | ||
}) | ||
.on('visitExpression', (_ctx) => {}) // required for the types :shrug: | ||
.on('visitRenameExpression', (ctx) => { | ||
const [oldArg, newArg] = ctx.node.args; | ||
addToVariables(oldArg, newArg, fields, variables); | ||
}) | ||
.on('visitFunctionCallExpression', (ctx) => { | ||
if (ctx.node.name === '=') { | ||
addVariableFromAssignment(ctx.node, variables, fields); | ||
} else { | ||
addVariableFromExpression(ctx.node, queryString, variables); | ||
} | ||
} | ||
if (command.name === 'enrich') { | ||
const commandOptionsWithAssignment = command.args.filter( | ||
(arg) => isOptionItem(arg) && arg.name === 'with' | ||
) as ESQLCommandOption[]; | ||
for (const commandOption of commandOptionsWithAssignment) { | ||
// Enrich assignment has some special behaviour, so do not use the version above here... | ||
for (const assignFn of commandOption.args) { | ||
}) | ||
.on('visitCommandOption', (ctx) => { | ||
if (ctx.node.name === 'by') { | ||
return [...ctx.visitArguments()]; | ||
} else if (ctx.node.name === 'with') { | ||
for (const assignFn of ctx.node.args) { | ||
if (isFunctionItem(assignFn)) { | ||
const [newArg, oldArg] = assignFn?.args || []; | ||
// TODO why is oldArg an array? | ||
if (Array.isArray(oldArg)) { | ||
addToVariables(oldArg[0], newArg, fields, variables); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
if (command.name === 'rename') { | ||
const commandOptionsWithAssignment = command.args.filter( | ||
(arg) => isOptionItem(arg) && arg.name === 'as' | ||
) as ESQLCommandOption[]; | ||
for (const commandOption of commandOptionsWithAssignment) { | ||
const [oldArg, newArg] = commandOption.args; | ||
addToVariables(oldArg, newArg, fields, variables); | ||
}) | ||
.on('visitCommand', (ctx) => { | ||
const ret = []; | ||
if (['row', 'eval', 'stats', 'inlinestats', 'metrics', 'rename'].includes(ctx.node.name)) { | ||
ret.push(...ctx.visitArgs()); | ||
} | ||
} | ||
} | ||
if (['stats', 'inlinestats', 'enrich'].includes(ctx.node.name)) { | ||
// BY and WITH can contain variables | ||
ret.push(...ctx.visitOptions()); | ||
} | ||
return ret; | ||
}) | ||
.on('visitQuery', (ctx) => [...ctx.visitCommands()]); | ||
|
||
visitor.visitQuery(ast); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just a refactor. No functionality change
if (root.castType === 'int') { | ||
return 'integer'; | ||
} | ||
if (firstArg.type === 'literal') { | ||
return firstArg.literalType; | ||
if (root.castType === 'bool') { | ||
return 'boolean'; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will be nice to automate keeping this in sync in #186409
Pinging @elastic/kibana-esql (Team:ESQL) |
packages/kbn-esql-validation-autocomplete/src/validation/__tests__/fields_and_variables.test.ts
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks ok and in some parts the code has been simplified which is good. There is a test failing though and seems related to the iteam you put on our agenda today (?)
@@ -83,8 +83,6 @@ export const compareTypesWithLiterals = ( | |||
return b === 'timeInterval'; | |||
if (b === 'time_literal' || b === 'time_duration' || b === 'date_period') | |||
return a === 'timeInterval'; | |||
if (a === 'time_literal') return b === 'time_duration'; | |||
if (b === 'time_literal') return a === 'time_duration'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did we remove this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is covered in the 2 branches above, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haha blindness! Correct!
* | ||
* TODO - this function needs a lot of work. For example, it needs to find the best-matching function signature | ||
* which it isn't currently doing. | ||
* @param root |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ℹ️ this function signature here seems to miss some params and the returns type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed in 9a52497
@stratoula I don't see a failing test, but I will resolve the merge conflict! |
@elasticmachine merge upstream |
💚 Build Succeeded
Metrics [docs]Module Count
Public APIs missing comments
Page load bundle
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanx for fixing this Drew! LGTM!
Starting backport for target branches: 8.x |
## Summary Closes elastic#191111 Closes elastic#191105 This change cleans up the way variables and fields were being stored and checked against. We now populate the field and variable cache with unescaped column names. This means that there are fewer cache misses because of escaped names checked against unescaped/differently-escaped names. It also introduces a [suite of tests](https://github.com/elastic/kibana/pull/195149/files#diff-6e4802e45f72257ca6f765bedd3ad65d2cbb587adb5befadb5f27ad5ab08a5a6R113) for variable type detection. Most of those tests are currently skipped, but they are there to represent what should happen when we resolve elastic#195682 User-facing improvements - Validation used to fail for field names with multiple escaped parts (e.g. `geo`.`dest`) - elastic#191105 - Variables assigned the result of an inline cast used to get the wrong type. - Escaped field/variable suggestions now work with field list autocomplete https://github.com/user-attachments/assets/2162f392-3ac3-4bb4-bf37-c73318c7f751 ### 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: Elastic Machine <[email protected]> (cherry picked from commit 731c4e4)
💚 All backports created successfully
Note: Successful backport PRs will be merged automatically after passing CI. Questions ?Please refer to the Backport tool documentation |
…5936) # Backport This will backport the following commits from `main` to `8.x`: - [[ES|QL] Improve variable and field name handling (#195149)](#195149) <!--- Backport version: 9.4.3 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) <!--BACKPORT [{"author":{"name":"Drew Tate","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-10-11T14:33:04Z","message":"[ES|QL] Improve variable and field name handling (#195149)\n\n## Summary\r\n\r\nCloses https://github.com/elastic/kibana/issues/191111\r\nCloses https://github.com/elastic/kibana/issues/191105\r\n\r\nThis change cleans up the way variables and fields were being stored and\r\nchecked against. We now populate the field and variable cache with\r\nunescaped column names. This means that there are fewer cache misses\r\nbecause of escaped names checked against unescaped/differently-escaped\r\nnames.\r\n\r\nIt also introduces a [suite of\r\ntests](https://github.com/elastic/kibana/pull/195149/files#diff-6e4802e45f72257ca6f765bedd3ad65d2cbb587adb5befadb5f27ad5ab08a5a6R113)\r\nfor variable type detection. Most of those tests are currently skipped,\r\nbut they are there to represent what should happen when we resolve\r\nhttps://github.com//issues/195682\r\n\r\nUser-facing improvements\r\n- Validation used to fail for field names with multiple escaped parts\r\n(e.g. `geo`.`dest`)\r\n- https://github.com/elastic/kibana/issues/191105\r\n- Variables assigned the result of an inline cast used to get the wrong\r\ntype.\r\n- Escaped field/variable suggestions now work with field list\r\nautocomplete\r\n\r\n\r\n\r\nhttps://github.com/user-attachments/assets/2162f392-3ac3-4bb4-bf37-c73318c7f751\r\n\r\n\r\n\r\n### Checklist\r\n\r\n- [x]\r\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\r\nwas added for features that require explanation or tutorials\r\n- [x] [Unit or functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere updated or added to match the most common scenarios\r\n\r\n---------\r\n\r\nCo-authored-by: Elastic Machine <[email protected]>","sha":"731c4e4aabbacfdc36ae44bce3e10f438b55a4cd","branchLabelMapping":{"^v9.0.0$":"main","^v8.16.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","v9.0.0","backport:prev-minor","Feature:ES|QL","Team:ESQL"],"title":"[ES|QL] Improve variable and field name handling","number":195149,"url":"https://github.com/elastic/kibana/pull/195149","mergeCommit":{"message":"[ES|QL] Improve variable and field name handling (#195149)\n\n## Summary\r\n\r\nCloses https://github.com/elastic/kibana/issues/191111\r\nCloses https://github.com/elastic/kibana/issues/191105\r\n\r\nThis change cleans up the way variables and fields were being stored and\r\nchecked against. We now populate the field and variable cache with\r\nunescaped column names. This means that there are fewer cache misses\r\nbecause of escaped names checked against unescaped/differently-escaped\r\nnames.\r\n\r\nIt also introduces a [suite of\r\ntests](https://github.com/elastic/kibana/pull/195149/files#diff-6e4802e45f72257ca6f765bedd3ad65d2cbb587adb5befadb5f27ad5ab08a5a6R113)\r\nfor variable type detection. Most of those tests are currently skipped,\r\nbut they are there to represent what should happen when we resolve\r\nhttps://github.com//issues/195682\r\n\r\nUser-facing improvements\r\n- Validation used to fail for field names with multiple escaped parts\r\n(e.g. `geo`.`dest`)\r\n- https://github.com/elastic/kibana/issues/191105\r\n- Variables assigned the result of an inline cast used to get the wrong\r\ntype.\r\n- Escaped field/variable suggestions now work with field list\r\nautocomplete\r\n\r\n\r\n\r\nhttps://github.com/user-attachments/assets/2162f392-3ac3-4bb4-bf37-c73318c7f751\r\n\r\n\r\n\r\n### Checklist\r\n\r\n- [x]\r\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\r\nwas added for features that require explanation or tutorials\r\n- [x] [Unit or functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere updated or added to match the most common scenarios\r\n\r\n---------\r\n\r\nCo-authored-by: Elastic Machine <[email protected]>","sha":"731c4e4aabbacfdc36ae44bce3e10f438b55a4cd"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/195149","number":195149,"mergeCommit":{"message":"[ES|QL] Improve variable and field name handling (#195149)\n\n## Summary\r\n\r\nCloses https://github.com/elastic/kibana/issues/191111\r\nCloses https://github.com/elastic/kibana/issues/191105\r\n\r\nThis change cleans up the way variables and fields were being stored and\r\nchecked against. We now populate the field and variable cache with\r\nunescaped column names. This means that there are fewer cache misses\r\nbecause of escaped names checked against unescaped/differently-escaped\r\nnames.\r\n\r\nIt also introduces a [suite of\r\ntests](https://github.com/elastic/kibana/pull/195149/files#diff-6e4802e45f72257ca6f765bedd3ad65d2cbb587adb5befadb5f27ad5ab08a5a6R113)\r\nfor variable type detection. Most of those tests are currently skipped,\r\nbut they are there to represent what should happen when we resolve\r\nhttps://github.com//issues/195682\r\n\r\nUser-facing improvements\r\n- Validation used to fail for field names with multiple escaped parts\r\n(e.g. `geo`.`dest`)\r\n- https://github.com/elastic/kibana/issues/191105\r\n- Variables assigned the result of an inline cast used to get the wrong\r\ntype.\r\n- Escaped field/variable suggestions now work with field list\r\nautocomplete\r\n\r\n\r\n\r\nhttps://github.com/user-attachments/assets/2162f392-3ac3-4bb4-bf37-c73318c7f751\r\n\r\n\r\n\r\n### Checklist\r\n\r\n- [x]\r\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\r\nwas added for features that require explanation or tutorials\r\n- [x] [Unit or functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere updated or added to match the most common scenarios\r\n\r\n---------\r\n\r\nCo-authored-by: Elastic Machine <[email protected]>","sha":"731c4e4aabbacfdc36ae44bce3e10f438b55a4cd"}}]}] BACKPORT--> Co-authored-by: Drew Tate <[email protected]>
Summary
Closes #191111
Closes #191105
This change cleans up the way variables and fields were being stored and checked against. We now populate the field and variable cache with unescaped column names. This means that there are fewer cache misses because of escaped names checked against unescaped/differently-escaped names.
It also introduces a suite of tests for variable type detection. Most of those tests are currently skipped, but they are there to represent what should happen when we resolve #195682
User-facing improvements
Screen.Recording.2024-10-09.at.9.32.23.AM.mov
Checklist