iridium_core 0.1.12

SQL Server-compatible Rust engine core for Iridium SQL
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
use iridium_core::{parse_sql, types::Value, Engine};

fn exec(engine: &mut Engine, sql: &str) {
    let stmt = parse_sql(sql).expect("parse failed");
    engine.execute(stmt).expect("execute failed");
}

fn query(engine: &mut Engine, sql: &str) -> iridium_core::QueryResult {
    let stmt = parse_sql(sql).expect("parse failed");
    engine
        .execute(stmt)
        .expect("execute failed")
        .expect("expected result")
}

fn setup_sales(engine: &mut Engine) {
    exec(
        engine,
        "CREATE TABLE sales (region VARCHAR(50), quarter VARCHAR(10), amount INT)",
    );
    exec(
        engine,
        "INSERT INTO sales VALUES ('East', 'Q1', 1000), ('East', 'Q2', 2000), ('East', 'Q3', 1500), ('East', 'Q4', 3000)",
    );
    exec(
        engine,
        "INSERT INTO sales VALUES ('West', 'Q1', 500), ('West', 'Q2', 1500), ('West', 'Q3', 1000), ('West', 'Q4', 2000)",
    );
    exec(
        engine,
        "INSERT INTO sales VALUES ('North', 'Q1', 800), ('North', 'Q2', 1200), ('North', 'Q3', 900), ('North', 'Q4', 1100)",
    );
}

#[allow(dead_code)]
fn setup_employees(engine: &mut Engine) {
    exec(
        engine,
        "CREATE TABLE employees (dept VARCHAR(50), emp VARCHAR(50), sales INT)",
    );
    exec(
        engine,
        "INSERT INTO employees VALUES ('Sales', 'Alice', 100), ('Sales', 'Bob', 200), ('Sales', 'Carol', 150)",
    );
    exec(
        engine,
        "INSERT INTO employees VALUES ('Marketing', 'Dave', 80), ('Marketing', 'Eve', 120)",
    );
}

// ─── Basic PIVOT with SUM ────────────────────────────────────────────────

#[test]
fn test_pivot_basic_sum() {
    let mut e = Engine::new();
    setup_sales(&mut e);

    let r = query(
        &mut e,
        "SELECT * FROM (SELECT region, quarter, amount FROM sales) AS s \
         PIVOT (SUM(amount) FOR quarter IN (Q1, Q2, Q3, Q4)) AS p \
         ORDER BY region",
    );

    assert_eq!(r.columns.len(), 5);
    assert_eq!(r.rows.len(), 3);

    assert_eq!(r.rows[0][0], Value::VarChar("East".to_string()));
    assert_eq!(r.rows[0][1], Value::Int(1000));
    assert_eq!(r.rows[0][2], Value::Int(2000));
    assert_eq!(r.rows[0][3], Value::Int(1500));
    assert_eq!(r.rows[0][4], Value::Int(3000));

    assert_eq!(r.rows[1][0], Value::VarChar("North".to_string()));
    assert_eq!(r.rows[1][1], Value::Int(800));
    assert_eq!(r.rows[1][4], Value::Int(1100));

    assert_eq!(r.rows[2][0], Value::VarChar("West".to_string()));
    assert_eq!(r.rows[2][1], Value::Int(500));
    assert_eq!(r.rows[2][4], Value::Int(2000));
}

#[test]
fn test_pivot_basic_count() {
    let mut e = Engine::new();
    setup_sales(&mut e);

    let r = query(
        &mut e,
        "SELECT region, Q1, Q2, Q3, Q4 FROM (SELECT region, quarter, amount FROM sales) AS s \
         PIVOT (COUNT(amount) FOR quarter IN (Q1, Q2, Q3, Q4)) AS p \
         ORDER BY region",
    );

    assert_eq!(r.rows.len(), 3);
    assert_eq!(r.rows[0][1], Value::Int(1));
    assert_eq!(r.rows[0][2], Value::Int(1));
    assert_eq!(r.rows[0][3], Value::Int(1));
    assert_eq!(r.rows[0][4], Value::Int(1));
}

