apache / apache/datafusion

Expression Simplifier doesn't consider associativity (`(i + 1) + 2)` is not simplified to `i + 3`)

Open
#11,594 12 comments 2 reactions 0 assignees View on GitHub
enhancement
Dominant language
Rust
Stars
9.3k
Forks
2.4k
Avg merge
3d 7h
Merged PRs (30d)
344

Description

### Is your feature request related to a problem or challenge?

DataFusion will simplify expressions like this: `i + (1 + 2)` => `i + 3`

However, it will not simplify `i + 1 + 2` (remains `i + 1 + 2`)

You can see this in the explain plans

```
> explain select column1 + (1 + 2) from values (100);
+---------------+-----------------------------------------------------------------------+
| plan_type | plan |
+---------------+-----------------------------------------------------------------------+
| logical_plan | Projection: column1 + Int64(3) AS column1 + Int64(1) + Int64(2) |
| | Values: (Int64(100)) |
| physical_plan | ProjectionExec: expr=[column1@0 + 3 as column1 + Int64(1) + Int64(2)] | <-- computed expression is `column1@0 + 3`
| | ValuesExec |
| | |
+---------------+-----------------------------------------------------------------------+
2 row(s) fetched.
Elapsed 0.013 seconds.

> explain select column1 + 1 + 2 from values (100);
+---------------+---------------------------------------------------------------------------+
| plan_type | plan |
+---------------+---------------------------------------------------------------------------+
| logical_plan | Projection: column1 + Int64(1) + Int64(2) |
| | Values: (Int64(100)) |
| physical_plan | ProjectionExec: expr=[column1@0 + 1 + 2 as column1 + Int64(1) + Int64(2)] | <-- expression is STILL `column1@0 + 1 + 2`
| | ValuesExec |
| | |
+---------------+---------------------------------------------------------------------------+
2 row(s) fetched.
Elapsed 0.002 seconds.
```

@timsaucer has identified the problem

> I don’t have time to look through the code right now, but I would guess the operations happen left to right when you don’t have parentheses to indicate order. So the second would be equivalent to (col(“i”) + lit(1)) + lit(2). That is, would guess it isn’t checking for associativity of operations.

So in this case `i + 1 + 2` is parsed as `(i + 1) + 2` and since `(i + 1)` can't be reduced, the entire expression isn't either

### Describe the solution you'd like

It would be nice to properly support this simplification

### Describe alternatives you've considered

We'll have to consider how to apply associativity (I am sure there is prior art in this area) as to solve the above issue it would need to potentially reorder the operations so constants are together and then also re-apply the const evaluator

https://github.com/apache/datafusion/blob/63efaee2555ddd1381b4885867860621ec791f82/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs#L711-L1544

### Additional context

Came up on discord ( @kavirajk) https://discord.com/channels/885562378132000778/1166447479609376850/1264325499971436594

Contributor guide

Open the contributing guide

Research direction

Read datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs in the linked range, then reproduce the two EXPLAIN examples to compare parenthesized and left-associative expressions. Trace the simplifier's constant evaluation and determine how associativity should be handled; done means `i + 1 + 2` simplifies to `i + 3` without changing expression semantics.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust, sql
Domain
data-engineering
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.