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

   > KeepInPlace to be "leave it alone, we don't know if it's cheap or 
expensive"
   
   Looks like this is treated differently in different optimizer rules though 🤔 
For example:
   
   - `OptimizeProjections` and CSE treat a `KeepInPlace` expression as "could 
be expensive" and then `OptimizeProjections` won't merge projections if that 
would duplicate it, and CSE computes it once.
   - `PushDownFilter` does the reverse: it treats `KeepInPlace` as "could be 
cheap" and inlines the expression into the filter, once per reference. 
   
   So `PushDownFilter` seems to be the only rule that duplicates `KeepInPlace` 
expressions, with no limit on how many copies it can make.
   
   > Computed | The engine has to evaluate this. It's the default and makes no 
claim about cost or placement.
   
   I think this would bite in some cases though, If `Computed` makes no claim 
about cost then a rule like "never duplicate a `Computed` expression" can't 
tell cheap expressions like `col + 1` or a `CAST` apart from an expensive UDF 
or a lambda, then copying is actually the better choice when the expression is 
cheap, here are some examples:
   
   ```
   - WHERE a + 1 > 10 OR a + 1 < 0 --> computing a + 1 twice is cheaper than 
materializing a new column, and the filter can still reach the scan.
   - WHERE t > 1000 OR t < 0 with t = CAST(ts AS …) from a view --> the filter 
has to contain CAST(ts) to be unwrapped to ts > … and reach the scan for 
pruning. This is an OR variant of the case that regressed in #25388.
   - WHERE floor(x) = 1 OR floor(x) = 2 through a view: the preimage rewrite 
only works if the filter contains floor(x).
   - (u = 'A' AND v = 'x') OR (u = 'B' AND v = 'y'): today v is only evaluated 
on rows where u matched. Computing it once in a projection evaluates it for 
every row.
   ```
   
   iiuc this is part of the reason why #25388 was dropped.
   
   Alternatively, if we had something more granular like Spark's `is_expensive` 
flag, we could avoid these regressions, because cheap expressions would be 
explicitly marked as cheap and could still be copied.
   
   Another (short-term, non-breaking) option I was thinking of is that for each 
conjunct of the pushed filter, count references to each `KeepInPlace` column. 
if the num of references < n, inline as today, if its above n, compute it once 
and filter on the column. The cases above would still regress if a single 
conjunct referenced them more than n times, but filters like that should be 
rare I think.


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