alopex-sql 0.8.9

SQL parser components for the Alopex DB dialect
Documentation
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
//! GROUPING SETS / ROLLUP / CUBE public behavior (issue #149, contract 0.13.0).

use std::sync::{Arc, RwLock};

use alopex_core::kv::memory::MemoryKV;
use alopex_sql::catalog::MemoryCatalog;
use alopex_sql::dialect::AlopexDialect;
use alopex_sql::executor::{ExecutionResult, Executor, QueryResult};
use alopex_sql::parser::Parser;
use alopex_sql::planner::Planner;
use alopex_sql::storage::SqlValue;

struct Harness {
    executor: Executor<MemoryKV, MemoryCatalog>,
    catalog: Arc<RwLock<MemoryCatalog>>,
}

impl Harness {
    fn new() -> Self {
        let store = Arc::new(MemoryKV::new());
        let catalog = Arc::new(RwLock::new(MemoryCatalog::new()));
        let executor = Executor::new(store, Arc::clone(&catalog));
        Self { executor, catalog }
    }

    /// Harness with the shared four-row sales dataset. Row 4 carries a real
    /// NULL region so placeholder NULLs can be distinguished (D7).
    fn with_sales() -> Self {
        let mut harness = Self::new();
        harness
            .run(
                "CREATE TABLE sales (id INTEGER PRIMARY KEY, region TEXT, product TEXT, \
                 amount INTEGER); \
                 INSERT INTO sales VALUES (1, 'east', 'a', 10), (2, 'east', 'b', 20), \
                 (3, 'west', 'a', 30), (4, NULL, 'b', 40)",
            )
            .expect("create sales dataset");
        harness
    }

    fn run(&mut self, sql: &str) -> Result<Option<QueryResult>, String> {
        let statements =
            Parser::parse_sql(&AlopexDialect, sql).map_err(|error| format!("parse: {error}"))?;
        let mut last = None;
        for statement in statements {
            let plan = {
                let catalog = self.catalog.read().expect("catalog read");
                Planner::new(&*catalog)
                    .plan(&statement)
                    .map_err(|error| error.to_string())?
            };
            if let ExecutionResult::Query(result) = self
                .executor
                .execute(plan)
                .map_err(|error| error.to_string())?
            {
                last = Some(result);
            }
        }
        Ok(last)
    }

    fn query(&mut self, sql: &str) -> QueryResult {
        self.run(sql)
            .unwrap_or_else(|error| panic!("expected `{}` to succeed: {error}", sql.trim()))
            .unwrap_or_else(|| panic!("expected `{}` to return rows", sql.trim()))
    }
}

fn text(value: &str) -> SqlValue {
    SqlValue::Text(value.into())
}

#[test]
fn rollup_distinguishes_placeholder_null_from_data_null() {
    let result = Harness::with_sales().query(
        "SELECT region, SUM(amount) AS total, GROUPING(region) AS g FROM sales \
         GROUP BY ROLLUP(region) ORDER BY g, region NULLS FIRST",
    );

    // The real-NULL region row keeps g = 0; only the grand total row has
    // g = 1 even though both print region as NULL (D7).
    assert_eq!(
        result.rows,
        vec![
            vec![SqlValue::Null, SqlValue::BigInt(40), SqlValue::BigInt(0)],
            vec![text("east"), SqlValue::BigInt(30), SqlValue::BigInt(0)],
            vec![text("west"), SqlValue::BigInt(30), SqlValue::BigInt(0)],
            vec![SqlValue::Null, SqlValue::BigInt(100), SqlValue::BigInt(1)],
        ]
    );
    assert_eq!(
        result
            .columns
            .iter()
            .map(|column| column.name.as_str())
            .collect::<Vec<_>>(),
        vec!["region", "total", "g"]
    );
}

