[
https://issues.apache.org/jira/browse/CALCITE-685?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17461108#comment-17461108
]
Vladimir Steshin commented on CALCITE-685:
------------------------------------------
If I'm correct, one should look into LogicalAggregate and/or LogicalProject.
Working plan:
{code:java}
LogicalProject(department_id=[$7], EXPR$1=[$SCALAR_QUERY({
LogicalProject(EXPR$0=[1])
LogicalFilter(condition=[=($cor0.department_id, -($7, 5000))])
LogicalTableScan(table=[[foodmart2, employee]])
})])
LogicalTableScan(table=[[foodmart2, employee]])
{code}
Malfunction:
{code:java}
LogicalProject(department_id=[$0], EXPR$1=[$SCALAR_QUERY({
LogicalProject(EXPR$0=[1])
LogicalFilter(condition=[=($cor0.department_id, $7)])
LogicalTableScan(table=[[foodmart2, employee]])
})])
LogicalAggregate(group=[{0}])
LogicalProject(department_id=[$7])
LogicalTableScan(table=[[foodmart2, employee]])
{code}
The correlate being created on TableScan gets as 'left' full rowType holding
the required columns (Correlate.requiredColumns). Otherwise, Correlate receives
only 1 and other column.
> Correlated scalar sub-query in SELECT clause throws
> ---------------------------------------------------
>
> Key: CALCITE-685
> URL: https://issues.apache.org/jira/browse/CALCITE-685
> Project: Calcite
> Issue Type: Bug
> Reporter: Jinfeng Ni
> Priority: Major
> Labels: sub-query
>
> For the following query, where a correlated scalar subquery is put in select
> list, Calcite will hit NPE in RelDecorrelator.
> {code}
> select e.department_id, sum(e.employee_id),
> ( select sum(e2.employee_id)
> from employee e2
> where e.department_id = e2.department_id
> )
> from employee e
> group by e.department_id;
> {code}
> {code}
> Caused by: java.lang.NullPointerException
> at
> org.apache.calcite.sql2rel.RelDecorrelator.createValueGenerator(RelDecorrelator.java:733)
> at
> org.apache.calcite.sql2rel.RelDecorrelator.decorrelateInputWithValueGenerator(RelDecorrelator.java:842)
> at
> org.apache.calcite.sql2rel.RelDecorrelator.decorrelateRel(RelDecorrelator.java:902)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
> at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> at
> org.apache.calcite.util.ReflectUtil.invokeVisitorInternal(ReflectUtil.java:256)
> at
> org.apache.calcite.util.ReflectUtil.invokeVisitor(ReflectUtil.java:213)
> at
> org.apache.calcite.util.ReflectUtil$1.invokeVisitor(ReflectUtil.java:476)
> at
> org.apache.calcite.sql2rel.RelDecorrelator$DecorrelateRelVisitor.visit(RelDecorrelator.java:1420)
> at org.apache.calcite.rel.SingleRel.childrenAccept(SingleRel.java:72)
> .....
> {code}
> Here is the unit test case I used to re-produce this problem in JdbcTest.java
> {code}
> @Test public void testCorreScalarSubQInSelect()
> throws ClassNotFoundException, SQLException {
> String query = "select e.department_id, sum(e.employee_id),\n"
> + " ( select sum(e2.employee_id)\n"
> + " from employee e2\n"
> + " where e.department_id = e2.department_id\n"
> + " )\n"
> + " from employee e\n"
> + " group by e.department_id\n";
> CalciteAssert.that()
> .with(CalciteAssert.Config.FOODMART_CLONE)
> .with(Lex.JAVA)
> .query(query)
> .returnsCount(0);
> }
> {code}
--
This message was sent by Atlassian Jira
(v8.20.1#820001)