Program: 111882
Project: 111882
Buggy source:
Expression translateCast(RelDataType sourceType,RelDataType targetType,Expression operand){
  Expression convert=null;
switch (targetType.getSqlTypeName()) {
case BOOLEAN:
switch (sourceType.getSqlTypeName()) {
case CHAR:
case VARCHAR:
      convert=Expressions.call(BuiltinMethod.STRING_TO_BOOLEAN.method,operand);
  }
break;
case CHAR:
case VARCHAR:
switch (sourceType.getSqlTypeName()) {
case DATE:
convert=RexImpTable.optimize2(operand,Expressions.call(BuiltinMethod.UNIX_DATE_TO_STRING.method,operand));
break;
case TIME:
convert=RexImpTable.optimize2(operand,Expressions.call(BuiltinMethod.UNIX_TIME_TO_STRING.method,operand));
break;
case TIMESTAMP:
convert=RexImpTable.optimize2(operand,Expressions.call(BuiltinMethod.UNIX_TIMESTAMP_TO_STRING.method,operand));
break;
case BOOLEAN:
convert=RexImpTable.optimize2(operand,Expressions.call(BuiltinMethod.BOOLEAN_TO_STRING.method,operand));
break;
}
}
if (convert == null) {
convert=convert(operand,typeFactory.getJavaClass(targetType));
}
switch (sourceType.getSqlTypeName()) {
case CHAR:
switch (targetType.getSqlTypeName()) {
case VARCHAR:
convert=Expressions.call(BuiltinMethod.TRIM.method,convert);
}
}
truncate: switch (targetType.getSqlTypeName()) {
case CHAR:
case VARCHAR:
final int targetPrecision=targetType.getPrecision();
if (targetPrecision >= 0) {
switch (sourceType.getSqlTypeName()) {
case CHAR:
case VARCHAR:
final int sourcePrecision=sourceType.getPrecision();
if (sourcePrecision >= 0 && sourcePrecision <= targetPrecision) {
break truncate;
}
default :
convert=Expressions.call(BuiltinMethod.TRUNCATE.method,convert,Expressions.constant(targetPrecision));
}
}
break;
case TIMESTAMP:
int targetScale=targetType.getScale();
if (targetScale == RelDataType.SCALE_NOT_SPECIFIED) {
targetScale=0;
}
if (targetScale < sourceType.getScale()) {
convert=Expressions.call(BuiltinMethod.ROUND_LONG.method,convert,Expressions.constant((long)Math.pow(10,3 - targetScale)));
}
break;
}
return convert;
}

Fixed version (ground truth):
Expression translateCast(RelDataType sourceType,RelDataType targetType,Expression operand){
  Expression convert=null;
switch (targetType.getSqlTypeName()) {
case BOOLEAN:
switch (sourceType.getSqlTypeName()) {
case CHAR:
case VARCHAR:
      convert=Expressions.call(BuiltinMethod.STRING_TO_BOOLEAN.method,operand);
  }
break;
case CHAR:
case VARCHAR:
switch (sourceType.getSqlTypeName()) {
case DATE:
convert=RexImpTable.optimize2(operand,Expressions.call(BuiltinMethod.UNIX_DATE_TO_STRING.method,operand));
break;
case TIME:
convert=RexImpTable.optimize2(operand,Expressions.call(BuiltinMethod.UNIX_TIME_TO_STRING.method,operand));
break;
case TIMESTAMP:
convert=RexImpTable.optimize2(operand,Expressions.call(BuiltinMethod.UNIX_TIMESTAMP_TO_STRING.method,operand));
break;
case BOOLEAN:
convert=RexImpTable.optimize2(operand,Expressions.call(BuiltinMethod.BOOLEAN_TO_STRING.method,operand));
break;
}
}
if (convert == null) {
convert=convert(operand,typeFactory.getJavaClass(targetType));
}
switch (sourceType.getSqlTypeName()) {
case CHAR:
switch (targetType.getSqlTypeName()) {
case VARCHAR:
convert=Expressions.call(BuiltinMethod.TRIM.method,convert);
}
}
truncate: switch (targetType.getSqlTypeName()) {
case CHAR:
case VARCHAR:
final int targetPrecision=targetType.getPrecision();
if (targetPrecision >= 0) {
switch (sourceType.getSqlTypeName()) {
case CHAR:
case VARCHAR:
final int sourcePrecision=sourceType.getPrecision();
if (sourcePrecision < 0 || sourcePrecision >= 0 && sourcePrecision <= targetPrecision) {
break truncate;
}
default :
convert=Expressions.call(BuiltinMethod.TRUNCATE.method,convert,Expressions.constant(targetPrecision));
}
}
break;
case TIMESTAMP:
int targetScale=targetType.getScale();
if (targetScale == RelDataType.SCALE_NOT_SPECIFIED) {
targetScale=0;
}
if (targetScale < sourceType.getScale()) {
convert=Expressions.call(BuiltinMethod.ROUND_LONG.method,convert,Expressions.constant((long)Math.pow(10,3 - targetScale)));
}
break;
}
return convert;
}