oxisql-datafusion 0.3.1

Apache DataFusion TableProvider over oxisql Connection — enables OLAP SQL queries against oxisql-backed tables
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
//! Structural Filter + Project lowering tests for `plan_bridge` (feature-gated on `parse`).
//!
//! Covers:
//! - `Filter` node lowers to a DataFusion Filter plan node.
//! - `Project` node lowers to a DataFusion Projection plan node.
//! - Both produce the same rows as the SQL round-trip on an in-memory table.
//! - Wildcard `SELECT *` falls back cleanly (returns `UnsupportedType`).
//! - Unsupported shape (`Aggregate`) falls back cleanly (returns `UnsupportedType`).

mod common;

#[cfg(feature = "parse")]
use std::sync::Arc;

#[cfg(feature = "parse")]
use arrow::array::{Float64Array, Int64Array, StringArray};
#[cfg(feature = "parse")]
use datafusion::prelude::SessionContext;
#[cfg(feature = "parse")]
use oxisql_datafusion::{to_datafusion_plan, OxiSqlFusionError, OxiSqlTableProvider};
#[cfg(feature = "parse")]
use oxisql_parse::LogicalPlan as OxiPlan;

// ── Test 1: Filter node lowers structurally ───────────────────────────────────

/// A `Filter` over a registered table with a valid simple predicate (`id > 1`)
/// lowers to a DataFusion `Filter` plan node.  Verified via the plan's
/// `display_indent()` string, which contains the word "Filter".
#[cfg(feature = "parse")]
#[tokio::test]
async fn test_filter_lowers_structurally() {
    let (rows, schema) = common::make_test_rows();
    let provider = OxiSqlTableProvider::from_rows(rows, schema);

    let ctx = SessionContext::new();
    ctx.register_table("filter_struct", Arc::new(provider))
        .expect("register_table");

    let oxi_plan = OxiPlan::Filter {
        input: Box::new(OxiPlan::Scan {
            table: "filter_struct".to_string(),
            alias: None,
            limit: None,
        }),
        predicate: "id > 1".to_string(),
    };

    let df_plan = to_datafusion_plan(&oxi_plan, &ctx)
        .await
        .expect("Filter structural lowering should succeed for a valid predicate");

    // The plan display must mention "Filter" — confirms structural node creation.
    let display = format!("{}", df_plan.display_indent());
    assert!(
        display.contains("Filter") || display.contains("filter"),
        "Structural lowering of Filter must produce a DF Filter node; got:\n{display}"
    );
    // Schema must be non-empty (columns from the scan are preserved).
    assert!(
        !df_plan.schema().fields().is_empty(),
        "Filter plan schema must be non-empty"
    );
}

// ── Test 2: Project node lowers structurally ──────────────────────────────────

/// A `Project` over a registered table with explicit (non-wildcard) column
/// names lowers to a DataFusion `Projection` plan node.
#[cfg(feature = "parse")]
#[tokio::test]
async fn test_project_lowers_structurally() {
    let (rows, schema) = common::make_test_rows();
    let provider = OxiSqlTableProvider::from_rows(rows, schema);

    let ctx = SessionContext::new();
    ctx.register_table("proj_struct", Arc::new(provider))
        .expect("register_table");

    let oxi_plan = OxiPlan::Project {
        input: Box::new(OxiPlan::Scan {
            table: "proj_struct".to_string(),
            alias: None,
            limit: None,
        }),
        columns: vec!["id".to_string(), "name".to_string()],
    };

    let df_plan = to_datafusion_plan(&oxi_plan, &ctx)
        .await
        .expect("Project structural lowering should succeed for explicit column list");

    // The plan display must mention "Projection".
    let display = format!("{}", df_plan.display_indent());
    assert!(
        display.contains("Projection") || display.contains("projection"),
        "Structural lowering of Project must produce a DF Projection node; got:\n{display}"
    );
    // Projected schema must contain exactly the two requested columns.
    let field_names: Vec<&str> = df_plan
        .schema()
        .fields()
        .iter()
        .map(|f| f.name().as_str())
        .collect();
    assert!(
        field_names.contains(&"id"),
        "Projected schema must contain 'id'; got {:?}",
        field_names
    );
    assert!(
        field_names.contains(&"name"),
        "Projected schema must contain 'name'; got {:?}",
        field_names
    );
}

