apache / apache/datafusion

Simplify predicate expressions like 'a > 1 and a < 1' to constant false

Open
#5,830 12 comments 0 reactions 1 assignee Claimed by @wudidapaopao 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?

```sql
❯ explain select * from t where a > 1 and a < 1;
+---------------+-------------------------------------------------------------------------------+
| plan_type | plan |
+---------------+-------------------------------------------------------------------------------+
| logical_plan | Filter: t.a > Int32(1) AND t.a < Int32(1) |
| | TableScan: t projection=[a, b] |
| physical_plan | CoalesceBatchesExec: target_batch_size=8192 |
| | FilterExec: a@0 > 1 AND a@0 < 1 |
| | MemoryExec: partitions=10, partition_sizes=[1, 0, 0, 0, 0, 0, 0, 0, 0, 0] |
| | |
+---------------+-------------------------------------------------------------------------------+
```
Datafusion needs to scan table t and execute the filter, although it's unnecessary.

### Describe the solution you'd like

Add an optimizer rule to simplify this predicate to a constant bool value and make the logical plan equal to 'select * from t where false' like
```sql
❯ explain select * from t where false;
+---------------+----------------------------------+
| plan_type | plan |
+---------------+----------------------------------+
| logical_plan | EmptyRelation |
| physical_plan | EmptyExec: produce_one_row=false |
| | |
+---------------+----------------------------------+
```

### Describe alternatives you've considered

_No response_

### Additional context

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.