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
use std::any::Any;
use std::fmt::{Debug, Formatter};

use polars_core::prelude::*;

pub use super::options::AnonymousScanOptions;
use crate::dsl::Expr;

pub struct AnonymousScanArgs {
    pub n_rows: Option<usize>,
    pub with_columns: Option<Arc<[String]>>,
    pub schema: SchemaRef,
    pub output_schema: Option<SchemaRef>,
    pub predicate: Option<Expr>,
}

pub trait AnonymousScan: Send + Sync {
    fn as_any(&self) -> &dyn Any;
    /// Creates a DataFrame from the supplied function & scan options.
    fn scan(&self, scan_opts: AnonymousScanArgs) -> PolarsResult<DataFrame>;

    /// function to supply the schema.
    /// Allows for an optional infer schema argument for data sources with dynamic schemas
    fn schema(&self, _infer_schema_length: Option<usize>) -> PolarsResult<SchemaRef> {
        polars_bail!(ComputeError: "must supply either a schema or a schema function");
    }
    /// specify if the scan provider should allow predicate pushdowns
    ///
    /// Defaults to `false`
    fn allows_predicate_pushdown(&self) -> bool {
        false
    }
    /// specify if the scan provider should allow projection pushdowns
    ///
    /// Defaults to `false`
    fn allows_projection_pushdown(&self) -> bool {
        false
    }
    /// specify if the scan provider should allow slice pushdowns
    ///
    /// Defaults to `false`
    fn allows_slice_pushdown(&self) -> bool {
        false
    }
}

impl<F> AnonymousScan for F
where
    F: Fn(AnonymousScanArgs) -> PolarsResult<DataFrame> + Send + Sync,
{
    fn as_any(&self) -> &dyn Any {
        unimplemented!()
    }

    fn scan(&self, scan_opts: AnonymousScanArgs) -> PolarsResult<DataFrame> {
        self(scan_opts)
    }
}

impl Debug for dyn AnonymousScan {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "anonymous_scan")
    }
}