datafusion-table-providers 0.12.0

Extend the capabilities of DataFusion to support additional data sources via implementations of the `TableProvider` trait.
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
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
use crate::mongodb::connection_pool::MongoDBConnectionPool;
use crate::mongodb::utils::expression::{combine_exprs_with_and, expr_to_mongo_filter};
use crate::mongodb::Error;
use async_trait::async_trait;
use datafusion::arrow::datatypes::SchemaRef;
use datafusion::catalog::{Session, TableProvider};
use datafusion::common::project_schema;
use datafusion::error::{DataFusionError, Result as DataFusionResult};
use datafusion::execution::TaskContext;
use datafusion::logical_expr::{Expr, TableProviderFilterPushDown, TableType};
use datafusion::physical_expr::{EquivalenceProperties, PhysicalSortExpr};
use datafusion::physical_plan::execution_plan::{Boundedness, EmissionType};
use datafusion::physical_plan::sort_pushdown::SortOrderPushdownResult;
use datafusion::physical_plan::stream::RecordBatchStreamAdapter;
use datafusion::physical_plan::{
    DisplayAs, DisplayFormatType, ExecutionPlan, Partitioning, PlanProperties,
    SendableRecordBatchStream,
};
use datafusion::sql::TableReference;
use futures::TryStreamExt;
use mongodb::bson::Document;
use serde_json;
use std::{fmt, sync::Arc};

#[derive(Debug)]
pub struct MongoDBTable {
    pool: Arc<MongoDBConnectionPool>,
    schema: SchemaRef,
    table_reference: Arc<TableReference>,
}

impl MongoDBTable {
    pub async fn new(
        pool: &Arc<MongoDBConnectionPool>,
        table_reference: impl Into<TableReference>,
    ) -> Result<Self, Error> {
        let table_reference = table_reference.into();
        let schema = pool.connect().await?.get_schema(&table_reference).await?;

        Ok(Self {
            pool: Arc::clone(pool),
            schema,
            table_reference: Arc::new(table_reference),
        })
    }
}

#[async_trait]
impl TableProvider for MongoDBTable {
    fn schema(&self) -> SchemaRef {
        Arc::clone(&self.schema)
    }

    fn table_type(&self) -> TableType {
        TableType::Base
    }

    async fn scan(
        &self,
        _state: &dyn Session,
        projection: Option<&Vec<usize>>,
        filters: &[Expr],
        limit: Option<usize>,
    ) -> DataFusionResult<Arc<dyn ExecutionPlan>> {
        Ok(Arc::new(MongoDBExec::new(
            Arc::clone(&self.table_reference),
            Arc::clone(&self.pool),
            Arc::clone(&self.schema),
            projection,
            filters,
            limit,
        )?))
    }

    fn supports_filters_pushdown(
        &self,
        filters: &[&Expr],
    ) -> DataFusionResult<Vec<TableProviderFilterPushDown>> {
        supports_filters_pushdown(filters)
    }
}

fn supports_filters_pushdown(
    filters: &[&Expr],
) -> DataFusionResult<Vec<TableProviderFilterPushDown>> {
    let filter_push_down: Vec<TableProviderFilterPushDown> = filters
        .iter()
        .map(|f| match expr_to_mongo_filter(f) {
            Some(_) => TableProviderFilterPushDown::Exact,
            None => TableProviderFilterPushDown::Unsupported,
        })
        .collect();

    Ok(filter_push_down)
}

#[derive(Debug)]
struct MongoDBExec {
    table_reference: Arc<TableReference>,
    pool: Arc<MongoDBConnectionPool>,
    projected_schema: SchemaRef,
    filters_doc: Document,
    sort_doc: Document,
    limit: Option<i32>,
    properties: Arc<PlanProperties>,
}

