Skip to content

Commit 2152a27

Browse files
vadimkibanastratoula
authored andcommitted
[ES|QL] Composer and Synth API for SET instruction (elastic#238896)
## Summary - Adds support for `SET` header commands in `synth` and `composer` modules of `@kbn/ast-esql` package. - Allows to add `SET` instructions to the beginning of query: ```ts synth.query `SET a = 123; FROM index`; esql `SET a = 123; FROM index`; ``` - Adds `synth` and `Parser` methods to construct only single header command: ```ts // Parser: Parser.parseHeaderCommand('SET a = 123'); // synth: synth.header `SET a = ${value}`; ``` - Adds `.header` template tag to `ComposerQuery` for adding header commands to the query: ```ts const query = esql `FROM index`; query .header `SET a = 123` .header `SET b = 456`; ``` - Adds `ComposerQuery` helper methods for working with header *set*-instructions: - `query.addSetCommand(name, value)` - add a `SET` instruction to the query - `query.removeSetCommand(name)` - remove specific header `SET` commands - `query.clearSetCommands()` - remove all header `SET` commands - `query.getSetCommands()` - retrieve all `SET` instructions as a lossless array - `query.getSetCommnadRecord()` - retrieve flattened `SET` instruction `Record` ### 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: Stratou <[email protected]>
1 parent 33bc7ca commit 2152a27

File tree

19 files changed

+1078
-16
lines changed

19 files changed

+1078
-16
lines changed

src/platform/packages/shared/kbn-esql-ast/src/ast/is.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ export const isQuery = (node: unknown): node is types.ESQLAstQueryExpression =>
2424
export const isCommand = (node: unknown): node is types.ESQLCommand =>
2525
isProperNode(node) && node.type === 'command';
2626

27+
export const isHeaderCommand = (node: unknown): node is types.ESQLAstHeaderCommand =>
28+
isProperNode(node) && node.type === 'header-command';
29+
2730
export const isFunctionExpression = (node: unknown): node is types.ESQLFunction =>
2831
isProperNode(node) && node.type === 'function';
2932

@@ -72,7 +75,7 @@ export const isIntegerLiteral = (node: unknown): node is types.ESQLIntegerLitera
7275
export const isDoubleLiteral = (node: unknown): node is types.ESQLIntegerLiteral =>
7376
isLiteral(node) && node.literalType === 'double';
7477

75-
export const isBooleanLiteral = (node: unknown): node is types.ESQLStringLiteral =>
78+
export const isBooleanLiteral = (node: unknown): node is types.ESQLBooleanLiteral =>
7679
isLiteral(node) && node.literalType === 'boolean';
7780

7881
export const isTimeDurationLiteral = (node: unknown): node is types.ESQLTimeDurationLiteral =>

src/platform/packages/shared/kbn-esql-ast/src/composer/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,39 @@ const query = esql.from('index').where`status == ${status}`
245245
.limit(50);
246246
```
247247

248+
#### SET Header Instructions
249+
250+
ES|QL supports SET instructions at the beginning of queries to configure query-level settings. You can add SET instructions either directly in the template or programmatically:
251+
252+
```ts
253+
// Direct in template (recommended)
254+
const query = esql`SET a = 123; FROM index | LIMIT 10`;
255+
256+
// Multiple SET instructions
257+
const query = esql`
258+
SET threshold = 100;
259+
SET limit = 50;
260+
FROM logs | WHERE value > ?threshold | LIMIT ?limit`;
261+
262+
// Programmatically add SET instructions
263+
const query = esql`FROM index | LIMIT 10`;
264+
query.addSetCommand('setting1', 'value1');
265+
query.addSetCommand('setting2', 42);
266+
query.addSetCommand('setting3', true);
267+
268+
// Get all SET instructions
269+
const sets = query.getSetCommands();
270+
// Returns: [{ name: 'setting1', value: '"value1"' }, ...]
271+
272+
// Remove specific SET instruction
273+
query.removeSetCommand('setting2');
274+
275+
// Clear all SET instructions
276+
query.clearSetCommands();
277+
```
278+
279+
SET instructions support string, numeric, and boolean values and are preserved when combining queries or adding more commands.
280+
248281
#### Output Methods
249282

250283
```ts

0 commit comments

Comments
 (0)