davidradl commented on code in PR #27158:
URL: https://github.com/apache/flink/pull/27158#discussion_r2469799782


##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/calcite/FlinkRexBuilder.java:
##########
@@ -102,4 +91,57 @@ public RexLiteral makeZeroLiteral(RelDataType type) {
                 return super.makeZeroLiteral(type);
         }
     }
+
+    private RexNode makeFieldAccess(RexNode expr, RexNode field) {
+        final RexNode fieldWithRemovedCast = 
removeCastNullableFromFieldAccess(field);
+        if (field.getType().isNullable() != 
fieldWithRemovedCast.getType().isNullable()
+                || expr.getType().isNullable() && 
!field.getType().isNullable()) {
+            return makeCast(
+                    typeFactory.createTypeWithNullability(field.getType(), 
true),
+                    fieldWithRemovedCast,
+                    true,
+                    false);
+        }
+
+        return expr.getType().isNullable() && 
fieldWithRemovedCast.getType().isNullable()
+                ? fieldWithRemovedCast
+                : field;
+    }
+
+    /**
+     * {@link FlinkRexBuilder#makeFieldAccess} will adjust nullability based 
on nullability of the
+     * enclosing type. However, it might be a deeply nested column and for 
every step {@link
+     * FlinkRexBuilder#makeFieldAccess} will try to insert a cast. This method 
will remove previous
+     * cast in order to keep only one.
+     */
+    private RexNode removeCastNullableFromFieldAccess(RexNode rexFieldAccess) {
+        if (!(rexFieldAccess instanceof RexFieldAccess)) {
+            return rexFieldAccess;
+        }
+        RexNode rexNode = rexFieldAccess;
+        while (rexNode instanceof RexFieldAccess) {
+            rexNode = ((RexFieldAccess) rexNode).getReferenceExpr();
+        }
+        if (rexNode.getKind() != SqlKind.CAST) {
+            return rexFieldAccess;
+        }
+        RexShuttle visitor =
+                new RexShuttle() {
+                    @Override
+                    public RexNode visitCall(final RexCall call) {
+                        if (call.getKind() == SqlKind.CAST

Review Comment:
   A basic question - I am curious why we are doing cast processing when the 
reported problem was for a computed row - are they related?



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