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::*, 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}