coral-beam/src/main/java/com/linkedin/beam/utils/RexBeamUtils.java
public static Expression convertType(Expression expr, RelDataType targetType) {
    // Target type should be nullable (or immutable) to be generic.
    final Type targetJavaType = AvroJavaTypeFactory.AVRO_TYPE_FACTORY.getImmutableJavaClass(targetType);
    final Type exprType = expr.getType();

    if (exprType == targetJavaType || exprType == Types.unbox(targetJavaType)) {
      // Same type, no cast needed
      return expr;
    }

    if (targetType.getSqlTypeName().getFamily() == SqlTypeFamily.CHARACTER) {
      // Convert to String type
      return toJavaStringType(expr);
    }

    if (targetType.getSqlTypeName() == SqlTypeName.BINARY) {
      // Convert to binary type
      return Expressions.call(BeamExecUtil.class, MethodNames.TO_BYTE_BUFFER, toObjectType(expr));
    }

    Expression castExpr;
    if (targetType.getSqlTypeName().getFamily() == SqlTypeFamily.NUMERIC && expr.getType() == String.class) {
      // Convert from String to numbers
      castExpr = Expressions.call(targetJavaType, MethodNames.VALUE_OF, expr);
    } else {
      // All remaining cases, rely on Calcite casting expression
      castExpr = Types.castIfNecessary(targetJavaType, expr);
    }

    final Primitive fromPrimitive = Primitive.ofBox(expr.getType());
    final Primitive toPrimitive = Primitive.ofBox(targetJavaType);
    if (fromPrimitive != null && fromPrimitive.isNumeric() && toPrimitive != null && toPrimitive.isNumeric()) {
      // If casting from one numeric to another numeric we need to have null safe protection for avoid
      // NullPointerException when accessing the value of the expression.
      // Example casting var1 of type Integer to Long:
      // Long var2 = var1 != null ? var1.longValue() : null;
      final Expression nullSafeExpr = buildNullSafeExpr(ImmutableList.of(expr));
      return Expressions.condition(nullSafeExpr, castExpr, Expressions.constant(null));
    }
    return castExpr;
  }
/**
   * Casts an expression to a type specified by a rel data type.
   *
   * @param expr Expression to cast
   * @param targetType The target rel data type
   * @return Cast expression
   */
Casts an expression to a type specified by a rel data type.
@param expr Expression to cast
@param targetType The target rel data type
@return Cast expression
Casts an expression to a type specified by a rel data type . @param expr Expression to cast @param targetType The target rel data type @return Cast expression
public static Expression convertType(Expression expr, RelDataType targetType) {
    final Type targetJavaType = AvroJavaTypeFactory.AVRO_TYPE_FACTORY.getImmutableJavaClass(targetType);
    final Type exprType = expr.getType();
    if (exprType == targetJavaType || exprType == Types.unbox(targetJavaType)) {
      return expr;
    }
    if (targetType.getSqlTypeName().getFamily() == SqlTypeFamily.CHARACTER) {
      return toJavaStringType(expr);
    }
    if (targetType.getSqlTypeName() == SqlTypeName.BINARY) {
      return Expressions.call(BeamExecUtil.class, MethodNames.TO_BYTE_BUFFER, toObjectType(expr));
    }
    Expression castExpr;
    if (targetType.getSqlTypeName().getFamily() == SqlTypeFamily.NUMERIC && expr.getType() == String.class) {
      castExpr = Expressions.call(targetJavaType, MethodNames.VALUE_OF, expr);
    } else {
      castExpr = Types.castIfNecessary(targetJavaType, expr);
    }
    final Primitive fromPrimitive = Primitive.ofBox(expr.getType());
    final Primitive toPrimitive = Primitive.ofBox(targetJavaType);
    if (fromPrimitive != null && fromPrimitive.isNumeric() && toPrimitive != null && toPrimitive.isNumeric()) {
      final Expression nullSafeExpr = buildNullSafeExpr(ImmutableList.of(expr));
      return Expressions.condition(nullSafeExpr, castExpr, Expressions.constant(null));
    }
    return castExpr;
  }
public static Expression convertType ( Expression expr , RelDataType targetType ) { final Type targetJavaType = AvroJavaTypeFactory . AVRO_TYPE_FACTORY . getImmutableJavaClass ( targetType ) ; final Type exprType = expr . getType ( ) ; if ( exprType == targetJavaType || exprType == Types . unbox ( targetJavaType ) ) { return expr ; } if ( targetType . getSqlTypeName ( ) . getFamily ( ) == SqlTypeFamily . CHARACTER ) { return toJavaStringType ( expr ) ; } if ( targetType . getSqlTypeName ( ) == SqlTypeName . BINARY ) { return Expressions . call ( BeamExecUtil . class , MethodNames . TO_BYTE_BUFFER , toObjectType ( expr ) ) ; } Expression castExpr ; if ( targetType . getSqlTypeName ( ) . getFamily ( ) == SqlTypeFamily . NUMERIC && expr . getType ( ) == String . class ) { castExpr = Expressions . call ( targetJavaType , MethodNames . VALUE_OF , expr ) ; } else { castExpr = Types . castIfNecessary ( targetJavaType , expr ) ; } final Primitive fromPrimitive = Primitive . ofBox ( expr . getType ( ) ) ; final Primitive toPrimitive = Primitive . ofBox ( targetJavaType ) ; if ( fromPrimitive != null && fromPrimitive . isNumeric ( ) && toPrimitive != null && toPrimitive . isNumeric ( ) ) { final Expression nullSafeExpr = buildNullSafeExpr ( ImmutableList . of ( expr ) ) ; return Expressions . condition ( nullSafeExpr , castExpr , Expressions . constant ( null ) ) ; } return castExpr ; }
Casts an expression to a type specified by a rel data type.
Casts an expression to a type specified by a rel data type .
// Target type should be nullable (or immutable) to be generic. // Same type, no cast needed // Convert to String type // Convert to binary type // Convert from String to numbers // All remaining cases, rely on Calcite casting expression // If casting from one numeric to another numeric we need to have null safe protection for avoid // NullPointerException when accessing the value of the expression. // Example casting var1 of type Integer to Long: // Long var2 = var1 != null ? var1.longValue() : null;