[ 
https://issues.apache.org/jira/browse/SPARK-39893?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Wan Kun updated SPARK-39893:
----------------------------
    Description: 
If all group expressions are foldable, the result of this aggregate will always 
be OneRowRelation.
And if all aggregate expressions are foldable, we can add limit 1 to the child 
plan.

For example:

Table : 
{code:sql}
create table tab(key int, value string) using parquet
{code}

Query : 
{code:sql}
SELECT distinct 1001 as id , cast('2022-06-03' as date) AS DT 
FROM testData
{code}
is equal to 
{code:sql}
SELECT distinct 1001 as id , cast('2022-06-03' as date) AS DT 
FROM (
    SELECT *
    FROM tab
    LIMIT 1
)
{code}

Query:
{code:sql}
SELECT 1001 AS id, cast('2022-06-03' as date) AS DT 
FROM tab
group by 'x'
{code}
is equal to 
{code:sql}
SELECT 1001 AS id, cast('2022-06-03' as date) AS DT 
FROM (
    SELECT *
    FROM tab
    LIMIT 1
)
group by 'x'
{code}

  was:
If all group expressions are foldable, the result of this aggregate will always 
be OneRowRelation.
And if all aggregate expressions are foldable, we can add limit 1 to the child 
plan.

For example:

Table : 
{code:sql}
create table tab(key int, value string) using parquet
{code}

Query : 
{code:sql}
SELECT distinct 1001 as id , cast('2022-06-03' as date) AS DT 
FROM testData
{code}

can change to 
{code:sql}
SELECT distinct 1001 as id , cast('2022-06-03' as date) AS DT 
FROM (
    SELECT *
    FROM tab
    LIMIT 1
)
{code}

Query:
{code:sql}
SELECT 1001 AS id, cast('2022-06-03' as date) AS DT 
FROM tab
group by 'x'
{code}

is equal to 
{code:sql}
SELECT 1001 AS id, cast('2022-06-03' as date) AS DT 
FROM (
    SELECT *
    FROM tab
    LIMIT 1
)
group by 'x'
{code}


> Push limit 1 to the aggregate's child plan if grouping expressions and 
> aggregate expressions are foldable
> ---------------------------------------------------------------------------------------------------------
>
>                 Key: SPARK-39893
>                 URL: https://issues.apache.org/jira/browse/SPARK-39893
>             Project: Spark
>          Issue Type: Improvement
>          Components: SQL
>    Affects Versions: 3.4.0
>            Reporter: Wan Kun
>            Priority: Major
>
> If all group expressions are foldable, the result of this aggregate will 
> always be OneRowRelation.
> And if all aggregate expressions are foldable, we can add limit 1 to the 
> child plan.
> For example:
> Table : 
> {code:sql}
> create table tab(key int, value string) using parquet
> {code}
> Query : 
> {code:sql}
> SELECT distinct 1001 as id , cast('2022-06-03' as date) AS DT 
> FROM testData
> {code}
> is equal to 
> {code:sql}
> SELECT distinct 1001 as id , cast('2022-06-03' as date) AS DT 
> FROM (
>     SELECT *
>     FROM tab
>     LIMIT 1
> )
> {code}
> Query:
> {code:sql}
> SELECT 1001 AS id, cast('2022-06-03' as date) AS DT 
> FROM tab
> group by 'x'
> {code}
> is equal to 
> {code:sql}
> SELECT 1001 AS id, cast('2022-06-03' as date) AS DT 
> FROM (
>     SELECT *
>     FROM tab
>     LIMIT 1
> )
> group by 'x'
> {code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@spark.apache.org
For additional commands, e-mail: issues-h...@spark.apache.org

Reply via email to