xudong963 commented on code in PR #19731:
URL: https://github.com/apache/datafusion/pull/19731#discussion_r2681871129


##########
datafusion/optimizer/src/simplify_expressions/utils.rs:
##########
@@ -290,6 +290,58 @@ pub fn is_lit(expr: &Expr) -> bool {
     matches!(expr, Expr::Literal(_, _))
 }
 
+/// Checks if `eq_expr` is `A = L1` and `ne_expr` is `A != L2` where L1 != L2.
+/// This pattern can be simplified to just `A = L1` since if A equals L1
+/// and L1 is different from L2, then A is automatically not equal to L2.
+pub fn is_eq_and_ne_with_different_literal(eq_expr: &Expr, ne_expr: &Expr) -> 
bool {

Review Comment:
   nit: how about using the refactoring to reduce duplication
   
   ```rust
   fn extract_var_and_literal(expr: &Expr) -> Option<(&Expr, &Expr)> {
       match expr {
           Expr::BinaryExpr(BinaryExpr { left, op: Operator::Eq, right }) |
           Expr::BinaryExpr(BinaryExpr { left, op: Operator::NotEq, right }) => 
{
               match (left.as_ref(), right.as_ref()) {
                   (Expr::Literal(_, _), var) => Some((var, left)),
                   (var, Expr::Literal(_, _)) => Some((var, right)),
                   _ => None,
               }
           }
           _ => None,
       }
   }
   ```
   ```rust
   pub fn is_eq_and_ne_with_different_literal(eq_expr: &Expr, ne_expr: &Expr) 
-> bool {
       use Operator::*;
       match (eq_expr, ne_expr) {
           (
               Expr::BinaryExpr(BinaryExpr { op: Eq, .. }),
               Expr::BinaryExpr(BinaryExpr { op: NotEq, .. }),
           ) => {
               if let (Some((var1, lit1)), Some((var2, lit2))) =
                   (extract_var_and_literal(eq_expr), 
extract_var_and_literal(ne_expr))
               {
                   if var1 == var2 && lit1 != lit2 {
                       return true;
                   }
               }
               false
           }
           _ => false,
       }
   }
   ```
   



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to