// ── Test 3: Filter produces same rows as SQL round-trip ───────────────────────

/// Execute a structurally-lowered `Filter` and compare the collected rows to the
/// result of the equivalent SQL round-trip.  Both must return the same single row.
#[cfg(feature = "parse")]
#[tokio::test]
async fn test_filter_rows_match_sql_roundtrip() {
    use datafusion::prelude::col;
    use oxisql_datafusion::sql_to_datafusion_plan;

    let (rows, schema) = common::make_test_rows();
    let provider_a = OxiSqlTableProvider::from_rows(rows.clone(), schema.clone());
    let provider_b = OxiSqlTableProvider::from_rows(rows, schema);

    let ctx_structural = SessionContext::new();
    ctx_structural
        .register_table("filter_match_a", Arc::new(provider_a))
        .expect("register_table a");

    let ctx_roundtrip = SessionContext::new();
    ctx_roundtrip
        .register_table("filter_match_b", Arc::new(provider_b))
        .expect("register_table b");

    // Structural path: Filter { Scan("filter_match_a"), predicate="id = 1" }
    let oxi_plan = OxiPlan::Filter {
        input: Box::new(OxiPlan::Scan {
            table: "filter_match_a".to_string(),
            alias: None,
            limit: None,
        }),
        predicate: "id = 1".to_string(),
    };
    let df_struct = to_datafusion_plan(&oxi_plan, &ctx_structural)
        .await
        .expect("structural Filter should succeed");
    let structural_batches = ctx_structural
        .execute_logical_plan(df_struct)
        .await
        .expect("execute structural plan")
        .collect()
        .await
        .expect("collect structural results");

    // SQL round-trip path
    let df_roundtrip =
        sql_to_datafusion_plan("SELECT * FROM filter_match_b WHERE id = 1", &ctx_roundtrip)
            .await
            .expect("sql round-trip plan should succeed");
    let roundtrip_batches = ctx_roundtrip
        .execute_logical_plan(df_roundtrip)
        .await
        .expect("execute roundtrip plan")
        .collect()
        .await
        .expect("collect roundtrip results");

    let structural_rows: usize = structural_batches.iter().map(|b| b.num_rows()).sum();
    let roundtrip_rows: usize = roundtrip_batches.iter().map(|b| b.num_rows()).sum();

    assert_eq!(
        structural_rows, roundtrip_rows,
        "structural Filter must return the same row count as the SQL round-trip: \
         structural={structural_rows}, roundtrip={roundtrip_rows}"
    );
    assert_eq!(
        structural_rows, 1,
        "id=1 filter must return exactly 1 row, got {structural_rows}"
    );

    // Verify the id value in the returned row.
    let ids_structural: Vec<i64> = structural_batches
        .iter()
        .flat_map(|b| {
            // find the "id" column by name
            let schema = b.schema();
            let idx = schema.index_of("id").expect("id column present");
            b.column(idx)
                .as_any()
                .downcast_ref::<Int64Array>()
                .expect("id is Int64")
                .values()
                .to_vec()
        })
        .collect();
    assert_eq!(
        ids_structural,
        vec![1i64],
        "structural Filter must return id=1, got {:?}",
        ids_structural
    );
    // suppress unused import warning in non-parse builds
    let _ = col("id");
}

// ── Test 3b: Project produces same rows as SQL round-trip ─────────────────────

