[ 
https://issues.apache.org/jira/browse/CALCITE-6452?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17867117#comment-17867117
 ] 

Julian Hyde edited comment on CALCITE-6452 at 7/18/24 9:40 PM:
---------------------------------------------------------------

You're right. The value should be 1 for Allen, Martin, Turner, Ward (who have 
distinct, not-null commission values) and 10 for the rest (who have null 
commission values).

Confirmed on Postgres sqlfiddle.com:
{code:java}
with dept (deptno,dname,location) as
 (VALUES(10,'ACCOUNTING','NEW YORK'),
        (20,'RESEARCH','DALLAS'),
        (30,'SALES','CHICAGO'), 
        (40,'OPERATIONS','BOSTON')),
  emp (empno, ename, job, mgrno, hiredate, sal, comm, deptno) as
 (VALUES(7369,'SMITH','CLERK',7902,'1980-12-17',800.00,NULL,20), 
        (7499,'ALLEN','SALESMAN',7698,'1981-02-20',1600.00,300.00,30), 
        (7521,'WARD','SALESMAN',7698,'1981-02-22',1250.00,500.00,30), 
        (7566,'JONES','MANAGER',7839,'1981-02-04',2975.00,NULL,20), 
        (7654,'MARTIN','SALESMAN',7698,'1981-09-28',1250.00,1400.00,30), 
        (7698,'BLAKE','MANAGER',7839,'1981-01-05',2850.00,NULL,30), 
        (7782,'CLARK','MANAGER',7839,'1981-06-09',2450.00,NULL,10), 
        (7788,'SCOTT','ANALYST',7566,'1987-04-19',3000.00,NULL,20),
        (7839,'KING','PRESIDENT',NULL,'1981-11-17',5000.00,NULL,10),
        (7844,'TURNER','SALESMAN',7698,'1981-09-08',1500.00,0.00,30),
        (7876,'ADAMS','CLERK',7788,'1987-05-23',1100.00,NULL,20),
        (7900,'JAMES','CLERK',7698,'1981-12-03',950.00,NULL,30),
        (7902,'FORD','ANALYST',7566,'1981-12-03',3000.00,NULL,20),
        (7934,'MILLER','CLERK',7782,'1982-01-23',1300.00,NULL,10))
select e.ename,
  (select count(*)
    from emp as f
    where f.comm is not distinct from e.comm) as c
from emp as e; {code}


was (Author: julianhyde):
You're right. The value should be 1 for Allen, Martin, Turner, Ward (who have 
distinct, not-null commission values) and 10 for the rest (who have null 
commission values).

> Scalar sub-query that uses IS NOT DISTINCT FROM returns incorrect result
> ------------------------------------------------------------------------
>
>                 Key: CALCITE-6452
>                 URL: https://issues.apache.org/jira/browse/CALCITE-6452
>             Project: Calcite
>          Issue Type: Improvement
>            Reporter: Julian Hyde
>            Assignee: TJ Banghart
>            Priority: Major
>
> Scalar sub-query that uses an {{IS NOT DISTINCT FROM}} condition returns 
> incorrect result. For example,
> {code:java}
> select e.ename,
>   (select count(*)
>     from emp as f
>     where f.comm is not distinct from e.comm) as c
> from emp as e{code}
> returns
> {noformat}
> +--------+----+
> | ENAME  | C  |
> +--------+----+
> | ADAMS  |  0 |
> | ALLEN  |  1 |
> | BLAKE  |  0 |
> | CLARK  |  0 |
> | FORD   |  0 |
> | JAMES  |  0 |
> | JONES  |  0 |
> | KING   |  0 |
> | MARTIN |  1 |
> | MILLER |  0 |
> | SCOTT  |  0 |
> | SMITH  |  0 |
> | TURNER |  1 |
> | WARD   |  1 |
> +--------+----+
> (14 rows)
> {noformat}
> but should return
> {noformat}
> +--------+----+
> | ENAME  | C  |
> +--------+----+
> | ADAMS  |  0 |
> | ALLEN  | 10 |
> | BLAKE  |  0 |
> | CLARK  |  0 |
> | FORD   |  0 |
> | JAMES  |  0 |
> | JONES  |  0 |
> | KING   |  0 |
> | MARTIN | 10 |
> | MILLER |  0 |
> | SCOTT  |  0 |
> | SMITH  |  0 |
> | TURNER | 10 |
> | WARD   | 10 |
> +--------+----+
> (14 rows)
> {noformat}
> Also, and perhaps related, if we add a {{WHERE}} to the above query, like 
> this:
> {code:java}
> select e.ename,
>   (select count(*)
>     from emp as f
>     where f.comm is not distinct from e.comm) as c
> from emp as e
> where e.deptno = 10{code}
> Calcite throws:
> {noformat}
> Caused by: java.lang.ArrayIndexOutOfBoundsException: Index 6 out of bounds 
> for length 5
> >     at 
> > com.google.common.collect.RegularImmutableList.get(RegularImmutableList.java:77)
> >     at org.apache.calcite.rex.RexBuilder.makeInputRef(RexBuilder.java:1037)
> >     at 
> > org.apache.calcite.sql2rel.RelDecorrelator$AdjustProjectForCountAggregateRule.lambda$onMatch2$4(RelDecorrelator.java:2679)
> {noformat}
> I ran into this error while trying to rewrite queries that had measures and 
> used {{ROLLUP}}. (Since {{ROLLUP}} will generate group keys whose value is 
> NULL, we cannot use regular {{=}} when doing a self-join.)



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

Reply via email to