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
#![allow(clippy::missing_errors_doc, clippy::doc_markdown)]
pub mod aggregation;
pub mod comparison;
pub mod destructured_query;
pub mod error;
pub mod filter;
pub mod query_metadata;
pub mod support;
pub mod table;

#[cfg(test)]
mod tests {

    use crate::query_metadata::QueryMetadata;
    use crate::table::TabIdent;
    use crate::{internal, malformed_query, unsupported};

    use super::aggregation::{Aggregation, KoronFunction};
    use super::comparison::CompareOp;
    use super::error::ParseError;
    use super::filter::Filter;

    fn sample_sum() -> Aggregation {
        Aggregation {
            function: KoronFunction::Sum,
            column: "test_column_2".to_string(),
            alias: None,
        }
    }

    fn sample_tab_ident() -> TabIdent {
        TabIdent {
            db: Some("test_db".to_string()),
            schema: Some("test_schema".to_string()),
            table: "test_table_1".to_string(),
        }
    }

    #[test]
    fn basic_aggregation() {
        let cases = [
            ("SUM(test_column_2)", KoronFunction::Sum),
            ("COUNT(test_column_2)", KoronFunction::Count),
        ];

        for (projection, function) in cases {
            let query = &format!("SELECT {projection} FROM test_db.test_schema.test_table_1");

            let expected = Ok(QueryMetadata {
                table: sample_tab_ident(),
                aggregation: Aggregation {
                    function,
                    column: "test_column_2".to_string(),
                    alias: None,
                },
                filter: None,
            });
            assert_eq!(
                QueryMetadata::parse(query),
                expected,
                "\nfailed for aggregation {projection}",
            );
        }
    }

    #[test]
    fn parenthesized_query() {
        let query = "(((SELECT SUM(test_column_2) FROM test_db.test_schema.test_table_1)))";
        let expected = Ok(QueryMetadata {
            table: sample_tab_ident(),
            aggregation: sample_sum(),
            filter: None,
        });
        assert_eq!(QueryMetadata::parse(query), expected);
    }

    #[test]
    fn parenthesized_function() {
        let query = "SELECT (((SUM(test_column_2)))) FROM test_db.test_schema.test_table_1";
        let expected = Ok(QueryMetadata {
            table: sample_tab_ident(),
            aggregation: sample_sum(),
            filter: None,
        });
        assert_eq!(QueryMetadata::parse(query), expected);
    }

    #[test]
    fn parenthesized_column() {
        let query = "SELECT SUM((((test_column_2)))) FROM test_db.test_schema.test_table_1";
        let expected = Ok(QueryMetadata {
            table: sample_tab_ident(),
            aggregation: sample_sum(),
            filter: None,
        });
        assert_eq!(QueryMetadata::parse(query), expected);
    }

    #[test]
    fn result_alias() {
        let query = "SELECT SUM(test_column_2) AS s FROM test_db.test_schema.test_table_1";
        let expected = Ok(QueryMetadata {
            table: sample_tab_ident(),
            aggregation: Aggregation {
                function: KoronFunction::Sum,
                column: "test_column_2".to_string(),
                alias: Some("s".to_string()),
            },
            filter: None,
        });
        assert_eq!(QueryMetadata::parse(query), expected);
    }

    #[test]
    fn table_alias() {
        let query = "SELECT SUM(test_column_2) FROM test_db.test_schema.test_table_1 AS t";
        let expected = Ok(QueryMetadata {
            table: sample_tab_ident(),
            aggregation: sample_sum(),
            filter: None,
        });
        assert_eq!(QueryMetadata::parse(query), expected);
    }

    #[test]
    fn unquoted_function_case_insensitive() {
        let query = "SELECT sum(test_column_2) FROM test_db.test_schema.test_table_1";
        let expected = Ok(QueryMetadata {
            table: sample_tab_ident(),
            aggregation: sample_sum(),
            filter: None,
        });
        assert_eq!(QueryMetadata::parse(query), expected);
    }