/// Execute a structurally-lowered `Project` and compare collected rows to the
/// SQL round-trip.  Both must project the same `id` and `name` values.
#[cfg(feature = "parse")]
#[tokio::test]
async fn test_project_rows_match_sql_roundtrip() {
    use oxisql_datafusion::sql_to_datafusion_plan;

    let (rows, schema) = common::make_test_rows();
    let provider_a = OxiSqlTableProvider::from_rows(rows.clone(), schema.clone());
    let provider_b = OxiSqlTableProvider::from_rows(rows, schema);

    let ctx_structural = SessionContext::new();
    ctx_structural
        .register_table("proj_match_a", Arc::new(provider_a))
        .expect("register_table a");

    let ctx_roundtrip = SessionContext::new();
    ctx_roundtrip
        .register_table("proj_match_b", Arc::new(provider_b))
        .expect("register_table b");

    // Structural path: Project { Scan("proj_match_a"), columns=["id","name"] }
    let oxi_plan = OxiPlan::Project {
        input: Box::new(OxiPlan::Scan {
            table: "proj_match_a".to_string(),
            alias: None,
            limit: None,
        }),
        columns: vec!["id".to_string(), "name".to_string()],
    };
    let df_struct = to_datafusion_plan(&oxi_plan, &ctx_structural)
        .await
        .expect("structural Project should succeed");
    let structural_batches = ctx_structural
        .execute_logical_plan(df_struct)
        .await
        .expect("execute structural project plan")
        .collect()
        .await
        .expect("collect structural project results");

    // SQL round-trip path
    let df_roundtrip = sql_to_datafusion_plan("SELECT id, name FROM proj_match_b", &ctx_roundtrip)
        .await
        .expect("sql round-trip plan should succeed");
    let roundtrip_batches = ctx_roundtrip
        .execute_logical_plan(df_roundtrip)
        .await
        .expect("execute roundtrip project plan")
        .collect()
        .await
        .expect("collect roundtrip project results");

    let structural_rows: usize = structural_batches.iter().map(|b| b.num_rows()).sum();
    let roundtrip_rows: usize = roundtrip_batches.iter().map(|b| b.num_rows()).sum();

    assert_eq!(
        structural_rows, roundtrip_rows,
        "structural Project must return same row count as SQL round-trip: \
         structural={structural_rows}, roundtrip={roundtrip_rows}"
    );

    // Collect id and name columns from structural result.
    let ids_structural: Vec<i64> = structural_batches
        .iter()
        .flat_map(|b| {
            let schema = b.schema();
            let idx = schema.index_of("id").expect("id column present");
            b.column(idx)
                .as_any()
                .downcast_ref::<Int64Array>()
                .expect("id is Int64")
                .values()
                .to_vec()
        })
        .collect();
    let names_structural: Vec<String> = structural_batches
        .iter()
        .flat_map(|b| {
            let schema = b.schema();
            let idx = schema.index_of("name").expect("name column present");
            b.column(idx)
                .as_any()
                .downcast_ref::<StringArray>()
                .expect("name is Utf8")
                .iter()
                .filter_map(|v| v.map(|s| s.to_string()))
                .collect::<Vec<_>>()
        })
        .collect();

    assert_eq!(
        ids_structural,
        vec![1i64, 2i64],
        "structural Project must return id=[1,2], got {:?}",
        ids_structural
    );
    assert_eq!(
        names_structural,
        vec!["Alice".to_string(), "Bob".to_string()],
        "structural Project must return name=[Alice,Bob], got {:?}",
        names_structural
    );

    // Confirm `score` is NOT in the projected schema.
    let field_names: Vec<String> = structural_batches
        .first()
        .map(|b| {
            b.schema()
                .fields()
                .iter()
                .map(|f| f.name().clone())
                .collect::<Vec<_>>()
        })
        .unwrap_or_default();
    assert!(
        !field_names.iter().any(|n| n == "score"),
        "structural Project must drop 'score' column; schema fields: {:?}",
        field_names
    );
    // suppress unused import warning
    let _ = Float64Array::from(vec![0.0f64]);
}

// ── Test 3c: Sort node lowers structurally ────────────────────────────────────

/// A `Sort` over a registered table with a valid column key lowers to a
/// DataFusion `Sort` plan node.  The explain output must contain "Sort" and
/// the collected rows must be in the requested order.
#[cfg(feature = "parse")]
#[tokio::test]
async fn test_sort_lowers_structurally() {
    use arrow::array::Int64Array;
    use oxisql_parse::SortExpr as OxiSortExpr;

    let (rows, schema) = common::make_test_rows();
    let provider = OxiSqlTableProvider::from_rows(rows, schema);

    let ctx = SessionContext::new();
    ctx.register_table("sort_struct", Arc::new(provider))
        .expect("register_table");

    // Sort descending by id — rows should come back in [2, 1] order.
    let oxi_plan = OxiPlan::Sort {
        input: Box::new(OxiPlan::Scan {
            table: "sort_struct".to_string(),
            alias: None,
            limit: None,
        }),
        order_by: vec![OxiSortExpr {
            column: "id".to_string(),
            ascending: false,
        }],
    };

    let df_plan = to_datafusion_plan(&oxi_plan, &ctx)
        .await
        .expect("Sort structural lowering should succeed for a valid column key");

    // The plan display must mention "Sort".
    let display = format!("{}", df_plan.display_indent());
    assert!(
        display.to_lowercase().contains("sort"),
        "Structural lowering of Sort must produce a DF Sort node; got:\n{display}"
    );

    // Execute and verify descending row order.
    let batches = ctx
        .execute_logical_plan(df_plan)
        .await
        .expect("execute sort plan")
        .collect()
        .await
        .expect("collect sort results");

    let all_ids: Vec<i64> = batches
        .iter()
        .flat_map(|b| {
            let idx = b.schema().index_of("id").expect("id column present");
            b.column(idx)
                .as_any()
                .downcast_ref::<Int64Array>()
                .expect("id is Int64")
                .values()
                .to_vec()
        })
        .collect();

    assert_eq!(
        all_ids,
        vec![2i64, 1i64],
        "Sort DESC on id must yield [2, 1]; got {:?}",
        all_ids
    );
}

