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

Vladimir Ozerov commented on CALCITE-3484:
------------------------------------------

This appears to be a problem with the handling of nullable results on the right 
side of the correlation. The following query works fine:
{code}
select dept.name from dept WHERE dept.deptno NOT IN (SELECT emp.deptno FROM emp 
WHERE dept.deptno = emp.deptno)
{code}
But the following will not:
{code}
select dept.name from dept WHERE dept.deptno NOT IN (SELECT emp.mgr FROM emp 
WHERE dept.deptno = emp.deptno)
{code}
The only difference, is that {{emp.deptno}} is defined as {{NOT NULL}}, while 
{{emp.mgr}} is nullable. As a result the following happens:
1) {{SqlToRelConverter.substituteSubQuery}}: an {{RelOptUtil.Exists}} is 
created with {{indicator=true}}
2) {{SqlToRelConverter.translateIn}} is invoked with {{UNKNOWN_AS_TRUE}} 
strategy 
3) We end up in a branch which doesn't expect a {{Correlate}} node, and we fail 
with {{ClassCastException}}

There are basically only two usages of that bad {{Join}}:
1) To get the list of left keys. This could be fixed easily by casting to 
{{BiRel}} instead of {{Join}}
2) But later on, we rely on Join's {{JoinInfo.leftKeys}} to build the proper 
CASE condition.

[~julianhyde] could you please help to understand what is the right way to 
construct that CASE condition in case of {{Correlate}}?


> SqlToRelConverter: ClassCastException when processing a query with ANY and 
> UNNEST
> ---------------------------------------------------------------------------------
>
>                 Key: CALCITE-3484
>                 URL: https://issues.apache.org/jira/browse/CALCITE-3484
>             Project: Calcite
>          Issue Type: Bug
>    Affects Versions: 1.21.0
>            Reporter: Ruben Q L
>            Priority: Major
>
> The problem can be reproduced by adding the following test to 
> SqlToRelConverterTest.java:
> {code}
>   @Test public void testEqAnyUnnest() {
>     final String sql = "select d.deptno from dept_nested d WHERE 'john' = ANY 
> (SELECT e.ename from UNNEST(d.employees) e)";
>     sql(sql).ok();
>   }
> {code}
> Which throws the following exception:
> {code}
> java.lang.ClassCastException: org.apache.calcite.rel.logical.LogicalCorrelate 
> cannot be cast to org.apache.calcite.rel.core.Join
>       at 
> org.apache.calcite.sql2rel.SqlToRelConverter.translateIn(SqlToRelConverter.java:1285)
>       at 
> org.apache.calcite.sql2rel.SqlToRelConverter.substituteSubQuery(SqlToRelConverter.java:1157)
>       at 
> org.apache.calcite.sql2rel.SqlToRelConverter.replaceSubQueries(SqlToRelConverter.java:1013)
>       at 
> org.apache.calcite.sql2rel.SqlToRelConverter.convertWhere(SqlToRelConverter.java:979)
>       at 
> org.apache.calcite.sql2rel.SqlToRelConverter.convertSelectImpl(SqlToRelConverter.java:648)
>       at 
> org.apache.calcite.sql2rel.SqlToRelConverter.convertSelect(SqlToRelConverter.java:626)
>       at 
> org.apache.calcite.sql2rel.SqlToRelConverter.convertQueryRecursive(SqlToRelConverter.java:3180)
>       at 
> org.apache.calcite.sql2rel.SqlToRelConverter.convertQuery(SqlToRelConverter.java:562)
>       at 
> org.apache.calcite.test.SqlToRelTestBase$TesterImpl.convertSqlToRel(SqlToRelTestBase.java:626)
>       at 
> org.apache.calcite.test.SqlToRelTestBase$TesterImpl.assertConvertsTo(SqlToRelTestBase.java:745)
>       at 
> org.apache.calcite.test.SqlToRelConverterTest$Sql.convertsTo(SqlToRelConverterTest.java:3664)
>       at 
> org.apache.calcite.test.SqlToRelConverterTest$Sql.ok(SqlToRelConverterTest.java:3656)
>       at 
> org.apache.calcite.test.SqlToRelConverterTest.testEqAnyUnnest(SqlToRelConverterTest.java:1266)
>       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>       at 
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
>       at 
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>       at java.lang.reflect.Method.invoke(Method.java:498)
>       at 
> org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
>       at 
> org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
>       at 
> org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
>       at 
> org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
>       at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
>       at 
> org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
>       at 
> org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
>       at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
>       at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
>       at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
>       at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
>       at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
>       at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
>       at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
>       at 
> com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
>       at 
> com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
>       at 
> com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
>       at 
> com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
> {code}
> Note that the following "similar" queries are correctly converted:
> {code}
>   @Test public void testUnnestEq() {
>     final String sql = "select d.deptno from dept_nested d, 
> UNNEST(d.employees) e WHERE e.ename = 'john'";
>     sql(sql).ok();
>   }
>   @Test public void testInUnnest() {
>     final String sql = "select d.deptno from dept_nested d WHERE 'john' IN 
> (SELECT e.ename from UNNEST(d.employees) e)";
>     sql(sql).ok();
>   }
>   @Test public void testExistsUnnest() {
>     final String sql = "select d.deptno from dept_nested d WHERE EXISTS 
> (SELECT 1 from UNNEST(d.employees) e WHERE e.ename = 'john')";
>     sql(sql).ok();
>   }
> {code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to