#[test]
fn cube_emits_all_subsets_with_multi_argument_grouping() {
    let result = Harness::with_sales().query(
        "SELECT region, product, SUM(amount) AS total, GROUPING(region, product) AS gid \
         FROM sales GROUP BY CUBE(region, product) \
         ORDER BY gid, region NULLS FIRST, product NULLS FIRST",
    );

    assert_eq!(
        result.rows,
        vec![
            vec![
                SqlValue::Null,
                text("b"),
                SqlValue::BigInt(40),
                SqlValue::BigInt(0)
            ],
            vec![
                text("east"),
                text("a"),
                SqlValue::BigInt(10),
                SqlValue::BigInt(0)
            ],
            vec![
                text("east"),
                text("b"),
                SqlValue::BigInt(20),
                SqlValue::BigInt(0)
            ],
            vec![
                text("west"),
                text("a"),
                SqlValue::BigInt(30),
                SqlValue::BigInt(0)
            ],
            vec![
                SqlValue::Null,
                SqlValue::Null,
                SqlValue::BigInt(40),
                SqlValue::BigInt(1)
            ],
            vec![
                text("east"),
                SqlValue::Null,
                SqlValue::BigInt(30),
                SqlValue::BigInt(1)
            ],
            vec![
                text("west"),
                SqlValue::Null,
                SqlValue::BigInt(30),
                SqlValue::BigInt(1)
            ],
            vec![
                SqlValue::Null,
                text("a"),
                SqlValue::BigInt(40),
                SqlValue::BigInt(2)
            ],
            vec![
                SqlValue::Null,
                text("b"),
                SqlValue::BigInt(60),
                SqlValue::BigInt(2)
            ],
            vec![
                SqlValue::Null,
                SqlValue::Null,
                SqlValue::BigInt(100),
                SqlValue::BigInt(3)
            ],
        ]
    );
}

#[test]
fn grouping_id_is_an_accepted_alias_of_grouping() {
    let mut harness = Harness::with_sales();
    let with_grouping = harness.query(
        "SELECT region, product, GROUPING(region, product) AS gid FROM sales \
         GROUP BY CUBE(region, product) \
         ORDER BY gid, region NULLS FIRST, product NULLS FIRST",
    );
    let with_grouping_id = harness.query(
        "SELECT region, product, GROUPING_ID(region, product) AS gid FROM sales \
         GROUP BY CUBE(region, product) \
         ORDER BY gid, region NULLS FIRST, product NULLS FIRST",
    );

    assert_eq!(with_grouping.rows, with_grouping_id.rows);
}

#[test]
fn grouping_sets_include_the_empty_set() {
    let result = Harness::with_sales().query(
        "SELECT region, product, COUNT(*) AS c, GROUPING(region, product) AS gid \
         FROM sales GROUP BY GROUPING SETS ((region), (product), ()) \
         ORDER BY gid, region NULLS FIRST, product NULLS FIRST",
    );

    assert_eq!(
        result.rows,
        vec![
            vec![
                SqlValue::Null,
                SqlValue::Null,
                SqlValue::BigInt(1),
                SqlValue::BigInt(1)
            ],
            vec![
                text("east"),
                SqlValue::Null,
                SqlValue::BigInt(2),
                SqlValue::BigInt(1)
            ],
            vec![
                text("west"),
                SqlValue::Null,
                SqlValue::BigInt(1),
                SqlValue::BigInt(1)
            ],
            vec![
                SqlValue::Null,
                text("a"),
                SqlValue::BigInt(2),
                SqlValue::BigInt(2)
            ],
            vec![
                SqlValue::Null,
                text("b"),
                SqlValue::BigInt(2),
                SqlValue::BigInt(2)
            ],
            vec![
                SqlValue::Null,
                SqlValue::Null,
                SqlValue::BigInt(4),
                SqlValue::BigInt(3)
            ],
        ]
    );
}

#[test]
fn ordinary_group_by_cross_products_with_rollup() {
    let result = Harness::with_sales().query(
        "SELECT region, product, SUM(amount) AS total FROM sales \
         GROUP BY region, ROLLUP(product) \
         ORDER BY region NULLS FIRST, GROUPING(product), product NULLS FIRST",
    );

    // D2: GROUP BY region, ROLLUP(product) = {region} x {(product), ()},
    // so each region gains one subtotal row with a product placeholder.
    assert_eq!(
        result.rows,
        vec![
            vec![SqlValue::Null, text("b"), SqlValue::BigInt(40)],
            vec![SqlValue::Null, SqlValue::Null, SqlValue::BigInt(40)],
            vec![text("east"), text("a"), SqlValue::BigInt(10)],
            vec![text("east"), text("b"), SqlValue::BigInt(20)],
            vec![text("east"), SqlValue::Null, SqlValue::BigInt(30)],
            vec![text("west"), text("a"), SqlValue::BigInt(30)],
            vec![text("west"), SqlValue::Null, SqlValue::BigInt(30)],
        ]
    );
}