// ── Test 3d: Limit node lowers structurally ───────────────────────────────────

/// A `Limit` plan over a registered table restricts the number of returned rows.
/// With a 2-row table and `count = Some(1)`, exactly one row must be returned.
#[cfg(feature = "parse")]
#[tokio::test]
async fn test_limit_lowers_structurally() {
    let (rows, schema) = common::make_test_rows();
    let provider = OxiSqlTableProvider::from_rows(rows, schema);

    let ctx = SessionContext::new();
    ctx.register_table("limit_struct", Arc::new(provider))
        .expect("register_table");

    let oxi_plan = OxiPlan::Limit {
        input: Box::new(OxiPlan::Scan {
            table: "limit_struct".to_string(),
            alias: None,
            limit: None,
        }),
        count: Some(1),
        offset: None,
    };

    let df_plan = to_datafusion_plan(&oxi_plan, &ctx)
        .await
        .expect("Limit structural lowering should succeed");

    let batches = ctx
        .execute_logical_plan(df_plan)
        .await
        .expect("execute limit plan")
        .collect()
        .await
        .expect("collect limit results");

    let total_rows: usize = batches.iter().map(|b| b.num_rows()).sum();
    assert_eq!(
        total_rows, 1,
        "Limit(1) on a 2-row table must return exactly 1 row; got {total_rows}"
    );
}

// ── Test 4: Wildcard `SELECT *` falls back cleanly ────────────────────────────

/// A `Project` with a wildcard (`*`) column must return `UnsupportedType`
/// rather than panicking or returning a wrong result.
#[cfg(feature = "parse")]
#[tokio::test]
async fn test_project_wildcard_fallback() {
    let (rows, schema) = common::make_test_rows();
    let provider = OxiSqlTableProvider::from_rows(rows, schema);

    let ctx = SessionContext::new();
    ctx.register_table("wildcard_tbl", Arc::new(provider))
        .expect("register_table");

    let oxi_plan = OxiPlan::Project {
        input: Box::new(OxiPlan::Scan {
            table: "wildcard_tbl".to_string(),
            alias: None,
            limit: None,
        }),
        columns: vec!["*".to_string()],
    };

    let result = to_datafusion_plan(&oxi_plan, &ctx).await;
    assert!(
        matches!(result, Err(OxiSqlFusionError::UnsupportedType(_))),
        "Wildcard Project must return UnsupportedType (no panic); got: {result:?}"
    );
}

// ── Test 5: Unsupported shape (Aggregate) falls back cleanly ──────────────────

/// An `Aggregate` node is not structurally lowered and must return
/// `UnsupportedType` without panicking.
#[cfg(feature = "parse")]
#[tokio::test]
async fn test_aggregate_falls_back_to_unsupported() {
    let ctx = SessionContext::new();

    let oxi_plan = OxiPlan::Aggregate {
        input: Box::new(OxiPlan::Empty),
        group_by: vec!["category".to_string()],
        aggregates: vec!["COUNT(*)".to_string()],
    };

    let result = to_datafusion_plan(&oxi_plan, &ctx).await;
    assert!(
        matches!(result, Err(OxiSqlFusionError::UnsupportedType(_))),
        "Aggregate must return UnsupportedType (use sql_to_datafusion_plan); got: {result:?}"
    );
}