vineetgarg02 commented on a change in pull request #970: Hive 23100
URL: https://github.com/apache/hive/pull/970#discussion_r407815212
 
 

 ##########
 File path: 
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/HiveCalciteUtil.java
 ##########
 @@ -1157,4 +1174,126 @@ 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> fixNullability(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 fixNullability(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) {
+        return new RexInputRef(ref.getIndex(), refType2);
+      } else if (refType2.getFamily() == SqlTypeFamily.CHARACTER &&
+          rightType.getSqlTypeName() == SqlTypeName.CHAR && 
!rightType.isNullable()) {
+        return new RexInputRef(ref.getIndex(), rightType);
+      }
+      throw new AssertionError("mismatched type " + ref + " " + rightType);
+    }
+  }
+
+  /**
+   * The method tries to rewrite an IN function call into an OR/AND function 
call.
+   * For instance:
+   * <pre>
+   * (c) IN ( v1, v2, ...) =&gt; c=v1 || c=v2 || ...
+   * </pre>
+   * Or:
+   * <pre>
+   * (c,d) IN ( (v1,v2), (v3,v4), ...) =&gt; (c=v1 && d=v2) || (c=v3 && d=v4) 
|| ...
+   * </pre>
+   *
+   * Returns null if the transformation fails, e.g., when non-deterministic
+   * calls are found in the expressions.
+   */
+  public static List<RexNode> transformIntoOrAndClause(List<RexNode> operands, 
RexBuilder rexBuilder) {
+    final List<RexNode> disjuncts = new ArrayList<>(operands.size() - 2);
 
 Review comment:
   Nit - This doesn't really convert IN expressions into OR but a list of 
expressions into OR.

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