wasabii commented on code in PR #5276:
URL: https://github.com/apache/calcite/pull/5276#discussion_r4071601355


##########
core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java:
##########
@@ -5636,6 +5637,194 @@ public static BigDecimal toBigDecimal(Object o) {
         : toBigDecimal(o.toString());
   }
 
+  /** Converts a value to the SQL type {@code typeName}, as {@code CAST}
+   * does, when the type of the value is not known until run time.
+   *
+   * <p>Throws if the value cannot be converted, and for a target type that
+   * this method does not handle, so that a caller such as
+   * {@code JSON_VALUE} can apply its {@code ON ERROR} clause.
+   *
+   * @param value        Value to convert
+   * @param typeName     Type to convert it to; {@link SqlTypeName#ANY}
+   *                     returns the value unchanged
+   * @param precision    Precision of the target type, or negative
+   * @param scale        Scale of the target type, or negative
+   * @param roundingMode Rounding mode of the type system
+   */
+  static @Nullable Object cast(@Nullable Object value,
+      SqlTypeName typeName, int precision, int scale,
+      RoundingMode roundingMode) {
+    if (value == null || typeName == SqlTypeName.ANY) {
+      return value;
+    }
+    switch (typeName) {
+    case BOOLEAN:
+      return toBoolean(value);
+    case TINYINT:
+      return castToExact(value, Primitive.BYTE, roundingMode);
+    case SMALLINT:
+      return castToExact(value, Primitive.SHORT, roundingMode);
+    case INTEGER:
+      return castToExact(value, Primitive.INT, roundingMode);
+    case BIGINT:
+      return castToExact(value, Primitive.LONG, roundingMode);
+    case REAL:
+      return toFloat(value);
+    case FLOAT:
+    case DOUBLE:
+      return toDouble(value);
+    case DECIMAL:
+      return castToDecimal(value, precision, scale, roundingMode);
+    case CHAR:
+      return precision < 0 ? value.toString()
+          : truncateOrPad(value.toString(), precision);
+    case VARCHAR:
+      return precision < 0 ? value.toString()
+          : truncate(value.toString(), precision);
+    case DATE:
+      return DateTimeUtils.dateStringToUnixDate(charValue(value, typeName));
+    case TIME:
+      return (int) truncateFraction(

Review Comment:
   This is probably a legit bug. I have to dig into it more.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to