#[test]
fn test_pivot_basic_avg() {
    let mut e = Engine::new();
    setup_sales(&mut e);

    let r = query(
        &mut e,
        "SELECT region, Q1, Q2 FROM (SELECT region, quarter, amount FROM sales) AS s \
         PIVOT (AVG(amount) FOR quarter IN (Q1, Q2)) AS p \
         ORDER BY region",
    );

    assert_eq!(r.rows.len(), 3);
    assert_eq!(r.rows[0][0], Value::VarChar("East".to_string()));
    assert_eq!(r.rows[0][1], Value::BigInt(1000));
    assert_eq!(r.rows[0][2], Value::BigInt(2000));
    assert_eq!(r.rows[1][1], Value::BigInt(800));
    assert_eq!(r.rows[1][2], Value::BigInt(1200));
}

#[test]
fn test_pivot_basic_min_max() {
    let mut e = Engine::new();
    exec(
        &mut e,
        "CREATE TABLE t (category VARCHAR(50), period VARCHAR(10), val INT)",
    );
    exec(
        &mut e,
        "INSERT INTO t VALUES ('A', 'P1', 100), ('A', 'P1', 200), ('A', 'P2', 150)",
    );
    exec(
        &mut e,
        "INSERT INTO t VALUES ('B', 'P1', 50), ('B', 'P2', 75)",
    );

    let r = query(
        &mut e,
        "SELECT category, P1, P2 FROM (SELECT category, period, val FROM t) AS s \
         PIVOT (MIN(val) FOR period IN (P1, P2)) AS p",
    );

    assert_eq!(r.rows.len(), 2);
    assert_eq!(r.rows[0][0], Value::VarChar("A".to_string()));
    assert_eq!(r.rows[0][1], Value::Int(100));
    assert_eq!(r.rows[0][2], Value::Int(150));

    let r2 = query(
        &mut e,
        "SELECT category, P1, P2 FROM (SELECT category, period, val FROM t) AS s \
         PIVOT (MAX(val) FOR period IN (P1, P2)) AS p",
    );

    assert_eq!(r2.rows[0][1], Value::Int(200));
    assert_eq!(r2.rows[0][2], Value::Int(150));
}

// ─── PIVOT with ORDER BY ─────────────────────────────────────────────────

#[test]
fn test_pivot_with_order_by() {
    let mut e = Engine::new();
    setup_sales(&mut e);

    let r = query(
        &mut e,
        "SELECT * FROM (SELECT region, quarter, amount FROM sales) AS s \
         PIVOT (SUM(amount) FOR quarter IN (Q1, Q2, Q3, Q4)) AS p \
         ORDER BY region DESC",
    );

    assert_eq!(r.rows.len(), 3);
    assert_eq!(r.rows[0][0], Value::VarChar("West".to_string()));
    assert_eq!(r.rows[2][0], Value::VarChar("East".to_string()));
}

// ─── PIVOT with TOP ──────────────────────────────────────────────────────

#[test]
fn test_pivot_with_top() {
    let mut e = Engine::new();
    setup_sales(&mut e);

    let r = query(
        &mut e,
        "SELECT TOP 2 * FROM (SELECT region, quarter, amount FROM sales) AS s \
         PIVOT (SUM(amount) FOR quarter IN (Q1, Q2, Q3, Q4)) AS p \
         ORDER BY region",
    );

    assert_eq!(r.rows.len(), 2);
}

// ─── PIVOT with missing pivot values (returns NULL) ─────────────────────

#[test]
fn test_pivot_missing_values_null() {
    let mut e = Engine::new();
    exec(&mut e, "CREATE TABLE t (cat VARCHAR(10), val INT)");
    exec(&mut e, "INSERT INTO t VALUES ('A', 100), ('B', 200)");

    let r = query(
        &mut e,
        "SELECT * FROM (SELECT cat, val FROM t) AS s \
         PIVOT (SUM(val) FOR cat IN (A, B, C)) AS p",
    );

    assert_eq!(r.rows.len(), 1);
    assert_eq!(r.rows[0][0], Value::Int(100));
    assert_eq!(r.rows[0][1], Value::Int(200));
    assert_eq!(r.rows[0][2], Value::Null);
}

