Skip to content

Commit

Permalink
MINOR: Add Expr::canonical_name and improve docs on Expr::name (#…
Browse files Browse the repository at this point in the history
…3706)

* Add Expr::canonical_name

* update docs
  • Loading branch information
andygrove committed Oct 4, 2022
1 parent a07bebc commit 45fc415
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions datafusion/expr/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,11 +346,17 @@ impl PartialOrd for Expr {
}

impl Expr {
/// Returns the name of this expression as it should appear in a schema.
/// Returns the name of this expression as it should appear in a schema. This name
/// will not include any CAST expressions.
pub fn name(&self) -> Result<String> {
create_name(self)
}

/// Returns a full and complete string representation of this expression.
pub fn canonical_name(&self) -> String {
format!("{}", self)
}

/// Return String representation of the variant represented by `self`
/// Useful for non-rust based bindings
pub fn variant_name(&self) -> &str {
Expand Down Expand Up @@ -1158,8 +1164,10 @@ mod test {
.when(lit(1), lit(true))
.when(lit(0), lit(false))
.otherwise(lit(ScalarValue::Null))?;
assert_eq!("CASE #a WHEN Int32(1) THEN Boolean(true) WHEN Int32(0) THEN Boolean(false) ELSE NULL END", format!("{}", expr));
assert_eq!("CASE #a WHEN Int32(1) THEN Boolean(true) WHEN Int32(0) THEN Boolean(false) ELSE NULL END", format!("{:?}", expr));
let expected_canonical = "CASE #a WHEN Int32(1) THEN Boolean(true) WHEN Int32(0) THEN Boolean(false) ELSE NULL END";
assert_eq!(expected_canonical, expr.canonical_name());
assert_eq!(expected_canonical, format!("{}", expr));
assert_eq!(expected_canonical, format!("{:?}", expr));
assert_eq!("CASE a WHEN Int32(1) THEN Boolean(true) WHEN Int32(0) THEN Boolean(false) ELSE NULL END", expr.name()?);
Ok(())
}
Expand All @@ -1170,8 +1178,10 @@ mod test {
expr: Box::new(Expr::Literal(ScalarValue::Float32(Some(1.23)))),
data_type: DataType::Utf8,
};
assert_eq!("CAST(Float32(1.23) AS Utf8)", format!("{}", expr));
assert_eq!("CAST(Float32(1.23) AS Utf8)", format!("{:?}", expr));
let expected_canonical = "CAST(Float32(1.23) AS Utf8)";
assert_eq!(expected_canonical, expr.canonical_name());
assert_eq!(expected_canonical, format!("{}", expr));
assert_eq!(expected_canonical, format!("{:?}", expr));
// note that CAST intentionally has a name that is different from its `Display`
// representation. CAST does not change the name of expressions.
assert_eq!("Float32(1.23)", expr.name()?);
Expand Down

0 comments on commit 45fc415

Please sign in to comment.