@quillsql/react 2.12.31 → 2.12.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/package.json +1 -1
  2. package/dist/esm/components/ReportBuilder/convert.uspec.d.ts +0 -2
  3. package/dist/esm/components/ReportBuilder/convert.uspec.d.ts.map +0 -1
  4. package/dist/esm/components/ReportBuilder/convert.uspec.js +0 -1419
  5. package/dist/esm/test-utils/constants.d.ts +0 -56
  6. package/dist/esm/test-utils/constants.d.ts.map +0 -1
  7. package/dist/esm/test-utils/constants.js +0 -271
  8. package/dist/esm/test-utils/generators.d.ts +0 -4
  9. package/dist/esm/test-utils/generators.d.ts.map +0 -1
  10. package/dist/esm/test-utils/generators.js +0 -37
  11. package/dist/esm/utils/astFilterProcessing.uspec.d.ts +0 -2
  12. package/dist/esm/utils/astFilterProcessing.uspec.d.ts.map +0 -1
  13. package/dist/esm/utils/astFilterProcessing.uspec.js +0 -2877
  14. package/dist/esm/utils/columnProcessing.uspec.d.ts +0 -2
  15. package/dist/esm/utils/columnProcessing.uspec.d.ts.map +0 -1
  16. package/dist/esm/utils/columnProcessing.uspec.js +0 -65
  17. package/dist/esm/utils/dataProcessing.uspec.d.ts +0 -2
  18. package/dist/esm/utils/dataProcessing.uspec.d.ts.map +0 -1
  19. package/dist/esm/utils/dataProcessing.uspec.js +0 -205
  20. package/dist/esm/utils/filterProcessing.uspec.d.ts +0 -2
  21. package/dist/esm/utils/filterProcessing.uspec.d.ts.map +0 -1
  22. package/dist/esm/utils/filterProcessing.uspec.js +0 -245
  23. package/dist/esm/utils/queryConstructor.uspec.d.ts +0 -2
  24. package/dist/esm/utils/queryConstructor.uspec.d.ts.map +0 -1
  25. package/dist/esm/utils/queryConstructor.uspec.js +0 -223
  26. package/dist/esm/utils/report.ispec.d.ts +0 -2
  27. package/dist/esm/utils/report.ispec.d.ts.map +0 -1
  28. package/dist/esm/utils/report.ispec.js +0 -46
  29. package/dist/esm/utils/report.uspec.d.ts +0 -2
  30. package/dist/esm/utils/report.uspec.d.ts.map +0 -1
  31. package/dist/esm/utils/report.uspec.js +0 -66
  32. package/dist/esm/utils/tableProcessing.ispec.d.ts +0 -2
  33. package/dist/esm/utils/tableProcessing.ispec.d.ts.map +0 -1
  34. package/dist/esm/utils/tableProcessing.ispec.js +0 -61
