datafusion_python/expr/
limit.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 datafusion::logical_expr::logical_plan::Limit;
19use pyo3::{prelude::*, IntoPyObjectExt};
20use std::fmt::{self, Display, Formatter};
21
22use crate::common::df_schema::PyDFSchema;
23use crate::expr::logical_node::LogicalNode;
24use crate::sql::logical::PyLogicalPlan;
25
26#[pyclass(name = "Limit", module = "datafusion.expr", subclass)]
27#[derive(Clone)]
28pub struct PyLimit {
29    limit: Limit,
30}
31
32impl From<Limit> for PyLimit {
33    fn from(limit: Limit) -> PyLimit {
34        PyLimit { limit }
35    }
36}
37
38impl From<PyLimit> for Limit {
39    fn from(limit: PyLimit) -> Self {
40        limit.limit
41    }
42}
43
44impl Display for PyLimit {
45    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
46        write!(
47            f,
48            "Limit
49            Skip: {:?}
50            Fetch: {:?}
51            Input: {:?}",
52            &self.limit.skip, &self.limit.fetch, &self.limit.input
53        )
54    }
55}
56
57#[pymethods]
58impl PyLimit {
59    // NOTE: Upstream now has expressions for skip and fetch
60    // TODO: Do we still want to expose these?
61    // REF: https://github.com/apache/datafusion/pull/12836
62
63    // /// Retrieves the skip value for this `Limit`
64    // fn skip(&self) -> usize {
65    //     self.limit.skip
66    // }
67
68    // /// Retrieves the fetch value for this `Limit`
69    // fn fetch(&self) -> Option<usize> {
70    //     self.limit.fetch
71    // }
72
73    /// Retrieves the input `LogicalPlan` to this `Limit` node
74    fn input(&self) -> PyResult<Vec<PyLogicalPlan>> {
75        Ok(Self::inputs(self))
76    }
77
78    /// Resulting Schema for this `Limit` node instance
79    fn schema(&self) -> PyResult<PyDFSchema> {
80        Ok(self.limit.input.schema().as_ref().clone().into())
81    }
82
83    fn __repr__(&self) -> PyResult<String> {
84        Ok(format!("Limit({})", self))
85    }
86}
87
88impl LogicalNode for PyLimit {
89    fn inputs(&self) -> Vec<PyLogicalPlan> {
90        vec![PyLogicalPlan::from((*self.limit.input).clone())]
91    }
92
93    fn to_variant<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
94        self.clone().into_bound_py_any(py)
95    }
96}