impl MongoDBExec {
    pub fn new(
        table_reference: Arc<TableReference>,
        pool: Arc<MongoDBConnectionPool>,
        schema: SchemaRef,
        projections: Option<&Vec<usize>>,
        filters: &[Expr],
        limit: Option<usize>,
    ) -> DataFusionResult<Self> {
        let mut projected_schema = project_schema(&schema, projections)?;

        // If no columns are specified, use _id - otherwise mongo returns an error
        if projected_schema.fields.is_empty() {
            let idx = schema.index_of("_id")?;
            projected_schema = SchemaRef::from(schema.project(&[idx])?);
        }

        let limit = limit
            .map(|u| {
                let Ok(u) = u32::try_from(u) else {
                    return Err(DataFusionError::Execution(
                        "Value is too large to fit in a u32".to_string(),
                    ));
                };
                if let Ok(u) = i32::try_from(u) {
                    Ok(u)
                } else {
                    Err(DataFusionError::Execution(
                        "Value is too large to fit in an i32".to_string(),
                    ))
                }
            })
            .transpose()?;

        let combined_exprs = combine_exprs_with_and(filters);

        let mongo_filters_doc = match combined_exprs {
            Some(e) => expr_to_mongo_filter(&e).ok_or(DataFusionError::Execution(
                "Failed to convert expressions".to_string(),
            ))?,
            None => Document::new(),
        };

        Ok(Self {
            table_reference: Arc::clone(&table_reference),
            pool,
            projected_schema: Arc::clone(&projected_schema),
            filters_doc: mongo_filters_doc,
            sort_doc: Document::new(),
            limit,
            properties: Arc::new(PlanProperties::new(
                EquivalenceProperties::new(projected_schema),
                Partitioning::UnknownPartitioning(1),
                EmissionType::Final,
                Boundedness::Bounded,
            )),
        })
    }
}

impl DisplayAs for MongoDBExec {
    fn fmt_as(&self, _t: DisplayFormatType, f: &mut fmt::Formatter) -> std::fmt::Result {
        let columns = self
            .projected_schema
            .fields()
            .iter()
            .map(|f| f.name().as_str())
            .collect::<Vec<_>>();

        let filters = serde_json::to_string(&self.filters_doc).map_err(|_| fmt::Error)?;

        write!(
            f,
            "MongoDBExec projection=[{}] filters=[{}]",
            columns.join(", "),
            filters,
        )?;

        if !self.sort_doc.is_empty() {
            let sort = serde_json::to_string(&self.sort_doc).map_err(|_| fmt::Error)?;
            write!(f, " sort=[{sort}]")?;
        }

        if let Some(limit) = self.limit {
            write!(f, " limit=[{limit}]")?;
        }

        Ok(())
    }
}

