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] add validation for VALUES and MV_SORT #179874

Merged
merged 29 commits into from
Apr 10, 2024

Conversation

drewdaemon
Copy link
Contributor

@drewdaemon drewdaemon commented Apr 2, 2024

Summary

Adds validation support for VALUES and MV_SORT.

Validation
Screenshot 2024-04-03 at 3 32 06 PM

Autocomplete
Screenshot 2024-04-09 at 7 22 57 AM

Checklist

@drewdaemon drewdaemon added release_note:skip Skip the PR/issue when compiling release notes Team:ESQL ES|QL related features in Kibana labels Apr 2, 2024
@drewdaemon drewdaemon requested a review from a team as a code owner April 2, 2024 21:11
@elasticmachine
Copy link
Contributor

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

@drewdaemon drewdaemon marked this pull request as draft April 2, 2024 21:27
@drewdaemon drewdaemon marked this pull request as ready for review April 3, 2024 21:33
@drewdaemon drewdaemon changed the title [ES|QL] add "values" definition [ES|QL] add validation for VALUES and MV_SORT Apr 3, 2024
@drewdaemon
Copy link
Contributor Author

@stratoula I think this is ready for review

packages/kbn-esql-ast/src/types.ts Outdated Show resolved Hide resolved
packages/kbn-esql-ast/src/types.ts Outdated Show resolved Hide resolved
* we can't check the return value of a function to see if it
* matches one of the options prior to runtime.
*/
literalOptions?: string[];
Copy link
Contributor

Choose a reason for hiding this comment

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

can we call it values to align with the CommandDefinition thing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could you clarify a bit? I'm thinking options better describes what this is. "value" implies what the user actually chooses, whereas "options" says to me a set of values they are allowed to choose.

@stratoula
Copy link
Contributor

Shouldn't we have here the parser and lexer changes too? 🤔

@drewdaemon
Copy link
Contributor Author

@stratoula those were taken care of in #179850

@stratoula
Copy link
Contributor

Gotcha! Thanx, I will review tomorrow!

@stratoula
Copy link
Contributor

/ci

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.

It fails on a duplicate i18n keyword but it works fine so I am approving to unblock!

Copy link
Contributor

@dej611 dej611 left a comment

Choose a reason for hiding this comment

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

I've left few comments on code to revisit.

@drewdaemon
Copy link
Contributor Author

They're on my radar @dej611 :)

@drewdaemon
Copy link
Contributor Author

@elasticmachine merge upstream

@stratoula
Copy link
Contributor

I like the idea of suggesting asc , desc. Looks nice. When I am clicking one of these though it is not added as a string having as a result the validator to complain.

image

@drewdaemon
Copy link
Contributor Author

When I am clicking one of these though it is not added as a string having as a result the validator to complain.

Good catch... this was working but it looks like I broke it. The tests are yelling about it too :D

@drewdaemon drewdaemon requested review from dej611 and stratoula April 9, 2024 13:25
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.

Autocomplete works great. @dej611 can you check code wise and if you are ok let's merge

Copy link
Contributor

@dej611 dej611 left a comment

Choose a reason for hiding this comment

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

The structure seems better now. I would ask for a minor tweak as in code comments.

Comment on lines 81 to 99
export interface ESQLNumberLiteral extends ESQLAstBaseItem {
type: 'literal';
literalType: 'number';
value: number;
}

export interface ESQLBooleanLiteral extends ESQLAstBaseItem {
type: 'literal';
literalType: 'boolean';
value: string;
}

export interface ESQLNullLiteral extends ESQLAstBaseItem {
type: 'literal';
literalType: 'null';
value: string;
}

export interface ESQLStringLiteral extends ESQLAstBaseItem {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not against to exporting for internal usage (which I think should be marked) rather for public one.
As long as they are not publicly exported I'm ok.

packages/kbn-esql-ast/src/ast_helpers.ts Outdated Show resolved Hide resolved
@@ -165,6 +165,16 @@ export const buildConstantsDefinitions = (
sortText: 'A',
}));

export const buildValueDefinitions = (values: string[]): SuggestionRawDefinition[] =>
Copy link
Contributor

Choose a reason for hiding this comment

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

