Skip to main content

datafusion_python/expr/
create_index.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::fmt::{self, Display, Formatter};
19use std::sync::Arc;
20
21use datafusion::logical_expr::CreateIndex;
22use pyo3::IntoPyObjectExt;
23use pyo3::prelude::*;
24
25use super::logical_node::LogicalNode;
26use super::sort_expr::PySortExpr;
27use crate::common::df_schema::PyDFSchema;
28use crate::sql::logical::PyLogicalPlan;
29
30#[pyclass(
31    from_py_object,
32    frozen,
33    name = "CreateIndex",
34    module = "datafusion.expr",
35    subclass
36)]
37#[derive(Clone)]
38pub struct PyCreateIndex {
39    create: CreateIndex,
40}
41
42impl From<PyCreateIndex> for CreateIndex {
43    fn from(create: PyCreateIndex) -> Self {
44        create.create
45    }
46}
47
48impl From<CreateIndex> for PyCreateIndex {
49    fn from(create: CreateIndex) -> PyCreateIndex {
50        PyCreateIndex { create }
51    }
52}
53
54impl Display for PyCreateIndex {
55    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
56        write!(f, "CreateIndex: {:?}", self.create.name)
57    }
58}
59
60#[pymethods]
61impl PyCreateIndex {
62    #[new]
63    #[pyo3(signature = (table, columns, unique, if_not_exists, schema, name=None, using=None))]
64    pub fn new(
65        table: String,
66        columns: Vec<PySortExpr>,
67        unique: bool,
68        if_not_exists: bool,
69        schema: PyDFSchema,
70        name: Option<String>,
71        using: Option<String>,
72    ) -> PyResult<Self> {
73        Ok(PyCreateIndex {
74            create: CreateIndex {
75                name,
76                table: table.into(),
77                using,
78                columns: columns.iter().map(|c| c.clone().into()).collect(),
79                unique,
80                if_not_exists,
81                schema: Arc::new(schema.into()),
82            },
83        })
84    }
85
86    pub fn name(&self) -> Option<String> {
87        self.create.name.clone()
88    }
89
90    pub fn table(&self) -> PyResult<String> {
91        Ok(self.create.table.to_string())
92    }
93
94    pub fn using(&self) -> Option<String> {
95        self.create.using.clone()
96    }
97
98    pub fn columns(&self) -> Vec<PySortExpr> {
99        self.create
100            .columns
101            .iter()
102            .map(|c| c.clone().into())
103            .collect()
104    }
105
106    pub fn unique(&self) -> bool {
107        self.create.unique
108    }
109
110    pub fn if_not_exists(&self) -> bool {
111        self.create.if_not_exists
112    }
113
114    pub fn schema(&self) -> PyDFSchema {
115        (*self.create.schema).clone().into()
116    }
117
118    fn __repr__(&self) -> PyResult<String> {
119        Ok(format!("CreateIndex({self})"))
120    }
121
122    fn __name__(&self) -> PyResult<String> {
123        Ok("CreateIndex".to_string())
124    }
125}
126
127impl LogicalNode for PyCreateIndex {
128    fn inputs(&self) -> Vec<PyLogicalPlan> {
129        vec![]
130    }
131
132    fn to_variant<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
133        self.clone().into_bound_py_any(py)
134    }
135}