Skip to content

Commit

Permalink
[8.x] [ES|QL] Add`SORT` command mutation APIs (#197185) (#1…
Browse files Browse the repository at this point in the history
…98440)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[ES|QL] Add`SORT` command mutation APIs
(#197185)](#197185)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Vadim
Kibana","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-10-30T20:34:57Z","message":"[ES|QL]
Add`SORT` command mutation APIs (#197185)\n\n## Summary\r\n\r\nPartially
addresses https://github.com/elastic/kibana/issues/191812\r\n\r\n- Adds
traversal and manipulation APIs for `SORT` command.\r\n -
`commands.sort.listCommands()`\r\n - `commands.sort.getCommand()`\r\n -
`commands.sort.list()`\r\n - `commands.sort.findByPredicate()`\r\n -
`commands.sort.find()`\r\n - `commands.sort.remove()`\r\n -
`commands.sort.insertIntoCommand()`\r\n -
`commands.sort.insertExpression()`\r\n -
`commands.sort.insertCommand()`\r\n- Refactors \"generic\" AST
manipulation routines into (1) `commands`, (2)\r\n`commands.args`, (3)
`commands.options`.\r\n - `generic.commands.*`\r\n -
`generic.commands.args.*`\r\n -
`generic.commands.options.*`\r\n\r\n\r\n### Checklist\r\n\r\nDelete any
items that are not applicable to this PR.\r\n\r\n\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### For
maintainers\r\n\r\n- [x] This was checked for breaking API changes and
was
[labeled\r\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#_add_your_labels)\r\n\r\n---------\r\n\r\nCo-authored-by:
Stratoula Kalafateli
<[email protected]>","sha":"0cc89457124f9cefbdd08766a7a22b1c4bdcc440","branchLabelMapping":{"^v9.0.0$":"main","^v8.17.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["review","release_note:skip","v9.0.0","backport:prev-minor","Feature:ES|QL","Team:ESQL","v8.17.0"],"title":"[ES|QL]
Add`SORT` command mutation
APIs","number":197185,"url":"https://github.com/elastic/kibana/pull/197185","mergeCommit":{"message":"[ES|QL]
Add`SORT` command mutation APIs (#197185)\n\n## Summary\r\n\r\nPartially
addresses https://github.com/elastic/kibana/issues/191812\r\n\r\n- Adds
traversal and manipulation APIs for `SORT` command.\r\n -
`commands.sort.listCommands()`\r\n - `commands.sort.getCommand()`\r\n -
`commands.sort.list()`\r\n - `commands.sort.findByPredicate()`\r\n -
`commands.sort.find()`\r\n - `commands.sort.remove()`\r\n -
`commands.sort.insertIntoCommand()`\r\n -
`commands.sort.insertExpression()`\r\n -
`commands.sort.insertCommand()`\r\n- Refactors \"generic\" AST
manipulation routines into (1) `commands`, (2)\r\n`commands.args`, (3)
`commands.options`.\r\n - `generic.commands.*`\r\n -
`generic.commands.args.*`\r\n -
`generic.commands.options.*`\r\n\r\n\r\n### Checklist\r\n\r\nDelete any
items that are not applicable to this PR.\r\n\r\n\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### For
maintainers\r\n\r\n- [x] This was checked for breaking API changes and
was
[labeled\r\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#_add_your_labels)\r\n\r\n---------\r\n\r\nCo-authored-by:
Stratoula Kalafateli
<[email protected]>","sha":"0cc89457124f9cefbdd08766a7a22b1c4bdcc440"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/197185","number":197185,"mergeCommit":{"message":"[ES|QL]
Add`SORT` command mutation APIs (#197185)\n\n## Summary\r\n\r\nPartially
addresses https://github.com/elastic/kibana/issues/191812\r\n\r\n- Adds
traversal and manipulation APIs for `SORT` command.\r\n -
`commands.sort.listCommands()`\r\n - `commands.sort.getCommand()`\r\n -
`commands.sort.list()`\r\n - `commands.sort.findByPredicate()`\r\n -
`commands.sort.find()`\r\n - `commands.sort.remove()`\r\n -
`commands.sort.insertIntoCommand()`\r\n -
`commands.sort.insertExpression()`\r\n -
`commands.sort.insertCommand()`\r\n- Refactors \"generic\" AST
manipulation routines into (1) `commands`, (2)\r\n`commands.args`, (3)
`commands.options`.\r\n - `generic.commands.*`\r\n -
`generic.commands.args.*`\r\n -
`generic.commands.options.*`\r\n\r\n\r\n### Checklist\r\n\r\nDelete any
items that are not applicable to this PR.\r\n\r\n\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### For
maintainers\r\n\r\n- [x] This was checked for breaking API changes and
was
[labeled\r\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#_add_your_labels)\r\n\r\n---------\r\n\r\nCo-authored-by:
Stratoula Kalafateli
<[email protected]>","sha":"0cc89457124f9cefbdd08766a7a22b1c4bdcc440"}},{"branch":"8.x","label":"v8.17.0","branchLabelMappingKey":"^v8.17.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Vadim Kibana <[email protected]>
  • Loading branch information
kibanamachine and vadimkibana authored Oct 30, 2024
1 parent 8e98784 commit 2230a55
Show file tree
Hide file tree
Showing 20 changed files with 1,449 additions and 374 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ export const highlight = (query: EsqlQuery): Annotation[] => {
});

Walker.visitComments(query.ast, (comment) => {
if (!comment.location) return;

annotations.push([
comment.location.min,
comment.location.max,
Expand Down
27 changes: 22 additions & 5 deletions packages/kbn-esql-ast/src/builder/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import {
ESQLAstComment,
ESQLAstCommentMultiLine,
ESQLAstCommentSingleLine,
ESQLAstQueryExpression,
ESQLColumn,
ESQLCommand,
Expand All @@ -20,6 +22,7 @@ import {
ESQLIntegerLiteral,
ESQLList,
ESQLLocation,
ESQLOrderExpression,
ESQLSource,
} from '../types';
import { AstNodeParserFields, AstNodeTemplate, PartialFields } from './types';
Expand Down Expand Up @@ -63,17 +66,17 @@ export namespace Builder {
};
};

export const comment = (
subtype: ESQLAstComment['subtype'],
export const comment = <S extends ESQLAstComment['subtype']>(
subtype: S,
text: string,
location: ESQLLocation
): ESQLAstComment => {
location?: ESQLLocation
): S extends 'multi-line' ? ESQLAstCommentMultiLine : ESQLAstCommentSingleLine => {
return {
type: 'comment',
subtype,
text,
location,
};
} as S extends 'multi-line' ? ESQLAstCommentMultiLine : ESQLAstCommentSingleLine;
};

export namespace expression {
Expand Down Expand Up @@ -130,6 +133,20 @@ export namespace Builder {
};
};

export const order = (
operand: ESQLColumn,
template: Omit<AstNodeTemplate<ESQLOrderExpression>, 'name' | 'args'>,
fromParser?: Partial<AstNodeParserFields>
): ESQLOrderExpression => {
return {
...template,
...Builder.parserFields(fromParser),
name: '',
args: [operand],
type: 'order',
};
};

export const inlineCast = (
template: Omit<AstNodeTemplate<ESQLInlineCast>, 'name'>,
fromParser?: Partial<AstNodeParserFields>
Expand Down
10 changes: 5 additions & 5 deletions packages/kbn-esql-ast/src/mutate/commands/from/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export const removeByPredicate = (
option.args.splice(index, 1);

if (option.args.length === 0) {
generic.removeCommandOption(ast, option);
generic.commands.options.remove(ast, option);
}

return tuple;
Expand Down Expand Up @@ -148,16 +148,16 @@ export const insert = (
fieldName: string | string[],
index: number = -1
): [column: ESQLColumn, option: ESQLCommandOption] | undefined => {
let option = generic.findCommandOptionByName(ast, 'from', 'metadata');
let option = generic.commands.options.findByName(ast, 'from', 'metadata');

if (!option) {
const command = generic.findCommandByName(ast, 'from');
const command = generic.commands.findByName(ast, 'from');

if (!command) {
return;
}

option = generic.appendCommandOption(command, 'metadata');
option = generic.commands.options.append(command, 'metadata');
}

const parts: string[] = typeof fieldName === 'string' ? [fieldName] : fieldName;
Expand Down Expand Up @@ -189,7 +189,7 @@ export const upsert = (
fieldName: string | string[],
index: number = -1
): [column: ESQLColumn, option: ESQLCommandOption] | undefined => {
const option = generic.findCommandOptionByName(ast, 'from', 'metadata');
const option = generic.commands.options.findByName(ast, 'from', 'metadata');

if (option) {
const parts = Array.isArray(fieldName) ? fieldName : [fieldName];
Expand Down
6 changes: 3 additions & 3 deletions packages/kbn-esql-ast/src/mutate/commands/from/sources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export const remove = (
return undefined;
}

const success = generic.removeCommandArgument(ast, node);
const success = generic.commands.args.remove(ast, node);

return success ? node : undefined;
};
Expand All @@ -78,7 +78,7 @@ export const insert = (
clusterName?: string,
index: number = -1
): ESQLSource | undefined => {
const command = generic.findCommandByName(ast, 'from');
const command = generic.commands.findByName(ast, 'from');

if (!command) {
return;
Expand All @@ -87,7 +87,7 @@ export const insert = (
const source = Builder.expression.indexSource(indexName, clusterName);

if (index === -1) {
generic.appendCommandArgument(command, source);
generic.commands.args.append(command, source);
} else {
command.args.splice(index, 0, source);
}
Expand Down
3 changes: 2 additions & 1 deletion packages/kbn-esql-ast/src/mutate/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@

import * as from from './from';
import * as limit from './limit';
import * as sort from './sort';

export { from, limit };
export { from, limit, sort };
8 changes: 4 additions & 4 deletions packages/kbn-esql-ast/src/mutate/commands/limit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Predicate } from '../../types';
* @returns A collection of "LIMIT" commands.
*/
export const list = (ast: ESQLAstQueryExpression): IterableIterator<ESQLCommand> => {
return generic.listCommands(ast, (cmd) => cmd.name === 'limit');
return generic.commands.list(ast, (cmd) => cmd.name === 'limit');
};

/**
Expand Down Expand Up @@ -55,13 +55,13 @@ export const find = (
* @returns The removed "LIMIT" command, if any.
*/
export const remove = (ast: ESQLAstQueryExpression, index: number = 0): ESQLCommand | undefined => {
const command = generic.findCommandByName(ast, 'limit', index);
const command = generic.commands.findByName(ast, 'limit', index);

if (!command) {
return;
}

const success = generic.removeCommand(ast, command);
const success = !!generic.commands.remove(ast, command);

if (!success) {
return;
Expand Down Expand Up @@ -128,7 +128,7 @@ export const upsert = (
args: [literal],
});

generic.appendCommand(ast, command);
generic.commands.append(ast, command);

return command;
};
Loading

0 comments on commit 2230a55

Please sign in to comment.