wojustme commented on code in PR #2686:
URL: https://github.com/apache/calcite/pull/2686#discussion_r1005507479


##########
core/src/main/java/org/apache/calcite/rel/rules/ProjectJoinTransposeRule.java:
##########
@@ -157,7 +159,24 @@ public ProjectJoinTransposeRule(
   @Value.Immutable(singleton = false)
   public interface Config extends RelRule.Config {
     Config DEFAULT = ImmutableProjectJoinTransposeRule.Config.builder()
-        .withPreserveExprCondition(expr -> !(expr instanceof RexOver))
+        .withPreserveExprCondition(expr -> {
+          // Non push down over's expression by default

Review Comment:
   @libenchao Thanks for your reminder, but I check this pr, and I found that I 
have update this code, screenshot as follows:
   
![image](https://user-images.githubusercontent.com/8967656/198003453-016b8697-b580-4c8f-8448-46afcd2d10be.png)
   



##########
core/src/main/java/org/apache/calcite/rel/rules/ProjectJoinTransposeRule.java:
##########
@@ -157,7 +159,24 @@ public ProjectJoinTransposeRule(
   @Value.Immutable(singleton = false)
   public interface Config extends RelRule.Config {
     Config DEFAULT = ImmutableProjectJoinTransposeRule.Config.builder()
-        .withPreserveExprCondition(expr -> !(expr instanceof RexOver))
+        .withPreserveExprCondition(expr -> {
+          // Non push down over's expression by default
+          if (expr instanceof RexOver) {
+            return false;
+          }
+          final RelDataType relType = expr.getType();
+          if (SqlKind.CAST == expr.getKind()) {
+            final RexCall castCall = (RexCall) expr;
+            final RelDataType operand0Type = 
castCall.getOperands().get(0).getType();
+            if (relType.getSqlTypeName() == operand0Type.getSqlTypeName()
+                && relType.isNullable() != operand0Type.isNullable()) {

Review Comment:
   @libenchao Yes, It's my carelessness.
   Code has been updated, please review it.
   Thanks.



##########
core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml:
##########
@@ -9004,6 +9005,38 @@ LogicalProject(EXPR$0=[$1], EXPR$1=[$0])
           LogicalTableScan(table=[[CATALOG, SALES, EMP]])
         LogicalProject(ENAME=[$0])
           LogicalTableScan(table=[[CATALOG, SALES, BONUS]])
+]]>
+    </Resource>
+  </TestCase>
+  <TestCase name="testPushProjectPastLeftJoin2">
+    <Resource name="sql">
+      <![CDATA[select coalesce(d.name, b.job, '')
+from emp e
+left join bonus b on e.ename = b.ename
+left join dept d on e.deptno = d.deptno]]>
+    </Resource>
+    <Resource name="planBefore">
+      <![CDATA[
+LogicalProject(EXPR$0=[CASE(IS NOT NULL($14), CAST($14):VARCHAR(10) NOT NULL, 
IS NOT NULL($10), CAST($10):VARCHAR(10) NOT NULL, '':VARCHAR(10))])
+  LogicalJoin(condition=[=($7, $13)], joinType=[left])
+    LogicalJoin(condition=[=($1, $9)], joinType=[left])
+      LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+      LogicalTableScan(table=[[CATALOG, SALES, BONUS]])
+    LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
+]]>
+    </Resource>
+    <Resource name="planAfter">
+      <![CDATA[
+LogicalProject(EXPR$0=[CASE(IS NOT NULL($4), CAST($4):VARCHAR(10) NOT NULL, 
$1, $2, '':VARCHAR(10))])
+  LogicalJoin(condition=[=($0, $3)], joinType=[left])
+    LogicalProject(DEPTNO=[$1], EXPR$0=[IS NOT NULL($3)], 
EXPR$1=[CAST($3):VARCHAR(10) NOT NULL])

Review Comment:
   Hi @NobiGo 
   I debug the 
code`org.apache.calcite.adapter.enumerable.RexImpTable.AbstractRexCallImplementor#genValueStatement`,
 I found it's really handle it.
   `CAST xxx NOT NULL` will be translated to `input_isNull0 ?  null : 
input_value0`
   
![image](https://user-images.githubusercontent.com/8967656/150818056-dfe709ca-d0c4-4018-a981-68eb1969d593.png)
   
   ------
   code: 
https://github.com/wojustme/calcite/commit/d1baa3d0a33dcd3402a6b886f88aa6442774bd5e



-- 
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