// ─── PIVOT with multiple grouping columns ───────────────────────────────

#[test]
fn test_pivot_multiple_grouping_columns() {
    let mut e = Engine::new();
    exec(
        &mut e,
        "CREATE TABLE sales2 (region VARCHAR(50), country VARCHAR(50), product VARCHAR(50), qty INT)",
    );
    exec(
        &mut e,
        "INSERT INTO sales2 VALUES ('East', 'USA', 'Widget', 100), ('East', 'USA', 'Gadget', 200)",
    );
    exec(
        &mut e,
        "INSERT INTO sales2 VALUES ('East', 'Canada', 'Widget', 50), ('East', 'Canada', 'Gadget', 75)",
    );
    exec(
        &mut e,
        "INSERT INTO sales2 VALUES ('West', 'USA', 'Widget', 150), ('West', 'USA', 'Gadget', 250)",
    );

    let r = query(
        &mut e,
        "SELECT region, country, Widget, Gadget FROM (SELECT region, country, product, qty FROM sales2) AS s \
         PIVOT (SUM(qty) FOR product IN (Widget, Gadget)) AS p \
         ORDER BY region, country",
    );

    assert_eq!(r.rows.len(), 3);
    assert_eq!(r.rows[0][0], Value::VarChar("East".to_string()));
    assert_eq!(r.rows[0][1], Value::VarChar("Canada".to_string()));
    assert_eq!(r.rows[0][2], Value::Int(50));
    assert_eq!(r.rows[0][3], Value::Int(75));

    assert_eq!(r.rows[1][0], Value::VarChar("East".to_string()));
    assert_eq!(r.rows[1][1], Value::VarChar("USA".to_string()));
    assert_eq!(r.rows[1][2], Value::Int(100));
    assert_eq!(r.rows[1][3], Value::Int(200));
}

// ─── PIVOT with NULLs in aggregate column ───────────────────────────────

#[test]
fn test_pivot_nulls_in_aggregate() {
    let mut e = Engine::new();
    exec(&mut e, "CREATE TABLE t (cat VARCHAR(10), val INT)");
    exec(
        &mut e,
        "INSERT INTO t VALUES ('A', 100), ('A', NULL), ('A', 200), ('B', NULL)",
    );

    let r = query(
        &mut e,
        "SELECT * FROM (SELECT cat, val FROM t) AS s \
         PIVOT (SUM(val) FOR cat IN (A, B)) AS p",
    );

    assert_eq!(r.rows.len(), 1);
    assert_eq!(r.rows[0][0], Value::BigInt(300));
    assert_eq!(r.rows[0][1], Value::Null);
}

// ─── PIVOT COUNT_BIG ────────────────────────────────────────────────────

#[test]
fn test_pivot_count_big() {
    let mut e = Engine::new();
    exec(&mut e, "CREATE TABLE t (cat VARCHAR(10), val INT)");
    exec(
        &mut e,
        "INSERT INTO t VALUES ('A', 100), ('A', 200), ('A', NULL)",
    );

    let r = query(
        &mut e,
        "SELECT A FROM (SELECT cat, val FROM t) AS s \
         PIVOT (COUNT_BIG(val) FOR cat IN (A)) AS p",
    );

    assert_eq!(r.rows.len(), 1);
    assert_eq!(r.rows[0][0], Value::BigInt(2));
}

// ─── PIVOT STRING_AGG ───────────────────────────────────────────────────

#[test]
fn test_pivot_string_agg() {
    let mut e = Engine::new();
    exec(&mut e, "CREATE TABLE t (cat VARCHAR(10), val VARCHAR(10))");
    exec(
        &mut e,
        "INSERT INTO t VALUES ('A', 'x'), ('A', 'y'), ('A', 'z')",
    );

    let r = query(
        &mut e,
        "SELECT A FROM (SELECT cat, val FROM t) AS s \
         PIVOT (STRING_AGG(val) FOR cat IN (A)) AS p",
    );

    assert_eq!(r.rows.len(), 1);
    let aggregated = r.rows[0][0].to_string_value();
    assert!(aggregated.contains('x'));
    assert!(aggregated.contains('y'));
    assert!(aggregated.contains('z'));
}

