forked from elastic/esql-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrapping_pretty_printer.test.ts
More file actions
1572 lines (1341 loc) · 51.1 KB
/
wrapping_pretty_printer.test.ts
File metadata and controls
1572 lines (1341 loc) · 51.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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 type { ESQLMap } from '../../types';
import { Walker } from '../../ast/walker';
import type { WrappingPrettyPrinterOptions } from '../wrapping_pretty_printer';
import { WrappingPrettyPrinter } from '../wrapping_pretty_printer';
const reprint = (src: string, opts?: WrappingPrettyPrinterOptions) => {
const { root } = Parser.parse(src);
const text = WrappingPrettyPrinter.print(root, opts);
// console.log(JSON.stringify(root.commands, null, 2));
return { text };
};
const assertReprint = (src: string, expected: string = src) => {
const { text } = reprint(src);
expect(text).toBe(expected);
};
describe('commands', () => {
describe('header commands', () => {
describe('SET', () => {
test('single SET command with short query', () => {
const { text } = reprint('SET timeout = "30s"; FROM index');
expect(text).toBe('SET timeout = "30s";\nFROM index');
});
test('multiple SET commands with short query', () => {
const { text } = reprint(
'SET timeout = "30s"; SET max_results = 100; FROM index | LIMIT 10'
);
expect('\n' + text).toBe(`
SET timeout = "30s";
SET max_results = 100;
FROM index | LIMIT 10`);
});
test('SET command with long query wraps correctly', () => {
const { text } = reprint(
'SET timeout = "30s"; FROM very_long_index_name_that_exceeds_line_length | WHERE very_long_field_name == "value" | LIMIT 100'
);
expect('\n' + text).toBe(`
SET timeout = "30s";
FROM very_long_index_name_that_exceeds_line_length
| WHERE very_long_field_name == "value"
| LIMIT 100`);
});
test('multiple SET commands with long query', () => {
const { text } = reprint(
'SET timeout = "30s"; SET max_results = 1000; FROM very_long_index_name_that_exceeds_line_length | WHERE field == "value"'
);
expect('\n' + text).toBe(`
SET timeout = "30s";
SET max_results = 1000;
FROM very_long_index_name_that_exceeds_line_length | WHERE field == "value"`);
});
test('SET with keyword identifier', () => {
const { text } = reprint('SET key = "value"; FROM index');
expect(text).toBe('SET `key` = "value";\nFROM index');
});
test('SET with numeric value', () => {
const { text } = reprint('SET max_results = 500; FROM index');
expect(text).toBe('SET max_results = 500;\nFROM index');
});
test('SET with boolean value', () => {
const { text } = reprint('SET debug = true; FROM index');
expect(text).toBe('SET debug = TRUE;\nFROM index');
});
});
});
describe('JOIN', () => {
test('with short identifiers', () => {
const { text } = reprint('FROM a | RIGHT JOIN b ON d, e');
expect(text).toBe('FROM a | RIGHT JOIN b ON d, e');
});
test('with long identifiers', () => {
const { text } = reprint(
'FROM aaaaaaaaaaaa | RIGHT JOIN bbbbbbbbbbbbbbbbb ON dddddddddddddddddddddddddddddddddddddddd, eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'
);
expect('\n' + text).toBe(`
FROM aaaaaaaaaaaa
| RIGHT JOIN bbbbbbbbbbbbbbbbb
ON
dddddddddddddddddddddddddddddddddddddddd,
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee`);
});
test('supports binary expressions', () => {
assertReprint(
`FROM employees
| LEFT JOIN asdf
ON
aaaaaaaaaaaaaaaaaaaaaaaaa > bbbbbbbbbbbbbbbbbbbbb AND
ccccccccccccccccccc == dddddddddddddddddddddddddddddddddddddddd`
);
});
});
describe('GROK', () => {
test('two basic arguments', () => {
const { text } = reprint('FROM search-movies | GROK Awards "text"');
expect(text).toBe('FROM search-movies | GROK Awards "text"');
});
test('two long arguments', () => {
const { text } = reprint(
'FROM search-movies | GROK AwardsAwardsAwardsAwardsAwardsAwardsAwardsAwards "texttexttexttexttexttexttexttexttexttexttexttexttexttexttext"'
);
expect('\n' + text).toBe(`
FROM search-movies
| GROK
AwardsAwardsAwardsAwardsAwardsAwardsAwardsAwards
"texttexttexttexttexttexttexttexttexttexttexttexttexttexttext"`);
});
});
describe('DISSECT', () => {
test('two basic arguments', () => {
const { text } = reprint('FROM index | DISSECT input "pattern"');
expect(text).toBe('FROM index | DISSECT input "pattern"');
});
test('two long arguments', () => {
const { text } = reprint(
'FROM index | DISSECT InputInputInputInputInputInputInputInputInputInputInputInputInputInput "PatternPatternPatternPatternPatternPatternPatternPatternPatternPattern"'
);
expect('\n' + text).toBe(`
FROM index
| DISSECT
InputInputInputInputInputInputInputInputInputInputInputInputInputInput
"PatternPatternPatternPatternPatternPatternPatternPatternPatternPattern"`);
});
test('with APPEND_SEPARATOR option', () => {
const { text } = reprint(
'FROM index | DISSECT input "pattern" APPEND_SEPARATOR="<separator>"'
);
expect(text).toBe('FROM index | DISSECT input "pattern" APPEND_SEPARATOR = "<separator>"');
});
test('two long arguments with short APPEND_SEPARATOR option', () => {
const { text } = reprint(
'FROM index | DISSECT InputInputInputInputInputInputInputInputInputInputInputInputInputInput "PatternPatternPatternPatternPatternPatternPatternPatternPatternPattern" APPEND_SEPARATOR="sep"'
);
expect('\n' + text).toBe(`
FROM index
| DISSECT
InputInputInputInputInputInputInputInputInputInputInputInputInputInput
"PatternPatternPatternPatternPatternPatternPatternPatternPatternPattern"
APPEND_SEPARATOR = "sep"`);
});
test('two long arguments with long APPEND_SEPARATOR option', () => {
const { text } = reprint(
'FROM index | DISSECT InputInputInputInputInputInputInputInputInputInputInputInputInputInput "PatternPatternPatternPatternPatternPatternPatternPatternPatternPattern" APPEND_SEPARATOR="<SeparatorSeparatorSeparatorSeparatorSeparatorSeparatorSeparatorSeparator>"'
);
expect('\n' + text).toBe(`
FROM index
| DISSECT
InputInputInputInputInputInputInputInputInputInputInputInputInputInput
"PatternPatternPatternPatternPatternPatternPatternPatternPatternPattern"
APPEND_SEPARATOR =
"<SeparatorSeparatorSeparatorSeparatorSeparatorSeparatorSeparatorSeparator>"`);
});
});
describe('CHANGE_POINT', () => {
test('value only', () => {
const { text } = reprint(`FROM a | CHANGE_POINT value`);
expect(text).toBe('FROM a | CHANGE_POINT value');
});
test('value and key', () => {
const { text } = reprint(`FROM a | CHANGE_POINT value ON key`);
expect(text).toBe('FROM a | CHANGE_POINT value ON `key`');
});
test('value and target', () => {
const { text } = reprint(`FROM a | CHANGE_POINT value AS type, pvalue`);
expect(text).toBe('FROM a | CHANGE_POINT value AS type, pvalue');
});
test('value, key, and target', () => {
const { text } = reprint(`FROM a | CHANGE_POINT value ON key AS type, pvalue`);
expect(text).toBe('FROM a | CHANGE_POINT value ON `key` AS type, pvalue');
});
test('example from docs', () => {
const { text } = reprint(`
FROM k8s
| STATS count=COUNT() BY @timestamp=BUCKET(@timestamp, 1 MINUTE)
| CHANGE_POINT count ON @timestamp AS type, pvalue
| LIMIT 123
`);
expect(text).toBe(
`FROM k8s
| STATS count = COUNT() BY @timestamp = BUCKET(@timestamp, 1 MINUTE)
| CHANGE_POINT count
ON @timestamp
AS type, pvalue
| LIMIT 123`
);
});
});
describe('RERANK', () => {
test('default example', () => {
const { text } = reprint(`FROM a | RERANK "query" ON field1 WITH {"inference_id": "model"}`);
expect(text).toBe('FROM a | RERANK "query" ON field1 WITH {"inference_id": "model"}');
});
test('wraps long query', () => {
const { text } = reprint(
`FROM a | RERANK "this is a very long long long long long long long long long long long long text" ON field1 WITH {"inference_id": "model"}`
);
expect(text).toBe(`FROM a
| RERANK
"this is a very long long long long long long long long long long long long text"
ON field1
WITH {"inference_id": "model"}`);
});
test('two fields', () => {
const { text } = reprint(
`FROM a | RERANK "query" ON field1,field2 WITH {"inference_id": "model"}`
);
expect(text).toBe('FROM a | RERANK "query" ON field1, field2 WITH {"inference_id": "model"}');
});
test('wraps many fields', () => {
const { text } = reprint(
`FROM a | RERANK "query" ON field1,field2,field3,field4,field5,field6,field7,field8,field9,field10,field11,field12 WITH {"inference_id": "model"}`
);
expect(text).toBe(`FROM a
| RERANK "query"
ON field1, field2, field3, field4, field5, field6, field7, field8, field9,
field10, field11, field12
WITH {"inference_id": "model"}`);
});
});
describe('MMR', () => {
test('with query vector', () => {
const { text } = reprint(
'FROM a | MMR [0.5,0.4,0.3,0.2]::dense_vector ON genre LIMIT 10 WITH {"lambda": 0.5}'
);
expect(text).toBe(
`FROM a
| MMR ([0.5, 0.4, 0.3, 0.2])::DENSE_VECTOR
ON genre
LIMIT 10
WITH {"lambda": 0.5}`
);
});
});
describe('FORK', () => {
test('basic fork with simple subqueries', () => {
const { text } = reprint('FROM index | FORK ( KEEP a ) ( KEEP b )');
expect(text).toBe('FROM index | FORK (KEEP a) (KEEP b)');
});
test('fork with longer subqueries', () => {
const { text } = reprint(
'FROM index | FORK ( KEEP field1, field2, field3 | WHERE x > 100 ) ( DROP field4, field5 | LIMIT 50 )',
{ multiline: true }
);
expect(text).toBe(`FROM index
| FORK
(
KEEP field1, field2, field3
| WHERE x > 100
)
(
DROP field4, field5
| LIMIT 50
)`);
});
test('fork with multiple complex subqueries', () => {
const { text } = reprint(
'FROM index | FORK ( STATS count=COUNT() BY category | WHERE count > 10 | SORT count DESC ) ( KEEP name, value | WHERE value IS NOT NULL | LIMIT 100 )',
{ multiline: true }
);
expect(text).toBe(`FROM index
| FORK
(
STATS count = COUNT() BY category
| WHERE count > 10
| SORT count DESC
)
(
KEEP name, value
| WHERE value IS NOT NULL
| LIMIT 100
)`);
});
test('fork with commands before and after it', () => {
const { text } = reprint(
'FROM index | DROP a | FORK ( KEEP field1 | WHERE x > 100 ) ( DROP field2 | LIMIT 50 ) | LIMIT 100',
{ multiline: true }
);
expect(text).toBe(`FROM index
| DROP a
| FORK
(
KEEP field1
| WHERE x > 100
)
(
DROP field2
| LIMIT 50
)
| LIMIT 100`);
});
test('fork with a very long command within', () => {
const { text } = reprint(
'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)',
{ multiline: true }
);
expect(text).toBe(`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
)`);
});
});
describe('PROMQL', () => {
test('realistic command', () => {
const src =
'PROMQL step = "5m" start = ?_tstart end = ?_tend index = kibana_sample_data_logstsdb col0 = (sum(avg(quantile_over_time(0.9, bytes{event.dataset="job"}[5m]))))';
const { text } = reprint(src);
expect('\n' + text).toBe(`
PROMQL
step = "5m" start = ?_tstart end = ?_tend index = kibana_sample_data_logstsdb
col0 = (sum(avg(quantile_over_time(0.9, bytes{event.dataset="job"}[5m]))))`);
});
test('no parens query', () => {
assertReprint('PROMQL query');
assertReprint('PROMQL a = b query');
assertReprint(
'PROMQL some_very_very_ridiculously_long_query_such_that_it_needs_a_newline_break'
);
assertReprint(
'PROMQL a = b some_very_very_ridiculously_long_query_such_that_it_needs_a_newline_break',
[
'PROMQL',
' a = b',
' some_very_very_ridiculously_long_query_such_that_it_needs_a_newline_break',
].join('\n')
);
});
test('query with parens', () => {
assertReprint('PROMQL (query)');
assertReprint('PROMQL a = b (query)');
assertReprint(
'PROMQL (some_very_very_ridiculously_long_query_such_that_it_needs_a_newline_break)',
[
'PROMQL',
' (some_very_very_ridiculously_long_query_such_that_it_needs_a_newline_break)',
].join('\n')
);
assertReprint(
'PROMQL a = b (some_very_very_ridiculously_long_query_such_that_it_needs_a_newline_break)',
[
'PROMQL',
' a = b',
' (some_very_very_ridiculously_long_query_such_that_it_needs_a_newline_break)',
].join('\n')
);
});
test('named query with parens', () => {
assertReprint('PROMQL name = (query)');
assertReprint('PROMQL a = b name = (query)');
assertReprint(
'PROMQL name = (some_very_very_ridiculously_long_query_such_that_it_needs_a_newline_break)',
[
'PROMQL',
' name =',
' (some_very_very_ridiculously_long_query_such_that_it_needs_a_newline_break)',
].join('\n')
);
assertReprint(
'PROMQL a = b name = (some_very_very_ridiculously_long_query_such_that_it_needs_a_newline_break)',
[
'PROMQL',
' a = b',
' name =',
' (some_very_very_ridiculously_long_query_such_that_it_needs_a_newline_break)',
].join('\n')
);
});
});
});
describe('casing', () => {
test('can chose command name casing', () => {
const query = 'FROM index | WHERE a == 123';
const text1 = reprint(query, { lowercase: true }).text;
const text2 = reprint(query, { lowercaseCommands: true }).text;
const text3 = reprint(query, { lowercaseCommands: false }).text;
expect(text1).toBe('from index | where a == 123');
expect(text2).toBe('from index | where a == 123');
expect(text3).toBe('FROM index | WHERE a == 123');
});
test('can chose command option name casing', () => {
const text1 = reprint('FROM a METADATA b', { lowercaseOptions: true }).text;
const text2 = reprint('FROM a METADATA b', { lowercaseOptions: false }).text;
expect(text1).toBe('FROM a metadata b');
expect(text2).toBe('FROM a METADATA b');
});
test('can chose function name casing', () => {
const query = 'FROM index | STATS FN1(), FN2(), FN3()';
const text1 = reprint(query, { lowercase: true }).text;
const text2 = reprint(query, { lowercaseFunctions: true }).text;
const text3 = reprint(query, { lowercaseFunctions: false }).text;
expect(text1).toBe('from index | stats fn1(), fn2(), fn3()');
expect(text2).toBe('FROM index | STATS fn1(), fn2(), fn3()');
expect(text3).toBe('FROM index | STATS FN1(), FN2(), FN3()');
});
test('parameter function name is printed as specified', () => {
const text = reprint('ROW ??functionName(*)').text;
expect(text).toBe('ROW ??functionName(*)');
});
test('parameter function name is printed as specified (single ?)', () => {
const text = reprint('ROW ?functionName(42)').text;
expect(text).toBe('ROW ?functionName(42)');
});
test('can choose keyword casing', () => {
const query = 'FROM index | RENAME a AS b';
const text1 = reprint(query, { lowercase: true }).text;
const text2 = reprint(query, { lowercaseKeywords: true }).text;
const text3 = reprint(query, { lowercaseKeywords: false }).text;
expect(text1).toBe('from index | rename a as b');
expect(text2).toBe('FROM index | RENAME a as b');
expect(text3).toBe('FROM index | RENAME a AS b');
});
test('can chose keyword casing (function nodes)', () => {
const query = 'FROM index | WHERE a LIKE "b"';
const text1 = reprint(query, { lowercase: true }).text;
const text2 = reprint(query, { lowercaseKeywords: true }).text;
const text3 = reprint(query, { lowercaseKeywords: false }).text;
expect(text1).toBe('from index | where a like "b"');
expect(text2).toBe('FROM index | WHERE a like "b"');
expect(text3).toBe('FROM index | WHERE a LIKE "b"');
});
});
describe('short query', () => {
test('can format a simple query to one line', () => {
const query = 'FROM index | WHERE a == 123';
const text = reprint(query).text;
expect(text).toBe('FROM index | WHERE a == 123');
});
test('one line query respects indentation option', () => {
const query = 'FROM index | WHERE a == 123';
const text = reprint(query, { indent: ' ' }).text;
expect(text).toBe(' FROM index | WHERE a == 123');
});
test('can force small query onto multiple lines', () => {
const query = 'FROM index | WHERE a == 123';
const text = reprint(query, { multiline: true }).text;
expect('\n' + text).toBe(`
FROM index
| WHERE a == 123`);
});
test('with initial indentation', () => {
const query = 'FROM index | WHERE a == 123';
const text = reprint(query, { multiline: true, indent: '>' }).text;
expect('\n' + text).toBe(`
>FROM index
> | WHERE a == 123`);
});
describe('map expression', () => {
describe('bare map', () => {
test('with initial indentation', () => {
const query = 'PROMQL a = b c = d e = f g = (query)';
const text = reprint(query).text;
expect('\n' + text).toBe(`
PROMQL a = b c = d e = f g = (query)`);
});
});
});
});
describe('long query', () => {
describe('command arguments', () => {
test('wraps source list', () => {
const query =
'FROM index, another_index, yet_another_index, on-more-index, last_index, very_last_index, ok_this_is_the_last_index';
const text = reprint(query, { indent: '- ' }).text;
expect('\n' + text).toBe(`
- FROM index, another_index, yet_another_index, on-more-index, last_index,
- very_last_index, ok_this_is_the_last_index`);
});
test('wraps source list, leaves one item on last line', () => {
const query =
'FROM index, another_index, yet_another_index, on-more-index, last_index, very_last_index';
const text = reprint(query).text;
expect('\n' + text).toBe(`
FROM index, another_index, yet_another_index, on-more-index, last_index,
very_last_index`);
});
test('for a single very long source, prints a standalone line', () => {
const query =
'FROM index_another_index_yet_another_index_on-more-index_last_index_very_last_index';
const text = reprint(query).text;
expect('\n' + text).toBe(`
FROM
index_another_index_yet_another_index_on-more-index_last_index_very_last_index`);
});
test('keeps sources in a list, as long as at least two fit per line', () => {
const query = `
FROM xxxxxxxxxx, yyyyyyyyyyy, zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz, aaaa,
bbbbbbbbbbbbbbbbbbb, ccccccccccccccccccccccccccc, gggggggggggggggg
`;
const text = reprint(query).text;
expect('\n' + text).toBe(`
FROM xxxxxxxxxx, yyyyyyyyyyy, zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz, aaaa,
bbbbbbbbbbbbbbbbbbb, ccccccccccccccccccccccccccc, gggggggggggggggg`);
});
test('keeps sources in a list, even if the last item consumes more than a line', () => {
const query = `
FROM xxxxxxxxxx, yyyyyyyyyyy, zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz, aaaa,
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
`;
const text = reprint(query).text;
expect('\n' + text).toBe(`
FROM xxxxxxxxxx, yyyyyyyyyyy, zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz, aaaa,
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb`);
});
test('breaks sources per-line, if list layout results into alone source per line', () => {
const query = `
FROM xxxxxxxxxx, yyyyyyyyyyy, zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, // <------------ this one
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, ccccccc, ggggggggg
`;
const text = reprint(query).text;
expect('\n' + text).toBe(`
FROM
xxxxxxxxxx,
yyyyyyyyyyy,
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
ccccccc,
ggggggggg`);
});
test('breaks sources per-line, whe there is one large source', () => {
const query = `
FROM xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, // <------------ this one
yyyyyyyyyyy, ccccccc, ggggggggg
`;
const text = reprint(query).text;
expect('\n' + text).toBe(`
FROM
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
yyyyyyyyyyy,
ccccccc,
ggggggggg`);
});
});
describe('command option', () => {
test('prints short query on a single line', () => {
const query = 'FROM index METADATA _id';
const text = reprint(query).text;
expect(text).toBe(`FROM index METADATA _id`);
});
test('breaks METADATA option to new line, when query reaches wrapping threshold', () => {
const query = `
FROM index1, index2, index2, index3, index4, index5, index6 METADATA _id, _source`;
const text = reprint(query, { pipeTab: ' ' }).text;
expect('\n' + text).toBe(`
FROM index1, index2, index2, index3, index4, index5, index6
METADATA _id, _source`);
});
test("indents options such that they don't align with sub-commands", () => {
const query = `
FROM index1, index2, index2, index3, index4, index5, index6 METADATA _id, _source
| WHERE language == "javascript"
| LIMIT 123`;
const text = reprint(query, { pipeTab: ' ' }).text;
expect('\n' + text).toBe(`
FROM index1, index2, index2, index3, index4, index5, index6
METADATA _id, _source
| WHERE language == "javascript"
| LIMIT 123`);
});
test('indents METADATA option differently than the LIMIT pipe', () => {
const query = `
FROM index1, index2, index2, index3, index4, index5, index6 METADATA _id, _source | LIMIT 10`;
const text = reprint(query, { pipeTab: ' ' }).text;
expect('\n' + text).toBe(`
FROM index1, index2, index2, index3, index4, index5, index6
METADATA _id, _source
| LIMIT 10`);
});
test('indents METADATA option differently than main FROM arguments', () => {
const query = `
FROM index1, index2, index2, index3, index4, index5, index6, index7, index8, index9, index10, index11, index12, index13, index14, index15, index16, index17 METADATA _id, _source`;
const text = reprint(query, { pipeTab: ' ' }).text;
expect('\n' + text).toBe(`
FROM index1, index2, index2, index3, index4, index5, index6, index7, index8,
index9, index10, index11, index12, index13, index14, index15, index16,
index17
METADATA _id, _source`);
});
test('indents METADATA option differently than main FROM arguments when broken per line', () => {
const query = `
FROM index_index_index_index_index_index_index_index_index_index_index_index_1, index_index_index_index_index_index_index_index_index_index_index_index_2, index_index_index_index_index_index_index_index_index_index_index_index_3 METADATA _id, _source`;
const text = reprint(query, { pipeTab: ' ' }).text;
expect('\n' + text).toBe(`
FROM
index_index_index_index_index_index_index_index_index_index_index_index_1,
index_index_index_index_index_index_index_index_index_index_index_index_2,
index_index_index_index_index_index_index_index_index_index_index_index_3
METADATA _id, _source`);
});
test('indents METADATA option different than the source list', () => {
const query =
'FROM index, another_index, another_index, a_very_very_long_index_a_very_very_long_index_a_very_very_long_index, another_index, another_index METADATA _id, _source';
const text = reprint(query, { indent: '👉 ' }).text;
expect('\n' + text).toBe(`
👉 FROM
👉 index,
👉 another_index,
👉 another_index,
👉 a_very_very_long_index_a_very_very_long_index_a_very_very_long_index,
👉 another_index,
👉 another_index
👉 METADATA _id, _source`);
});
test('can break multiple options', () => {
const query =
'from a | enrich policy ON match_field_which_is_very_long WITH new_name1 = field1, new_name2 = field2';
const text = reprint(query, { indent: '👉 ' }).text;
expect('\n' + text).toBe(`
👉 FROM a
👉 | ENRICH policy
👉 ON match_field_which_is_very_long
👉 WITH new_name1 = field1, new_name2 = field2`);
});
test('can break multiple options and wrap option arguments', () => {
const query =
'from a | enrich policy ON match_field WITH new_name1 = field1, new_name2 = field2, new_name3 = field3, new_name4 = field4, new_name5 = field5, new_name6 = field6, new_name7 = field7, new_name8 = field8, new_name9 = field9, new_name10 = field10';
const text = reprint(query, { indent: '👉 ' }).text;
expect('\n' + text).toBe(`
👉 FROM a
👉 | ENRICH policy
👉 ON match_field
👉 WITH new_name1 = field1, new_name2 = field2, new_name3 = field3,
👉 new_name4 = field4, new_name5 = field5, new_name6 = field6,
👉 new_name7 = field7, new_name8 = field8, new_name9 = field9,
👉 new_name10 = field10`);
});
});
describe('function call arguments', () => {
test('renders a one line list, if there is enough space', () => {
const query = `
FROM index
| STATS avg(height), sum(weight), min(age), max(age), count(*)
| LIMIT 10
`;
const text = reprint(query, { indent: '- ' }).text;
expect('\n' + text).toBe(`
- FROM index
- | STATS AVG(height), SUM(weight), MIN(age), MAX(age), COUNT(*)
- | LIMIT 10`);
});
test('STATS with multiple agg fields and BY: with default wrap, breaks at BY when line exceeds width', () => {
const query = `FROM kibana_sample_data_logs
| STATS count = COUNT(*), avg = AVG(bytes), p95 = PERCENTILE(bytes, 95), ext = VALUES(tags.keyword) BY ip
| EVAL newField = CASE(count < 100, "groupA", count > 100 AND count < 500, "groupB", "Other")
| KEEP newField`;
const text = reprint(query).text;
// With default wrap (80), STATS args overflow so options (BY) are broken to a new line
expect(text).toMatch(/\|\s*STATS[\s\S]*?\n\s*BY ip/m);
});
test('STATS with multiple agg fields and BY: with large wrap, query fits on one line so BY stays on same line as STATS', () => {
const query = `FROM kibana_sample_data_logs
| STATS count = COUNT(*), avg = AVG(bytes), p95 = PERCENTILE(bytes, 95), ext = VALUES(tags.keyword) BY ip
| EVAL newField = CASE(count < 100, "groupA", count > 100 AND count < 500, "groupB", "Other")
| KEEP newField`;
const text = reprint(query, { wrap: 120, multiline: true }).text;
// With multiline: true and wrap 120, STATS line fits so BY is not broken to a new line
const statsLine = text.split('\n').find((l) => l.includes('STATS') && l.includes('BY ip'));
expect(statsLine).toBeDefined();
expect(statsLine!.trim()).toMatch(/^\| STATS .+ BY ip$/);
});
test('wraps function list', () => {
const query = `
FROM index
| STATS avg(height), sum(weight), min(age), max(age), count(*), super_function(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)
| LIMIT 10
`;
const text = reprint(query, { indent: '- ' }).text;
expect('\n' + text).toBe(`
- FROM index
- | STATS AVG(height), SUM(weight), MIN(age), MAX(age), COUNT(*),
- SUPER_FUNCTION(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)
- | LIMIT 10`);
});
test('wraps function arguments', () => {
const query = `
FROM index
| STATS avg(height),
super_function(some_column, another_column == "this is string", 1234567890.999991),
sum(weight)
| LIMIT 10
`;
const text = reprint(query).text;
expect('\n' + text).toBe(`
FROM index
| STATS
AVG(height),
SUPER_FUNCTION(some_column, another_column == "this is string",
1234567890.999991),
SUM(weight)
| LIMIT 10`);
});
test('single long function argument is broken by line', () => {
const query = `
FROM index | STATS super_function("xxxx-xxxx-xxxxxxxxxxxx-xxxx-xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxx-xxxx-xxxxxxxx")
`;
const text = reprint(query).text;
expect('\n' + text).toBe(`
FROM index
| STATS
SUPER_FUNCTION(
"xxxx-xxxx-xxxxxxxxxxxx-xxxx-xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxx-xxxx-xxxxxxxx")`);
});
test('break by line function arguments, when wrapping is not enough', () => {
const query = `
FROM index
| STATS avg(height),
super_function("xxxx-xxxx-xxxxxxxxxxxx-xxxx-xxxxxxxx", "yyyy-yyyy-yyyyyyyyyyyy-yyyy-yyyyyyyyyyyy", "zzzz-zzzz-zzzzzzzzzzzzzzz-zzzz-zzzzzzzzzzzzzz"),
sum(weight)
| LIMIT 10
`;
const text = reprint(query).text;
expect('\n' + text).toBe(`
FROM index
| STATS
AVG(height),
SUPER_FUNCTION(
"xxxx-xxxx-xxxxxxxxxxxx-xxxx-xxxxxxxx",
"yyyy-yyyy-yyyyyyyyyyyy-yyyy-yyyyyyyyyyyy",
"zzzz-zzzz-zzzzzzzzzzzzzzz-zzzz-zzzzzzzzzzzzzz"),
SUM(weight)
| LIMIT 10`);
});
test('break by line last function arguments, when wrapping is not enough', () => {
const query = `
FROM index
| STATS avg(height),
super_function("xxxx-xxxx-xxxxxxxxxxxx-xxxx-xxxxxxxx", "yyyy-yyyy-yyyyyyyyyyyy-yyyy-yyyyyyyyyyyy", "zzzz-zzzz-zzzzzzzzzzzzzzz-zzzz-zzzzzzzzzzzzzz"),
| LIMIT 10
`;
const text = reprint(query).text;
expect('\n' + text).toBe(`
FROM index
| STATS
AVG(height),
SUPER_FUNCTION(
"xxxx-xxxx-xxxxxxxxxxxx-xxxx-xxxxxxxx",
"yyyy-yyyy-yyyyyyyyyyyy-yyyy-yyyyyyyyyyyy",
"zzzz-zzzz-zzzzzzzzzzzzzzz-zzzz-zzzzzzzzzzzzzz")
| LIMIT 10`);
});
test('break by line when wrapping would results in lines with a single item', () => {
const query = `
FROM index
| STATS avg(height),
super_function("xxxx-xxxx-xxxxxxxxxxxxx-xxxxx-xxxxxxxx",
1234567890 + 1234567890,
"zzzz-zzzz-zzzzzzzzzzzzzzzzz-zzzz-zzzzzzzzzzzzzz"),
| LIMIT 10
`;
const text = reprint(query).text;
expect('\n' + text).toBe(`
FROM index
| STATS
AVG(height),
SUPER_FUNCTION(
"xxxx-xxxx-xxxxxxxxxxxxx-xxxxx-xxxxxxxx",
1234567890 + 1234567890,
"zzzz-zzzz-zzzzzzzzzzzzzzzzz-zzzz-zzzzzzzzzzzzzz")
| LIMIT 10`);
});
test('break by line when wrapping would results in lines with a single item - 2', () => {
const query = `
FROM index
| STATS avg(height),
super_function(func1(123 + 123123 - 12333.33 / FALSE), func2("abrakadabra what?"), func3(), func4()),
| LIMIT 10
`;
const text = reprint(query).text;
expect('\n' + text).toBe(`
FROM index
| STATS
AVG(height),
SUPER_FUNCTION(
FUNC1(123 + 123123 - 12333.33 / FALSE),
FUNC2("abrakadabra what?"),
FUNC3(),
FUNC4())
| LIMIT 10`);
});
test('can vertically flatten adjacent binary expressions of the same precedence', () => {
const query = `
FROM index
| STATS super_function_name(0.123123123123123 + 888811112.232323123123 + 123123123123.123123123 + 23232323.23232323123 - 123 + 999),
| LIMIT 10
`;
const text = reprint(query).text;
expect('\n' + text).toBe(`
FROM index
| STATS
SUPER_FUNCTION_NAME(
0.123123123123123 +
888811112.2323232 +
123123123123.12312 +
23232323.232323233 -
123 +
999)
| LIMIT 10`);
});
});
describe('map expression', () => {
test('empty map', () => {
const src = `ROW F(0, {"a": 0})`;
const { root } = Parser.parse(src);
const map = Walker.match(root, { type: 'map' }) as ESQLMap;
map.entries = [];
const text = WrappingPrettyPrinter.print(root);
expect(text).toBe(`ROW F(0, {})`);
});
test('supports nested maps', () => {
assertReprint('ROW FN(1, {"foo": "bar", "baz": {"a": 1, "b": 2}})');
});
test('empty map (multiline)', () => {
const src = `ROW F(0, {"a": 0}) | LIMIT 1`;
const { root } = Parser.parse(src);
const map = Walker.match(root, { type: 'map' }) as ESQLMap;
map.entries = [];
const text = WrappingPrettyPrinter.print(root, { multiline: true });
expect(text).toBe(`ROW F(0, {})
| LIMIT 1`);
});
test('single entry map', () => {
const src = `ROW F(0, {"a": 0})`;
const text = reprint(src).text;
expect(text).toBe(`ROW F(0, {"a": 0})`);
});
test('single entry map (multiline)', () => {
const src = `ROW F(0, {"a": 0}) | LIMIT 1`;
const text = reprint(src, { multiline: true }).text;