hadrian-reppas opened a new pull request, #23215:
URL: https://github.com/apache/datafusion/pull/23215

   ## Which issue does this PR close?
   
   Closes #17158
   
   ## Rationale for this change
   
   The query planner currently does not optimize expressions like `c1 || 'a' || 
'b' || c2` (which it could simplify it to `c1 || 'ab' || c2`). This is because 
the `ConstEvaluator` looks for `BinaryExpr`s where both arguments are 
`Literal`s. The expression above looks like
   
   ```
           ||
          /  \
         ||   c2
        /  \
      ||   'b'
     /  \
   c1   'a'
   ```
   
   so it does not get optimized. Since string concatenation is associative, we 
can rewrite the expression to look like
   
   ```
           ||
          /  \
         ||   c2
        /  \
      c1   ||
          /  \
        'a'  'b'
   ```
   
   which the `ConstEvaluator` can optimize to
   
   ```
           ||
          /  \
         ||   c2
        /  \
      c1   'ab'
   ```
   
   We do this by flattening nested occurrences of an associative operator into 
a list and then rebuilding the expression tree so that adjacent `Literal`s 
appear together in the same subtree. This rewrite happens in the 
`expr_simplifier::Simplifier` so that the `ConstEvaluator` can do it's work on 
a subsequent pass.
   
   ## What changes are included in this PR?
   
   This PR implements the `has_adjacent_literals` and `reassociate_literals` 
functions.
   
   There are some remaining questions:
   1. What other operators are associative?
   2. How can we check the expression's type? (since float addition isn't 
associative)
   3. Should we do something similar for commutative operations (eg move all 
literals to the front so they can be constant folded together)?
   4. 
   
   ## Are these changes tested?
   
   Yes, but I'll ad more tests.
   
   ## Are there any user-facing changes?
   
   No
   


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