impl ExecutionPlan for MongoDBExec {
    fn name(&self) -> &'static str {
        "MongoDBExec"
    }

    fn schema(&self) -> SchemaRef {
        Arc::clone(&self.projected_schema)
    }

    fn properties(&self) -> &Arc<PlanProperties> {
        &self.properties
    }

    fn children(&self) -> Vec<&Arc<dyn ExecutionPlan>> {
        vec![]
    }

    fn with_new_children(
        self: Arc<Self>,
        _children: Vec<Arc<dyn ExecutionPlan>>,
    ) -> DataFusionResult<Arc<dyn ExecutionPlan>> {
        Ok(self)
    }

    fn try_pushdown_sort(
        &self,
        order: &[PhysicalSortExpr],
    ) -> DataFusionResult<SortOrderPushdownResult<Arc<dyn ExecutionPlan>>> {
        use datafusion::physical_expr::expressions::Column;

        let mut sort_doc = Document::new();
        for sort_expr in order {
            let Some(col) = sort_expr.expr.downcast_ref::<Column>() else {
                // Can only push down simple column references
                return Ok(SortOrderPushdownResult::Unsupported);
            };
            let direction = if sort_expr.options.descending { -1 } else { 1 };
            sort_doc.insert(col.name().to_string(), direction);
        }

        let mut new_exec = MongoDBExec {
            table_reference: Arc::clone(&self.table_reference),
            pool: Arc::clone(&self.pool),
            projected_schema: Arc::clone(&self.projected_schema),
            filters_doc: self.filters_doc.clone(),
            sort_doc,
            limit: self.limit,
            properties: self.properties.clone(),
        };

        // Update equivalence properties to reflect the output ordering
        let eq_properties = EquivalenceProperties::new_with_orderings(
            Arc::clone(&self.projected_schema),
            vec![order.to_vec()],
        );
        new_exec.properties =
            Arc::new(PlanProperties::clone(&new_exec.properties).with_eq_properties(eq_properties));

        // Return Inexact rather than Exact so DataFusion keeps the SortExec wrapper
        // above us. Exact would replace the SortExec with `inner`, which loses the
        // SortExec's embedded fetch (`ORDER BY ... LIMIT N` is represented as a
        // single SortExec with fetch=N in DF 52). Keeping the SortExec preserves the
        // fetch as a TopK applied to our already-sorted SQL output.
        // We can use Exact once we use DF version which includes PR https://github.com/apache/datafusion/pull/21182
        Ok(SortOrderPushdownResult::Inexact {
            inner: Arc::new(new_exec),
        })
    }

    fn execute(
        &self,
        _partition: usize,
        _context: Arc<TaskContext>,
    ) -> DataFusionResult<SendableRecordBatchStream> {
        let schema = self.schema();

        let table_reference = Arc::clone(&self.table_reference);
        let pool = Arc::clone(&self.pool);
        let projected_schema = Arc::clone(&self.projected_schema);
        let filters_doc = self.filters_doc.clone();
        let sort_doc = self.sort_doc.clone();
        let limit = self.limit;

        let stream = futures::stream::once(async move {
            let conn = pool.connect().await.map_err(to_execution_error)?;

            conn.query_arrow(
                &table_reference,
                &projected_schema,
                &filters_doc,
                limit,
                &sort_doc,
            )
            .await
            .map_err(to_execution_error)
        })
        .try_flatten();

        Ok(Box::pin(RecordBatchStreamAdapter::new(schema, stream)))
    }
}

#[allow(clippy::needless_pass_by_value)]
pub fn to_execution_error(
    e: impl Into<Box<dyn std::error::Error + Send + Sync>>,
) -> DataFusionError {
    DataFusionError::Execution(format!("{}", e.into()).to_string())
}

#[cfg(test)]
mod tests {
    use super::*;
    use datafusion::arrow::datatypes::{DataType, Field, Schema};
    use datafusion::logical_expr::{col, lit, BinaryExpr, Expr, Operator};
    use mongodb::bson::doc;

    fn test_schema() -> SchemaRef {
        Arc::new(Schema::new(vec![
            Field::new("_id", DataType::Utf8, false),
            Field::new("name", DataType::Utf8, true),
            Field::new("age", DataType::Int32, true),
            Field::new("active", DataType::Boolean, true),
        ]))
    }

