public LogicalExpression visitCastExpression(CastExpression e, FunctionImplementationRegistry value) {
// if the cast is pointless, remove it.
LogicalExpression input = e.getInput().accept(this, value);
MajorType newMajor = e.getMajorType();
MinorType newMinor = input.getMajorType().getMinorType();
if (castEqual(e.getPosition(), newMajor, input.getMajorType())) {
return input; // don't do pointless cast.
}
if (newMinor == MinorType.LATE) {
// if the type still isn't fully bound, leave as cast expression.
return new CastExpression(input, e.getMajorType(), e.getPosition());
} else if (newMinor == MinorType.NULL) {
// if input is a NULL expression, remove cast expression and return a TypedNullConstant directly.
return new TypedNullConstant(Types.optional(e.getMajorType().getMinorType()));
} else {
// if the type is fully bound, convert to functioncall and materialze the function.
MajorType type = e.getMajorType();
// Get the cast function name from the map
String castFuncWithType = CastFunctions.getCastFunc(type.getMinorType());
List<LogicalExpression> newArgs = Lists.newArrayList();
newArgs.add(e.getInput()); //input_expr
//VarLen type
if (!Types.isFixedWidthType(type)) {
newArgs.add(new ValueExpressions.LongExpression(type.getWidth(), null));
} else if (type.getMinorType().name().startsWith("DECIMAL")) {
newArgs.add(new ValueExpressions.LongExpression(type.getPrecision(), null));
newArgs.add(new ValueExpressions.LongExpression(type.getScale(), null));
}
FunctionCall fc = new FunctionCall(castFuncWithType, newArgs, e.getPosition());
return fc.accept(this, value);
}
}