@@ -1,1419 +0,0 @@
1
- import { convertASTToPivotData, convertGroupBy, convertUnaryToBinary, } from './convert';
2
- import { convertColumnInfoToColumnInternal } from '../../utils/columnProcessing';
3
- function generateCaseWhenColumnAst(columnField, valueField, caseValue) {
4
- const defaultCaseWhen = {
5
- type: 'expr',
6
- expr: {
7
- type: 'aggr_func',
8
- name: 'SUM',
9
- args: {
10
- expr: {
11
- type: 'case',
12
- expr: null,
13
- args: [
14
- {
15
- type: 'when',
16
- cond: {
17
- type: 'binary_expr',
18
- operator: '=',
19
- left: {
20
- type: 'column_ref',
21
- table: null,
22
- column: 'category',
23
- },
24
- right: {
25
- type: 'single_quote_string',
26
- value: 'Food',
27
- },
28
- },
29
- result: {
30
- type: 'column_ref',
31
- table: null,
32
- column: 'amount',
33
- },
34
- },
35
- {
36
- type: 'else',
37
- result: {
38
- type: 'number',
39
- value: 0,
40
- },
41
- },
42
- ],
43
- },
44
- },
45
- over: null,
46
- },
47
- as: 'total_food_spend',
48
- };
49
- defaultCaseWhen.expr.args.expr.args[0].cond.left.column = columnField;
50
- defaultCaseWhen.expr.args.expr.args[0].result.column = valueField;
51
- defaultCaseWhen.expr.args.expr.args[0].cond.right.value = caseValue;
52
- return defaultCaseWhen;
53
- }
54
- describe('convert', () => {
55
- const emptySelect = {
56
- with: null,
57
- type: 'select',
58
- options: null,
59
- distinct: null,
60
- columns: [],
61
- from: [
62
- {
63
- db: null,
64
- table: 'transactions',
65
- as: null,
66
- },
67
- ],
68
- where: null,
69
- groupby: null,
70
- having: null,
71
- orderby: null,
72
- limit: null,
73
- };
74
- const schema = [
75
- {
76
- viewQuery: 'SELECT * FROM transactions',
77
- name: 'transactions',
78
- isVisible: true,
79
- _id: '1',
80
- clientId: 'dummy',
81
- displayName: 'transactions',
82
- columns: [
83
- {
84
- name: 'created_at',
85
- fieldType: 'date',
86
- displayName: 'created_at',
87
- isVisible: true,
88
- _id: '1',
89
- },
90
- {
91
- name: 'amount',
92
- fieldType: 'int4',
93
- displayName: 'amount',
94
- isVisible: true,
95
- _id: '2',
96
- },
97
- {
98
- name: 'merchant',
99
- fieldType: 'text',
100
- displayName: 'merchant',
101
- isVisible: true,
102
- _id: '3',
103
- },
104
- {
105
- name: 'user_name',
106
- fieldType: 'text',
107
- displayName: 'user_name',
108
- isVisible: true,
109
- _id: '4',
110
- },
111
- ],
112
- },
113
- ];
114
- describe('convertGroupBy', () => {
115
- const defaultSelectAST = {
116
- with: null,
117
- type: 'select',
118
- options: null,
119
- distinct: null,
120
- columns: [
121
- {
122
- type: 'expr',
123
- expr: {
124
- type: 'column_ref',
125
- table: null,
126
- column: 'created_at',
127
- },
128
- as: null,
129
- },
130
- {
131
- type: 'expr',
132
- expr: {
133
- type: 'column_ref',
134
- table: null,
135
- column: 'amount',
136
- },
137
- as: null,
138
- },
139
- {
140
- type: 'expr',
141
- expr: {
142
- type: 'column_ref',
143
- table: null,
144
- column: 'merchant',
145
- },
146
- as: null,
147
- },
148
- ],
149
- from: [
150
- {
151
- db: null,
152
- table: 'transactions',
153
- as: null,
154
- },
155
- ],
156
- where: null,
157
- groupby: null,
158
- having: null,
159
- orderby: null,
160
- limit: null,
161
- };
162
- const sumAggAST = {
163
- type: 'expr',
164
- expr: {
165
- type: 'aggr_func',
166
- name: 'SUM',
167
- args: {
168
- expr: {
169
- type: 'column_ref',
170
- table: null,
171
- column: 'amount',
172
- },
173
- },
174
- over: null,
175
- },
176
- as: 'amount',
177
- };
178
- it('should process the aggregation, value and row fields from the ast', () => {
179
- const pivotAST = { ...defaultSelectAST };
180
- pivotAST.columns = [...pivotAST.columns, sumAggAST];
181
- pivotAST.groupby = [
182
- {
183
- type: 'column_ref',
184
- table: null,
185
- column: {
186
- expr: {
187
- type: 'default',
188
- value: 'created_at',
189
- },
190
- },
191
- },
192
- ];
193
- const result = convertGroupBy(pivotAST, null, schema);
194
- expect(result).toBeDefined();
195
- expect(result.pivot?.rowField).toEqual('created_at');
196
- expect(result.pivot?.valueField).toEqual('amount');
197
- expect(result.ast).toBeDefined();
198
- // @ts-ignore
199
- expect(result.ast.columns.length).toEqual(3);
200
- });
201
- it('should process an ast with 2 group by columns for the 2d pivot information from the ast and modify the ast to only have basic column selections', () => {
202
- const pivotAST = { ...defaultSelectAST };
203
- pivotAST.columns = [...pivotAST.columns, sumAggAST];
204
- pivotAST.groupby = [
205
- {
206
- type: 'double_quote_string',
207
- value: 'merchant',
208
- },
209
- {
210
- type: 'double_quote_string',
211
- value: 'created_at',
212
- },
213
- ];
214
- const result = convertGroupBy(pivotAST, null, schema);
215
- expect(result?.pivot?.rowField).toEqual('created_at');
216
- expect(result?.pivot?.columnField).toEqual('merchant');
217
- expect(result?.pivot?.valueField).toEqual('amount');
218
- // @ts-ignore
219
- expect(result.ast.groupby).toEqual(null);
220
- // @ts-ignore
221
- expect(result.ast.columns.length).toEqual(3);
222
- });
223
- it('should process an ast with case when columns for the 2d pivot information from the ast and modify the ast to only have basic column selections', () => {
224
- const pivotAST = { ...defaultSelectAST };
225
- pivotAST.columns = [
226
- ...pivotAST.columns,
227
- generateCaseWhenColumnAst('merchant', 'amount', 'Food'),
228
- generateCaseWhenColumnAst('merchant', 'amount', 'Clothing'),
229
- ];
230
- pivotAST.groupby = [
231
- {
232
- type: 'double_quote_string',
233
- value: 'created_at',
234
- },
235
- ];
236
- const result = convertGroupBy(pivotAST, null, schema);
237
- expect(result?.pivot?.rowField).toEqual('created_at');
238
- expect(result?.pivot?.columnField).toEqual('merchant');
239
- expect(result?.pivot?.valueField).toEqual('amount');
240
- // @ts-ignore
241
- expect(result.ast.groupby).toEqual(null);
242
- // @ts-ignore
243
- expect(result.ast.columns.length).toEqual(3);
244
- });
245
- it('should maintain columns that are in the schema but convert them to a basic ast column select format', () => {
246
- const dateTruncAst = { ...defaultSelectAST };
247
- const schemaInfo = [...schema];
248
- // @ts-ignore
249
- schemaInfo[0].columns.push({
250
- name: 'updated_at',
251
- fieldType: 'date',
252
- displayName: 'updated_at',
253
- isVisible: true,
254
- _id: '3',
255
- });
256
- dateTruncAst.columns = [
257
- ...defaultSelectAST.columns,
258
- {
259
- type: 'expr',
260
- expr: {
261
- type: 'function',
262
- name: 'DATE_TRUNC',
263
- args: {
264
- type: 'expr_list',
265
- value: [
266
- {
267
- type: 'single_quote_string',
268
- value: 'month',
269
- },
270
- {
271
- type: 'column_ref',
272
- table: null,
273
- column: 'updated_at',
274
- },
275
- ],
276
- },
277
- },
278
- as: 'month',
279
- },
280
- ];
281
- const result = convertGroupBy(dateTruncAst, null, schemaInfo);
282
- // @ts-ignore
283
- expect(result.ast.columns.length).toEqual(4);
284
- });
285
- it('should parse out extract columns from the ast column list and replace them with the basic column select', () => {
286
- const extractAst = { ...defaultSelectAST };
287
- const schemaInfo = [...schema];
288
- schemaInfo[0] &&
289
- schemaInfo[0].columns.push({
290
- name: 'updated_at',
291
- fieldType: 'date',
292
- displayName: 'updated_at',
293
- isVisible: true,
294
- _id: '3',
295
- });
296
- extractAst.columns = [
297
- ...defaultSelectAST.columns,
298
- {
299
- expr: {
300
- type: 'extract',
301
- args: {
302
- field: 'month',
303
- source: {
304
- type: 'column_ref',
305
- table: null,
306
- column: 'updated_at',
307
- },
308
- },
309
- },
310
- as: 'month',
311
- },
312
- ];
313
- extractAst.groupby = [
314
- {
315
- type: 'column_ref',
316
- table: null,
317
- column: 'user_name',
318
- },
319
- {
320
- type: 'column_ref',
321
- table: null,
322
- column: 'updated_at',
323
- },
324
- ];
325
- const result = convertGroupBy(extractAst, null, schemaInfo);
326
- // @ts-ignore
327
- expect(result.ast.columns.length).toEqual(5);
328
- });
329
- it('should take field type into account and apply a numeric value field if it is present', () => {
330
- const extractAst = emptySelect;
331
- extractAst.columns = [
332
- {
333
- type: 'expr',
334
- expr: {
335
- type: 'double_quote_string',
336
- column: 'created_at',
337
- },
338
- as: 'created_at',
339
- },
340
- {
341
- type: 'expr',
342
- expr: {
343
- type: 'double_quote_string',
344
- column: 'amount',
345
- },
346
- as: 'amount',
347
- },
348
- {
349
- type: 'expr',
350
- expr: {
351
- type: 'double_quote_string',
352
- column: 'user_name',
353
- },
354
- as: 'user_name',
355
- },
356
- ];
357
- extractAst.groupby = [
358
- {
359
- type: 'column_ref',
360
- table: null,
361
- column: 'created_at',
362
- },
363
- {
364
- type: 'column_ref',
365
- table: null,
366
- column: 'amount',
367
- },
368
- {
369
- type: 'column_ref',
370
- table: null,
371
- column: 'user_name',
372
- },
373
- ];
374
- const result = convertGroupBy(extractAst, null, schema);
375
- // @ts-ignore
376
- expect(result.pivot.valueField).toEqual('amount');
377
- // @ts-ignore
378
- expect(result.pivot.rowField).toEqual('created_at');
379
- });
380
- it('should set aggregation type to count if there is no numeric field', () => {
381
- const extractAst = emptySelect;
382
- extractAst.columns = [
383
- {
384
- type: 'expr',
385
- expr: {
386
- type: 'double_quote_string',
387
- column: 'created_at',
388
- },
389
- as: 'created_at',
390
- },
391
- {
392
- type: 'expr',
393
- expr: {
394
- type: 'double_quote_string',
395
- column: 'user_name',
396
- },
397
- as: 'user_name',
398
- },
399
- ];
400
- extractAst.groupby = [
401
- {
402
- type: 'column_ref',
403
- table: null,
404
- column: 'created_at',
405
- },
406
- {
407
- type: 'column_ref',
408
- table: null,
409
- column: 'amount',
410
- },
411
- {
412
- type: 'column_ref',
413
- table: null,
414
- column: 'user_name',
415
- },
416
- ];
417
- const result = convertGroupBy(extractAst, null, schema);
418
- // @ts-ignore
419
- expect(result.pivot.aggregationType).toEqual('count');
420
- // @ts-ignore
421
- expect(result.pivot.rowField).toEqual('created_at');
422
- });
423
- it('should extract out non numeric value fields and replace it with null and set the aggregation type to count', () => {
424
- const extractAst = {
425
- with: null,
426
- type: 'select',
427
- options: null,
428
- distinct: null,
429
- columns: [
430
- {
431
- type: 'expr',
432
- expr: {
433
- type: 'column_ref',
434
- table: null,
435
- column: 'user_name',
436
- },
437
- as: null,
438
- },
439
- {
440
- type: 'expr',
441
- expr: {
442
- type: 'aggr_func',
443
- name: 'SUM',
444
- args: {
445
- expr: {
446
- type: 'case',
447
- expr: null,
448
- args: [
449
- {
450
- type: 'when',
451
- cond: {
452
- type: 'binary_expr',
453
- operator: '=',
454
- left: {
455
- type: 'column_ref',
456
- table: null,
457
- column: 'category',
458
- },
459
- right: {
460
- type: 'single_quote_string',
461
- value: 'Food',
462
- },
463
- },
464
- result: {
465
- type: 'number',
466
- value: 1,
467
- },
468
- },
469
- {
470
- type: 'else',
471
- result: {
472
- type: 'number',
473
- value: 0,
474
- },
475
- },
476
- ],
477
- },
478
- },
479
- over: null,
480
- },
481
- as: 'total_food_spend',
482
- },
483
- {
484
- type: 'expr',
485
- expr: {
486
- type: 'aggr_func',
487
- name: 'SUM',
488
- args: {
489
- expr: {
490
- type: 'case',
491
- expr: null,
492
- args: [
493
- {
494
- type: 'when',
495
- cond: {
496
- type: 'binary_expr',
497
- operator: '=',
498
- left: {
499
- type: 'column_ref',
500
- table: null,
501
- column: 'category',
502
- },
503
- right: {
504
- type: 'single_quote_string',
505
- value: 'Fuel',
506
- },
507
- },
508
- result: {
509
- type: 'number',
510
- value: 1,
511
- },
512
- },
513
- {
514
- type: 'else',
515
- result: {
516
- type: 'number',
517
- value: 0,
518
- },
519
- },
520
- ],
521
- },
522
- },
523
- over: null,
524
- },
525
- as: 'total_fuel_spend',
526
- },
527
- {
528
- type: 'expr',
529
- expr: {
530
- type: 'function',
531
- name: 'DATE_TRUNC',
532
- args: {
533
- type: 'expr_list',
534
- value: [
535
- {
536
- type: 'single_quote_string',
537
- value: 'month',
538
- },
539
- {
540
- type: 'column_ref',
541
- table: null,
542
- column: 'created_at',
543
- },
544
- ],
545
- },
546
- },
547
- as: 'month',
548
- },
549
- ],
550
- from: [
551
- {
552
- db: null,
553
- table: 'transactions',
554
- as: null,
555
- },
556
- ],
557
- where: null,
558
- groupby: [
559
- {
560
- type: 'column_ref',
561
- table: null,
562
- column: 'user_name',
563
- },
564
- {
565
- type: 'function',
566
- name: 'DATE_TRUNC',
567
- args: {
568
- type: 'expr_list',
569
- value: [
570
- {
571
- type: 'single_quote_string',
572
- value: 'month',
573
- },
574
- {
575
- type: 'column_ref',
576
- table: null,
577
- column: 'created_at',
578
- },
579
- ],
580
- },
581
- },
582
- ],
583
- having: null,
584
- orderby: [
585
- {
586
- expr: {
587
- type: 'column_ref',
588
- table: null,
589
- column: 'user_name',
590
- },
591
- type: 'ASC',
592
- },
593
- {
594
- expr: {
595
- type: 'function',
596
- name: 'DATE_TRUNC',
597
- args: {
598
- type: 'expr_list',
599
- value: [
600
- {
601
- type: 'single_quote_string',
602
- value: 'month',
603
- },
604
- {
605
- type: 'column_ref',
606
- table: null,
607
- column: 'created_at',
608
- },
609
- ],
610
- },
611
- },
612
- type: 'ASC',
613
- },
614
- ],
615
- limit: null,
616
- };
617
- const result = convertGroupBy(extractAst, null, schema);
618
- // @ts-ignore
619
- expect(result.pivot.aggregationType).toEqual('count');
620
- // @ts-ignore
621
- expect(result.pivot.rowField).toEqual('created_at');
622
- });
623
- it('should be able to handle this case', () => {
624
- const newAST = {
625
- with: null,
626
- type: 'select',
627
- options: null,
628
- distinct: {
629
- type: null,
630
- },
631
- columns: [
632
- {
633
- type: 'expr',
634
- expr: {
635
- type: 'function',
636
- name: 'DATE_TRUNC',
637
- args: {
638
- type: 'expr_list',
639
- value: [
640
- {
641
- type: 'single_quote_string',
642
- value: 'week',
643
- },
644
- {
645
- type: 'double_quote_string',
646
- value: 'created_at',
647
- },
648
- ],
649
- },
650
- },
651
- as: 'week_start',
652
- },
653
- {
654
- type: 'expr',
655
- expr: {
656
- type: 'aggr_func',
657
- name: 'SUM',
658
- args: {
659
- expr: {
660
- type: 'double_quote_string',
661
- value: 'amount',
662
- },
663
- },
664
- over: null,
665
- },
666
- as: 'total_amount',
667
- },
668
- {
669
- type: 'expr',
670
- expr: {
671
- type: 'aggr_func',
672
- name: 'COUNT',
673
- args: {
674
- expr: {
675
- type: 'star',
676
- value: '*',
677
- },
678
- },
679
- over: null,
680
- },
681
- as: 'transaction_count',
682
- },
683
- ],
684
- from: [
685
- {
686
- db: null,
687
- table: 'transactions',
688
- as: null,
689
- },
690
- ],
691
- where: null,
692
- groupby: [
693
- {
694
- type: 'function',
695
- name: 'DATE_TRUNC',
696
- args: {
697
- type: 'expr_list',
698
- value: [
699
- {
700
- type: 'single_quote_string',
701
- value: 'week',
702
- },
703
- {
704
- type: 'double_quote_string',
705
- value: 'created_at',
706
- },
707
- ],
708
- },
709
- },
710
- ],
711
- having: null,
712
- orderby: null,
713
- limit: null,
714
- window: null,
715
- };
716
- // @ts-ignore
717
- const result = convertGroupBy(newAST, null, schema);
718
- // @ts-ignore
719
- expect(result.pivot.aggregationType).toEqual('sum');
720
- // @ts-ignore
721
- expect(result.pivot.rowField).toEqual('created_at');
722
- });
723
- it('should extract any date_trunc, timestamp_trunc and date_part to row field', () => {
724
- const extractAst = {
725
- with: null,
726
- type: 'select',
727
- options: null,
728
- distinct: null,
729
- columns: [
730
- {
731
- type: 'expr',
732
- expr: {
733
- type: 'function',
734
- name: {
735
- name: [
736
- {
737
- type: 'default',
738
- value: 'date_trunc',
739
- },
740
- ],
741
- },
742
- args: {
743
- type: 'expr_list',
744
- value: [
745
- {
746
- type: 'single_quote_string',
747
- value: 'year',
748
- },
749
- {
750
- type: 'column_ref',
751
- table: null,
752
- column: {
753
- expr: {
754
- type: 'double_quote_string',
755
- value: 'created_at',
756
- },
757
- },
758
- },
759
- ],
760
- },
761
- },
762
- as: 'year',
763
- },
764
- {
765
- type: 'expr',
766
- expr: {
767
- type: 'column_ref',
768
- table: null,
769
- column: {
770
- expr: {
771
- type: 'double_quote_string',
772
- value: 'id',
773
- },
774
- },
775
- },
776
- as: null,
777
- },
778
- {
779
- type: 'expr',
780
- expr: {
781
- type: 'column_ref',
782
- table: null,
783
- column: {
784
- expr: {
785
- type: 'double_quote_string',
786
- value: 'amount',
787
- },
788
- },
789
- },
790
- as: null,
791
- },
792
- {
793
- type: 'expr',
794
- expr: {
795
- type: 'column_ref',
796
- table: null,
797
- column: {
798
- expr: {
799
- type: 'double_quote_string',
800
- value: 'merchant',
801
- },
802
- },
803
- },
804
- as: null,
805
- },
806
- {
807
- type: 'expr',
808
- expr: {
809
- type: 'column_ref',
810
- table: null,
811
- column: {
812
- expr: {
813
- type: 'double_quote_string',
814
- value: 'user_name',
815
- },
816
- },
817
- },
818
- as: null,
819
- },
820
- {
821
- type: 'expr',
822
- expr: {
823
- type: 'column_ref',
824
- table: null,
825
- column: {
826
- expr: {
827
- type: 'double_quote_string',
828
- value: 'category',
829
- },
830
- },
831
- },
832
- as: null,
833
- },
834
- ],
835
- from: [
836
- {
837
- db: null,
838
- table: 'transactions',
839
- as: null,
840
- },
841
- ],
842
- where: null,
843
- groupby: null,
844
- having: null,
845
- orderby: null,
846
- limit: {
847
- seperator: '',
848
- value: [],
849
- },
850
- };
851
- const result = convertGroupBy(extractAst, null, schema);
852
- // @ts-ignore
853
- expect(result.pivot.aggregationType).toEqual('sum');
854
- // @ts-ignore
855
- expect(result.pivot.valueField).toEqual('amount');
856
- // @ts-ignore
857
- expect(result.pivot.rowField).toEqual('created_at');
858
- });
859
- });
860
- describe('convertASTToPivotData', () => {
861
- it('should convert an aggregate function to a value field only pivot', () => {
862
- const extractAst = emptySelect;
863
- extractAst.columns = [
864
- {
865
- type: 'expr',
866
- expr: {
867
- type: 'aggr_func',
868
- name: 'SUM',
869
- args: {
870
- expr: {
871
- type: 'column_ref',
872
- table: null,
873
- column: 'amount',
874
- },
875
- },
876
- over: null,
877
- },
878
- as: null,
879
- },
880
- ];
881
- // @ts-ignore
882
- const columnInteralArray = schema[0].columns.map((column) => {
883
- return convertColumnInfoToColumnInternal(column);
884
- });
885
- const result = convertASTToPivotData(extractAst, columnInteralArray);
886
- expect(result).toBeDefined();
887
- expect(result?.valueField).toEqual('amount');
888
- });
889
- it('should convert a CASE WHEN function to the right pivot', () => {
890
- const extractAst = emptySelect;
891
- extractAst.columns = [
892
- {
893
- type: 'expr',
894
- expr: {
895
- type: 'double_quote_string',
896
- column: 'user_name',
897
- },
898
- as: null,
899
- },
900
- generateCaseWhenColumnAst('merchant', 'amount', 'Food'),
901
- ];
902
- extractAst.groupby = [
903
- {
904
- type: 'column_ref',
905
- table: null,
906
- column: 'user_name',
907
- },
908
- ];
909
- // @ts-ignore
910
- const columnInteralArray = schema[0].columns.map((column) => {
911
- return convertColumnInfoToColumnInternal(column);
912
- });
913
- const result = convertASTToPivotData(extractAst, columnInteralArray);
914
- expect(result).toBeDefined();
915
- expect(result?.valueField).toEqual('amount');
916
- expect(result?.rowField).toEqual('user_name');
917
- expect(result?.columnField).toEqual('merchant');
918
- });
919
- it('should use the first two group by fields as the pivot row field and column field', () => {
920
- const extractAst = emptySelect;
921
- extractAst.columns = [
922
- {
923
- type: 'expr',
924
- expr: {
925
- type: 'aggr_func',
926
- name: 'SUM',
927
- args: {
928
- expr: {
929
- type: 'column_ref',
930
- table: null,
931
- column: 'amount',
932
- },
933
- },
934
- over: null,
935
- },
936
- as: null,
937
- },
938
- {
939
- type: 'expr',
940
- expr: {
941
- type: 'double_quote_string',
942
- column: 'user_name',
943
- },
944
- as: null,
945
- },
946
- {
947
- type: 'expr',
948
- expr: {
949
- type: 'double_quote_string',
950
- column: 'merchant',
951
- },
952
- as: null,
953
- },
954
- ];
955
- extractAst.groupby = [
956
- {
957
- type: 'column_ref',
958
- table: null,
959
- column: 'user_name',
960
- },
961
- {
962
- type: 'column_ref',
963
- table: null,
964
- column: 'merchant',
965
- },
966
- ];
967
- // @ts-ignore
968
- const columnInteralArray = schema[0].columns.map((column) => {
969
- return convertColumnInfoToColumnInternal(column);
970
- });
971
- const result = convertASTToPivotData(extractAst, columnInteralArray);
972
- expect(result).toBeDefined();
973
- expect(result?.valueField).toEqual('amount');
974
- expect(result?.rowField).toEqual('user_name');
975
- expect(result?.columnField).toEqual('merchant');
976
- });
977
- it('should always use date_trunc columns as the row field', () => {
978
- const extractAst = emptySelect;
979
- extractAst.columns = [
980
- {
981
- type: 'expr',
982
- expr: {
983
- type: 'aggr_func',
984
- name: 'SUM',
985
- args: {
986
- expr: {
987
- type: 'column_ref',
988
- table: null,
989
- column: 'amount',
990
- },
991
- },
992
- over: null,
993
- },
994
- as: null,
995
- },
996
- {
997
- type: 'expr',
998
- expr: {
999
- type: 'double_quote_string',
1000
- column: 'user_name',
1001
- },
1002
- as: null,
1003
- },
1004
- {
1005
- type: 'expr',
1006
- expr: {
1007
- type: 'double_quote_string',
1008
- column: 'created_at',
1009
- },
1010
- as: null,
1011
- },
1012
- ];
1013
- extractAst.groupby = [
1014
- {
1015
- type: 'column_ref',
1016
- table: null,
1017
- column: 'user_name',
1018
- },
1019
- {
1020
- type: 'function',
1021
- name: 'date_trunct',
1022
- args: {
1023
- type: 'expr_list',
1024
- value: [
1025
- {
1026
- type: 'single_quote_string',
1027
- value: 'month',
1028
- },
1029
- {
1030
- type: 'column_ref',
1031
- table: null,
1032
- column: 'created_at',
1033
- },
1034
- ],
1035
- },
1036
- },
1037
- ];
1038
- // @ts-ignore
1039
- const columnInteralArray = schema[0].columns.map((column) => {
1040
- return convertColumnInfoToColumnInternal(column);
1041
- });
1042
- const result = convertASTToPivotData(extractAst, columnInteralArray);
1043
- expect(result).toBeDefined();
1044
- expect(result?.valueField).toEqual('amount');
1045
- expect(result?.rowField).toEqual('created_at');
1046
- expect(result?.columnField).toEqual('user_name');
1047
- });
1048
- it('should process aliasing properly for pivots', () => {
1049
- const extractAst = emptySelect;
1050
- extractAst.columns = [
1051
- {
1052
- type: 'expr',
1053
- expr: {
1054
- type: 'aggr_func',
1055
- name: 'SUM',
1056
- args: {
1057
- expr: {
1058
- type: 'column_ref',
1059
- table: null,
1060
- column: 'amount',
1061
- },
1062
- },
1063
- over: null,
1064
- },
1065
- as: 'total_sum',
1066
- },
1067
- {
1068
- type: 'expr',
1069
- expr: {
1070
- type: 'double_quote_string',
1071
- column: 'created_at',
1072
- },
1073
- as: 'date_info',
1074
- },
1075
- ];
1076
- extractAst.groupby = [
1077
- {
1078
- type: 'column_ref',
1079
- table: null,
1080
- column: 'date_info',
1081
- },
1082
- ];
1083
- // @ts-ignore
1084
- const columnInteralArray = schema[0].columns.map((column) => {
1085
- return convertColumnInfoToColumnInternal(column);
1086
- });
1087
- const result = convertASTToPivotData(extractAst, columnInteralArray);
1088
- expect(result).toBeDefined();
1089
- expect(result?.valueField).toEqual('amount');
1090
- expect(result?.rowField).toEqual('created_at');
1091
- expect(result?.aggregationType).toEqual('sum');
1092
- expect(result?.rowFieldType).toEqual('date');
1093
- expect(result?.sortFieldType).toEqual('date');
1094
- });
1095
- it('should not sign a numeric type to row or column field', () => {
1096
- const extractAst = emptySelect;
1097
- extractAst.columns = [
1098
- {
1099
- type: 'expr',
1100
- expr: {
1101
- type: 'double_quote_string',
1102
- column: 'created_at',
1103
- },
1104
- as: 'created_at',
1105
- },
1106
- {
1107
- type: 'expr',
1108
- expr: {
1109
- type: 'double_quote_string',
1110
- column: 'amount',
1111
- },
1112
- as: 'amount',
1113
- },
1114
- {
1115
- type: 'expr',
1116
- expr: {
1117
- type: 'double_quote_string',
1118
- column: 'user_name',
1119
- },
1120
- as: 'user_name',
1121
- },
1122
- ];
1123
- extractAst.groupby = [
1124
- {
1125
- type: 'column_ref',
1126
- table: null,
1127
- column: 'created_at',
1128
- },
1129
- {
1130
- type: 'column_ref',
1131
- table: null,
1132
- column: 'amount',
1133
- },
1134
- {
1135
- type: 'column_ref',
1136
- table: null,
1137
- column: 'user_name',
1138
- },
1139
- ];
1140
- // @ts-ignore
1141
- const columnInteralArray = schema[0].columns.map((column) => {
1142
- return convertColumnInfoToColumnInternal(column);
1143
- });
1144
- const result = convertASTToPivotData(extractAst, columnInteralArray);
1145
- expect(result).toBeDefined();
1146
- expect(result?.rowField).toEqual('created_at');
1147
- expect(result?.aggregationType).toEqual(undefined);
1148
- expect(result?.rowFieldType).toEqual('date');
1149
- expect(result?.columnField).toEqual('user_name');
1150
- });
1151
- });
1152
- describe('convertUnaryToBinary', () => {
1153
- const defaultSelectAST = {
1154
- with: null,
1155
- type: 'select',
1156
- options: null,
1157
- distinct: null,
1158
- columns: [
1159
- {
1160
- type: 'expr',
1161
- expr: {
1162
- type: 'column_ref',
1163
- table: null,
1164
- column: 'created_at',
1165
- },
1166
- as: null,
1167
- },
1168
- {
1169
- type: 'expr',
1170
- expr: {
1171
- type: 'column_ref',
1172
- table: null,
1173
- column: 'amount',
1174
- },
1175
- as: null,
1176
- },
1177
- {
1178
- type: 'expr',
1179
- expr: {
1180
- type: 'column_ref',
1181
- table: null,
1182
- column: 'merchant',
1183
- },
1184
- as: null,
1185
- },
1186
- ],
1187
- from: [
1188
- {
1189
- db: null,
1190
- table: 'transactions',
1191
- as: null,
1192
- },
1193
- ],
1194
- where: null,
1195
- groupby: null,
1196
- having: null,
1197
- orderby: null,
1198
- limit: null,
1199
- };
1200
- it('should not change a binary_expr where node', () => {
1201
- const ast = {
1202
- ...defaultSelectAST,
1203
- where: {
1204
- type: 'binary_expr',
1205
- operator: 'AND',
1206
- left: {
1207
- type: 'binary_expr',
1208
- operator: '>',
1209
- left: {
1210
- type: 'column_ref',
1211
- table: null,
1212
- column: 'amount',
1213
- },
1214
- right: {
1215
- type: 'number',
1216
- value: 100,
1217
- },
1218
- },
1219
- right: {
1220
- type: 'binary_expr',
1221
- operator: '<',
1222
- left: {
1223
- type: 'column_ref',
1224
- table: null,
1225
- column: 'created_at',
1226
- },
1227
- right: {
1228
- type: 'function',
1229
- name: {
1230
- name: [],
1231
- schema: {
1232
- type: 'default',
1233
- value: 'DATE_SUB',
1234
- },
1235
- },
1236
- args: {
1237
- type: 'expr_list',
1238
- value: [
1239
- {
1240
- type: 'function',
1241
- name: {
1242
- name: [],
1243
- schema: {
1244
- type: 'default',
1245
- value: 'CURDATE',
1246
- },
1247
- },
1248
- args: {
1249
- type: 'expr_list',
1250
- value: [],
1251
- },
1252
- over: null,
1253
- },
1254
- {
1255
- type: 'interval',
1256
- expr: {
1257
- type: 'number',
1258
- value: 1,
1259
- },
1260
- unit: 'month',
1261
- },
1262
- ],
1263
- },
1264
- over: null,
1265
- },
1266
- },
1267
- },
1268
- };
1269
- const result = convertUnaryToBinary(ast);
1270
- expect(result).toBeDefined();
1271
- expect(result).toEqual(ast);
1272
- });
1273
- it('should replace a unary_expr where node', () => {
1274
- const astWithUnary = {
1275
- ...defaultSelectAST,
1276
- where: {
1277
- type: 'unary_expr',
1278
- operator: 'NOT',
1279
- expr: {
1280
- type: 'column_ref',
1281
- table: null,
1282
- column: 'is_something',
1283
- },
1284
- },
1285
- };
1286
- const result = convertUnaryToBinary(astWithUnary);
1287
- expect(result).toBeDefined();
1288
- expect(result?.where?.type).toEqual('binary_expr');
1289
- });
1290
- it('should replace a direct column_ref where node', () => {
1291
- const astWithDirectRef = {
1292
- ...defaultSelectAST,
1293
- where: {
1294
- type: 'column_ref',
1295
- table: null,
1296
- column: 'something',
1297
- },
1298
- };
1299
- const result = convertUnaryToBinary(astWithDirectRef);
1300
- expect(result).toBeDefined();
1301
- expect(result?.where?.type).toEqual('binary_expr');
1302
- });
1303
- it('should replace a unary_expr nested in a where node', () => {
1304
- const astWithNestedUnary = {
1305
- ...defaultSelectAST,
1306
- where: {
1307
- type: 'binary_expr',
1308
- operator: 'AND',
1309
- left: {
1310
- type: 'binary_expr',
1311
- operator: '>',
1312
- left: {
1313
- type: 'column_ref',
1314
- table: null,
1315
- column: 'amount',
1316
- },
1317
- right: {
1318
- type: 'number',
1319
- value: 100,
1320
- },
1321
- },
1322
- right: {
1323
- type: 'unary_expr',
1324
- operator: 'NOT',
1325
- expr: {
1326
- type: 'column_ref',
1327
- table: null,
1328
- column: 'something',
1329
- },
1330
- },
1331
- },
1332
- };
1333
- const result = convertUnaryToBinary(astWithNestedUnary);
1334
- expect(result).toBeDefined();
1335
- expect(result?.where?.type).toEqual('binary_expr');
1336
- expect(result?.where?.left?.type).toEqual('binary_expr');
1337
- expect(result?.where?.right?.type).toEqual('binary_expr');
1338
- });
1339
- it('should replace a direct column_ref nested in a where node', () => {
1340
- const astWithNestedRef = {
1341
- ...defaultSelectAST,
1342
- where: {
1343
- type: 'binary_expr',
1344
- operator: 'AND',
1345
- left: {
1346
- type: 'binary_expr',
1347
- operator: '>',
1348
- left: {
1349
- type: 'column_ref',
1350
- table: null,
1351
- column: 'amount',
1352
- },
1353
- right: {
1354
- type: 'number',
1355
- value: 100,
1356
- },
1357
- },
1358
- right: {
1359
- type: 'column_ref',
1360
- table: null,
1361
- column: 'something',
1362
- },
1363
- },
1364
- };
1365
- const result = convertUnaryToBinary(astWithNestedRef);
1366
- expect(result).toBeDefined();
1367
- expect(result?.where?.type).toEqual('binary_expr');
1368
- expect(result?.where?.left?.type).toEqual('binary_expr');
1369
- expect(result?.where?.right?.type).toEqual('binary_expr');
1370
- });
1371
- it('should replace all unary and direct column_ref in a where node', () => {
1372
- const astWithUnaryAndRef = {
1373
- ...defaultSelectAST,
1374
- where: {
1375
- type: 'binary_expr',
1376
- operator: 'OR',
1377
- left: {
1378
- type: 'binary_expr',
1379
- operator: 'AND',
1380
- left: {
1381
- type: 'unary_expr',
1382
- operator: 'NOT',
1383
- expr: {
1384
- type: 'column_ref',
1385
- table: null,
1386
- column: 'is_something',
1387
- },
1388
- },
1389
- right: {
1390
- type: 'column_ref',
1391
- table: null,
1392
- column: 'something_else',
1393
- },
1394
- },
1395
- right: {
1396
- type: 'binary_expr',
1397
- operator: '=',
1398
- left: {
1399
- type: 'column_ref',
1400
- table: null,
1401
- column: 'this',
1402
- },
1403
- right: {
1404
- type: 'bool',
1405
- value: true,
1406
- },
1407
- },
1408
- },
1409
- };
1410
- const result = convertUnaryToBinary(astWithUnaryAndRef);
1411
- expect(result).toBeDefined();
1412
- expect(result?.where?.type).toEqual('binary_expr');
1413
- expect(result?.where?.left?.type).toEqual('binary_expr');
1414
- expect(result?.where?.right?.type).toEqual('binary_expr');
1415
- expect(result?.where?.left?.left?.type).toEqual('binary_expr');
1416
- expect(result?.where?.left?.right?.type).toEqual('binary_expr');
1417
- });
1418
- });
1419
- });