-
Notifications
You must be signed in to change notification settings - Fork 118
Add bindings for Projection #180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d08f4d0
Add bindings for Projection
jdye64 4cbc925
Address review comments
jdye64 18c09d0
cargo fmt
jdye64 dda3f02
Clippy updates and uncommented DFSchema code that is now merged into …
jdye64 752fe1c
datafusion_common::DFField bindings
jdye64 9dacbbe
Revert "datafusion_common::DFField bindings"
jdye64 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// 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 crate::sql::logical::PyLogicalPlan; | ||
|
||
/// Representation of a `LogicalNode` in the in overall `LogicalPlan` | ||
/// any "node" shares these common traits in common. | ||
pub trait LogicalNode { | ||
/// The input plan to the current logical node instance. | ||
fn input(&self) -> Vec<PyLogicalPlan>; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// 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_common::DataFusionError; | ||
use datafusion_expr::logical_plan::Projection; | ||
use pyo3::prelude::*; | ||
use std::fmt::{self, Display, Formatter}; | ||
|
||
use crate::common::df_schema::PyDFSchema; | ||
use crate::errors::py_runtime_err; | ||
use crate::expr::logical_node::LogicalNode; | ||
use crate::expr::PyExpr; | ||
use crate::sql::logical::PyLogicalPlan; | ||
|
||
#[pyclass(name = "Projection", module = "datafusion.expr", subclass)] | ||
#[derive(Clone)] | ||
pub struct PyProjection { | ||
projection: Projection, | ||
} | ||
|
||
impl From<Projection> for PyProjection { | ||
fn from(projection: Projection) -> PyProjection { | ||
PyProjection { projection } | ||
} | ||
} | ||
|
||
impl TryFrom<PyProjection> for Projection { | ||
type Error = DataFusionError; | ||
|
||
fn try_from(py_proj: PyProjection) -> Result<Self, Self::Error> { | ||
Projection::try_new_with_schema( | ||
py_proj.projection.expr, | ||
py_proj.projection.input.clone(), | ||
py_proj.projection.schema, | ||
) | ||
} | ||
} | ||
|
||
impl Display for PyProjection { | ||
fn fmt(&self, f: &mut Formatter) -> fmt::Result { | ||
write!( | ||
f, | ||
"Projection | ||
\nExpr(s): {:?} | ||
\nInput: {:?} | ||
\nProjected Schema: {:?}", | ||
&self.projection.expr, &self.projection.input, &self.projection.schema, | ||
) | ||
} | ||
} | ||
|
||
#[pymethods] | ||
impl PyProjection { | ||
/// Retrieves the expressions for this `Projection` | ||
#[pyo3(name = "projections")] | ||
fn py_projections(&self) -> PyResult<Vec<PyExpr>> { | ||
Ok(self | ||
.projection | ||
.expr | ||
.iter() | ||
.map(|e| PyExpr::from(e.clone())) | ||
.collect()) | ||
} | ||
|
||
// Retrieves the input `LogicalPlan` to this `Projection` node | ||
#[pyo3(name = "input")] | ||
fn py_input(&self) -> PyResult<PyLogicalPlan> { | ||
// DataFusion make a loose guarantee that each Projection should have an input, however | ||
// we check for that hear since we are performing explicit index retrieval | ||
let inputs = LogicalNode::input(self); | ||
if !inputs.is_empty() { | ||
return Ok(inputs[0].clone()); | ||
} | ||
|
||
Err(py_runtime_err(format!( | ||
"Expected `input` field for Projection node: {}", | ||
self | ||
))) | ||
} | ||
|
||
// Resulting Schema for this `Projection` node instance | ||
#[pyo3(name = "schema")] | ||
fn py_schema(&self) -> PyResult<PyDFSchema> { | ||
Ok((*self.projection.schema).clone().into()) | ||
} | ||
|
||
fn __repr__(&self) -> PyResult<String> { | ||
Ok(format!("Projection({})", self)) | ||
} | ||
} | ||
|
||
impl LogicalNode for PyProjection { | ||
fn input(&self) -> Vec<PyLogicalPlan> { | ||
vec![PyLogicalPlan::from((*self.projection.input).clone())] | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.