Skip to content

Commit

Permalink
update PyAlias to wrap upstream Alias
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-J-Ward committed Jul 30, 2024
1 parent 7a023d4 commit ac4ba9c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl PyExpr {
/// Return the specific expression
fn to_variant(&self, py: Python) -> PyResult<PyObject> {
Python::with_gil(|_| match &self.expr {
Expr::Alias(alias) => Ok(PyAlias::new(&alias.expr, &alias.name).into_py(py)),
Expr::Alias(alias) => Ok(PyAlias::from(alias.clone()).into_py(py)),
Expr::Column(col) => Ok(PyColumn::from(col.clone()).into_py(py)),
Expr::ScalarVariable(data_type, variables) => {
Ok(PyScalarVariable::new(data_type, variables).into_py(py))
Expand Down
32 changes: 17 additions & 15 deletions src/expr/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,24 @@ use crate::expr::PyExpr;
use pyo3::prelude::*;
use std::fmt::{self, Display, Formatter};

use datafusion_expr::Expr;
use datafusion_expr::expr::Alias;

#[pyclass(name = "Alias", module = "datafusion.expr", subclass)]
#[derive(Clone)]
pub struct PyAlias {
expr: PyExpr,
alias_name: String,
alias: Alias,
}

impl From<Alias> for PyAlias {
fn from(alias: Alias) -> Self {
Self { alias }
}
}

impl From<PyAlias> for Alias {
fn from(py_alias: PyAlias) -> Self {
py_alias.alias
}
}

impl Display for PyAlias {
Expand All @@ -35,29 +46,20 @@ impl Display for PyAlias {
"Alias
\nExpr: `{:?}`
\nAlias Name: `{}`",
&self.expr, &self.alias_name
&self.alias.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())
Ok(self.alias.name.clone())
}

fn expr(&self) -> PyResult<PyExpr> {
Ok(self.expr.clone())
Ok((*self.alias.expr.clone()).into())
}

/// Get a String representation of this column
Expand Down

0 comments on commit ac4ba9c

Please sign in to comment.