datafusion_python/expr/
values.rs1use std::sync::Arc;
19
20use datafusion::logical_expr::Values;
21use pyo3::{prelude::*, IntoPyObjectExt};
22use pyo3::{pyclass, PyErr, PyResult, Python};
23
24use crate::{common::df_schema::PyDFSchema, sql::logical::PyLogicalPlan};
25
26use super::{logical_node::LogicalNode, PyExpr};
27
28#[pyclass(name = "Values", module = "datafusion.expr", subclass)]
29#[derive(Clone)]
30pub struct PyValues {
31 values: Values,
32}
33
34impl From<Values> for PyValues {
35 fn from(values: Values) -> PyValues {
36 PyValues { values }
37 }
38}
39
40impl TryFrom<PyValues> for Values {
41 type Error = PyErr;
42
43 fn try_from(py: PyValues) -> Result<Self, Self::Error> {
44 Ok(py.values)
45 }
46}
47
48impl LogicalNode for PyValues {
49 fn inputs(&self) -> Vec<PyLogicalPlan> {
50 vec![]
51 }
52
53 fn to_variant<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
54 self.clone().into_bound_py_any(py)
55 }
56}
57
58#[pymethods]
59impl PyValues {
60 #[new]
61 pub fn new(schema: PyDFSchema, values: Vec<Vec<PyExpr>>) -> PyResult<Self> {
62 let values = values
63 .into_iter()
64 .map(|row| row.into_iter().map(|expr| expr.into()).collect())
65 .collect();
66 Ok(PyValues {
67 values: Values {
68 schema: Arc::new(schema.into()),
69 values,
70 },
71 })
72 }
73
74 pub fn schema(&self) -> PyResult<PyDFSchema> {
75 Ok((*self.values.schema).clone().into())
76 }
77
78 pub fn values(&self) -> Vec<Vec<PyExpr>> {
79 self.values
80 .values
81 .clone()
82 .into_iter()
83 .map(|row| row.into_iter().map(|expr| expr.into()).collect())
84 .collect()
85 }
86}