8000 Add Column, Literal, BinaryExpr Python wrappers by andygrove · Pull Request #194 · apache/datafusion-python · GitHub
[go: up one dir, main page]

Skip to content

Add Column, Literal, BinaryExpr Python wrappers #194

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

Closed
wants to merge 6 commits into from
Closed
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
53 changes: 53 additions & 0 deletions datafusion/tests/test_expr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# 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.

from datafusion import SessionContext
from datafusion.expr import Column, Literal, BinaryExpr, Projection
import pytest


@pytest.fixture
def test_ctx():
ctx = SessionContext()
ctx.register_csv("test", "testing/data/csv/aggregate_test_100.csv")
return ctx


def test_logical_plan(test_ctx):
df = test_ctx.sql("select c1, 123, c1 < 123 from test")
plan = df.logical_plan()

projection = plan.to_variant()
assert isinstance(projection, Projection)

expr = projection.projections()

col1 = expr[0].to_variant()
assert isinstance(col1, Column)
assert col1.name() == "c1"
assert col1.qualified_name() == "test.c1"

col2 = expr[1].to_variant()
assert isinstance(col2, Literal)
assert col2.data_type() == "Int64"
assert col2.value_i64() == 123

col3 = expr[2].to_variant()
assert isinstance(col3, BinaryExpr)
assert isinstance(col3.left().to_variant(), Column)
assert col3.op() == "<"
assert isinstance(col3.right().to_variant(), Literal)
8 changes: 7 additions & 1 deletion datafusion/tests/test_imports.py
8000
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@

from datafusion.expr import (
Expr,
Column,
Literal,
BinaryExpr,
Projection,
TableScan,
Filter,
Expand All @@ -59,7 +62,10 @@ def test_class_module_is_datafusion():
]:
assert klass.__module__ == "datafusion"

for klass in [Expr, Projection, TableScan, Aggregate, Sort, Limit, Filter]:
for klass in [Expr, Column, Literal, BinaryExpr]:
assert klass.__module__ == "datafusion.expr"

for klass in [Projection, TableScan, Aggregate, Sort, Limit, Filter]:
assert klass.__module__ == "datafusion.expr"

for klass in [DFField, DFSchema]:
Expand Down
24 changes: 24 additions & 0 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,18 @@ use datafusion::arrow::datatypes::DataType;
use datafusion::arrow::pyarrow::PyArrowType;
use datafusion_expr::{col, lit, Cast, Expr, GetIndexedField};

use crate::errors::py_runtime_err;
use crate::expr::binary_expr::PyBinaryExpr;
use crate::expr::column::PyColumn;
use crate::expr::literal::PyLiteral;
use datafusion::scalar::ScalarValue;

