Skip to content
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
8 changes: 6 additions & 2 deletions src/ast/mutate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,9 @@ console.log(src); // FROM index METADATA _lang, _id
- `.join`
- `.list()` — List all `JOIN` commands.
- `.byIndex()` — Find a `JOIN` command by index.


- `.set`
- `.list()` — List all `SET` header commands.
- `.findBySettingName()` — Find a `SET` command by its setting name.
- `.update()` — Modify the value of an existing `SET` setting.
- `.upsert()` — Modify a `SET` setting value, or add a new `SET` command if it does not exist.
- `.remove()` — Deletes a SET command by setting name.
3 changes: 2 additions & 1 deletion src/ast/mutate/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

import * as from from './from';
import * as limit from './limit';
import * as set from './set';
import * as sort from './sort';
import * as stats from './stats';
import * as where from './where';
import * as join from './join';
import * as rerank from './rerank';

export { from, limit, sort, stats, where, join, rerank };
export { from, limit, set, sort, stats, where, join, rerank };
229 changes: 229 additions & 0 deletions src/ast/mutate/commands/set/index.test.ts
Comment thread
stratoula marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { Parser } from '../../../../parser';
import { BasicPrettyPrinter } from '../../../../pretty_print';
import * as commands from '..';

describe('commands.set', () => {
describe('.list()', () => {
it('lists all SET header commands', () => {
const src = 'SET approximation = true; SET unmapped_fields = "DEFAULT"; FROM index';
const { root } = Parser.parse(src);

const nodes = [...commands.set.list(root)];

expect(nodes).toHaveLength(2);
});

it('returns empty iterator when no SET commands exist', () => {
const src = 'FROM index | LIMIT 10';
const { root } = Parser.parse(src);

const nodes = [...commands.set.list(root)];

expect(nodes).toHaveLength(0);
});
});

describe('.findBySettingName()', () => {
it('finds a SET command by setting name', () => {
const src = 'SET approximation = true; SET unmapped_fields = "DEFAULT"; FROM index';
const { root } = Parser.parse(src);

const node = commands.set.findBySettingName(root, 'unmapped_fields');

expect(node).toMatchObject({
type: 'header-command',
name: 'set',
args: [
{
type: 'function',
subtype: 'binary-expression',
name: '=',
args: [
{ type: 'identifier', name: 'unmapped_fields' },
{ type: 'literal', literalType: 'keyword', valueUnquoted: 'DEFAULT' },
],
},
],
});
});

it('returns undefined when the setting does not exist', () => {
const src = 'SET approximation = true; FROM index';
const { root } = Parser.parse(src);

const node = commands.set.findBySettingName(root, 'unmapped_fields');

expect(node).toBeUndefined();
});
});

describe('.update()', () => {
it('modifies the value of an existing setting', () => {
const src = 'SET unmapped_fields = "DEFAULT"; FROM index';
const { root } = Parser.parse(src);

commands.set.update(root, 'unmapped_fields', '"LOAD"');
const printed = BasicPrettyPrinter.print(root);

expect(printed).toBe('SET unmapped_fields = "LOAD"; FROM index');
});

it('returns undefined when the setting does not exist', () => {
const src = 'SET approximation = TRUE; FROM index';
const { root } = Parser.parse(src);

const node = commands.set.update(root, 'unmapped_fields', '"LOAD"');
const printed = BasicPrettyPrinter.print(root);
expect(printed).toBe('SET approximation = TRUE; FROM index');
expect(node).toBeUndefined();
});

it('only modifies the targeted setting when multiple exist', () => {
const src = 'SET approximation = TRUE; SET unmapped_fields = "DEFAULT"; FROM index';
const { root } = Parser.parse(src);

commands.set.update(root, 'unmapped_fields', '"LOAD"');
const printed = BasicPrettyPrinter.print(root);

expect(printed).toBe('SET approximation = TRUE; SET unmapped_fields = "LOAD"; FROM index');
});

it('modifies the value of an existing setting with a boolean value', () => {
const src = 'SET approximation = TRUE; FROM index';
const { root } = Parser.parse(src);

commands.set.update(root, 'approximation', 'FALSE');
const printed = BasicPrettyPrinter.print(root);

expect(printed).toBe('SET approximation = FALSE; FROM index');
});

it('modifies the value of an existing setting with a MAP value', () => {
const src = 'SET approximation = TRUE; FROM index';
const { root } = Parser.parse(src);

const node = commands.set.update(root, 'approximation', '{ "confidence_level": 0.99 }');
const printed = BasicPrettyPrinter.print(root);

expect(printed).toBe('SET approximation = {"confidence_level": 0.99}; FROM index');
expect(node).toMatchObject({
type: 'header-command',
name: 'set',
args: [
{
type: 'function',
subtype: 'binary-expression',
name: '=',
args: [
{ type: 'identifier', name: 'approximation' },
{
type: 'map',
entries: [
{
key: {
type: 'literal',
literalType: 'keyword',
valueUnquoted: 'confidence_level',
},
value: { type: 'literal', literalType: 'double', value: 0.99 },
},
],
},
],
},
],
});
});
});

describe('.upsert()', () => {
it('modifies the value when the setting already exists', () => {
const src = 'SET unmapped_fields = "DEFAULT"; FROM index';
const { root } = Parser.parse(src);

const node = commands.set.upsert(root, 'unmapped_fields', '"LOAD"');
const printed = BasicPrettyPrinter.print(root);

expect(printed).toBe('SET unmapped_fields = "LOAD"; FROM index');
expect(node).toMatchObject({
type: 'header-command',
name: 'set',
});
});

it('adds a new SET command when the setting does not exist', () => {
const src = 'FROM index | LIMIT 10';
const { root } = Parser.parse(src);

const node = commands.set.upsert(root, 'unmapped_fields', '"LOAD"');
const printed = BasicPrettyPrinter.print(root);

expect(printed).toBe('SET unmapped_fields = "LOAD"; FROM index | LIMIT 10');
expect(node).toMatchObject({
type: 'header-command',
name: 'set',
args: [
{
type: 'function',
subtype: 'binary-expression',
name: '=',
args: [
{ type: 'identifier', name: 'unmapped_fields' },
{ type: 'literal', literalType: 'keyword', valueUnquoted: 'LOAD' },
],
},
],
});
});

it('appends alongside existing SET commands', () => {
const src = 'SET approximation = TRUE; FROM index';
const { root } = Parser.parse(src);

commands.set.upsert(root, 'unmapped_fields', '"LOAD"');
const printed = BasicPrettyPrinter.print(root);

expect(printed).toBe('SET approximation = TRUE; SET unmapped_fields = "LOAD"; FROM index');
});
});

describe('.remove()', () => {
it('removes a SET command by setting name', () => {
const src = 'SET unmapped_fields = "DEFAULT"; FROM index';
const { root } = Parser.parse(src);

commands.set.remove(root, 'unmapped_fields');
const printed = BasicPrettyPrinter.print(root);

expect(printed).toBe('FROM index');
});

it('returns undefined when the setting does not exist', () => {
const src = 'SET approximation = TRUE; FROM index';
const { root } = Parser.parse(src);

const removed = commands.set.remove(root, 'unmapped_fields');
const printed = BasicPrettyPrinter.print(root);

expect(removed).toBeUndefined();
expect(printed).toBe('SET approximation = TRUE; FROM index');
});

it('only removes the targeted setting when multiple exist', () => {
const src = 'SET approximation = TRUE; SET unmapped_fields = "DEFAULT"; FROM index';
const { root } = Parser.parse(src);

commands.set.remove(root, 'unmapped_fields');
const printed = BasicPrettyPrinter.print(root);

expect(printed).toBe('SET approximation = TRUE; FROM index');
});
});
});
144 changes: 144 additions & 0 deletions src/ast/mutate/commands/set/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { Builder } from '../../../builder';
import { isAssignment } from '../../../is';
import { Parser } from '../../../../parser';
import type {
ESQLAstQueryExpression,
ESQLAstSetHeaderCommand,
ESQLBinaryExpression,
BinaryExpressionAssignmentOperator,
ESQLIdentifier,
} from '../../../../types';