// ─── PIVOT with subquery ────────────────────────────────────────────────

#[test]
fn test_pivot_with_subquery() {
    let mut e = Engine::new();
    setup_sales(&mut e);

    let r = query(
        &mut e,
        "SELECT * FROM (SELECT region, quarter, amount FROM sales WHERE region = 'East') AS s \
         PIVOT (SUM(amount) FOR quarter IN (Q1, Q2, Q3, Q4)) AS p",
    );

    assert_eq!(r.rows.len(), 1);
    assert_eq!(r.rows[0][0], Value::VarChar("East".to_string()));
    assert_eq!(r.rows[0][1], Value::Int(1000));
    assert_eq!(r.rows[0][2], Value::Int(2000));
    assert_eq!(r.rows[0][3], Value::Int(1500));
    assert_eq!(r.rows[0][4], Value::Int(3000));
}

// ─── PIVOT: STDEV aggregate (now supported) ──────────────────────────────────

#[test]
fn test_pivot_stdev_aggregate() {
    let mut e = Engine::new();
    setup_sales(&mut e);

    let r = query(
        &mut e,
        "SELECT * FROM (SELECT region, quarter, amount FROM sales) AS s \
         PIVOT (STDEV(amount) FOR quarter IN (Q1)) AS p",
    );
    assert_eq!(r.rows.len(), 3);
    // STDEV with sample of 1 should return NULL
    assert!(r
        .rows
        .iter()
        .any(|row| row[1] == Value::Null || matches!(row[1], Value::Float(_))));
}

// ─── PIVOT with non-matching pivot column values ────────────────────────

#[test]
fn test_pivot_with_nonexistent_pivot_values() {
    let mut e = Engine::new();
    setup_sales(&mut e);

    let r = query(
        &mut e,
        "SELECT * FROM (SELECT region, quarter, amount FROM sales) AS s \
         PIVOT (SUM(amount) FOR quarter IN (Q1, Q999)) AS p",
    );

    assert_eq!(r.rows.len(), 3);
    assert_eq!(r.rows[0][1], Value::Int(1000));
    assert_eq!(r.rows[0][2], Value::Null);
}

// ─── PIVOT on empty source ──────────────────────────────────────────────

#[test]
fn test_pivot_empty_source() {
    let mut e = Engine::new();
    exec(&mut e, "CREATE TABLE empty_t (cat VARCHAR(10), val INT)");

    let r = query(
        &mut e,
        "SELECT * FROM (SELECT cat, val FROM empty_t) AS s \
         PIVOT (SUM(val) FOR cat IN (A, B)) AS p",
    );

    assert_eq!(r.rows.len(), 0);
}

// ─── PIVOT with aliased aggregate ──────────────────────────────────────

#[test]
fn test_pivot_with_region_example() {
    let mut e = Engine::new();
    exec(
        &mut e,
        "CREATE TABLE regions (category VARCHAR(50), region VARCHAR(50), sales INT)",
    );
    exec(&mut e, "INSERT INTO regions VALUES ('Sales', 'East', 300), ('Sales', 'West', 150), ('Marketing', 'East', 80), ('Marketing', 'West', 120)");

    let r = query(
        &mut e,
        "SELECT category, East, West FROM (SELECT category, region, sales FROM regions) AS s \
         PIVOT (SUM(sales) FOR region IN (East, West)) AS p \
         ORDER BY category",
    );

    assert_eq!(r.rows.len(), 2);
    assert_eq!(r.rows[0][0], Value::VarChar("Marketing".to_string()));
    assert_eq!(r.rows[0][1], Value::Int(80));
    assert_eq!(r.rows[0][2], Value::Int(120));

    assert_eq!(r.rows[1][0], Value::VarChar("Sales".to_string()));
    assert_eq!(r.rows[1][1], Value::Int(300));
    assert_eq!(r.rows[1][2], Value::Int(150));
}