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

[8.x] [ES|QL] Add`SORT` command mutation APIs (#197185) #198440

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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