Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-plan/src/dsl/python_dsl/source.rs
6940 views
1
use std::sync::Arc;
2
3
use either::Either;
4
use polars_core::error::{PolarsResult, polars_err};
5
use polars_core::schema::SchemaRef;
6
use polars_utils::python_function::PythonFunction;
7
use pyo3::prelude::*;
8
#[cfg(feature = "serde")]
9
use serde::{Deserialize, Serialize};
10
11
use crate::dsl::SpecialEq;
12
13
#[derive(Clone, PartialEq, Eq, Debug, Default)]
14
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
15
#[cfg_attr(feature = "dsl-schema", derive(schemars::JsonSchema))]
16
pub struct PythonOptionsDsl {
17
/// A function that returns a Python Generator.
18
/// The generator should produce Polars DataFrame's.
19
pub scan_fn: Option<PythonFunction>,
20
/// Either the schema fn or schema is set.
21
pub schema_fn: Option<SpecialEq<Arc<Either<PythonFunction, SchemaRef>>>>,
22
pub python_source: PythonScanSource,
23
pub validate_schema: bool,
24
pub is_pure: bool,
25
}
26
27
impl PythonOptionsDsl {
28
pub fn get_schema(&self) -> PolarsResult<SchemaRef> {
29
match self.schema_fn.as_ref().expect("should be set").as_ref() {
30
Either::Left(func) => Python::with_gil(|py| {
31
let schema = func
32
.0
33
.call0(py)
34
.map_err(|e| polars_err!(ComputeError: "schema callable failed: {}", e))?;
35
crate::plans::python::python_schema_to_rust(py, schema.into_bound(py))
36
}),
37
Either::Right(schema) => Ok(schema.clone()),
38
}
39
}
40
}
41
42
#[derive(Clone, PartialEq, Eq, Debug, Default, Hash)]
43
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
44
#[cfg_attr(feature = "dsl-schema", derive(schemars::JsonSchema))]
45
pub enum PythonScanSource {
46
Pyarrow,
47
Cuda,
48
#[default]
49
IOPlugin,
50
}
51
52