Code review comment: Else throw in these ifs? Do you need to compare metadata? Why isn't it enough to compare `CalciteUtils.TIME.equals(toType)`? Would it be easier to read in `switch/case` format?

Code under review:
private Expression castOutput(Expression value, FieldType toType) {
    if (value.getType() == Object.class) {
      
      return value;
    } else if (toType.getTypeName().isDateType()
        && value.getType() instanceof Class
        && !Types.isAssignableFrom(ReadableInstant.class, (Class) value.getType())) {
      Expression valueDateTime = value;
      if (toType.getMetadata() == null) {
        if (value.getType() == java.sql.Timestamp.class) {
          valueDateTime = Expressions.call(BuiltInMethod.TIMESTAMP_TO_LONG.method, valueDateTime);
        }
      } else if (Arrays.equals(toType.getMetadata(), CalciteUtils.TIME.getMetadata())) {
        if (value.getType() == java.sql.Time.class) {
          valueDateTime = Expressions.call(BuiltInMethod.TIME_TO_INT.method, valueDateTime);
        }
      } else if (Arrays.equals(toType.getMetadata(), CalciteUtils.DATE.getMetadata())) {
        if (value.getType() == java.sql.Date.class) {
          valueDateTime = Expressions.call(BuiltInMethod.DATE_TO_INT.method, valueDateTime);
        }
        valueDateTime = Expressions.multiply(valueDateTime, Expressions.constant(MILLIS_PER_DAY));
      } else {
        throw new IllegalArgumentException(
            "Unknown DateTime type " + new String(toType.getMetadata(), UTF_8));
      }
      valueDateTime =
          Expressions.new_(
              DateTime.class,
              valueDateTime,
              Expressions.parameter(DateTimeZone.class, "org.joda.time.DateTimeZone.UTC"));

      if (((Class) value.getType()).isPrimitive()) {
        return valueDateTime;
      } else {
        return Expressions.condition(
            Expressions.equal(value, Expressions.constant(null)),
            Expressions.constant(null),
            valueDateTime);
      }
    } else if (toType.getTypeName() == TypeName.DECIMAL
        && value.getType() instanceof Class
        && !Types.isAssignableFrom(BigDecimal.class, (Class) value.getType())) {
      return Expressions.new_(BigDecimal.class, value);

    } else if (value.getType() instanceof Class
        && (((Class) value.getType()).isPrimitive()
            || Types.isAssignableFrom(Number.class, (Class) value.getType()))) {
      Type rawType = rawTypeMap.get(toType.getTypeName());
      if (rawType != null) {
        return Types.castIfNecessary(rawType, value);
      }
    }
    return value;
  }

Line commented on: if (value.getType() == java.sql.Timestamp.class) {

Revised code:
private Expression castOutput(Expression value, FieldType toType) {
    if (value.getType() == Object.class || !(value.getType() instanceof Class)) {
      
      return value;
    } else if (toType.getTypeName().isDateType()
        && !Types.isAssignableFrom(ReadableInstant.class, (Class) value.getType())) {
      return castOutputTime(value, toType);

    } else if (toType.getTypeName() == TypeName.DECIMAL
        && !Types.isAssignableFrom(BigDecimal.class, (Class) value.getType())) {
      return Expressions.new_(BigDecimal.class, value);

    } else if (((Class) value.getType()).isPrimitive()
        || Types.isAssignableFrom(Number.class, (Class) value.getType())) {
      Type rawType = rawTypeMap.get(toType.getTypeName());
      if (rawType != null) {
        return Types.castIfNecessary(rawType, value);
      }
    }
    return value;
  }