#[test]
fn duplicate_grouping_sets_emit_duplicate_rows() {
    let result = Harness::with_sales().query(
        "SELECT region, COUNT(*) AS c FROM sales \
         GROUP BY GROUPING SETS ((region), (region)) ORDER BY region NULLS FIRST",
    );

    // D3: each listed set produces rows independently; no deduplication.
    assert_eq!(
        result.rows,
        vec![
            vec![SqlValue::Null, SqlValue::BigInt(1)],
            vec![SqlValue::Null, SqlValue::BigInt(1)],
            vec![text("east"), SqlValue::BigInt(2)],
            vec![text("east"), SqlValue::BigInt(2)],
            vec![text("west"), SqlValue::BigInt(1)],
            vec![text("west"), SqlValue::BigInt(1)],
        ]
    );
}

#[test]
fn having_composes_with_grouping() {
    let mut harness = Harness::with_sales();

    let totals_only = harness.query(
        "SELECT region, SUM(amount) AS total FROM sales GROUP BY ROLLUP(region) \
         HAVING GROUPING(region) = 1",
    );
    assert_eq!(
        totals_only.rows,
        vec![vec![SqlValue::Null, SqlValue::BigInt(100)]]
    );

    let details = harness.query(
        "SELECT region, SUM(amount) AS total FROM sales GROUP BY ROLLUP(region) \
         HAVING GROUPING(region) = 0 AND SUM(amount) >= 30 \
         ORDER BY region NULLS FIRST",
    );
    assert_eq!(
        details.rows,
        vec![
            vec![SqlValue::Null, SqlValue::BigInt(40)],
            vec![text("east"), SqlValue::BigInt(30)],
            vec![text("west"), SqlValue::BigInt(30)],
        ]
    );
}

#[test]
fn order_by_breaks_aggregate_ties_deterministically() {
    let result = Harness::with_sales().query(
        "SELECT region, SUM(amount) AS t, GROUPING(region) AS g FROM sales \
         GROUP BY ROLLUP(region) ORDER BY t DESC, g, region NULLS FIRST",
    );

    assert_eq!(
        result.rows,
        vec![
            vec![SqlValue::Null, SqlValue::BigInt(100), SqlValue::BigInt(1)],
            vec![SqlValue::Null, SqlValue::BigInt(40), SqlValue::BigInt(0)],
            vec![text("east"), SqlValue::BigInt(30), SqlValue::BigInt(0)],
            vec![text("west"), SqlValue::BigInt(30), SqlValue::BigInt(0)],
        ]
    );
}

#[test]
fn window_functions_compose_with_rollup() {
    let result = Harness::with_sales().query(
        "SELECT region, SUM(amount) AS t, RANK() OVER (ORDER BY SUM(amount) DESC) AS r \
         FROM sales GROUP BY ROLLUP(region) ORDER BY r, region NULLS FIRST",
    );

    // D11: the grand-total row participates in the window like any group row.
    assert_eq!(
        result.rows,
        vec![
            vec![SqlValue::Null, SqlValue::BigInt(100), SqlValue::BigInt(1)],
            vec![SqlValue::Null, SqlValue::BigInt(40), SqlValue::BigInt(2)],
            vec![text("east"), SqlValue::BigInt(30), SqlValue::BigInt(3)],
            vec![text("west"), SqlValue::BigInt(30), SqlValue::BigInt(3)],
        ]
    );
}