pub mod aggregate;
pub mod binary_expr;
pub mod column;
pub mod filter;
pub mod limit;
pub mod literal;
pub mod logical_node;
pub mod projection;
pub mod sort;
Expand All @@ -53,6 +60,19 @@ impl From<Expr> for PyExpr {

#[pymethods]
impl PyExpr {
/// Return the specific expression
fn to_variant(&self, py: Python) -> PyResult<PyObject> {
Python::with_gil(|_| match &self.expr {
Expr::Column(col) => Ok(PyColumn::from(col.clone()).into_py(py)),
Expr::Literal(value) => Ok(PyLiteral::from(value.clone()).into_py(py)),
Expr::BinaryExpr(expr) => Ok(PyBinaryExpr::from(expr.clone()).into_py(py)),
other => Err(py_runtime_err(format!(
"Cannot convert this Expr to a Python object: {:?}",
other
))),
})
}

fn __richcmp__(&self, other: PyExpr, op: CompareOp) -> PyExpr {
let expr = match op {
CompareOp::Lt => self.expr.clone().lt(other.expr),
Expand Down Expand Up @@ -147,6 +167,10 @@ pub(crate) fn init_module(m: &PyModule) -> PyResult<()> {
m.add_class::<PyExpr>()?;
m.add_class::<table_scan::PyTableScan>()?;
m.add_class::<projection::PyProjection>()?;
m.add_class::<column::PyColumn>()?;
m.add_class::<literal::PyLiteral>()?;
m.add_class::<binary_expr::PyBinaryExpr>()?;
m.add_class::<literal::PyLiteral>()?;
m.add_class::<filter::PyFilter>()?;
m.add_class::<limit::PyLimit>()?;
m.add_class::<aggregate::PyAggregate>()?;
Expand Down
57 changes: 57 additions & 0 deletions src/expr/binary_expr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// 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::expr::PyExpr;
use datafusion_expr::BinaryExpr;
use pyo3::prelude::*;

#[pyclass(name = "BinaryExpr", module = "datafusion.expr", subclass)]
#[derive(Clone)]
pub struct PyBinaryExpr {
expr: BinaryExpr,
}

impl From<PyBinaryExpr> for BinaryExpr {
fn from(expr: PyBinaryExpr) -> Self {
expr.expr
}
}

impl From<BinaryExpr> for PyBinaryExpr {
fn from(expr: BinaryExpr) -> PyBinaryExpr {
PyBinaryExpr { expr }
}
}

#[pymethods]
impl PyBinaryExpr {
fn left(&self) -> PyExpr {
self.expr.left.as_ref().clone().into()
}

fn right(&self) -> PyExpr {
self.expr.right.as_ref().clone().into()
}

fn op(&self) -> String {
format!("{}", self.expr.op)
}

fn __repr__(&self) -> PyResult<String> {
Ok(format!("{}", self.expr))
}
}
60 changes: 60 additions & 0 deletions src/expr/column.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// 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::Column;
use pyo3::prelude::*;

#[pyclass(name = "Column", module = "datafusion.expr", subclass)]
#[derive(Clone)]
pub struct PyColumn {
pub col: Column,
}

impl PyColumn {
pub fn new(col: Column) -> Self {
Self { col }
}
}

impl From<Column> for PyColumn {
fn from(col: Column) -> PyColumn {
PyColumn { col }
}
}

#[pymethods]
impl PyColumn {
/// Get the column name
fn name(&self) -> String {
self.col.name.clone()
}

/// Get the column relation
fn relation(&self) -> Option<String> {
self.col.relation.clone()
}

/// Get the fully-qualified column name
fn qualified_name(&self) -> String {
self.col.flat_name()
}

/// Get a String representation of this column
fn __repr__(&self) -> String {
self.qualified_name()
}
}
74 changes: 74 additions & 0 deletions src/expr/literal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// 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::errors::py_runtime_err;
use datafusion_common::ScalarValue;
use pyo3::prelude::*;

#[pyclass(name = "Literal", module = "datafusion.expr", subclass)]
#[derive(Clone)]
pub struct PyLiteral {
pub value: ScalarValue,
}

impl From<PyLiteral> for ScalarValue {
fn from(lit: PyLiteral) -> ScalarValue {
lit.value
}
}

impl From<ScalarValue> for PyLiteral {
fn from(value: ScalarValue) -> PyLiteral {
PyLiteral { value }
}
}

#[pymethods]
impl PyLiteral {
/// Get the data type of this literal value
fn data_type(&self) -> String {
format!("{}", self.value.get_datatype())
}

fn value_i32(&self) -> PyResult<i32> {
if let ScalarValue::Int32(Some(n)) = &self.value {
Ok(*n)
} else {
Err(py_runtime_err("Cannot access value as i32"))
}
}

fn value_i64(&self) -> PyResult<i64> {
if let ScalarValue::Int64(Some(n)) = &self.value {
Ok(*n)
} else {
Err(py_runtime_err("Cannot access value as i64"))
}
}

fn value_str(&self) -> PyResult<String> {
if let ScalarValue::Utf8(Some(str)) = &self.value {
Ok(str.clone())
} else {
Err(py_runtime_err("Cannot access value as string"))
}
}

fn __repr__(&self) -> PyResult<String> {
Ok(format!("{}", self.value))
}
}
19 changes: 19 additions & 0 deletions src/sql/logical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

use std::sync::Arc;

use crate::errors::py_runtime_err;
use crate::expr::projection::PyProjection;
use crate::expr::table_scan::PyTableScan;
use datafusion_expr::LogicalPlan;
use pyo3::prelude::*;

Expand All @@ -37,6 +40,18 @@ impl PyLogicalPlan {

#[pymethods]
impl PyLogicalPlan {
/// Return the specific logical operator
fn to_variant(&self, py: Python) -> PyResult<PyObject> {
Python::with_gil(|_| match self.plan.as_ref() {
LogicalPlan::Projection(plan) => Ok(PyProjection::from(plan.clone()).into_py(py)),
LogicalPlan::TableScan(plan) => Ok(PyTableScan::from(plan.clone()).into_py(py)),
other => Err(py_runtime_err(format!(
"Cannot convert this plan to a LogicalNode: {:?}",
other
))),
})
}

/// Get the inputs to this plan
pub fn inputs(&self) -> Vec<PyLogicalPlan> {
let mut inputs = vec![];
Expand All @@ -46,6 +61,10 @@ impl PyLogicalPlan {
inputs
}

fn __repr__(&self) -> PyResult<String> {
Ok(format!("{:?}", self.plan))
}

pub fn display(&self) -> String {
format!("{}", self.plan.display())
}
Expand Down
0