Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion dask_planner/src/sql/exceptions.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
use pyo3::create_exception;
use datafusion::error::DataFusionError;
use pyo3::{create_exception, PyErr};

create_exception!(rust, ParsingException, pyo3::exceptions::PyException);

pub fn py_type_err(e: DataFusionError) -> PyErr {
PyErr::new::<pyo3::exceptions::PyTypeError, _>(format!("{:?}", e))
}
25 changes: 12 additions & 13 deletions dask_planner/src/sql/logical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ pub mod projection;

pub use datafusion_expr::LogicalPlan;

use datafusion::common::Result;
use datafusion::prelude::Column;
use pyo3::ffi::Py_FatalError;

use crate::sql::exceptions::py_type_err;
use pyo3::prelude::*;

#[pyclass(name = "LogicalPlan", module = "dask_planner", subclass)]
Expand Down Expand Up @@ -140,19 +143,15 @@ impl PyLogicalPlan {
}

#[pyo3(name = "getRowType")]
pub fn row_type(&self) -> RelDataType {
let fields: &Vec<DFField> = self.original_plan.schema().fields();
let mut rel_fields: Vec<RelDataTypeField> = Vec::new();
for i in 0..fields.len() {
rel_fields.push(
RelDataTypeField::from(
fields[i].clone(),
self.original_plan.schema().as_ref().clone(),
)
.unwrap(),
);
}
RelDataType::new(false, rel_fields)
pub fn row_type(&self) -> PyResult<RelDataType> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PyO3 will automatically wrap any return type from an annotated function with PyResult<T> but maybe it is best to explicitly lay that out. I like that better too.

let schema = self.original_plan.schema();
let mut rel_fields: Vec<RelDataTypeField> = schema
.fields()
.iter()
.map(|f| RelDataTypeField::from(f, schema.as_ref()))
.collect::<Result<Vec<_>>>()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The iterator is over items of type Result so collect() would normally return Vec<Result<_>> but we can use this turbofish notation to transform that into Result<Vec<_>> instead.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

much neater code here. Really like the turbofish use.

.map_err(|e| py_type_err(e))?;
Ok(RelDataType::new(false, rel_fields))
}
}

Expand Down
4 changes: 2 additions & 2 deletions dask_planner/src/sql/types/rel_data_type_field.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::sql::types::DaskTypeMap;
use crate::sql::types::SqlTypeName;

use datafusion::error::DataFusionError;
use datafusion::error::{DataFusionError, Result};
use datafusion::logical_plan::{DFField, DFSchema};

use std::fmt;
Expand All @@ -19,7 +19,7 @@ pub struct RelDataTypeField {

// Functions that should not be presented to Python are placed here
impl RelDataTypeField {
pub fn from(field: DFField, schema: DFSchema) -> Result<RelDataTypeField, DataFusionError> {
pub fn from(field: &DFField, schema: &DFSchema) -> Result<RelDataTypeField> {
Ok(RelDataTypeField {
name: field.name().clone(),
data_type: DaskTypeMap {
Expand Down