#[test]
fn grouping_composes_with_window_order_by() {
    let result = Harness::with_sales().query(
        "SELECT region, SUM(amount) AS t, \
         RANK() OVER (ORDER BY GROUPING(region), SUM(amount) DESC) AS r \
         FROM sales GROUP BY ROLLUP(region) ORDER BY r, region NULLS FIRST",
    );

    assert_eq!(
        result.rows,
        vec![
            vec![SqlValue::Null, SqlValue::BigInt(40), SqlValue::BigInt(1)],
            vec![text("east"), SqlValue::BigInt(30), SqlValue::BigInt(2)],
            vec![text("west"), SqlValue::BigInt(30), SqlValue::BigInt(2)],
            vec![SqlValue::Null, SqlValue::BigInt(100), SqlValue::BigInt(4)],
        ]
    );
}

#[test]
fn empty_grouping_set_aggregates_all_rows() {
    let mut harness = Harness::with_sales();

    let bare = harness.query("SELECT COUNT(*) AS c, SUM(amount) AS total FROM sales GROUP BY ()");
    assert_eq!(
        bare.rows,
        vec![vec![SqlValue::BigInt(4), SqlValue::BigInt(100)]]
    );

    let spelled = harness
        .query("SELECT COUNT(*) AS c, SUM(amount) AS total FROM sales GROUP BY GROUPING SETS (())");
    assert_eq!(spelled.rows, bare.rows);
}

#[test]
fn global_sets_emit_one_row_for_empty_input() {
    let mut harness = Harness::with_sales();
    let result = harness.query(
        "SELECT region, COUNT(*) AS c, GROUPING(region) AS g FROM sales \
         WHERE amount > 1000 GROUP BY ROLLUP(region) ORDER BY g",
    );

    // Only the () set groups over no key, so an empty input still yields
    // exactly its one global row (PostgreSQL/DuckDB semantics).
    assert_eq!(
        result.rows,
        vec![vec![
            SqlValue::Null,
            SqlValue::BigInt(0),
            SqlValue::BigInt(1)
        ]]
    );
}

#[test]
fn grouping_placement_errors_are_stable() {
    let ungrouped = Harness::with_sales()
        .run("SELECT GROUPING(region) FROM sales")
        .expect_err("GROUPING outside a grouped query must fail");
    assert!(
        ungrouped.contains("GROUPING is only allowed in grouped queries"),
        "unexpected error: {ungrouped}"
    );

    let non_key = Harness::with_sales()
        .run("SELECT region, GROUPING(amount) AS g FROM sales GROUP BY region")
        .expect_err("GROUPING of a non-key column must fail");
    assert!(
        non_key.contains("arguments to GROUPING must be grouping expressions"),
        "unexpected error: {non_key}"
    );

    let in_where = Harness::with_sales()
        .run("SELECT region FROM sales WHERE GROUPING(region) = 0 GROUP BY region")
        .expect_err("GROUPING in WHERE must fail");
    assert!(
        in_where.contains("GROUPING is not allowed in WHERE"),
        "unexpected error: {in_where}"
    );

    let in_group_by = Harness::with_sales()
        .run("SELECT region FROM sales GROUP BY region, GROUPING(region)")
        .expect_err("GROUPING inside GROUP BY must fail");
    assert!(
        in_group_by.contains("GROUPING is not allowed in GROUP BY"),
        "unexpected error: {in_group_by}"
    );

    let in_aggregate = Harness::with_sales()
        .run("SELECT region, SUM(GROUPING(region)) FROM sales GROUP BY ROLLUP(region)")
        .expect_err("GROUPING inside an aggregate argument must fail");
    assert!(
        in_aggregate.contains("GROUPING cannot appear inside aggregate function arguments"),
        "unexpected error: {in_aggregate}"
    );
}

#[test]
fn grouping_set_expansion_is_bounded() {
    let wide_cube = Harness::with_sales()
        .run(
            "SELECT COUNT(*) FROM sales GROUP BY CUBE(id, region, product, amount, id, region, \
             product, amount, id, region, product, amount, id)",
        )
        .expect_err("a 13-column CUBE must exceed the grouping-set bound");
    assert!(
        wide_cube.contains("too many grouping sets (max 4096)"),
        "unexpected error: {wide_cube}"
    );

    let cross_product = Harness::with_sales()
        .run(
            "SELECT COUNT(*) FROM sales GROUP BY CUBE(id, region, product, amount), \
             CUBE(id, region, product, amount), CUBE(id, region, product, amount), ROLLUP(id)",
        )
        .expect_err("a cross product beyond 4096 sets must fail");
    assert!(
        cross_product.contains("too many grouping sets (max 4096)"),
        "unexpected error: {cross_product}"
    );
}

