LiaCastaneda commented on issue #27631:
URL: https://github.com/apache/datafusion/issues/27631#issuecomment-7191010095
> How is the boolean more granular?
I think the "granular" word was imprecise. The thing is that I think the
enum values are deciding two things at once, which is where an expression is
evaluated and whether if it's okay to copy it into a filter. Expressions like
`CAST` and `array_transform() `need the same placement (`KeepInPlace`) since
pushing them to the scan is wasteful, but they have opposite answers on copying
which the KeepInPlace/MoveTowardsLeafNodes enum can't differentiate.
For example with a filter like `CAST(col) > 5` if we mark `CAST` as
`MoveTowardsLeafNodes` (because it's cheap), the cast will get computed for
every row before filtering. I think that would be a regression since we waste
work casting values that get filtered out afterwards. Today `CAST` is
`KeepInPlace` which is nice because `CAST(col) > 5` gets unwrapped to `col > 5`
that gets pushed to the scan, and the CAST only runs on the already filtered
data.
KeepInPlace:
```
ProjectionExec: expr=[CAST(x@0 AS Int64) as t] -- cast only on rows
that passed
FilterExec: x@0 > 5 -- unwrapped to the
plain column
DataSourceExec: projection=[x], predicate=x@0 > 5,
pruning_predicate=x_null_count@1 != row_count@2 AND
x_max@0 > 5
```
MoveTowardsLeafNodes:
```
FilterExec: t@0 > 5 -- filter runs
after the cast
ProjectionExec: expr=[CAST(x@0 AS Int64) as t] -- cast on every
row the scan returns
DataSourceExec: projection=[x], predicate=CAST(x@0 AS Int64) > 5,
pruning_predicate=x_null_count@1 != row_count@2 AND
x_max@0 > 5
```
But `array_transform(...)` (or any other expensive expression) is also
`KeepInPlace`, so `PushDownFilter` treats it the same way and copies it into
every branch of the filter which is an issue when there are too many branches
(the repro in this issue). For that one we'd want the opposite which is keep
its placement but don't duplicate it. None of the enum values mean "normal
placement but don't copy" so I think we'd need something like Spark's boolean
which is independent of placement.
KeepInPlace:
```
Projection: array_transform(__common_expr_1, (x) -> upper(x)) AS u,
array_transform(__common_expr_1, (x) -> lower(x)) AS v
Projection: make_array(t.s) AS __common_expr_1
Filter: array_transform(__common_expr_4, (x) -> upper(x)) = List([A])
AND array_transform(__common_expr_4, (x) -> lower(x)) = List([x])
OR array_transform(__common_expr_4, (x) -> upper(x)) = List([B])
AND array_transform(__common_expr_4, (x) -> lower(x)) = List([y])
OR array_transform(__common_expr_4, (x) -> upper(x)) = List([C])
AND array_transform(__common_expr_4, (x) -> lower(x)) = List([z])
Projection: make_array(t.s) AS __common_expr_4, t.s
TableScan: t projection=[s]
```
MoveTowardsLeafNodes
```
Filter: u = List([A]) AND v = List([x]) OR u = List([B]) AND v = List([y])
OR u = List([C]) AND v = List([z])
Projection: array_transform(__common_expr_1, (x) -> upper(x)) AS u,
array_transform(__common_expr_1, (x) -> lower(x)) AS v
Projection: make_array(t.s) AS __common_expr_1
TableScan: t projection=[s]
```
MoveTowardsLeafNodes does avoid the copies in this case, but I don't think
we can mark lambdas with it because it means "this is cheap and its best
evaluated at the source"
--
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]