    #[test]
    fn quoted_function_case_sensitive() {
        let query = "SELECT \"SUM\"(test_column_2) FROM test_db.test_schema.test_table_1";
        let expected = Err(unsupported!(
            "unrecognized or unsupported function: \"SUM\".".to_string()
        ));
        assert_eq!(QueryMetadata::parse(query), expected);
    }

    #[test]
    fn unquoted_result_alias_case_insensitive() {
        let query = "SELECT SUM(test_column_2) AS S FROM test_db.test_schema.test_table_1";
        let expected = Ok(QueryMetadata {
            table: sample_tab_ident(),
            aggregation: Aggregation {
                function: KoronFunction::Sum,
                column: "test_column_2".to_string(),
                alias: Some("s".to_string()),
            },
            filter: None,
        });
        assert_eq!(QueryMetadata::parse(query), expected);
    }

    #[test]
    fn quoted_result_alias_case_sensitive() {
        let query = "SELECT SUM(test_column_2) AS \"S\" FROM test_db.test_schema.test_table_1";
        let expected = Ok(QueryMetadata {
            table: sample_tab_ident(),
            aggregation: Aggregation {
                function: KoronFunction::Sum,
                column: "test_column_2".to_string(),
                alias: Some("S".to_string()),
            },
            filter: None,
        });
        assert_eq!(QueryMetadata::parse(query), expected);
    }

    #[test]
    fn quoted_table_alias_case_sensitive() {
        for (column, alias, extracted_alias) in [
            ("t.test_column_2", "\"T\"", "T"),
            ("\"T\".test_column_2", "\"t\"", "t"),
        ] {
            let query =
                &format!("SELECT SUM({column}) FROM test_db.test_schema.test_table_1 AS {alias}");

            let expected = Err(malformed_query!(format!(
                "the {column} column is not part of \
                     the table that's listed in the FROM clause ({extracted_alias}).",
            )));
            assert_eq!(
                QueryMetadata::parse(query),
                expected,
                "\nfailed for query {query:?}",
            );
        }
    }

    #[test]
    fn qualified_column_from_different_table() {
        for column in [
            "\"test_table_2\".test_column_2",
            "\"test_schema\".test_table_2.test_column_2",
        ] {
            let query = &format!("SELECT SUM({column}) FROM test_db.test_schema.test_table_1");

            let expected = Err(malformed_query!(format!(
                    "the {column} column is not part of \
                     the table that's listed in the FROM clause (test_db.test_schema.test_table_1).",
                )));
            assert_eq!(
                QueryMetadata::parse(query),
                expected,
                "\nfailed for query {query:?}",
            );
        }
    }

    #[test]
    fn qualified_column_not_from_table_alias() {
        for column in [
            "test_table_1.test_column_2",
            "test_schema.test_table_1.test_column_2",
        ] {
            let query = &format!("SELECT SUM({column}) FROM test_db.test_schema.test_table_1 AS t");
            let expected = Err(malformed_query!(format!(
                "the {column} column is not part of \
                     the table that's listed in the FROM clause (t).",
            )));
            assert_eq!(
                QueryMetadata::parse(query),
                expected,
                "\nfailed for query {query:?}",
            );
        }
    }

    #[test]
    fn sql_syntax_error() {
        let query = "SELECT * FROM";
        let expected = Err(malformed_query!(
            "sql parser error: Expected identifier, found: EOF".to_string()
        ));
        assert_eq!(QueryMetadata::parse(query), expected);
    }

    #[test]
    fn table_name_too_many_name_parts() {
        let query = "SELECT SUM(test_column_2) FROM x.test_db.test_schema.test_table_1";
        let expected = Err(internal!("found too many ident in table name (i.e., x.test_db.test_schema.test_table_1) in query AST.".to_string()));
        assert_eq!(QueryMetadata::parse(query), expected);
    }

    #[test]
    fn column_name_too_many_name_parts() {
        let query = "SELECT SUM(x.test_db.test_schema.test_table_1.test_column_2) FROM test_db.test_schema.test_table_1";
        let expected = Err(internal!("found too many ident in column name (i.e., x.test_db.test_schema.test_table_1.test_column_2)."
                .to_string()));
        assert_eq!(QueryMetadata::parse(query), expected);
    }

