snuyanzin commented on code in PR #27158:
URL: https://github.com/apache/flink/pull/27158#discussion_r2469825509
##########
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:
so we adjust nullability, however with such approach for heavily nested
columns it might happen that several casts might appear after that. The idea
here is to keep only one if needed and not more
--
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]