10000 Analyze table bindings by jdye64 · Pull Request #204 · apache/datafusion-python · GitHub
[go: up one dir, main page]

Skip to content

Analyze table bindings #204

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 6 commits into from
Feb 22, 2023
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
8 changes: 8 additions & 0 deletions datafusion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@
)

from .expr import (
Analyze,
Expr,
Filter,
Limit,
Projection,
Sort,
TableScan,
)

Expand All @@ -63,6 +67,10 @@
"Projection",
"DFSchema",
"DFField",
"Analyze",
"Sort",
"Limit",
"Filter",
]


Expand Down
1 change: 0 additions & 1 deletion datafusion/pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def register_parquet(self, name, path):
self.datafusion_ctx.register_parquet(name, path)

def to_pandas_expr(self, expr):

# get Python wrapper for logical expression
expr = expr.to_variant()

Expand Down
1 change: 0 additions & 1 deletion datafusion/polars.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ def register_parquet(self, name, path):
self.datafusion_ctx.register_parquet(name, path)

def to_polars_expr(self, expr):

# get Python wrapper for logical expression
expr = expr.to_variant()

Expand Down
1 change: 0 additions & 1 deletion datafusion/tests/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ def test_create_context_no_args():


def test_create_context_with_all_valid_args():

runtime = (
RuntimeConfig().with_disk_manager_os().with_fair_spill_pool(10000000)
)
Expand Down
11 changes: 10 additions & 1 deletion datafusion/tests/test_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
Limit,
Aggregate,
Sort,
Analyze,
)


Expand All @@ -68,7 +69,15 @@ def test_class_module_is_datafusion():
assert klass.__module__ == "datafusion.expr"

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

# schema
Expand Down
2 changes: 2 additions & 0 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use datafusion::scalar::ScalarValue;

pub mod aggregate;
pub mod aggregate_expr;
pub mod analyze;
pub mod binary_expr;
pub mod column;
pub mod filter;
Expand Down Expand Up @@ -183,5 +184,6 @@ pub(crate) fn init_module(m: &PyModule) -> PyResult<()> {
m.add_class::<limit::PyLimit>()?;
m.add_class::<aggregate::PyAggregate>()?;
m.add_class::<sort::PySort>()?;
m.add_class::<analyze::PyAnalyze>()?;
Ok(())
}
76 changes: 76 additions & 0 deletions src/expr/analyze.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// 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::Analyze;
use pyo3::prelude::*;
use std::fmt::{self, Display, Formatter};

use crate::common::df_schema::PyDFSchema;
use crate::expr::logical_node::LogicalNode;
use crate::sql::logical::PyLogicalPlan;

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

impl PyAnalyze {
pub fn new(analyze: Analyze) -> Self {
Self { analyze }
}
}

impl From<Analyze> for PyAnalyze {
fn from(analyze: Analyze) -> PyAnalyze {
PyAnalyze { analyze }
}
}

impl From<PyAnalyze> for Analyze {
fn from(analyze: PyAnalyze) -> Self {
analyze.analyze
}
}

impl Display for PyAnalyze {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "Analyze Table")
}
}

#[pymethods]
impl PyAnalyze {
fn verbose(&self) -> PyResult<bool> {
Ok(self.analyze.verbose)
}

/// Resulting Schema for this `Analyze` node instance
fn schema(&self) -> PyResult<PyDFSchema> {
Ok((*self.analyze.schema).clone().into())
}

fn __repr__(&self) -> PyResult<String> {
Ok(format!("Analyze({})", self))
}
}

impl LogicalNode for PyAnalyze {
fn input(&self) -> Vec<PyLogicalPlan> {
vec![PyLogicalPlan::from((*self.analyze.input).clone())]
}
}
22 changes: 14 additions & 8 deletions src/sql/logical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::sync::Arc;

use crate::errors::py_runtime_err;
use crate::expr::aggregate::PyAggregate;
use crate::expr::analyze::PyAnalyze;
use crate::expr::filter::PyFilter;
use crate::expr::limit::PyLimit;
use crate::expr::projection::PyProjection;
Expand All @@ -40,19 +41,24 @@ impl PyLogicalPlan {
plan: Arc::new(plan),
}
}

pub fn plan(&self) -> Arc<LogicalPlan> {
self.plan.clone()
}
}

#[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)),
LogicalPlan::Aggregate(plan) => Ok(PyAggregate::from(plan.clone()).into_py(py)),
LogicalPlan::Analyze(plan) => Ok(PyAnalyze::from(plan.clone()).into_py(py)),
LogicalPlan::Filter(plan) => Ok(PyFilter::from(plan.clone()).into_py(py)),
LogicalPlan::Limit(plan) => Ok(PyLimit::from(plan.clone()).into_py(py)),
LogicalPlan::Projection(plan) => Ok(PyProjection::from(plan.clone()).into_py(py)),
LogicalPlan::Sort(plan) => Ok(PySort::from(plan.clone()).into_py(py)),
LogicalPlan::Filter(plan) => Ok(PyFilter::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
Expand All @@ -61,7 +67,7 @@ impl PyLogicalPlan {
}

/// Get the inputs to this plan
pub fn inputs(&self) -> Vec<PyLogicalPlan> {
fn inputs(&self) -> Vec<PyLogicalPlan> {
let mut inputs = vec![];
for input in self.plan.inputs() {
inputs.push(input.to_owned().into());
Expand All @@ -73,19 +79,19 @@ impl PyLogicalPlan {
Ok(format!("{:?}", self.plan))
}

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

pub fn display_indent(&self) -> String {
fn display_indent(&self) -> String {
format!("{}", self.plan.display_indent())
}

pub fn display_indent_schema(&self) -> String {
fn display_indent_schema(&self) -> String {
format!("{}", self.plan.display_indent_schema())
}

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