alan910127 commented on issue #17158: URL: https://github.com/apache/datafusion/issues/17158#issuecomment-3309222680
Hi, I’m interested in this topic and have done a bit of research. Based on my understanding, LLVM performs similar optimizations in the `InstructionSimplify` module. For associative or commutative operators, it tries different combinations to see if expressions can be simplified ([`simplifyBinOp`](https://github.com/llvm/llvm-project/blob/36692aa7a40183c1b43358288c83f5cc08abf8f1/llvm/lib/Analysis/InstructionSimplify.cpp#L6089), [`expandCommutativeBinOp`](https://github.com/llvm/llvm-project/blob/36692aa7a40183c1b43358288c83f5cc08abf8f1/llvm/lib/Analysis/InstructionSimplify.cpp#L230C15-L230C37), [`simplifyAssociativeBinOp`](https://github.com/llvm/llvm-project/blob/36692aa7a40183c1b43358288c83f5cc08abf8f1/llvm/lib/Analysis/InstructionSimplify.cpp#L248C15-L248C39)). We might adopt a similar pattern for the SQL expression simplifier. A simplified Rust-style pseudocode inspired by LLVM: ```rust // Simplified Rust-style pseudocode of LLVM's InstructionSimplify logic fn simplify_bin_op(op: Operator, lhs: Value, rhs: Value) -> Option<Value> { ... } fn simplify_add_op(op: Operator, lhs: Value, rhs: Value) -> Option<Value> { ... } fn simplify_sub_op(op: Operator, lhs: Value, rhs: Value) -> Option<Value> { ... } fn simplify_mul_op(op: Operator, lhs: Value, rhs: Value) -> Option<Value> { ... } fn expand_commutative_bin_op(op: Operator, lhs: Value, rhs: Value) -> Option<Value> { ... } fn simplify_associative_bin_op(op: Operator, lhs: Value, rhs: Value) -> Option<Value> { ... } ``` By recursively calling `simplify_bin_op` through `expand_commutative_bin_op` and `simplify_associative_bin_op`, expressions like `c1 + 1 + 2 + c2` or even `c1 + 1 + c2 + 2` can be simplified to `c1 + c2 + 3`. -- 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]
