mihaibudiu commented on code in PR #3779:
URL: https://github.com/apache/calcite/pull/3779#discussion_r1596000032
##########
core/src/main/java/org/apache/calcite/sql/type/SqlTypeUtil.java:
##########
@@ -1822,6 +1823,35 @@ public static RelDataType
extractLastNFields(RelDataTypeFactory typeFactory,
type.getFieldList().subList(fieldsCnt - numToKeep, fieldsCnt));
}
+ /**
+ * Returns whether the decimal value can be represented without information
loss
+ * using the specified type.
+ * For example, 1111.11
+ * - cannot be represented exactly using DECIMAL(3, 1) since it overflows.
+ * - cannot be represented exactly using DECIMAL(6, 3) since it overflows.
+ * - cannot be represented exactly using DECIMAL(6, 1) since it requires
rounding.
+ * - can be represented exactly using DECIMAL(6, 2)
+ *
+ * @param value A decimal value
+ * @param toType A DECIMAL type.
+ * @return whether the value is valid for the type
+ */
+ public static boolean canBeRepresentedExactly(@Nullable BigDecimal value,
RelDataType toType) {
+ assert toType.getSqlTypeName() == SqlTypeName.DECIMAL;
+ if (value == null) {
+ return true;
+ }
+ value = value.stripTrailingZeros();
+ if (value.scale() < 0) {
Review Comment:
Frankly negative scales are something I don't understand well.
Many SQL dialects do not accept negative scales, but Calcite allegedly does,
although there aren't many tests about that.
Maybe we can file a separate issue about testing negative scales in Calcite,
I expect it may uncover quite a few bugs.
--
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]