Skip to main content

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::{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}