Skip to content
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

Merged

Conversation

drewdaemon
Copy link
Contributor

@drewdaemon drewdaemon commented Oct 4, 2024

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

@drewdaemon drewdaemon added the backport:prev-minor Backport to (8.x) the previous minor version (i.e. one version back from main) label Oct 8, 2024
@drewdaemon drewdaemon force-pushed the 191111/fix-field-and-variable-representations branch from 5cf9522 to 625b765 Compare October 9, 2024 14:38
Comment on lines +165 to +210

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);
Copy link
Contributor Author

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

Comment on lines +68 to 73
if (root.castType === 'int') {
return 'integer';
}
if (firstArg.type === 'literal') {
return firstArg.literalType;
if (root.castType === 'bool') {
return 'boolean';
}
Copy link
Contributor Author

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

@drewdaemon drewdaemon marked this pull request as ready for review October 9, 2024 21:31
@drewdaemon drewdaemon requested a review from a team as a code owner October 9, 2024 21:31
@drewdaemon drewdaemon added release_note:fix Feature:ES|QL ES|QL related features in Kibana Team:ESQL ES|QL related features in Kibana labels Oct 9, 2024
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-esql (Team:ESQL)

Copy link
Contributor

@stratoula stratoula left a 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';
Copy link
Contributor

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?

Copy link
Contributor Author

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?

Copy link
Contributor

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
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in 9a52497

@drewdaemon
Copy link
Contributor Author

@stratoula I don't see a failing test, but I will resolve the merge conflict!

@elastic elastic deleted a comment from elasticmachine Oct 10, 2024
@drewdaemon
Copy link
Contributor Author

@elasticmachine merge upstream

@elastic elastic deleted a comment from elasticmachine Oct 10, 2024
@elasticmachine
Copy link
Contributor

💚 Build Succeeded

Metrics [docs]

Module Count

Fewer modules leads to a faster build time

id before after diff
securitySolution 5950 5958 +8
unifiedSearch 348 356 +8
total +16

Public APIs missing comments

Total count of every public API that lacks a comment. Target amount is 0. Run node scripts/build_api_docs --plugin [yourplugin] --stats comments for more detailed information.

id before after diff
@kbn/esql-validation-autocomplete 192 190 -2

Page load bundle

Size of the bundles that are downloaded on every page load. Target size is below 100kb

id before after diff
kbnUiSharedDeps-srcJs 3.4MB 3.4MB +154.0B
Unknown metric groups

API count

id before after diff
@kbn/esql-validation-autocomplete 204 202 -2

References to deprecated APIs

id before after diff
@kbn/esql-validation-autocomplete 2 1 -1

Copy link
Contributor

@stratoula stratoula left a 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!

@drewdaemon drewdaemon merged commit 731c4e4 into elastic:main Oct 11, 2024
20 checks passed
@kibanamachine
Copy link
Contributor

Starting backport for target branches: 8.x

https://github.com/elastic/kibana/actions/runs/11294248732

kibanamachine pushed a commit to kibanamachine/kibana that referenced this pull request Oct 11, 2024
## 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)
@kibanamachine
Copy link
Contributor

💚 All backports created successfully

Status Branch Result
8.x

Note: Successful backport PRs will be merged automatically after passing CI.

Questions ?

Please refer to the Backport tool documentation

kibanamachine added a commit that referenced this pull request Oct 11, 2024
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:prev-minor Backport to (8.x) the previous minor version (i.e. one version back from main) Feature:ES|QL ES|QL related features in Kibana release_note:fix Team:ESQL ES|QL related features in Kibana v8.16.0 v9.0.0
Projects
None yet
5 participants