#[test]
fn plain_group_by_stays_free_of_grouping_id_columns() {
    let result = Harness::with_sales().query(
        "SELECT region, SUM(amount) AS total FROM sales GROUP BY region \
         ORDER BY region NULLS FIRST",
    );

    // D12: the unmodified GROUP BY path is unchanged — no hidden column
    // leaks into the output schema and GROUPING folds to constant 0.
    assert_eq!(
        result
            .columns
            .iter()
            .map(|column| column.name.as_str())
            .collect::<Vec<_>>(),
        vec!["region", "total"]
    );
    assert_eq!(
        result.rows,
        vec![
            vec![SqlValue::Null, SqlValue::BigInt(40)],
            vec![text("east"), SqlValue::BigInt(30)],
            vec![text("west"), SqlValue::BigInt(30)],
        ]
    );

    let constant_grouping = Harness::with_sales().query(
        "SELECT region, GROUPING(region) AS g FROM sales GROUP BY region \
         ORDER BY region NULLS FIRST",
    );
    assert_eq!(
        constant_grouping.rows,
        vec![
            vec![SqlValue::Null, SqlValue::BigInt(0)],
            vec![text("east"), SqlValue::BigInt(0)],
            vec![text("west"), SqlValue::BigInt(0)],
        ]
    );
}

#[test]
fn rollup_composes_with_limit_and_offset() {
    let result = Harness::with_sales().query(
        "SELECT region, SUM(amount) AS total, GROUPING(region) AS g FROM sales \
         GROUP BY ROLLUP(region) ORDER BY g, region NULLS FIRST LIMIT 2 OFFSET 1",
    );

    assert_eq!(
        result.rows,
        vec![
            vec![text("east"), SqlValue::BigInt(30), SqlValue::BigInt(0)],
            vec![text("west"), SqlValue::BigInt(30), SqlValue::BigInt(0)],
        ]
    );
}

fn reference_value(value: &SqlValue) -> serde_json::Value {
    match value {
        SqlValue::Null => serde_json::Value::Null,
        SqlValue::Boolean(value) => serde_json::json!(value),
        SqlValue::Integer(value) => serde_json::json!(value),
        SqlValue::BigInt(value) => serde_json::json!(value),
        SqlValue::Float(value) => serde_json::json!(value),
        SqlValue::Double(value) => serde_json::json!(value),
        SqlValue::Text(value) => serde_json::json!(value),
        other => panic!("reference fixture does not use {other:?}"),
    }
}

#[cfg_attr(not(feature = "lane_ci"), ignore)]
#[test]
fn duckdb_and_datafusion_reference_fixture_matches_exact_rows() {
    let reference: serde_json::Value =
        serde_json::from_str(include_str!("fixtures/grouping_sets_reference.json"))
            .expect("parse reference fixture");
    assert_eq!(reference["verified_with"]["duckdb"], "1.5.5");
    assert_eq!(reference["verified_with"]["datafusion"], "54.0.0");

    let mut harness = Harness::with_sales();
    for case in reference["cases"].as_array().expect("reference cases") {
        let result = harness.query(case["sql"].as_str().expect("reference SQL"));
        let columns = result
            .columns
            .iter()
            .map(|column| serde_json::json!(column.name))
            .collect::<Vec<_>>();
        let rows = result
            .rows
            .iter()
            .map(|row| row.iter().map(reference_value).collect::<Vec<_>>())
            .collect::<Vec<_>>();
        assert_eq!(
            serde_json::Value::Array(columns),
            case["columns"],
            "{} columns",
            case["name"]
        );
        assert_eq!(
            serde_json::json!(rows),
            case["rows"],
            "{} rows",
            case["name"]
        );
    }
}