    /// Helper to get the DisplayAs output from a MongoDBExec.
    fn format_exec(exec: &MongoDBExec) -> String {
        struct Wrapper<'a>(&'a MongoDBExec);
        impl fmt::Display for Wrapper<'_> {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                self.0.fmt_as(DisplayFormatType::Default, f)
            }
        }
        format!("{}", Wrapper(exec))
    }

    fn stub_pool() -> Arc<MongoDBConnectionPool> {
        Arc::new(MongoDBConnectionPool::new_stub())
    }

    // --- supports_filters_pushdown ---

    #[tokio::test]
    async fn test_supports_filters_pushdown_supported() {
        let expr = col("foo").eq(lit(42));
        let res = supports_filters_pushdown(&[&expr]).unwrap();
        assert_eq!(res, vec![TableProviderFilterPushDown::Exact]);
    }

    #[tokio::test]
    async fn test_supports_filters_pushdown_unsupported() {
        let expr = Expr::BinaryExpr(BinaryExpr {
            left: Box::new(col("foo")),
            op: Operator::Modulo,
            right: Box::new(lit("bar")),
        });
        let res = supports_filters_pushdown(&[&expr]).unwrap();
        assert_eq!(res, vec![TableProviderFilterPushDown::Unsupported]);
    }

    #[tokio::test]
    async fn test_supports_filters_pushdown_mixed() {
        let exprs = vec![
            col("foo").eq(lit(10)),
            col("bar").not_eq(lit("baz")),
            col("foo").is_null(),
        ];
        let refs: Vec<&Expr> = exprs.iter().collect();
        let res = supports_filters_pushdown(&refs).unwrap();
        assert_eq!(
            res,
            vec![
                TableProviderFilterPushDown::Exact,
                TableProviderFilterPushDown::Exact,
                TableProviderFilterPushDown::Exact,
            ]
        );
    }

    #[tokio::test]
    async fn test_supports_filters_pushdown_all_new_types() {
        let exprs = vec![
            col("x").is_null(),
            col("x").is_not_null(),
            col("x").is_true(),
            col("x").is_false(),
            col("x").is_not_true(),
            col("x").is_not_false(),
            col("x").between(lit(1), lit(10)),
            col("x").not_between(lit(1), lit(10)),
            col("x").in_list(vec![lit(1), lit(2)], false),
            col("x").in_list(vec![lit(1), lit(2)], true),
            col("x").like(lit("%foo%")),
            col("x").not_like(lit("bar%")),
            col("x").ilike(lit("%baz")),
            col("x").not_ilike(lit("qux%")),
            Expr::Not(Box::new(col("x").eq(lit(5)))),
        ];
        let refs: Vec<&Expr> = exprs.iter().collect();
        let res = supports_filters_pushdown(&refs).unwrap();
        assert!(
            res.iter().all(|r| *r == TableProviderFilterPushDown::Exact),
            "All new expression types should be Exact, got: {res:?}"
        );
    }

    #[tokio::test]
    async fn test_supports_filters_pushdown_empty() {
        let res = supports_filters_pushdown(&[]).unwrap();
        assert!(res.is_empty());
    }

    // --- DisplayAs / explain plan ---

    #[tokio::test]
    async fn test_display_no_filters_no_limit() {
        let schema = test_schema();
        let table_ref = Arc::new(TableReference::bare("users"));
        let exec = MongoDBExec::new(table_ref, stub_pool(), schema, None, &[], None).unwrap();

        let display = format_exec(&exec);
        assert!(
            display.contains("MongoDBExec projection=[_id, name, age, active]"),
            "Should show all columns: {display}"
        );
        assert!(display.contains("filters=[{}]"), "No filters: {display}");
        assert!(
            !display.contains("sort="),
            "No sort shown when empty: {display}"
        );
        assert!(
            !display.contains("limit="),
            "No limit shown when None: {display}"
        );
    }

    #[tokio::test]
    async fn test_display_with_projection() {
        let schema = test_schema();
        let table_ref = Arc::new(TableReference::bare("users"));
        let exec =
            MongoDBExec::new(table_ref, stub_pool(), schema, Some(&vec![1, 2]), &[], None).unwrap();

        let display = format_exec(&exec);
        assert!(
            display.contains("projection=[name, age]"),
            "Should show projected columns: {display}"
        );
    }

    #[tokio::test]
    async fn test_display_with_filters() {
        let schema = test_schema();
        let table_ref = Arc::new(TableReference::bare("users"));
        let filters = vec![col("age").gt(lit(21))];
        let exec = MongoDBExec::new(table_ref, stub_pool(), schema, None, &filters, None).unwrap();

        let display = format_exec(&exec);
        assert!(
            display.contains(r#""age":{"$gt":21}"#),
            "Should show filter doc: {display}"
        );
    }

    #[tokio::test]
    async fn test_display_with_limit() {
        let schema = test_schema();
        let table_ref = Arc::new(TableReference::bare("users"));
        let exec = MongoDBExec::new(table_ref, stub_pool(), schema, None, &[], Some(100)).unwrap();

        let display = format_exec(&exec);
        assert!(
            display.contains("limit=[100]"),
            "Should show limit: {display}"
        );
    }

    #[tokio::test]
    async fn test_display_with_sort() {
        let schema = test_schema();
        let table_ref = Arc::new(TableReference::bare("users"));
        let mut exec = MongoDBExec::new(table_ref, stub_pool(), schema, None, &[], None).unwrap();
        exec.sort_doc = doc! { "name": 1, "age": -1 };

        let display = format_exec(&exec);
        assert!(
            display.contains("sort=["),
            "Should show sort section: {display}"
        );
        assert!(
            display.contains(r#""name":1"#),
            "Should show sort fields: {display}"
        );
        assert!(
            display.contains(r#""age":-1"#),
            "Should show sort fields: {display}"
        );
    }

    #[tokio::test]
    async fn test_display_with_all_options() {
        let schema = test_schema();
        let table_ref = Arc::new(TableReference::bare("users"));
        let filters = vec![col("active").eq(lit(true))];
        let mut exec = MongoDBExec::new(
            table_ref,
            stub_pool(),
            schema,
            Some(&vec![1, 3]),
            &filters,
            Some(50),
        )
        .unwrap();
        exec.sort_doc = doc! { "name": 1 };

        let display = format_exec(&exec);
        assert!(
            display.contains("projection=[name, active]"),
            "projection: {display}"
        );
        assert!(display.contains(r#""active":true"#), "filter: {display}");
        assert!(display.contains("sort=["), "sort: {display}");
        assert!(display.contains("limit=[50]"), "limit: {display}");
    }

    #[tokio::test]
    async fn test_display_complex_filter() {
        let schema = test_schema();
        let table_ref = Arc::new(TableReference::bare("users"));
        let filters = vec![col("age").gt(lit(18)).and(col("name").eq(lit("Alice")))];
        let exec = MongoDBExec::new(table_ref, stub_pool(), schema, None, &filters, None).unwrap();

        let display = format_exec(&exec);
        assert!(
            display.contains("$and"),
            "Should show AND filter: {display}"
        );
        assert!(
            display.contains(r#""age":{"$gt":18}"#),
            "Should show age filter: {display}"
        );
        assert!(
            display.contains(r#""name":"Alice""#),
            "Should show name filter: {display}"
        );
    }

    // --- MongoDBExec edge cases ---

    #[tokio::test]
    async fn test_exec_empty_projection_falls_back_to_id() {
        let schema = test_schema();
        let table_ref = Arc::new(TableReference::bare("users"));
        let exec =
            MongoDBExec::new(table_ref, stub_pool(), schema, Some(&vec![]), &[], None).unwrap();

        let display = format_exec(&exec);
        assert!(
            display.contains("projection=[_id]"),
            "Empty projection should fall back to _id: {display}"
        );
    }

    #[tokio::test]
    async fn test_exec_limit_too_large() {
        let schema = test_schema();
        let table_ref = Arc::new(TableReference::bare("users"));
        let result = MongoDBExec::new(table_ref, stub_pool(), schema, None, &[], Some(usize::MAX));
        assert!(result.is_err(), "Should fail for limit that exceeds i32");
    }

    #[tokio::test]
    async fn test_exec_no_filters_produces_empty_doc() {
        let schema = test_schema();
        let table_ref = Arc::new(TableReference::bare("users"));
        let exec = MongoDBExec::new(table_ref, stub_pool(), schema, None, &[], None).unwrap();

        assert!(
            exec.filters_doc.is_empty(),
            "No filters should produce empty doc"
        );
    }

    #[tokio::test]
    async fn test_exec_multiple_filters_combined() {
        let schema = test_schema();
        let table_ref = Arc::new(TableReference::bare("users"));
        let filters = vec![col("age").gt(lit(18)), col("active").eq(lit(true))];
        let exec = MongoDBExec::new(table_ref, stub_pool(), schema, None, &filters, None).unwrap();

        assert!(
            exec.filters_doc.contains_key("$and"),
            "Multiple filters should be combined with $and: {:?}",
            exec.filters_doc
        );
    }

    #[tokio::test]
    async fn test_exec_properties() {
        let schema = test_schema();
        let table_ref = Arc::new(TableReference::bare("users"));
        let exec = MongoDBExec::new(table_ref, stub_pool(), schema, None, &[], None).unwrap();

        assert_eq!(exec.name(), "MongoDBExec");
        assert_eq!(exec.children().len(), 0);
        assert!(
            matches!(
                exec.properties().partitioning,
                Partitioning::UnknownPartitioning(1)
            ),
            "Expected UnknownPartitioning(1)"
        );
    }

    #[tokio::test]
    async fn test_exec_unconvertible_combined_filter_errors() {
        // Two filters where AND combines them, but the combined expr can't be converted
        // (e.g., one is a Modulo that passes combine_exprs_with_and but fails expr_to_mongo_filter)
        let schema = test_schema();
        let table_ref = Arc::new(TableReference::bare("users"));
        let filters = vec![Expr::BinaryExpr(BinaryExpr {
            left: Box::new(col("age")),
            op: Operator::Modulo,
            right: Box::new(lit(2)),
        })];
        let result = MongoDBExec::new(table_ref, stub_pool(), schema, None, &filters, None);
        assert!(
            result.is_err(),
            "Should error when combined filter can't be converted"
        );
    }

    #[tokio::test]
    async fn test_exec_with_new_children_returns_self() {
        let schema = test_schema();
        let table_ref = Arc::new(TableReference::bare("users"));
        let exec = MongoDBExec::new(table_ref, stub_pool(), schema, None, &[], None).unwrap();
        let exec_arc: Arc<dyn ExecutionPlan> = Arc::new(exec);
        let result = exec_arc.clone().with_new_children(vec![]).unwrap();
        assert_eq!(result.name(), "MongoDBExec");
    }

    // --- try_pushdown_sort ---

    #[tokio::test]
    async fn test_sort_pushdown_single_column_asc() {
        use datafusion::physical_expr::expressions::Column as PhysColumn;

        let schema = test_schema();
        let table_ref = Arc::new(TableReference::bare("users"));
        let exec = MongoDBExec::new(table_ref, stub_pool(), schema, None, &[], None).unwrap();

        let sort_exprs = vec![PhysicalSortExpr::new(
            Arc::new(PhysColumn::new("name", 1)),
            datafusion::arrow::compute::SortOptions {
                descending: false,
                nulls_first: true,
            },
        )];

        let result = exec.try_pushdown_sort(&sort_exprs).unwrap();
        match result {
            SortOrderPushdownResult::Inexact { inner } => {
                let mongo_exec = inner.downcast_ref::<MongoDBExec>().unwrap();
                assert_eq!(mongo_exec.sort_doc, doc! { "name": 1 });
                let display = format_exec(mongo_exec);
                assert!(
                    display.contains("sort=["),
                    "Display should show sort: {display}"
                );
            }
            other => panic!("Expected Inexact, got: {other:?}"),
        }
    }

    #[tokio::test]
    async fn test_sort_pushdown_single_column_desc() {
        use datafusion::physical_expr::expressions::Column as PhysColumn;

        let schema = test_schema();
        let table_ref = Arc::new(TableReference::bare("users"));
        let exec = MongoDBExec::new(table_ref, stub_pool(), schema, None, &[], None).unwrap();

        let sort_exprs = vec![PhysicalSortExpr::new(
            Arc::new(PhysColumn::new("age", 2)),
            datafusion::arrow::compute::SortOptions {
                descending: true,
                nulls_first: false,
            },
        )];

        let result = exec.try_pushdown_sort(&sort_exprs).unwrap();
        match result {
            SortOrderPushdownResult::Inexact { inner } => {
                let mongo_exec = inner.downcast_ref::<MongoDBExec>().unwrap();
                assert_eq!(mongo_exec.sort_doc, doc! { "age": -1 });
            }
            other => panic!("Expected Inexact, got: {other:?}"),
        }
    }

    #[tokio::test]
    async fn test_sort_pushdown_multiple_columns() {
        use datafusion::physical_expr::expressions::Column as PhysColumn;

        let schema = test_schema();
        let table_ref = Arc::new(TableReference::bare("users"));
        let exec = MongoDBExec::new(table_ref, stub_pool(), schema, None, &[], None).unwrap();

        let sort_exprs = vec![
            PhysicalSortExpr::new(
                Arc::new(PhysColumn::new("name", 1)),
                datafusion::arrow::compute::SortOptions {
                    descending: false,
                    nulls_first: true,
                },
            ),
            PhysicalSortExpr::new(
                Arc::new(PhysColumn::new("age", 2)),
                datafusion::arrow::compute::SortOptions {
                    descending: true,
                    nulls_first: false,
                },
            ),
        ];

        let result = exec.try_pushdown_sort(&sort_exprs).unwrap();
        match result {
            SortOrderPushdownResult::Inexact { inner } => {
                let mongo_exec = inner.downcast_ref::<MongoDBExec>().unwrap();
                assert_eq!(mongo_exec.sort_doc, doc! { "name": 1, "age": -1 });
            }
            other => panic!("Expected Inexact, got: {other:?}"),
        }
    }

    #[tokio::test]
    async fn test_sort_pushdown_non_column_returns_unsupported() {
        use datafusion::physical_expr::expressions::Literal;
        use datafusion::scalar::ScalarValue;

        let schema = test_schema();
        let table_ref = Arc::new(TableReference::bare("users"));
        let exec = MongoDBExec::new(table_ref, stub_pool(), schema, None, &[], None).unwrap();

        let sort_exprs = vec![PhysicalSortExpr::new(
            Arc::new(Literal::new(ScalarValue::Int32(Some(1)))),
            datafusion::arrow::compute::SortOptions::default(),
        )];

        let result = exec.try_pushdown_sort(&sort_exprs).unwrap();
        assert!(
            matches!(result, SortOrderPushdownResult::Unsupported),
            "Non-column sort should be Unsupported"
        );
    }

    #[tokio::test]
    async fn test_sort_pushdown_preserves_filters_and_limit() {
        use datafusion::physical_expr::expressions::Column as PhysColumn;

        let schema = test_schema();
        let table_ref = Arc::new(TableReference::bare("users"));
        let filters = vec![col("age").gt(lit(21))];
        let exec =
            MongoDBExec::new(table_ref, stub_pool(), schema, None, &filters, Some(10)).unwrap();

        let sort_exprs = vec![PhysicalSortExpr::new(
            Arc::new(PhysColumn::new("name", 1)),
            datafusion::arrow::compute::SortOptions::default(),
        )];

        let result = exec.try_pushdown_sort(&sort_exprs).unwrap();
        match result {
            SortOrderPushdownResult::Inexact { inner } => {
                let mongo_exec = inner.downcast_ref::<MongoDBExec>().unwrap();
                assert!(
                    !mongo_exec.filters_doc.is_empty(),
                    "Filters should be preserved"
                );
                assert_eq!(mongo_exec.limit, Some(10), "Limit should be preserved");
                assert_eq!(mongo_exec.sort_doc, doc! { "name": 1 });
            }
            other => panic!("Expected Inexact, got: {other:?}"),
        }
    }

    #[tokio::test]
    async fn test_sort_pushdown_empty_order() {
        let schema = test_schema();
        let table_ref = Arc::new(TableReference::bare("users"));
        let exec = MongoDBExec::new(table_ref, stub_pool(), schema, None, &[], None).unwrap();

        let result = exec.try_pushdown_sort(&[]).unwrap();
        match result {
            SortOrderPushdownResult::Inexact { inner } => {
                let mongo_exec = inner.downcast_ref::<MongoDBExec>().unwrap();
                assert!(
                    mongo_exec.sort_doc.is_empty(),
                    "Empty sort should produce empty doc"
                );
            }
            other => panic!("Expected Inexact, got: {other:?}"),
        }
    }
}