NobiGo commented on code in PR #4403:
URL: https://github.com/apache/calcite/pull/4403#discussion_r2123413147


##########
core/src/main/java/org/apache/calcite/sql/fun/SqlInternalOperators.java:
##########
@@ -224,6 +224,40 @@ private SqlInternalOperators() {
   public static final SqlAggFunction LITERAL_AGG =
       SqlLiteralAggFunction.INSTANCE;
 
+  /** CAST NOT NULL operator used for cast expression to make it non-nullable 
in SqlNode.
+   *
+   * <p>For example:
+   * <pre>{@code COALESCE(a,b)
+   * }</pre>
+   *
+   * <p>is converted to
+   *
+   * <pre>{@code CASE WHEN a is not null THEN a ELSE b
+   * }</pre>
+   * by {@code SqlCoalesceFunction#rewriteCall}.
+   *
+   * <p>When a is nullable and b is non-nullable, the {@code COALESCE(a,b)} 
data type will be
+   * non-nullable and {@code CASE WHEN a is not null THEN a ELSE b} data type 
will be nullable.

Review Comment:
   CALCITE-3675 can't resolve this issue.
   ```
      @Test void testCoalesceSubquery() {
       final String sql = "SELECT"
           + "  deptno, "
           + "  coalesce((select sum(empno) from emp "
           + "  where deptno = emp.deptno limit 1), 0) as w "
           + "FROM dept";
       sql(sql).ok();
     }
   ```
   This SQL generates RelNode is:
   ```
   LogicalProject(DEPTNO=[$0], W=[CASE(IS NOT NULL($2), $3, 0)])
     LogicalJoin(condition=[true], joinType=[left])
       LogicalJoin(condition=[true], joinType=[left])
         LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
         LogicalSort(fetch=[1])
           LogicalAggregate(group=[{}], EXPR$0=[SUM($0)])
             LogicalProject(EMPNO=[$0])
               LogicalFilter(condition=[=($7, $7)])
                 LogicalTableScan(table=[[CATALOG, SALES, EMP]])
       LogicalSort(fetch=[1])
         LogicalAggregate(group=[{}], EXPR$0=[SUM($0)])
           LogicalProject(EMPNO=[$0])
             LogicalFilter(condition=[=($7, $7)])
               LogicalTableScan(table=[[CATALOG, SALES, EMP]])
   ```
   When use the condition 
`isNotNullCall.getOperands().get(0).equals(thenNode)`, it is failed(`$2 != $3`).
   
   This PR will make CALCITE-3675 unnecessary.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to