lowka commented on code in PR #2220: URL: https://github.com/apache/ignite-3/pull/2220#discussion_r1245208804
########## modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/exp/IgniteSqlFunctions.java: ########## @@ -196,10 +198,42 @@ public static BigDecimal toBigDecimal(Object o, int precision, int scale) { if (o instanceof Boolean) { throw new UnsupportedOperationException(); + } else if (o instanceof Number) { + return toBigDecimal((Number) o, precision, scale); + } else { + return toBigDecimal(o.toString(), precision, scale); + } + } + + /** + * Converts the given {@code BigDecimal} to a decimal with the given {@code precision} and {@code scale} + * according to SQL spec for CAST specification: General Rules, 8. + */ + public static BigDecimal convertDecimal(BigDecimal value, int precision, int scale) { + assert precision > 0 : "Invalid precision: " + precision; + + int defaultPrecision = IgniteTypeSystem.INSTANCE.getDefaultPrecision(SqlTypeName.DECIMAL); + if (precision == defaultPrecision) { + // This branch covers at least one known case: access to dynamic parameter from context. + // In this scenario precision = DefaultTypePrecision, because types for dynamic params + // are created by toSql(createType(param.class)). + return value; + } + + boolean nonZero = !value.unscaledValue().equals(BigInteger.ZERO); + + if (nonZero && scale > precision) { + throw new SqlException(QUERY_INVALID_ERR, "Numeric overflow"); Review Comment: @zstan I looked at PostgeSQL and it returns the same error in both cases. -- 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: notifications-unsubscr...@ignite.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org