-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Implement Display for Expr, improve operator display #971
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -941,6 +941,33 @@ impl Not for Expr { | |
} | ||
} | ||
|
||
impl std::fmt::Display for Expr { | ||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
match self { | ||
Expr::BinaryExpr { | ||
ref left, | ||
ref right, | ||
ref op, | ||
} => write!(f, "{} {} {}", left, op, right), | ||
Expr::AggregateFunction { | ||
/// Name of the function | ||
ref fun, | ||
/// List of expressions to feed to the functions as arguments | ||
ref args, | ||
/// Whether this is a DISTINCT aggregation or not | ||
ref distinct, | ||
} => fmt_function(f, &fun.to_string(), *distinct, args, true), | ||
Expr::ScalarFunction { | ||
/// Name of the function | ||
ref fun, | ||
/// List of expressions to feed to the functions as arguments | ||
ref args, | ||
} => fmt_function(f, &fun.to_string(), false, args, true), | ||
_ => write!(f, "{:?}", self), | ||
} | ||
} | ||
} | ||
|
||
#[allow(clippy::boxed_local)] | ||
fn rewrite_boxed<R>(boxed_expr: Box<Expr>, rewriter: &mut R) -> Result<Box<Expr>> | ||
where | ||
|
@@ -1572,8 +1599,14 @@ fn fmt_function( | |
fun: &str, | ||
distinct: bool, | ||
args: &[Expr], | ||
display: bool, | ||
) -> fmt::Result { | ||
let args: Vec<String> = args.iter().map(|arg| format!("{:?}", arg)).collect(); | ||
let args: Vec<String> = match display { | ||
true => args.iter().map(|arg| format!("{}", arg)).collect(), | ||
false => args.iter().map(|arg| format!("{:?}", arg)).collect(), | ||
}; | ||
|
||
// let args: Vec<String> = args.iter().map(|arg| format!("{:?}", arg)).collect(); | ||
let distinct_str = match distinct { | ||
true => "DISTINCT ", | ||
false => "", | ||
|
@@ -1617,7 +1650,7 @@ impl fmt::Debug for Expr { | |
Expr::IsNull(expr) => write!(f, "{:?} IS NULL", expr), | ||
Expr::IsNotNull(expr) => write!(f, "{:?} IS NOT NULL", expr), | ||
Expr::BinaryExpr { left, op, right } => { | ||
write!(f, "{:?} {:?} {:?}", left, op, right) | ||
write!(f, "{:?} {} {:?}", left, op, right) | ||
Comment on lines
1652
to
+1653
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Dandandan FYI i updated here to use display and i think thats what made it work. i was able to get the expected results on some tests after this as well. Are you ok with this approach? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would be ok with this as intermediate solution. Ideally we'll move the formatting to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with @Dandandan |
||
} | ||
Expr::Sort { | ||
expr, | ||
|
@@ -1636,10 +1669,10 @@ impl fmt::Debug for Expr { | |
} | ||
} | ||
Expr::ScalarFunction { fun, args, .. } => { | ||
fmt_function(f, &fun.to_string(), false, args) | ||
fmt_function(f, &fun.to_string(), false, args, false) | ||
} | ||
Expr::ScalarUDF { fun, ref args, .. } => { | ||
fmt_function(f, &fun.name, false, args) | ||
fmt_function(f, &fun.name, false, args, false) | ||
} | ||
Expr::WindowFunction { | ||
fun, | ||
|
@@ -1648,7 +1681,7 @@ impl fmt::Debug for Expr { | |
order_by, | ||
window_frame, | ||
} => { | ||
fmt_function(f, &fun.to_string(), false, args)?; | ||
fmt_function(f, &fun.to_string(), false, args, false)?; | ||
if !partition_by.is_empty() { | ||
write!(f, " PARTITION BY {:?}", partition_by)?; | ||
} | ||
|
@@ -1671,9 +1704,9 @@ impl fmt::Debug for Expr { | |
distinct, | ||
ref args, | ||
.. | ||
} => fmt_function(f, &fun.to_string(), *distinct, args), | ||
} => fmt_function(f, &fun.to_string(), *distinct, args, true), | ||
Expr::AggregateUDF { fun, ref args, .. } => { | ||
fmt_function(f, &fun.name, false, args) | ||
fmt_function(f, &fun.name, false, args, false) | ||
} | ||
Expr::Between { | ||
expr, | ||
|
@@ -1731,7 +1764,7 @@ fn create_name(e: &Expr, input_schema: &DFSchema) -> Result<String> { | |
Expr::BinaryExpr { left, op, right } => { | ||
let left = create_name(left, input_schema)?; | ||
let right = create_name(right, input_schema)?; | ||
Ok(format!("{} {:?} {}", left, op, right)) | ||
Ok(format!("{} {} {}", left, op, right)) | ||
} | ||
Expr::Case { | ||
expr, | ||
|
@@ -1879,12 +1912,12 @@ mod tests { | |
assert_eq!( | ||
rewriter.v, | ||
vec![ | ||
"Previsited #state Eq Utf8(\"CO\")", | ||
"Previsited #state = Utf8(\"CO\")", | ||
"Previsited #state", | ||
"Mutated #state", | ||
"Previsited Utf8(\"CO\")", | ||
"Mutated Utf8(\"CO\")", | ||
"Mutated #state Eq Utf8(\"CO\")" | ||
"Mutated #state = Utf8(\"CO\")" | ||
] | ||
) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,19 +129,19 @@ mod tests { | |
fn test_operators() { | ||
assert_eq!( | ||
format!("{:?}", lit(1u32) + lit(2u32)), | ||
"UInt32(1) Plus UInt32(2)" | ||
"UInt32(1) + UInt32(2)" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that sure looks better |
||
); | ||
assert_eq!( | ||
format!("{:?}", lit(1u32) - lit(2u32)), | ||
"UInt32(1) Minus UInt32(2)" | ||
"UInt32(1) - UInt32(2)" | ||
); | ||
assert_eq!( | ||
format!("{:?}", lit(1u32) * lit(2u32)), | ||
"UInt32(1) Multiply UInt32(2)" | ||
"UInt32(1) * UInt32(2)" | ||
); | ||
assert_eq!( | ||
format!("{:?}", lit(1u32) / lit(2u32)), | ||
"UInt32(1) Divide UInt32(2)" | ||
"UInt32(1) / UInt32(2)" | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not used at the moment, as the implementation uses debug.