As a follow up of this PR I would probably reuse this for chrono_literals and time_literals as well

Comment on lines 88 to 92
actualArg.literalType === 'string' &&
argDef.literalOptions &&
!argDef.literalOptions
.map((option) => option.toLowerCase())
.includes(unwrapStringLiteralQuotes(actualArg.value).toLowerCase())
Copy link
Contributor

Choose a reason for hiding this comment

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

I would probably make this go into the omniscent isEqualType as:

// currently only supporting literalOptions with string literals
      actualArg.literalType === 'string' &&
      argDef.literalOptions &&
      !isEqualType(actualArg, argDef, references, parentCommand)

then the isEqualType signature will change as:

export function isEqualType(
  item: ESQLSingleAstItem,
  argDef: SignatureArgType | FunctionArg,
  references: ReferenceMaps,
  parentCommand?: string,
  nameHit?: string
) {
  const argType = 'innerType' in argDef && argDef.innerType ? argDef.innerType : argDef.type;
  if (argType === 'any') {
    return true;
  }
  if (item.type === 'literal') {
    return compareLiteralType(
      argType,
      item,
      'literalOptions' in argDef ? argDef.literalOptions : undefined
    );
  }
...

and the compareLiteralType function becomes:

function compareLiteralType(argTypes: string, item: ESQLLiteral, possibleOptions?: string[]) {
  if (item.literalType !== 'string') {
    return argTypes === item.literalType;
  }
  if (argTypes === 'chrono_literal') {
    return chronoLiterals.some(({ name }) => name === item.text);
  }
  if (possibleOptions) {
    return possibleOptions.includes(unwrapStringLiteralQuotes(item.text.toLowerCase()));
  }
  return argTypes === item.literalType;
}

(I think the assumption that the literalOptions are already lower case can be made).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I started down this path but I'm not sure isEqualType is the best fit. We want a different message if the type is correct but it doesn't match one of the literalOptions. All isEqualType returns is true or false.

If I were to incorporate this check into isEqualType, I would end up reporting that the type is incorrect when, in fact, it just doesn't match one of the supported options.

@drewdaemon drewdaemon requested a review from dej611 April 9, 2024 16:52
@drewdaemon
Copy link
Contributor Author

@dej611 I've addressed your comments. Can you take another look?

@drewdaemon
Copy link
Contributor Author

@elasticmachine merge upstream

@drewdaemon
Copy link
Contributor Author

@elasticmachine merge upstream

Copy link
Contributor

@dej611 dej611 left a comment

Choose a reason for hiding this comment

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

Approve to unblock.

Can you create a follow up PR with more tests over mv_sort with capitalized asc and desc args?

@drewdaemon
Copy link
Contributor Author

drewdaemon commented Apr 10, 2024

Good idea @dej611 . Done in 98c6b5f

@drewdaemon
Copy link
Contributor Author

@elasticmachine merge upstream

@kibana-ci
Copy link
Collaborator

💛 Build succeeded, but was flaky

Failed CI Steps

Test Failures

  • [job] [logs] FTR Configs #29 / transform /internal/transform/schedule_now_transforms bulk schedule "after each" hook for "should schedule multiple transforms by transformIds, even if one of the transformIds is invalid"

Metrics [docs]

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-ast 66 63 -3

Public APIs missing exports

Total count of every type that is part of your API that should be exported but is not. This will cause broken links in the API documentation system. Target amount is 0. Run node scripts/build_api_docs --plugin [yourplugin] --stats exports for more detailed information.

id before after diff
@kbn/esql-ast 2 6 +4

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 2.9MB 2.9MB +1.8KB
Unknown metric groups

API count

id before after diff
@kbn/esql-ast 66 63 -3
@kbn/esql-validation-autocomplete 193 194 +1
total -2

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@drewdaemon
Copy link
Contributor Author

💛 Build succeeded, but was flaky

I'll take it!

@drewdaemon drewdaemon merged commit f018af0 into elastic:main Apr 10, 2024
17 checks passed
@kibanamachine kibanamachine added v8.14.0 backport:skip This commit does not require backporting labels Apr 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:skip This commit does not require backporting release_note:skip Skip the PR/issue when compiling release notes Team:ESQL ES|QL related features in Kibana v8.14.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants