yyy1000 commented on code in PR #8780:
URL: https://github.com/apache/arrow-datafusion/pull/8780#discussion_r1444797946
##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -224,6 +226,63 @@ impl<S: SimplifyInfo> ExprSimplifier<S> {
}
}
+#[allow(rustdoc::private_intra_doc_links)]
+/// Canonicalize any BinaryExprs that are not in canonical form
+/// <literal> <op> <col> is rewritten to <col> <op> <literal> (remember to
switch the operator)
+/// <col> <op> <literal> is canonical
+/// <col1> <op> <col2> is rewritten so that the name of col1 sorts higher than
col2 (b > a would be canonicalized to a < b)
+struct Canonicalizer {}
+
+impl Canonicalizer {
+ fn new() -> Self {
+ Self {}
+ }
+}
+
+impl TreeNodeRewriter for Canonicalizer {
+ type N = Expr;
+
+ fn mutate(&mut self, expr: Expr) -> Result<Expr> {
+ if let Expr::BinaryExpr(BinaryExpr { left, op, right }) = expr {
+ // Case 1, <col1> <op> <col2>
+ let mut new_expr = BinaryExpr {
+ left: left.clone(),
+ op: op.clone(),
+ right: right.clone(),
+ };
+ let mut switch_op: Operator = op.clone();
+ if left.try_into_col().is_ok() && right.try_into_col().is_ok() {
+ let left_name = left.canonical_name();
+ let right_name = right.canonical_name();
+ if left_name < right_name {
+ if let Some(swap_op) = op.swap() {
+ switch_op = swap_op;
+ }
Review Comment:
Got it.
Here `swap` would return None if operator makes no sense, e.g.: `-`, see:
https://github.com/apache/arrow-datafusion/blob/cc4289484c33e478242bf5d2b59f695fdb427ab9/datafusion/expr/src/operator.rs#L172C5-L202C6
I think in this case we will not change the order of the Expr, and <col1>
and <col2> would remain the same order as before.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]