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