Repository navigation
Expand file tree
/
Copy pathtable_scan.rs
More file actions
119 lines (106 loc) · 3.93 KB
/
Copy pathtable_scan.rs
File metadata and controls
119 lines (106 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
use datafusion_expr::logical_plan::TableScan;
use pyo3::prelude::*;
use std::fmt::{self, Display, Formatter};
use crate::{common::df_schema::PyDFSchema, expr::PyExpr};
#[pyclass(name = "TableScan", module = "datafusion.expr", subclass)]
#[derive(Clone)]
pub struct PyTableScan {
table_scan: TableScan,
}
impl From<PyTableScan> for TableScan {
fn from(tbl_scan: PyTableScan) -> TableScan {
tbl_scan.table_scan
}
}
impl From<TableScan> for PyTableScan {
fn from(table_scan: TableScan) -> PyTableScan {
PyTableScan { table_scan }
}
}
impl Display for PyTableScan {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(
f,
"TableScan\nTable Name: {}
\nProjections: {:?}
\nProjected Schema: {:?}
\nFilters: {:?}",
&self.table_scan.table_name,
&self.py_projections(),
&self.py_schema(),
&self.py_filters(),
)
}
}
#[pymethods]
impl PyTableScan {
/// Retrieves the name of the table represented by this `TableScan` instance
#[pyo3(name = "table_name")]
fn py_table_name(&self) -> PyResult<&str> {
Ok(&self.table_scan.table_name)
}
/// TODO: Bindings for `TableSource` need to exist first. Left as a
/// placeholder to display intention to add when able to.
// #[pyo3(name = "source")]
// fn py_source(&self) -> PyResult<Arc<dyn TableSource>> {
// Ok(self.table_scan.source)
// }
/// The column indexes that should be. Note if this is empty then
/// all columns should be read by the `TableProvider`. This function
/// provides a Tuple of the (index, column_name) to make things simplier
/// for the calling code since often times the name is preferred to
/// the index which is a lower level abstraction.
#[pyo3(name = "projection")]
fn py_projections(&self) -> PyResult<Vec<(usize, String)>> {
match &self.table_scan.projection {
Some(indices) => {
let schema = self.table_scan.source.schema();
Ok(indices
.iter()
.map(|i| (*i, schema.field(*i).name().to_string()))
.collect())
}
None => Ok(vec![]),
}
}
/// Resulting schema from the `TableScan` operation
#[pyo3(name = "schema")]
fn py_schema(&self) -> PyResult<PyDFSchema> {
Ok((*self.table_scan.projected_schema).clone().into())
}
/// Certain `TableProvider` physical readers offer the capability to filter rows that
/// are read at read time. These `filters` are contained here.
#[pyo3(name = "filters")]
fn py_filters(&self) -> PyResult<Vec<PyExpr>> {
Ok(self
.table_scan
.filters
.iter()
.map(|expr| PyExpr::from(expr.clone()))
.collect())
}
/// Optional number of rows that should be read at read time by the `TableProvider`
#[pyo3(name = "fetch")]
fn py_fetch(&self) -> PyResult<Option<usize>> {
Ok(self.table_scan.fetch)
}
fn __repr__(&self) -> PyResult<String> {
Ok(format!("TableScan({})", self))
}
}