Skip to content

Commit

Permalink
Distinct bindings (apache#275)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdye64 authored Mar 14, 2023
1 parent 62dbd2a commit fad9a3f
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
2 changes: 2 additions & 0 deletions datafusion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
SubqueryAlias,
Extension,
CreateView,
Distinct,
)

__version__ = importlib_metadata.version(__name__)
Expand Down Expand Up @@ -137,6 +138,7 @@
"Extension",
"CreateMemoryTable",
"CreateView",
"Distinct",
]


Expand Down
2 changes: 2 additions & 0 deletions datafusion/tests/test_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
Extension,
CreateMemoryTable,
CreateView,
Distinct,
)


Expand Down Expand Up @@ -153,6 +154,7 @@ def test_class_module_is_datafusion():
Extension,
CreateMemoryTable,
CreateView,
Distinct,
]:
assert klass.__module__ == "datafusion.expr"

Expand Down
2 changes: 2 additions & 0 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub mod column;
pub mod create_memory_table;
pub mod create_view;
pub mod cross_join;
pub mod distinct;
pub mod empty_relation;
pub mod exists;
pub mod explain;
Expand Down Expand Up @@ -282,6 +283,7 @@ pub(crate) fn init_module(m: &PyModule) -> PyResult<()> {
m.add_class::<table_scan::PyTableScan>()?;
m.add_class::<create_memory_table::PyCreateMemoryTable>()?;
m.add_class::<create_view::PyCreateView>()?;
m.add_class::<distinct::PyDistinct>()?;
m.add_class::<subquery_alias::PySubqueryAlias>()?;
Ok(())
}
80 changes: 80 additions & 0 deletions src/expr/distinct.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// 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 std::fmt::{self, Display, Formatter};

use datafusion_expr::Distinct;
use pyo3::prelude::*;

use crate::sql::logical::PyLogicalPlan;

use super::logical_node::LogicalNode;

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

impl From<PyDistinct> for Distinct {
fn from(distinct: PyDistinct) -> Self {
distinct.distinct
}
}

impl From<Distinct> for PyDistinct {
fn from(distinct: Distinct) -> PyDistinct {
PyDistinct { distinct }
}
}

impl Display for PyDistinct {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(
f,
"Distinct
\nInput: {:?}",
self.distinct.input,
)
}
}

#[pymethods]
impl PyDistinct {
/// Retrieves the input `LogicalPlan` to this `Projection` node
fn input(&self) -> PyResult<Vec<PyLogicalPlan>> {
Ok(Self::inputs(self))
}

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

fn __name__(&self) -> PyResult<String> {
Ok("Distinct".to_string())
}
}

impl LogicalNode for PyDistinct {
fn inputs(&self) -> Vec<PyLogicalPlan> {
vec![PyLogicalPlan::from((*self.distinct.input).clone())]
}

fn to_variant(&self, py: Python) -> PyResult<PyObject> {
Ok(self.clone().into_py(py))
}
}

0 comments on commit fad9a3f

Please sign in to comment.