/**
* Lists all SET header commands in the query AST.
*
* @param ast The root AST node.
* @returns An iterable of SET header commands.
*/
export const list = function* (
ast: ESQLAstQueryExpression
): IterableIterator<ESQLAstSetHeaderCommand> {
if (!ast.header) return;
for (const cmd of ast.header) {
if (cmd.name === 'set') {
yield cmd as ESQLAstSetHeaderCommand;
}
}
};

/**
* Finds a SET header command by its setting name.
*
* @param ast The root AST node.
* @param settingName The name of the setting to find (e.g. "unmapped_fields").
* @returns The matching SET header command, or undefined.
*/
export const findBySettingName = (
ast: ESQLAstQueryExpression,
settingName: string
): ESQLAstSetHeaderCommand | undefined => {
for (const cmd of list(ast)) {
const assignment = cmd.args[0];
if (isAssignment(assignment)) {
const identifier = assignment.args[0] as ESQLIdentifier;
if (identifier.name === settingName) {
return cmd;
}
}
}
return undefined;
};

/**
* Updates the value of an existing SET setting. Returns the modified header
* command, or `undefined` if no matching setting was found.
*
* @param ast The root AST node.
* @param settingName The name of the setting to modify.
* @param value The new value, parsed as an ES|QL expression. For example:
* - `'"LOAD"'` → string literal `"LOAD"`
* - `'true'` → boolean literal `TRUE`
* - `'42'` → integer literal `42`
* - `'{ "key": "value" }'` → map expression
* @returns The modified SET header command, or undefined if not found.
*/
export const update = (
ast: ESQLAstQueryExpression,
settingName: string,
value: string
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What about boolean values? Like SET approximation = TRUE?? Same for the upsert

Copy link
Copy Markdown
Contributor Author

@sddonne sddonne Apr 10, 2026

Choose a reason for hiding this comment

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

Good call, modified it so the value is taken as an expression query, in this way you can also set maps as values.
3bc75e8
Using Synth to transform it into the correct AST node.
Synth added a circular dependency so just parsing the value now.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ok cool but type wise this value being string seems wrong no? As it can also be boolean or number

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We can implement this in two ways, as it is now, it expects the value to be written as you want it to appear in the query.
commands.set.update(root, 'unmapped_fields', 'true'); -> SET unmapped_fields = true;
commands.set.update(root, 'unmapped_fields', '"LOAD"'); -> SET unmapped_fields = "LOAD";
commands.set.update(root, 'unmapped_fields', '{"key" : "value"}'); -> SET unmapped_fields = {"key": "value"};

The other would be to expect a ESQLNode, delegating the responsibility to client to build it.

Mm there is also a third way that would be to set the value type as string | number | boolean | object (I think this is what you meant) and we should have some logic to transform them into ESQLNodes, but seems complex for this use case..

I think the current one is like in a middle ground between offering functionality and keeping it simple.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I see, as long as it works prperly is ok but it is confusing that we are typing all the values as string. At least add a JSDoc comment on the value parameter explaining that it's parsed as an ES|QL expression (so 'true' → boolean, '"LOAD"' → string literal, etc.). That way future callers don't have to guess.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

): ESQLAstSetHeaderCommand | undefined => {
const cmd = findBySettingName(ast, settingName);
if (!cmd) return undefined;

const newNode = Parser.parseExpression(value).root;

const assignment = cmd.args[0] as ESQLBinaryExpression<BinaryExpressionAssignmentOperator>;
assignment.args[1] = newNode;

return cmd;
};

