Skip to content

SQL case scalar_subquery logical_paln unexpected Aggregate: groupBy=[[col]] #5791

@jiangzhx

Description

@jiangzhx

Describe the bug

logical_paln unexpected
Aggregate: groupBy=[[t2.a]], aggr=[[COUNT(UInt8(1))]]

it's should
Aggregate: groupBy=[[]], aggr=[[COUNT(UInt8(1))]]

select a,b from t1 where (select count(*) from t2 where t1.a = t2.a)>0;

+---------------+----------------------------------------------------------------------------------------------------------------------------+
| plan_type     | plan                                                                                                                       |
+---------------+----------------------------------------------------------------------------------------------------------------------------+
| logical_plan  | Projection: t1.a, t1.b                                                                                                     |
|               |   Inner Join: t1.a = __scalar_sq_1.a                                                                                       |
|               |     TableScan: t1 projection=[a, b]                                                                                        |
|               |     SubqueryAlias: __scalar_sq_1                                                                                           |
|               |       Projection: t2.a                                                                                                     |
|               |         Filter: COUNT(UInt8(1)) > Int64(0)                                                                                 |
|               |           Aggregate: groupBy=[[t2.a]], aggr=[[COUNT(UInt8(1))]]                                                            |
|               |             TableScan: t2 projection=[a]                                                                                   |
| physical_plan | ProjectionExec: expr=[a@0 as a, b@1 as b]                                                                                  |
|               |   CoalesceBatchesExec: target_batch_size=8192                                                                              |
|               |     HashJoinExec: mode=Partitioned, join_type=Inner, on=[(Column { name: "a", index: 0 }, Column { name: "a", index: 0 })] |
|               |       CoalesceBatchesExec: target_batch_size=8192                                                                          |
|               |         RepartitionExec: partitioning=Hash([Column { name: "a", index: 0 }], 8), input_partitions=8                        |
|               |           RepartitionExec: partitioning=RoundRobinBatch(8), input_partitions=1                                             |
|               |             MemoryExec: partitions=1, partition_sizes=[1]                                                                  |
|               |       ProjectionExec: expr=[a@0 as a]                                                                                      |
|               |         CoalesceBatchesExec: target_batch_size=8192                                                                        |
|               |           FilterExec: COUNT(UInt8(1))@1 > 0                                                                                |
|               |             AggregateExec: mode=FinalPartitioned, gby=[a@0 as a], aggr=[COUNT(UInt8(1))]                                   |
|               |               CoalesceBatchesExec: target_batch_size=8192                                                                  |
|               |                 RepartitionExec: partitioning=Hash([Column { name: "a", index: 0 }], 8), input_partitions=8                |
|               |                   AggregateExec: mode=Partial, gby=[a@0 as a], aggr=[COUNT(UInt8(1))]                                      |
|               |                     RepartitionExec: partitioning=RoundRobinBatch(8), input_partitions=1                                   |
|               |                       MemoryExec: partitions=1, partition_sizes=[1]                                                        |
|               |                                                                                                                            |
+---------------+----------------------------------------------------------------------------------------------------------------------------+

To Reproduce

#[tokio::test]
async fn test_count_wildcard_on_where_scalar_subquery() -> Result<()> {
    // you can find create_join_context() in  datafusion/core/tests/dataframe.rs
    let ctx = create_join_context()?;
    ctx.sql("select a,b from t1 where (select count(*) from t2 where t1.a = t2.a)>0;")
        .await?
        .explain(false, false)?
        .show()
        .await?;
    Ok(())
}

Expected behavior

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions