scarlin-cloudera commented on a change in pull request #970: Hive 23100
URL: https://github.com/apache/hive/pull/970#discussion_r405696536
 
 

 ##########
 File path: 
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/HiveCalciteUtil.java
 ##########
 @@ -1157,4 +1174,112 @@ public Void visitInputRef(RexInputRef inputRef) {
       return inputRefSet;
     }
   }
+
+  /** Fixes up the type of all {@link RexInputRef}s in an
+   * expression to match differences in nullability.
+   *
+   * <p>Throws if there any greater inconsistencies of type. */
+  public static List<RexNode> fixUp(final RexBuilder rexBuilder,
+      List<RexNode> nodes, final List<RelDataType> fieldTypes) {
+    return new FixNullabilityShuttle(rexBuilder, fieldTypes).apply(nodes);
+  }
+
+  /** Fixes up the type of all {@link RexInputRef}s in an
+   * expression to match differences in nullability.
+   *
+   * <p>Throws if there any greater inconsistencies of type. */
+  public static RexNode fixUp(final RexBuilder rexBuilder,
+      RexNode node, final List<RelDataType> fieldTypes) {
+    return new FixNullabilityShuttle(rexBuilder, fieldTypes).apply(node);
+  }
+
+  /** Shuttle that fixes up an expression to match changes in nullability of
+   * input fields. */
+  public static class FixNullabilityShuttle extends RexShuttle {
+    private final List<RelDataType> typeList;
+    private final RexBuilder rexBuilder;
+
+    public FixNullabilityShuttle(RexBuilder rexBuilder,
+          List<RelDataType> typeList) {
+      this.typeList = typeList;
+      this.rexBuilder = rexBuilder;
+    }
+
+    @Override public RexNode visitInputRef(RexInputRef ref) {
+      final RelDataType rightType = typeList.get(ref.getIndex());
+      final RelDataType refType = ref.getType();
+      if (refType == rightType) {
+        return ref;
+      }
+      final RelDataType refType2 =
+          rexBuilder.getTypeFactory().createTypeWithNullability(refType,
+              rightType.isNullable());
+      // This is a validation check which can become quite handy debugging type
+      // issues. Basically, we need both types to be equal, only difference 
should
+      // be nullability.
+      // However, we make an exception for Hive wrt CHAR type because Hive 
encodes
+      // the STRING type for literals within CHAR value (see {@link 
HiveNlsString})
+      // while Calcite always considers these literals to be a CHAR, which 
means
+      // that the reference may be created as a STRING or VARCHAR from AST node
+      // at parsing time but the actual type referenced is a CHAR.
+      if (refType2 == rightType) {
 
 Review comment:
   equals?

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to