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