LiaCastaneda opened a new issue, #27631:
URL: https://github.com/apache/datafusion/issues/27631

   ### Describe the bug
   
   When a filter on a projection's output is pushed below the projection, 
`PushDownFilter` replaces each column with its expression. If the filter uses 
that column in several `OR` branches, the expression is copied into each branch 
and evaluated once per branch, for every row.
   
   With a few branches this is cheap. With hundreds of branches on a column 
computed by an expensive function, the filter does hundreds of evaluations per 
row instead of one.
   
   Part of #25459 (G4: "A rule does not duplicate an expensive expression").
   
   ### To Reproduce
   
   On `main` (477c7b9eb):
   
   ```sql
   CREATE TABLE t(s VARCHAR) AS VALUES ('a'), ('b');
   
   EXPLAIN SELECT u, v FROM (SELECT upper(s) AS u, lower(s) AS v FROM t)
   WHERE (u = 'A' AND v = 'x') OR (u = 'B' AND v = 'y') OR (u = 'C' AND v = 
'z');
   ```
   
   ```text
   logical_plan
   01)Projection: upper(t.s) AS u, lower(t.s) AS v
   02)--Filter: __common_expr_3 = Utf8View("A") AND lower(t.s) = Utf8View("x") 
OR __common_expr_3 = Utf8View("B") AND lower(t.s) = Utf8View("y") OR 
__common_expr_3 = Utf8View("C") AND lower(t.s) = Utf8View("z")
   03)----Projection: upper(t.s) AS __common_expr_3, t.s
   04)------TableScan: t projection=[s]
   ```
   
   `lower(t.s)` is in the filter 3 times. `upper(t.s)` is computed once only 
because its first use is not inside an `OR` branch, so common subexpression 
elimination extracts it.
   
   The same happens when the column contains a lambda. Here neither expression 
is extracted, so both are copied into every branch (6 copies):
   
   ```sql
   SET datafusion.sql_parser.dialect = databricks;
   
   EXPLAIN SELECT u, v FROM (
     SELECT array_transform(make_array(s), x -> upper(x))[1] AS u,
            array_transform(make_array(s), x -> lower(x))[1] AS v
     FROM t)
   WHERE (u = 'A' AND v = 'x') OR (u = 'B' AND v = 'y') OR (u = 'C' AND v = 
'z');
   ```
   
   ```text
   03)----Filter: array_element(array_transform(__common_expr_4, (x) -> 
upper(x)), Int64(1)) = Utf8View("A") AND 
array_element(array_transform(__common_expr_4, (x) -> lower(x)), Int64(1)) = 
Utf8View("x") OR array_element(array_transform(__common_expr_4, (x) -> 
upper(x)), Int64(1)) = Utf8View("B") AND ... (3 branches)
   04)------Projection: make_array(t.s) AS __common_expr_4, t.s
   ```
   
   ### Expected behavior
   
   Each projected expression is evaluated once per row, and the filter compares 
the resulting columns:
   
   ```text
   Filter: u = 'A' AND v = 'x' OR u = 'B' AND v = 'y' OR u = 'C' AND v = 'z'
     Projection: upper(t.s) AS u, lower(t.s) AS v
       TableScan: t projection=[s]
   ```
   
   ### Additional context
   
   **Impact.** We hit this with a UDF that parses a list of `key:value` strings 
into a map and reads one key, used in a filter with hundreds of `(a = .. AND b 
IN (..)) OR ..` branches. On 20K rows:
   
   | `OR` branches | `main` | expression evaluated once per row (*) | plain 
columns, no expression |
   |---|---|---|---|
   | 10 | 70 ms | 90 ms | 1.4 ms |
   | 100 | 646 ms | 110 ms | 14 ms |
   | 300 | 2.13 s | 199 ms | 85 ms |
   
   (*) Measured by making the UDF return 
`ExpressionPlacement::MoveTowardsLeafNodes`, which stops `PushDownFilter` from 
inlining it. This is only an experiment: it also stops common subexpression 
elimination from sharing work between the projected columns, which is why 10 
branches is slower.
   
   **Related work.** None of these covers the cases above:
   
   - #25456 defines one inlining policy ("Expensive: at most one site"), but in 
its current form it does not change `PushDownFilter`.
   - #26872 keeps a filter above a projection when it uses a `__common_expr` 
column more than once. Both queries above plan the same with #26872: the copied 
expressions are never extracted into a `__common_expr` column, so the guard 
does not apply.
   - #25388 tried to stop `PushDownFilter` from moving `KeepInPlace` 
expressions and was closed as stale. As noted in #25459, `KeepInPlace` is also 
the default for cheap expressions, so it cannot be the signal on its own.
   
   Is bringing `PushDownFilter` under the #25456 policy planned? A guard based 
on how many times the predicate would use a definition (for example: do not 
inline a non-column, non-literal definition into more than one place) would fix 
both queries. It should not block pushdown for a definition that is used once, 
so filters can still reach the scan.
   


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