    #[test]
    fn wrong_number_of_arguments() {
        let cases = [
            (
                "SUM()",
                "the SUM function takes exactly 1 argument, but 0 are provided.",
            ),
            (
                "SUM(test_column_2, test_column_2)",
                "the SUM function takes exactly 1 argument, but 2 are provided.",
            ),
        ];

        for (projection, reason) in cases {
            let query = &format!("SELECT {projection} FROM test_db.test_schema.test_table_1");
            let expected = Err(malformed_query!(reason.to_string()));
            assert_eq!(
                QueryMetadata::parse(query),
                expected,
                "\nfailed for aggregation {projection}",
            );
        }
    }

    #[test]
    fn unsupported_sql_features() {
        let cases = [
            (
                "SELECT * FROM test_db.test_schema.test_table_1; SELECT * FROM test_db.test_schema.test_table_1",
                "statements different from single SELECT statement.",
            ),
            (
                "DELETE FROM test_db.test_schema.test_table_1",
                "statements different from single SELECT statement.",
            ),
            (
                "WITH t AS (SELECT 1) SELECT SUM(test_column_2) FROM test_db.test_schema.test_table_1",
                "CTEs (i.e., WITH clause).",
            ),
            (
                "SELECT SUM(test_column_2) FROM test_db.test_schema.test_table_1 ORDER BY SUM",
                "ORDER BY.",
            ),
            (
                "SELECT SUM(test_column_2) FROM test_db.test_schema.test_table_1 LIMIT 1",
                "LIMIT.",
            ),
            (
                "SELECT SUM(test_column_2) FROM test_db.test_schema.test_table_1 OFFSET 1",
                "OFFSET.",
            ),
            (
                "SELECT SUM(test_column_2) FROM test_db.test_schema.test_table_1 FETCH FIRST 1 ROW ONLY",
                "FETCH.",
            ),
            (
                "SELECT SUM(test_column_2) FROM test_db.test_schema.test_table_1 drda FOR UPDATE",
                "locking clauses (i.e., FOR UPDATE).",
            ),
            (
                "SELECT SUM(test_column_2) FROM test_db.test_schema.test_table_1 \
                UNION \
                SELECT SUM(test_column_2) FROM test_db.test_schema.test_table_1",
                "set operations (i.e., UNION).",
            ),
            ("VALUES (1)", "VALUES."),
            (
                "INSERT INTO test_table_1(test_column_2) VALUES(1)",
                "statements different from single SELECT statement."
            ),
            (
                "SELECT DISTINCT SUM(test_column_2) FROM test_db.test_schema.test_table_1",
                "DISTINCT.",
            ),
            // TOP is MSSQL syntax.
            (
                "SELECT TOP 1 SUM(test_column_2) FROM test_db.test_schema.test_table_1",
                "TOP.",
            ),
            (
                "SELECT SUM(test_column_2) INTO t FROM test_db.test_schema.test_table_1",
                "SELECT INTO.",
            ),
            // LATERAL VIEW is HiveQL syntax.
            (
                "SELECT SUM(test_column_2) FROM test_db.test_schema.test_table_1 LATERAL VIEW (SELECT 1) t",
                "LATERAL VIEW.",
            ),
            (
                "SELECT SUM(test_column_2) FROM test_db.test_schema.test_table_1 GROUP BY SUM",
                "GROUP BY.",
            ),
            // CLUSTER BY is HiveQL syntax.
            (
                "SELECT SUM(test_column_2) FROM test_db.test_schema.test_table_1 CLUSTER BY SUM",
                "CLUSTER BY.",
            ),
            // DISTRIBUTE BY is HiveQL syntax.
            (
                "SELECT SUM(test_column_2) FROM test_db.test_schema.test_table_1 DISTRIBUTE BY SUM",
                "DISTRIBUTE BY.",
            ),
            // SORT BY is HiveQL syntax.
            (
                "SELECT SUM(test_column_2) FROM test_db.test_schema.test_table_1 SORT BY SUM",
                "SORT BY.",
            ),
            (
                "SELECT SUM(test_column_2) FROM test_db.test_schema.test_table_1 HAVING sum > 0",
                "HAVING.",
            ),
            (
                "SELECT SUM(test_column_2) FROM test_db.test_schema.test_table_1, treasury.attachment",
                "the FROM clause has multiple tables (no JOINs, subqueries or functions allowed).",
            ),
            (
                "SELECT SUM(test_column_2) FROM test_db.test_schema.test_table_1 CROSS JOIN treasury.attachment",
                "the FROM clause has multiple tables (no JOINs, subqueries or functions allowed).",
            ),
            (
                "SELECT SUM(test_column_2) FROM f('arg')",
                "the FROM clause has multiple tables (no JOINs, subqueries or functions allowed).",
            ),
            // table hints are MSSQL syntax.
            (
                "SELECT SUM(test_column_2) FROM test_db.test_schema.test_table_1 WITH (NOLOCK)",
                "table hints (WITH in FROM clauses).",
            ),
            (
                "SELECT SUM(test_column_2) FROM (SELECT * FROM test_db.test_schema.test_table_1)",
                "the FROM clause has multiple tables (no JOINs, subqueries or functions allowed).",
            ),
            (
                "SELECT SUM(test_column_2) FROM TABLE(f())",
                "the FROM clause has multiple tables (no JOINs, subqueries or functions allowed).",
            ),
            (
                "SELECT SUM(test_column_2) FROM (test_schema.test_table_1 CROSS JOIN treasury.attachment)",
                "the FROM clause has multiple tables (no JOINs, subqueries or functions allowed).",
            ),
            (
                "SELECT SUM(f) FROM test_db.test_schema.test_table_1 AS d (f, g)",
                "table aliases with columns (such as d (f, g)).",
            ),
            (
                "SELECT SUM(test_column_2), AVG(test_column_2) FROM test_db.test_schema.test_table_1",
                "the SELECT clause must contain exactly one aggregation / analytic function. Nothing else is accepted.",
            ),
            (
                "SELECT drda.* FROM test_db.test_schema.test_table_1",
                "the SELECT clause must contain exactly one aggregation / analytic function. Nothing else is accepted.",
            ),
            (
                "SELECT * FROM test_db.test_schema.test_table_1",
                "the SELECT clause must contain exactly one aggregation / analytic function. Nothing else is accepted.",
            ),
            (
                "SELECT id FROM test_db.test_schema.test_table_1",
                "the SELECT clause must contain exactly one aggregation / analytic function. Nothing else is accepted.",
            ),
            (
                "SELECT SUM(test_column_2) OVER (PARTITION BY id) FROM test_db.test_schema.test_table_1",
                "window functions (OVER).",
            ),
            (
                "SELECT SUM(DISTINCT test_column_2) FROM test_db.test_schema.test_table_1",
                "DISTINCT.",
            ),
            (
                "SELECT custom.aggregation(test_column_2) FROM test_db.test_schema.test_table_1",
                "unrecognized or unsupported function: custom.aggregation.",
            ),
            (
                "SELECT SUM(x => test_column_2) FROM test_db.test_schema.test_table_1",
                "named function arguments (such as x => test_column_2).",
            ),
            (
                "SELECT SUM(1) FROM test_db.test_schema.test_table_1",
                "only a column name is supported as the argument of the SUM function.",
            ),
            (
                "SELECT SUM(test_table_1.*) FROM test_db.test_schema.test_table_1",
                "only a column name is supported as the argument of the SUM function.",
            ),
            (
                "SELECT SUM(*) FROM test_db.test_schema.test_table_1",
                "only a column name is supported as the argument of the SUM function.",
            ),
            (
                "INSERT INTO test_table_1 SELECT * FROM test_db.test_schema.test_table_1",
                "statements different from single SELECT statement.",
            ),
            (
                "CREATE TABLE test_table_1 AS SELECT * FROM test_db.test_schema.test_table_1",
                "statements different from single SELECT statement.",
            ),
            (
                "SELECT SUM(test_column_2) FROM test_db.test_schema.test_table_1 WHERE test_column_2 BETWEEN 1 AND 2",
                "unsupported expression in the WHERE clause: test_column_2 BETWEEN 1 AND 2.",
            ),
            (
                "SELECT SUM(test_column_2) FROM test_db.test_schema.test_table_1 WHERE 2 < 1",
                "2 < 1. Only comparisons between a column and a constant are supported.",
            ),
            (
                "SELECT SUM(test_column_2) FROM test_db.test_schema.test_table_1 WHERE test_column_2 < test_column_3",
                "test_column_2 < test_column_3. Only comparisons between a column and a constant are supported.",
            ),
            // Unsupported functions
            (
                "SELECT MIN(test_column_2) FROM test_db.test_schema.test_table_1;",
                "unrecognized or unsupported function: MIN."
            ),
            (
                "SELECT MAX(test_column_2) FROM test_db.test_schema.test_table_1;",
                "unrecognized or unsupported function: MAX."
            ),
            (
                "SELECT AVG(test_column_2) FROM test_db.test_schema.test_table_1;",
                "unrecognized or unsupported function: AVG."
            ),
            (
                "SELECT STDDEV(test_column_2) FROM test_db.test_schema.test_table_1;",
                "unrecognized or unsupported function: STDDEV."
            ),
            (
                "SELECT VARIANCE(test_column_2) FROM test_db.test_schema.test_table_1;",
                "unrecognized or unsupported function: VARIANCE."
            ),
            (
                "SELECT MEDIAN(test_column_2) FROM test_db.test_schema.test_table_1;",
                "unrecognized or unsupported function: MEDIAN."
            ),
            (
                "SELECT KTHELEMENT(test_column_2, 3) FROM test_db.test_schema.test_table_1;",
                "unrecognized or unsupported function: KTHELEMENT."
            )
        ];

        for (query, reason) in cases {
            let expected = Err(unsupported!(reason.to_string()));
            assert_eq!(
                QueryMetadata::parse(query),
                expected,
                "\nfailed for query {query:?}",
            );
        }
    }

