Skip to content

Trivial WHERE filter not eliminated when combined with CTE #15387

@davidhewitt

Description

@davidhewitt

Describe the bug

I am testing against Datafusion main (d68fca9).

With the query below I expect there to be no FilterExec because the condition is trivial x = x. But it is not eliminated.

To Reproduce

> explain with test AS (SELECT unnest(generate_series(1, 10)) as x)
select count(*) from test WHERE x = x;
+---------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| plan_type     | plan                                                                                                                                                              |
+---------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| logical_plan  | Projection: count(Int64(1)) AS count(*)                                                                                                                           |
|               |   Aggregate: groupBy=[[]], aggr=[[count(Int64(1))]]                                                                                                               |
|               |     SubqueryAlias: test                                                                                                                                           |
|               |       Projection:                                                                                                                                                 |
|               |         Filter: __unnest_placeholder(generate_series(Int64(1),Int64(10)),depth=1) = __unnest_placeholder(generate_series(Int64(1),Int64(10)),depth=1)             |
|               |           Unnest: lists[__unnest_placeholder(generate_series(Int64(1),Int64(10)))|depth=1] structs[]                                                              |
|               |             Projection: List([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) AS __unnest_placeholder(generate_series(Int64(1),Int64(10)))                                        |
|               |               EmptyRelation                                                                                                                                       |
| physical_plan | ProjectionExec: expr=[count(Int64(1))@0 as count(*)]                                                                                                              |
|               |   AggregateExec: mode=Final, gby=[], aggr=[count(Int64(1))]                                                                                                       |
|               |     CoalescePartitionsExec                                                                                                                                        |
|               |       AggregateExec: mode=Partial, gby=[], aggr=[count(Int64(1))]                                                                                                 |
|               |         ProjectionExec: expr=[]                                                                                                                                   |
|               |           CoalesceBatchesExec: target_batch_size=8192                                                                                                             |
|               |             FilterExec: __unnest_placeholder(generate_series(Int64(1),Int64(10)),depth=1)@0 = __unnest_placeholder(generate_series(Int64(1),Int64(10)),depth=1)@0 |
|               |               RepartitionExec: partitioning=RoundRobinBatch(8), input_partitions=1                                                                                |
|               |                 UnnestExec                                                                                                                                        |
|               |                   ProjectionExec: expr=[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] as __unnest_placeholder(generate_series(Int64(1),Int64(10)))]                             |
|               |                     PlaceholderRowExec                                                                                                                            |
|               |                                                                                                                                                                   |
+---------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------+

Expected behavior

No response

Additional context

No response

Metadata

Metadata

Assignees

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions