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

Pavel Pereslegin commented on CALCITE-6570:
-------------------------------------------

I decided to remove it from 1.38 scope, because I found an issue

*issue description*

By default the `SqlToRelConverter.Config.isExpand() = false`, but 
`SqlToRelConverterTest uses` `withExapnd(true)`

Let's turn it off and see the difference in plan tree in existing test 
`testUpdateSubQuery` (it's not new test) 

Without this fix the plan is:
```
LogicalTableModify(table=[[CATALOG, SALES, EMP]], operation=[UPDATE], 
updateColumnList=[[EMPNO]], sourceExpressionList=[[$0]], flattened=[true])
  LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], 
SAL=[$5], COMM=[$6], DEPTNO=[$7], SLACKER=[$8], EXPR$0=[$SCALAR_QUERY({
LogicalAggregate(group=[{}], EXPR$0=[MIN($0)])
  LogicalProject(EMPNO=[$0])
    LogicalFilter(condition=[=($7, $cor1.DEPTNO)])
      LogicalTableScan(table=[[CATALOG, SALES, EMP]])
})])
    LogicalTableScan(table=[[CATALOG, SALES, EMP]])
```

Note that `sourceExpressionList=[[$0]]`, and SCALAR_QUERY resides only in 
additional logical project.

But after applying this patch the plan is: 
```
LogicalTableModify(table=[[CATALOG, SALES, EMP]], operation=[UPDATE], 
updateColumnList=[[EMPNO]], sourceExpressionList=[[$SCALAR_QUERY({
LogicalAggregate(group=[{}], EXPR$0=[MIN($0)])
  LogicalProject(EMPNO=[$0])
    LogicalFilter(condition=[=($7, $cor1.DEPTNO)])
      LogicalTableScan(table=[[CATALOG, SALES, EMP]])
})]], flattened=[true])
  LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], 
SAL=[$5], COMM=[$6], DEPTNO=[$7], SLACKER=[$8], EXPR$0=[$SCALAR_QUERY({
LogicalAggregate(group=[{}], EXPR$0=[MIN($0)])
  LogicalProject(EMPNO=[$0])
    LogicalFilter(condition=[=($7, $cor0.DEPTNO)])
      LogicalTableScan(table=[[CATALOG, SALES, EMP]])
})])
    LogicalTableScan(table=[[CATALOG, SALES, EMP]])
```
Note that  
```
sourceExpressionList=[[$SCALAR_QUERY({
LogicalAggregate(group=[{}], EXPR$0=[MIN($0)])
  LogicalProject(EMPNO=[$0])
    LogicalFilter(condition=[=($7, $cor1.DEPTNO)])
      LogicalTableScan(table=[[CATALOG, SALES, EMP]])
})]]
```
and this looks like an issue that needs to be fixed before merging

> UPDATE with sub-query that requires type cast gives AssertionError
> ------------------------------------------------------------------
>
>                 Key: CALCITE-6570
>                 URL: https://issues.apache.org/jira/browse/CALCITE-6570
>             Project: Calcite
>          Issue Type: Bug
>          Components: core
>    Affects Versions: 1.37.0
>            Reporter: Pavel Pereslegin
>            Assignee: Pavel Pereslegin
>            Priority: Major
>              Labels: pull-request-available
>             Fix For: 1.38.0
>
>         Attachments: debug.png
>
>
> Reproducer (for SqlToRelConverterTest)
> {code:java}
> @Test void testUpdateSubQuery() {
> final String sql = "update emp\n"
>     + "set empno = (\n"
>     + "  select cast(empno as BIGINT) from emp" +
>     ")";
> sql(sql).ok();
> }
> {code}
> Produces the following assertion error
> {noformat}
> java.lang.AssertionError
>     at 
> org.apache.calcite.sql.validate.implicit.AbstractTypeCoercion.needToCast(AbstractTypeCoercion.java:307)
>     at 
> org.apache.calcite.sql.validate.implicit.AbstractTypeCoercion.needToCast(AbstractTypeCoercion.java:250)
>     at 
> org.apache.calcite.sql.validate.implicit.AbstractTypeCoercion.coerceColumnType(AbstractTypeCoercion.java:210)
>     at 
> org.apache.calcite.sql.validate.implicit.TypeCoercionImpl.coerceSourceRowType(TypeCoercionImpl.java:676)
>     at 
> org.apache.calcite.sql.validate.implicit.TypeCoercionImpl.querySourceCoercion(TypeCoercionImpl.java:646)
>     at 
> org.apache.calcite.sql.validate.SqlValidatorImpl.checkTypeAssignment(SqlValidatorImpl.java:5313)
>     at 
> org.apache.calcite.sql.validate.SqlValidatorImpl.validateUpdate(SqlValidatorImpl.java:5420)
>     at org.apache.calcite.sql.SqlUpdate.validate(SqlUpdate.java:190)
>     at 
> org.apache.calcite.sql.validate.SqlValidatorImpl.validateScopedExpression(SqlValidatorImpl.java:1136)
>     at 
> org.apache.calcite.sql.validate.SqlValidatorImpl.validate(SqlValidatorImpl.java:841)
>     at 
> org.apache.calcite.sql.test.AbstractSqlTester.convertSqlToRel2(AbstractSqlTester.java:543)
>     at 
> org.apache.calcite.sql.test.AbstractSqlTester.assertSqlConvertsTo(AbstractSqlTester.java:486)
>     at 
> org.apache.calcite.sql.test.AbstractSqlTester.assertConvertsTo(AbstractSqlTester.java:464)
>     at 
> org.apache.calcite.test.SqlToRelFixture.convertsTo(SqlToRelFixture.java:106)
>     at org.apache.calcite.test.SqlToRelFixture.ok(SqlToRelFixture.java:94)
>     at 
> org.apache.calcite.test.SqlToRelConverterTest.testUpdateSubQuery(SqlToRelConverterTest.java:3077)
> {noformat}
> The same query without casting to BIGINT works fine.
> Assertion line
> {code:java}
>     // Should keep sync with rules in SqlTypeCoercionRule.
>     assert SqlTypeUtil.canCastFrom(toType, fromType, mappingRule);
>     return true;
> {code}
> When assertion fails, I see that
> toType = INTEGER
> fromType = RecordType(BIGINT EXPR$0)



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

Reply via email to