Skip to content

Commit aa1de3d

Browse files
authored
[ES|QL] Add support for FORK wrapping pretty printing (#233888)
Partially address #231577 ## Summary Add support for FORK wrapping pretty printing ## Before https://github.com/user-attachments/assets/785802b8-191d-438f-acbf-760c3d6b78b4 ## After <img width="463" height="364" alt="image" src="https://github.com/user-attachments/assets/cdf22cc2-8bc5-401d-8501-547b98e816d0" /> ### Checklist Check the PR satisfies following conditions. Reviewers should verify this PR satisfies this list as well. - [ ] Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md) - [ ] [Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html) was added for features that require explanation or tutorials - [ ] [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 - [ ] If a plugin configuration key changed, check if it needs to be allowlisted in the cloud and added to the [docker list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker) - [ ] This was checked for breaking HTTP API changes, and any breaking changes have been approved by the breaking-change committee. The `release_note:breaking` label should be applied in these situations. - [ ] [Flaky Test Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was used on any tests changed - [ ] The PR description includes the appropriate Release Notes section, and the correct `release_note:*` label is applied per the [guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) - [ ] Review the [backport guidelines](https://docs.google.com/document/d/1VyN5k91e5OVumlc0Gb9RPa3h1ewuPE705nRtioPiTvY/edit?usp=sharing) and apply applicable `backport:*` labels. ### Identify risks Does this PR introduce any risks? For example, consider risks like hard to test bugs, performance regression, potential of data loss. Describe the risk, its severity, and mitigation for each identified risk. Invite stakeholders and evaluate how to proceed before merging. - [ ] [See some risk examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx) - [ ] ...
1 parent 136a5a2 commit aa1de3d

File tree

3 files changed

+105
-10
lines changed

3 files changed

+105
-10
lines changed

src/platform/packages/shared/kbn-esql-ast/src/pretty_print/__tests__/wrapping_pretty_printer.test.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,91 @@ FROM index
208208
WITH {"inference_id": "model"}`);
209209
});
210210
});
211+
212+
describe('FORK', () => {
213+
test('basic fork with simple subqueries', () => {
214+
const { text } = reprint('FROM index | FORK ( KEEP a ) ( KEEP b )');
215+
216+
expect(text).toBe('FROM index | FORK (KEEP a) (KEEP b)');
217+
});
218+
219+
test('fork with longer subqueries', () => {
220+
const { text } = reprint(
221+
'FROM index | FORK ( KEEP field1, field2, field3 | WHERE x > 100 ) ( DROP field4, field5 | LIMIT 50 )',
222+
{ multiline: true }
223+
);
224+
225+
expect(text).toBe(`FROM index
226+
| FORK
227+
(
228+
KEEP field1, field2, field3
229+
| WHERE x > 100
230+
)
231+
(
232+
DROP field4, field5
233+
| LIMIT 50
234+
)`);
235+
});
236+
237+
test('fork with multiple complex subqueries', () => {
238+
const { text } = reprint(
239+
'FROM index | FORK ( STATS count=COUNT() BY category | WHERE count > 10 | SORT count DESC ) ( KEEP name, value | WHERE value IS NOT NULL | LIMIT 100 )',
240+
{ multiline: true }
241+
);
242+
243+
expect(text).toBe(`FROM index
244+
| FORK
245+
(
246+
STATS count = COUNT()
247+
BY category
248+
| WHERE count > 10
249+
| SORT count DESC
250+
)
251+
(
252+
KEEP name, value
253+
| WHERE value IS NOT NULL
254+
| LIMIT 100
255+
)`);
256+
});
257+
258+
test('fork with commands before and after it', () => {
259+
const { text } = reprint(
260+
'FROM index | DROP a | FORK ( KEEP field1 | WHERE x > 100 ) ( DROP field2 | LIMIT 50 ) | LIMIT 100',
261+
{ multiline: true }
262+
);
263+
264+
expect(text).toBe(`FROM index
265+
| DROP a
266+
| FORK
267+
(
268+
KEEP field1
269+
| WHERE x > 100
270+
)
271+
(
272+
DROP field2
273+
| LIMIT 50
274+
)
275+
| LIMIT 100`);
276+
});
277+
278+
test('fork with a very long command within', () => {
279+
const { text } = reprint(
280+
'FROM index | FORK ( WHERE x > 100 | KEEP field1, asd, asd, asd, asd, asd, asd, asd, asd, asd, asd, asd, asd, asd, asd, asd, asd, asd, asd) (LIMIT 10)',
281+
{ multiline: true }
282+
);
283+
284+
expect(text).toBe(`FROM index
285+
| FORK
286+
(
287+
WHERE x > 100
288+
| KEEP field1, asd, asd, asd, asd, asd, asd, asd, asd, asd, asd, asd,
289+
asd, asd, asd, asd, asd, asd, asd
290+
)
291+
(
292+
LIMIT 10
293+
)`);
294+
});
295+
});
211296
});
212297

213298
describe('casing', () => {

src/platform/packages/shared/kbn-esql-ast/src/pretty_print/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
* DISSECT input "pattern"
2626
* ```
2727
*/
28-
export const commandsWithNoCommaArgSeparator = new Set(['grok', 'dissect', 'sample']);
28+
export const commandsWithNoCommaArgSeparator = new Set(['grok', 'dissect', 'sample', 'fork']);
2929

3030
/**
3131
* This set tracks command options which use an equals sign to separate

src/platform/packages/shared/kbn-esql-ast/src/pretty_print/wrapping_pretty_printer.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,10 @@ export class WrappingPrettyPrinter {
302302
let remainingCurrentLine = inp.remaining;
303303
let oneArgumentPerLine = false;
304304

305+
if (ctx.node.name === 'fork') {
306+
oneArgumentPerLine = true;
307+
}
308+
305309
for (const child of children(ctx.node)) {
306310
if (getPrettyPrintStats(child).hasLineBreakingDecorations) {
307311
oneArgumentPerLine = true;
@@ -385,9 +389,13 @@ export class WrappingPrettyPrinter {
385389
remaining: this.opts.wrap - indent.length,
386390
suffix: isLastArg ? '' : commaBetweenArgs ? ',' : '',
387391
});
388-
const separator = isFirstArg ? '' : '\n';
389392
const indentation = arg.indented ? '' : indent;
390-
txt += separator + indentation + arg.txt;
393+
let formattedArg = arg.txt;
394+
if (args[i].type === 'query') {
395+
formattedArg = `(\n${this.opts.tab.repeat(2)}${formattedArg}\n${indentation})`;
396+
}
397+
const separator = isFirstArg ? '' : '\n';
398+
txt += separator + indentation + formattedArg;
391399
lines++;
392400
}
393401
}
@@ -733,9 +741,10 @@ export class WrappingPrettyPrinter {
733741
return { txt, lines: args.lines /* add options lines count */ };
734742
})
735743

