public CaseExpression() {
}
private static List<Expression> coerceIfNecessary(List<Expression> children) throws SQLException {
boolean isChildTypeUnknown = false;
PDataType returnType = children.get(0).getDataType();
for (int i = 2; i < children.size(); i+=2) {
Expression child = children.get(i);
PDataType childType = child.getDataType();
if (childType == null) {
isChildTypeUnknown = true;
} else if (returnType == null) {
returnType = childType;
isChildTypeUnknown = true;
} else if (returnType == childType || childType.isCoercibleTo(returnType)) {
continue;
} else if (returnType.isCoercibleTo(childType)) {
returnType = childType;
} else {
throw new SQLExceptionInfo.Builder(SQLExceptionCode.CANNOT_CONVERT_TYPE)
.setMessage("Case expressions must have common type: " + returnType + " cannot be coerced to " + childType)
.build().buildException();
}
}
// If we found an "unknown" child type and the return type is a number
// make the return type be the most general number type of DECIMAL.
if (isChildTypeUnknown && returnType != null && returnType.isCoercibleTo(PDataType.DECIMAL)) {
returnType = PDataType.DECIMAL;
}
List<Expression> newChildren = children;
for (int i = 0; i < children.size(); i+=2) {
Expression child = children.get(i);
PDataType childType = child.getDataType();
if (childType != returnType) {
if (newChildren == children) {
newChildren = new ArrayList<Expression>(children);
}
newChildren.set(i, CoerceExpression.create(child, returnType));