|
15 | 15 | // specific language governing permissions and limitations |
16 | 16 | // under the License. |
17 | 17 |
|
| 18 | +use std::fmt::{self, Display, Formatter}; |
| 19 | +use std::sync::Arc; |
18 | 20 | use std::{any::Any, borrow::Cow}; |
19 | 21 |
|
| 22 | +use arrow::datatypes::Schema; |
| 23 | +use arrow::pyarrow::PyArrowType; |
20 | 24 | use datafusion::arrow::datatypes::SchemaRef; |
| 25 | +use datafusion::common::Constraints; |
| 26 | +use datafusion::datasource::TableType; |
21 | 27 | use datafusion::logical_expr::{Expr, TableProviderFilterPushDown, TableSource}; |
22 | 28 | use pyo3::prelude::*; |
23 | 29 |
|
24 | 30 | use datafusion::logical_expr::utils::split_conjunction; |
25 | 31 |
|
| 32 | +use crate::sql::logical::PyLogicalPlan; |
| 33 | + |
26 | 34 | use super::{data_type::DataTypeMap, function::SqlFunction}; |
27 | 35 |
|
28 | 36 | #[pyclass(name = "SqlSchema", module = "datafusion.common", subclass)] |
@@ -218,3 +226,84 @@ impl SqlStatistics { |
218 | 226 | self.row_count |
219 | 227 | } |
220 | 228 | } |
| 229 | + |
| 230 | +#[pyclass(name = "Constraints", module = "datafusion.expr", subclass)] |
| 231 | +#[derive(Clone)] |
| 232 | +pub struct PyConstraints { |
| 233 | + pub constraints: Constraints, |
| 234 | +} |
| 235 | + |
| 236 | +impl From<PyConstraints> for Constraints { |
| 237 | + fn from(constraints: PyConstraints) -> Self { |
| 238 | + constraints.constraints |
| 239 | + } |
| 240 | +} |
| 241 | + |
| 242 | +impl From<Constraints> for PyConstraints { |
| 243 | + fn from(constraints: Constraints) -> Self { |
| 244 | + PyConstraints { constraints } |
| 245 | + } |
| 246 | +} |
| 247 | + |
| 248 | +impl Display for PyConstraints { |
| 249 | + fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
| 250 | + write!(f, "Constraints: {:?}", self.constraints) |
| 251 | + } |
| 252 | +} |
| 253 | + |
| 254 | +#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] |
| 255 | +#[pyclass(eq, eq_int, name = "TableType", module = "datafusion.common")] |
| 256 | +pub enum PyTableType { |
| 257 | + Base, |
| 258 | + View, |
| 259 | + Temporary, |
| 260 | +} |
| 261 | + |
| 262 | +impl From<PyTableType> for datafusion::logical_expr::TableType { |
| 263 | + fn from(table_type: PyTableType) -> Self { |
| 264 | + match table_type { |
| 265 | + PyTableType::Base => datafusion::logical_expr::TableType::Base, |
| 266 | + PyTableType::View => datafusion::logical_expr::TableType::View, |
| 267 | + PyTableType::Temporary => datafusion::logical_expr::TableType::Temporary, |
| 268 | + } |
| 269 | + } |
| 270 | +} |
| 271 | + |
| 272 | +impl From<TableType> for PyTableType { |
| 273 | + fn from(table_type: TableType) -> Self { |
| 274 | + match table_type { |
| 275 | + datafusion::logical_expr::TableType::Base => PyTableType::Base, |
| 276 | + datafusion::logical_expr::TableType::View => PyTableType::View, |
| 277 | + datafusion::logical_expr::TableType::Temporary => PyTableType::Temporary, |
| 278 | + } |
| 279 | + } |
| 280 | +} |
| 281 | + |
| 282 | +#[pyclass(name = "TableSource", module = "datafusion.common", subclass)] |
| 283 | +#[derive(Clone)] |
| 284 | +pub struct PyTableSource { |
| 285 | + pub table_source: Arc<dyn TableSource>, |
| 286 | +} |
| 287 | + |
| 288 | +#[pymethods] |
| 289 | +impl PyTableSource { |
| 290 | + pub fn schema(&self) -> PyArrowType<Schema> { |
| 291 | + (*self.table_source.schema()).clone().into() |
| 292 | + } |
| 293 | + |
| 294 | + pub fn constraints(&self) -> Option<PyConstraints> { |
| 295 | + self.table_source.constraints().map(|c| PyConstraints { |
| 296 | + constraints: c.clone(), |
| 297 | + }) |
| 298 | + } |
| 299 | + |
| 300 | + pub fn table_type(&self) -> PyTableType { |
| 301 | + self.table_source.table_type().into() |
| 302 | + } |
| 303 | + |
| 304 | + pub fn get_logical_plan(&self) -> Option<PyLogicalPlan> { |
| 305 | + self.table_source |
| 306 | + .get_logical_plan() |
| 307 | + .map(|plan| PyLogicalPlan::new(plan.into_owned())) |
| 308 | + } |
| 309 | +} |
0 commit comments