datafusion_python/expr/
values.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::sync::Arc;
19
20use datafusion::logical_expr::Values;
21use pyo3::prelude::*;
22use pyo3::{pyclass, IntoPyObjectExt, PyErr, PyResult, Python};
23
24use super::logical_node::LogicalNode;
25use super::PyExpr;
26use crate::common::df_schema::PyDFSchema;
27use crate::sql::logical::PyLogicalPlan;
28
29#[pyclass(frozen, name = "Values", module = "datafusion.expr", subclass)]
30#[derive(Clone)]
31pub struct PyValues {
32    values: Values,
33}
34
35impl From<Values> for PyValues {
36    fn from(values: Values) -> PyValues {
37        PyValues { values }
38    }
39}
40
41impl TryFrom<PyValues> for Values {
42    type Error = PyErr;
43
44    fn try_from(py: PyValues) -> Result<Self, Self::Error> {
45        Ok(py.values)
46    }
47}
48
49impl LogicalNode for PyValues {
50    fn inputs(&self) -> Vec<PyLogicalPlan> {
51        vec![]
52    }
53
54    fn to_variant<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
55        self.clone().into_bound_py_any(py)
56    }
57}
58
59#[pymethods]
60impl PyValues {
61    #[new]
62    pub fn new(schema: PyDFSchema, values: Vec<Vec<PyExpr>>) -> PyResult<Self> {
63        let values = values
64            .into_iter()
65            .map(|row| row.into_iter().map(|expr| expr.into()).collect())
66            .collect();
67        Ok(PyValues {
68            values: Values {
69                schema: Arc::new(schema.into()),
70                values,
71            },
72        })
73    }
74
75    pub fn schema(&self) -> PyResult<PyDFSchema> {
76        Ok((*self.values.schema).clone().into())
77    }
78
79    pub fn values(&self) -> Vec<Vec<PyExpr>> {
80        self.values
81            .values
82            .clone()
83            .into_iter()
84            .map(|row| row.into_iter().map(|expr| expr.into()).collect())
85            .collect()
86    }
87}