LiaCastaneda commented on issue #27631:
URL: https://github.com/apache/datafusion/issues/27631#issuecomment-7097050084

   >  Could an optimisation that turns the pattern you're describing into 
something like this help?
   
   Yes I think it would help, especially with hundreds of branches. I'm less 
sure about rewriting the filter itself into a CASE lookup though, not sure how 
well DF would handle the `CASE` (like for example in a filter pushdown to scans 
we would need to translate the CASE), also it only supports equality predicates
   
   ```
   Filter: u = 'A' AND v = 'x' OR u = 'B' AND v = 'y' OR u = 'C' AND v = 'z'
     Projection: u, lower(t.s) AS v
       Filter: u IN ('A', 'B', 'C')
         Projection: upper(t.s) AS u, t.s
           TableScan: t projection=[s]
   ```
   
   
   > It does sure look like you would want some kind of cost model to aid in 
deciding which variant is most appropriate though.
   
   yeah I think that would be the ideal solution, but today we can't tell which 
functions are expensive and which are cheap. I think some kinds are a 
reasonable default for "expensive" though, for example lambdas run once per 
array element, so I don't think duplicating them is ever worth it. Maybe we can 
start with a fix that doesn't need a cost model, or only a very simple one. For 
reference here's how other engines handle this:
   
   - 
[Trino](https://github.com/trinodb/trino/blob/6fc20cad30cc4ec877632269ec7eeadf50ab1533/core/trino-main/src/main/java/io/trino/sql/planner/iterative/rule/InlineProjections.java#L160-L192)
 -  inlines only constants and plain column references, functions are not 
inlined.
   
    - 
[postgres](https://github.com/postgres/postgres/blob/9b5d4e3331753b3399e84965093109cac8f364bf/src/backend/optimizer/util/clauses.c#L5975-L6005)
 -- doesn't inline as well if the function is used more than once. You can also 
[specify the cost of a function when creating 
it](https://www.postgresql.org/docs/current/sql-createfunction.html)
   
   - In Spark they don't inline if the expression is expensive, every 
expression[ holds a variable to decide if its 
expensive](https://github.com/apache/spark/blob/659273a41595e2ed86976c0be6d0a6e6b9923ef7/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Expression.scala#L429-L436),
 by default I think most of the functions are expensive.
   
   - Calcite 
[inlines](https://github.com/apache/calcite/blob/647e8f288b62d3fa594e901da5428f2c545f0609/core/src/main/java/org/apache/calcite/plan/RelOptUtil.java#L3377-L3402)
 the expressions only if that adds at most >100 expressions 
   
   Most of them seem to use some notion of cost, but we could do something 
simpler and less breaking like calcite or trino


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