736-
.on('visitQuery', (ctx) => {
744+
.on('visitQuery', (ctx, inp: Input): Output => {
737745
const opts = this.opts;
738-
const indent = opts.indent ?? '';
746+
const indent = inp?.indent ?? opts.indent ?? '';
747+
const remaining = inp?.remaining ?? opts.wrap;
739748
const commands = ctx.node.commands;
740749
const commandCount = commands.length;
741750

@@ -750,20 +759,21 @@ export class WrappingPrettyPrinter {
750759

751760
if (!multiline) {
752761
const oneLine = indent + BasicPrettyPrinter.print(ctx.node, opts);
753-
if (oneLine.length <= opts.wrap) {
754-
return oneLine;
762+
if (oneLine.length <= remaining) {
763+
return { txt: oneLine };
755764
} else {
756765
multiline = true;
757766
}
758767
}
759768

769+
// Regular top-level query formatting
760770
let text = indent;
761771
const pipedCommandIndent = `${indent}${opts.pipeTab ?? ' '}`;
762772
const cmdSeparator = multiline ? `${pipedCommandIndent}| ` : ' | ';
763773
let i = 0;
764774
let prevOut: Output | undefined;
765775

766-
for (const out of ctx.visitCommands({ indent, remaining: opts.wrap - indent.length })) {
776+
for (const out of ctx.visitCommands({ indent, remaining: remaining - indent.length })) {
767777
const isFirstCommand = i === 0;
768778
const isSecondCommand = i === 1;
769779

@@ -794,10 +804,10 @@ export class WrappingPrettyPrinter {
794804
prevOut = out;
795805
}
796806

797-
return text;
807+
return { txt: text };
798808
});
799809

800810
public print(query: ESQLAstQueryExpression) {
801-
return this.visitor.visitQuery(query, undefined);
811+
return this.visitor.visitQuery(query, undefined).txt;
802812
}
803813
}

0 commit comments

Comments
 (0)