This is an automated email from the ASF dual-hosted git repository.

agrove pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-datafusion-python.git


The following commit(s) were added to refs/heads/main by this push:
     new e694189  Add PyAlias bindings (#216)
e694189 is described below

commit e69418946fdd44bfa314674bb464bbb97dc39188
Author: Jeremy Dyer <[email protected]>
AuthorDate: Wed Feb 22 18:51:10 2023 -0500

    Add PyAlias bindings (#216)
---
 datafusion/__init__.py           |  2 ++
 datafusion/tests/test_imports.py |  2 ++
 src/expr.rs                      |  5 +++
 src/expr/alias.rs                | 67 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 76 insertions(+)

diff --git a/datafusion/__init__.py b/datafusion/__init__.py
index 46206f0..8bc0e23 100644
--- a/datafusion/__init__.py
+++ b/datafusion/__init__.py
@@ -41,6 +41,7 @@ from .common import (
 )
 
 from .expr import (
+    Alias,
     Analyze,
     Expr,
     Filter,
@@ -71,6 +72,7 @@ __all__ = [
     "Sort",
     "Limit",
     "Filter",
+    "Alias",
 ]
 
 
diff --git a/datafusion/tests/test_imports.py b/datafusion/tests/test_imports.py
index 7eb8b7c..cadd31e 100644
--- a/datafusion/tests/test_imports.py
+++ b/datafusion/tests/test_imports.py
@@ -44,6 +44,7 @@ from datafusion.expr import (
     Aggregate,
     Sort,
     Analyze,
+    Alias,
 )
 
 
@@ -77,6 +78,7 @@ def test_class_module_is_datafusion():
         Limit,
         Filter,
         Analyze,
+        Alias,
     ]:
         assert klass.__module__ == "datafusion.expr"
 
diff --git a/src/expr.rs b/src/expr.rs
index 2d4c38b..41bd6b1 100644
--- a/src/expr.rs
+++ b/src/expr.rs
@@ -29,8 +29,11 @@ use crate::expr::column::PyColumn;
 use crate::expr::literal::PyLiteral;
 use datafusion::scalar::ScalarValue;
 
+use self::alias::PyAlias;
+
 pub mod aggregate;
 pub mod aggregate_expr;
+pub mod alias;
 pub mod analyze;
 pub mod binary_expr;
 pub mod column;
@@ -67,6 +70,7 @@ impl PyExpr {
     /// Return the specific expression
     fn to_variant(&self, py: Python) -> PyResult<PyObject> {
         Python::with_gil(|_| match &self.expr {
+            Expr::Alias(alias, name) => Ok(PyAlias::new(alias, 
name).into_py(py)),
             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)),
@@ -189,6 +193,7 @@ pub(crate) fn init_module(m: &PyModule) -> PyResult<()> {
     m.add_class::<PyBinaryExpr>()?;
     m.add_class::<PyLiteral>()?;
     m.add_class::<PyAggregateFunction>()?;
+    m.add_class::<alias::PyAlias>()?;
     // operators
     m.add_class::<table_scan::PyTableScan>()?;
     m.add_class::<projection::PyProjection>()?;
diff --git a/src/expr/alias.rs b/src/expr/alias.rs
new file mode 100644
index 0000000..2ce6563
--- /dev/null
+++ b/src/expr/alias.rs
@@ -0,0 +1,67 @@
+// 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 pyo3::prelude::*;
+use std::fmt::{self, Display, Formatter};
+
+use datafusion_expr::Expr;
+
+#[pyclass(name = "Alias", module = "datafusion.expr", subclass)]
+#[derive(Clone)]
+pub struct PyAlias {
+    expr: PyExpr,
+    alias_name: String,
+}
+
+impl Display for PyAlias {
+    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
+        write!(
+            f,
+            "Alias
+            \nExpr: `{:?}`
+            \nAlias Name: `{}`",
+            &self.expr, &self.alias_name
+        )
+    }
+}
+
+impl PyAlias {
+    pub fn new(expr: &Expr, alias_name: &String) -> Self {
+        Self {
+            expr: expr.clone().into(),
+            alias_name: alias_name.to_owned(),
+        }
+    }
+}
+
+#[pymethods]
+impl PyAlias {
+    /// Retrieve the "name" of the alias
+    fn alias(&self) -> PyResult<String> {
+        Ok(self.alias_name.clone())
+    }
+
+    fn expr(&self) -> PyResult<PyExpr> {
+        Ok(self.expr.clone())
+    }
+
+    /// Get a String representation of this column
+    fn __repr__(&self) -> String {
+        format!("{}", self)
+    }
+}

Reply via email to