    #[test]
    fn aggregation_with_single_where() {
        let cases = [
            (
                "test_column_2 < 1",
                Filter {
                    column: "test_column_2".to_string(),
                    comparison: CompareOp::Lt {
                        value: "1".to_string(),
                    },
                },
            ),
            (
                "1 < test_column_2",
                Filter {
                    column: "test_column_2".to_string(),
                    comparison: CompareOp::Gt {
                        value: "1".to_string(),
                    },
                },
            ),
            (
                "test_column_2 <= 1",
                Filter {
                    column: "test_column_2".to_string(),
                    comparison: CompareOp::LtEq {
                        value: "1".to_string(),
                    },
                },
            ),
            (
                "1 <= test_column_2",
                Filter {
                    column: "test_column_2".to_string(),
                    comparison: CompareOp::GtEq {
                        value: "1".to_string(),
                    },
                },
            ),
            (
                "test_column_2 > 1",
                Filter {
                    column: "test_column_2".to_string(),
                    comparison: CompareOp::Gt {
                        value: "1".to_string(),
                    },
                },
            ),
            (
                "1 > test_column_2",
                Filter {
                    column: "test_column_2".to_string(),
                    comparison: CompareOp::Lt {
                        value: "1".to_string(),
                    },
                },
            ),
            (
                "test_column_2 >= 1",
                Filter {
                    column: "test_column_2".to_string(),
                    comparison: CompareOp::GtEq {
                        value: "1".to_string(),
                    },
                },
            ),
            (
                "1 >= test_column_2",
                Filter {
                    column: "test_column_2".to_string(),
                    comparison: CompareOp::LtEq {
                        value: "1".to_string(),
                    },
                },
            ),
            (
                "test_column_3 > '2021-04-02T05:02:16.04+03:00'",
                Filter {
                    column: "test_column_3".to_string(),
                    comparison: CompareOp::Gt {
                        value: "2021-04-02T05:02:16.04+03:00".to_string(),
                    },
                },
            ),
            (
                "-1 >= test_column_4",
                Filter {
                    column: "test_column_4".to_string(),
                    comparison: CompareOp::LtEq {
                        value: "-1".to_string(),
                    },
                },
            ),
            (
                "+1 >= test_column_2",
                Filter {
                    column: "test_column_2".to_string(),
                    comparison: CompareOp::LtEq {
                        value: "1".to_string(),
                    },
                },
            ),
            (
                "+1 = test_column_2",
                Filter {
                    column: "test_column_2".to_string(),
                    comparison: CompareOp::Eq {
                        value: "1".to_string(),
                    },
                },
            ),
            (
                "+1 <> test_column_2",
                Filter {
                    column: "test_column_2".to_string(),
                    comparison: CompareOp::NotEq {
                        value: "1".to_string(),
                    },
                },
            ),
            (
                "test_column_2 IS NULL",
                Filter {
                    column: "test_column_2".to_string(),
                    comparison: CompareOp::IsNull,
                },
            ),
            (
                "test_column_2 IS NOT NULL",
                Filter {
                    column: "test_column_2".to_string(),
                    comparison: CompareOp::IsNotNull,
                },
            ),
            (
                "test_column_1 = NULL",
                Filter {
                    column: "test_column_1".to_string(),
                    comparison: CompareOp::Eq {
                        value: "Null".to_string(),
                    },
                },
            ),
            (
                "test_column_2 = NULL",
                Filter {
                    column: "test_column_2".to_string(),
                    comparison: CompareOp::Eq {
                        value: "Null".to_string(),
                    },
                },
            ),
            (
                "test_column_3 = NULL",
                Filter {
                    column: "test_column_3".to_string(),
                    comparison: CompareOp::Eq {
                        value: "Null".to_string(),
                    },
                },
            ),
            (
                "test_column_4 = NULL",
                Filter {
                    column: "test_column_4".to_string(),
                    comparison: CompareOp::Eq {
                        value: "Null".to_string(),
                    },
                },
            ),
            (
                "test_column_5 IS TRUE",
                Filter {
                    column: "test_column_5".to_string(),
                    comparison: CompareOp::IsTrue,
                },
            ),
            (
                "test_column_5 IS NOT TRUE",
                Filter {
                    column: "test_column_5".to_string(),
                    comparison: CompareOp::IsNotTrue,
                },
            ),
            (
                "test_column_5 = true",
                Filter {
                    column: "test_column_5".to_string(),
                    comparison: CompareOp::Eq {
                        value: "true".to_string(),
                    },
                },
            ),
            (
                "test_column_5 <> true",
                Filter {
                    column: "test_column_5".to_string(),
                    comparison: CompareOp::NotEq {
                        value: "true".to_string(),
                    },
                },
            ),
            (
                "test_column_5 IS FALSE",
                Filter {
                    column: "test_column_5".to_string(),
                    comparison: CompareOp::IsFalse,
                },
            ),
            (
                "test_column_5 IS NOT FALSE",
                Filter {
                    column: "test_column_5".to_string(),
                    comparison: CompareOp::IsNotFalse,
                },
            ),
            (
                "test_column_5 = false",
                Filter {
                    column: "test_column_5".to_string(),
                    comparison: CompareOp::Eq {
                        value: "false".to_string(),
                    },
                },
            ),
            (
                "test_column_5 <> false",
                Filter {
                    column: "test_column_5".to_string(),
                    comparison: CompareOp::NotEq {
                        value: "false".to_string(),
                    },
                },
            ),
        ];

        let analytical_functions = [("SUM", KoronFunction::Sum), ("COUNT", KoronFunction::Count)];

        let test_cases = |enum_fn: KoronFunction, query: &String| {
            for (selection, filter) in cases.clone() {
                let query = &format!("{query} WHERE {selection}");
                let mut aggregation = sample_sum();
                aggregation.function = enum_fn;
                let expected = Ok(QueryMetadata {
                    table: sample_tab_ident(),
                    aggregation,
                    filter: Some(filter),
                });
                assert_eq!(
                    QueryMetadata::parse(query),
                    expected,
                    "\nfailed for selection {selection:?}",
                );
            }
        };

        for (function, enum_fn) in analytical_functions {
            let query =
                format!("SELECT {function}(test_column_2) FROM test_db.test_schema.test_table_1");
            test_cases(enum_fn, &query);
        }
    }
}