/**
* Updates the value of an existing SET setting, or inserts a new SET header
* command if the setting does not exist. The new command is inserted as the last
* command in the header.
*
* @param ast The root AST node.
* @param settingName The name of the setting (e.g. "unmapped_fields").
* @param value The new value, parsed as an ES|QL expression. For example:
* - `'"LOAD"'` → string literal `"LOAD"`
* - `'true'` → boolean literal `TRUE`
* - `'42'` → integer literal `42`
* - `'{ "key": "value" }'` → map expression
* @returns The modified or newly created SET header command.
*/
export const upsert = (
ast: ESQLAstQueryExpression,
settingName: string,
value: string
): ESQLAstSetHeaderCommand => {
const existing = update(ast, settingName, value);
if (existing) return existing;

const identifier = Builder.identifier(settingName);
const newNode = Parser.parseExpression(value).root;
const assignment = Builder.expression.func.binary('=', [identifier, newNode]);
const cmd = Builder.header.command.set([
assignment as ESQLBinaryExpression<BinaryExpressionAssignmentOperator>,
]);

if (!ast.header) {
ast.header = [];
}
ast.header.push(cmd);
Comment thread
stratoula marked this conversation as resolved.

return cmd;
};

/**
* Removes a SET header command by its setting name.
*
* @param ast The root AST node.
* @param settingName The name of the setting to remove.
* @returns The removed SET header command, or undefined if not found.
*/
export const remove = (
ast: ESQLAstQueryExpression,
settingName: string
): ESQLAstSetHeaderCommand | undefined => {
const cmd = findBySettingName(ast, settingName);
if (!cmd || !ast.header) return;

const index = ast.header.indexOf(cmd);
if (index === -1) return;

ast.header.splice(index, 1);
return cmd;
};