polars_plan/plans/
apply.rs

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

use polars_core::prelude::*;

pub trait DataFrameUdf: Send + Sync {
    fn call_udf(&self, df: DataFrame) -> PolarsResult<DataFrame>;
}

impl<F> DataFrameUdf for F
where
    F: Fn(DataFrame) -> PolarsResult<DataFrame> + Send + Sync,
{
    fn call_udf(&self, df: DataFrame) -> PolarsResult<DataFrame> {
        self(df)
    }
}

pub trait DataFrameUdfMut: Send + Sync {
    fn call_udf(&mut self, df: DataFrame) -> PolarsResult<DataFrame>;
}

impl<F> DataFrameUdfMut for F
where
    F: FnMut(DataFrame) -> PolarsResult<DataFrame> + Send + Sync,
{
    fn call_udf(&mut self, df: DataFrame) -> PolarsResult<DataFrame> {
        self(df)
    }
}

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

pub trait UdfSchema: Send + Sync {
    fn get_schema(&self, input_schema: &Schema) -> PolarsResult<SchemaRef>;
}

impl<F> UdfSchema for F
where
    F: Fn(&Schema) -> PolarsResult<SchemaRef> + Send + Sync,
{
    fn get_schema(&self, input_schema: &Schema) -> PolarsResult<SchemaRef> {
        self(input_schema)
    }
}

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