Skip to main content

polars_python/functions/
misc.rs

1use polars_plan::prelude::*;
2use pyo3::prelude::*;
3use pyo3::types::PyBytes;
4
5use crate::PyExpr;
6use crate::conversion::Wrap;
7use crate::expr::ToExprs;
8use crate::prelude::DataType;
9
10#[pyfunction]
11pub fn dtype_str_repr(dtype: Wrap<DataType>) -> PyResult<String> {
12    let dtype = dtype.0;
13    Ok(dtype.to_string())
14}
15
16#[cfg(feature = "ffi_plugin")]
17#[pyfunction]
18pub fn register_plugin_function(
19    plugin_path: &str,
20    function_name: &str,
21    args: Vec<PyExpr>,
22    kwargs: Vec<u8>,
23    is_elementwise: bool,
24    input_wildcard_expansion: bool,
25    returns_scalar: bool,
26    cast_to_supertype: bool,
27    pass_name_to_apply: bool,
28    changes_length: bool,
29) -> PyResult<PyExpr> {
30    let cast_to_supertypes = if cast_to_supertype {
31        Some(CastingRules::cast_to_supertypes())
32    } else {
33        None
34    };
35
36    let mut flags = FunctionFlags::default();
37    if is_elementwise {
38        flags.set_elementwise();
39    }
40    flags.set(FunctionFlags::LENGTH_PRESERVING, !changes_length);
41    flags.set(FunctionFlags::PASS_NAME_TO_APPLY, pass_name_to_apply);
42    flags.set(FunctionFlags::RETURNS_SCALAR, returns_scalar);
43    flags.set(
44        FunctionFlags::INPUT_WILDCARD_EXPANSION,
45        input_wildcard_expansion,
46    );
47
48    let options = FunctionOptions {
49        cast_options: cast_to_supertypes,
50        flags,
51        ..Default::default()
52    };
53
54    Ok(Expr::Function {
55        input: args.to_exprs(),
56        function: FunctionExpr::FfiPlugin {
57            flags: options,
58            lib: plugin_path.into(),
59            symbol: function_name.into(),
60            kwargs: kwargs.into(),
61        },
62    }
63    .into())
64}
65
66#[pyfunction]
67pub fn __register_startup_deps(warn_function: Py<PyAny>) {
68    #[cfg(feature = "object")]
69    unsafe {
70        crate::on_startup::register_startup_deps(true, warn_function)
71    }
72    #[cfg(not(feature = "object"))]
73    let _ = warn_function;
74}
75
76#[pyfunction]
77pub fn gen_uuid_v7(py: Python) -> Py<PyBytes> {
78    PyBytes::new(py, uuid::Uuid::now_v7().as_bytes()).unbind()
79}