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

Zhen Chen updated CALCITE-7463:
-------------------------------
    Description: 
The {{UnionToFilterRule}} produces incorrect results when applied to inputs 
that contain {{{}LIMIT{}}}.

Specifically, the rule incorrectly collapses:
{code:java}
(SELECT mgr, comm FROM emp LIMIT 2)
UNION
(SELECT mgr, comm FROM emp LIMIT 2)into:{code}
{code:java}
SELECT DISTINCT mgr, comm FROM emp LIMIT 2
{code}
This transformation is {*}not semantically equivalent{*}.
*Reproduction*
SQL
{code:java}
(SELECT mgr, comm FROM emp LIMIT 2)
UNION
(SELECT mgr, comm FROM emp LIMIT 2) {code}
h4. Plan Before
{code:java}
LogicalUnion(all=[false])
  LogicalSort(fetch=[2])
    LogicalProject(MGR=[$3], COMM=[$6])
      LogicalTableScan(table=[[CATALOG, SALES, EMP]])
  LogicalSort(fetch=[2])
    LogicalProject(MGR=[$3], COMM=[$6])
      LogicalTableScan(table=[[CATALOG, SALES, EMP]]) {code}
*Plan After (Incorrect)*
{code:java}
LogicalAggregate(group=[{0, 1}])
  LogicalSort(fetch=[2])
    LogicalProject(MGR=[$3], COMM=[$6])
      LogicalTableScan(table=[[CATALOG, SALES, EMP]]) {code}
*Expected Behavior*
The transformation should NOT be applied when any input of UNION contains 
LogicalSort(That contains ORDER BY, LIMIT, OFFSET). 
 

  was:
The {{UnionToFilterRule}} produces incorrect results when applied to inputs 
that contain {{{}LIMIT{}}}.

Specifically, the rule incorrectly collapses:

 
{code:java}
(SELECT mgr, comm FROM emp LIMIT 2)
UNION
(SELECT mgr, comm FROM emp LIMIT 2){code}
 
into:
{code:java}
SELECT DISTINCT mgr, comm FROM emp LIMIT 2
{code}
This transformation is {*}not semantically equivalent{*}.
*Reproduction*
SQL
 
{code:java}
(SELECT mgr, comm FROM emp LIMIT 2)
UNION
(SELECT mgr, comm FROM emp LIMIT 2) {code}
 
h4. Plan Before
{code:java}
LogicalUnion(all=[false])
  LogicalSort(fetch=[2])
    LogicalProject(MGR=[$3], COMM=[$6])
      LogicalTableScan(table=[[CATALOG, SALES, EMP]])
  LogicalSort(fetch=[2])
    LogicalProject(MGR=[$3], COMM=[$6])
      LogicalTableScan(table=[[CATALOG, SALES, EMP]]) {code}
Plan After (Incorrect)
{code:java}
LogicalAggregate(group=[{0, 1}])
  LogicalSort(fetch=[2])
    LogicalProject(MGR=[$3], COMM=[$6])
      LogicalTableScan(table=[[CATALOG, SALES, EMP]]) {code}
*Expected Behavior*
The transformation should NOT be applied when any input of UNION contains 
LogicalSort(That contains ORDER BY, LIMIT, OFFSET). 
 


> UnionToFilterRule incorrectly rewrites UNION with LIMIT
> -------------------------------------------------------
>
>                 Key: CALCITE-7463
>                 URL: https://issues.apache.org/jira/browse/CALCITE-7463
>             Project: Calcite
>          Issue Type: Bug
>          Components: core
>    Affects Versions: 1.41.0
>            Reporter: Zhen Chen
>            Priority: Major
>             Fix For: 1.42.0
>
>
> The {{UnionToFilterRule}} produces incorrect results when applied to inputs 
> that contain {{{}LIMIT{}}}.
> Specifically, the rule incorrectly collapses:
> {code:java}
> (SELECT mgr, comm FROM emp LIMIT 2)
> UNION
> (SELECT mgr, comm FROM emp LIMIT 2)into:{code}
> {code:java}
> SELECT DISTINCT mgr, comm FROM emp LIMIT 2
> {code}
> This transformation is {*}not semantically equivalent{*}.
> *Reproduction*
> SQL
> {code:java}
> (SELECT mgr, comm FROM emp LIMIT 2)
> UNION
> (SELECT mgr, comm FROM emp LIMIT 2) {code}
> h4. Plan Before
> {code:java}
> LogicalUnion(all=[false])
>   LogicalSort(fetch=[2])
>     LogicalProject(MGR=[$3], COMM=[$6])
>       LogicalTableScan(table=[[CATALOG, SALES, EMP]])
>   LogicalSort(fetch=[2])
>     LogicalProject(MGR=[$3], COMM=[$6])
>       LogicalTableScan(table=[[CATALOG, SALES, EMP]]) {code}
> *Plan After (Incorrect)*
> {code:java}
> LogicalAggregate(group=[{0, 1}])
>   LogicalSort(fetch=[2])
>     LogicalProject(MGR=[$3], COMM=[$6])
>       LogicalTableScan(table=[[CATALOG, SALES, EMP]]) {code}
> *Expected Behavior*
> The transformation should NOT be applied when any input of UNION contains 
> LogicalSort(That contains ORDER BY, LIMIT, OFFSET). 
>  



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

Reply via email to