tanclary commented on code in PR #3589:
URL: https://github.com/apache/calcite/pull/3589#discussion_r1435829039


##########
core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java:
##########
@@ -3334,20 +3333,11 @@ private void registerOperandSubQueries(
   @Override public void validateLiteral(SqlLiteral literal) {
     switch (literal.getTypeName()) {
     case DECIMAL:
-      // Decimal and long have the same precision (as 64-bit integers), so
-      // the unscaled value of a decimal must fit into a long.
-
-      // REVIEW jvs 4-Aug-2004:  This should probably be calling over to
-      // the available calculator implementations to see what they
-      // support.  For now use ESP instead.
-      //
-      // jhyde 2006/12/21: I think the limits should be baked into the
-      // type system, not dependent on the calculator implementation.
+      final RelDataTypeSystem typeSystem = getTypeFactory().getTypeSystem();
+      final int precision = typeSystem.getMaxNumericPrecision();
       BigDecimal bd = literal.getValueAs(BigDecimal.class);
-      BigInteger unscaled = bd.unscaledValue();
-      long longValue = unscaled.longValue();
-      if (!BigInteger.valueOf(longValue).equals(unscaled)) {
-        // overflow
+      BigDecimal simpler = bd.stripTrailingZeros();

Review Comment:
   Some of the naming here confuses me, what does `simpler` mean? (in this 
context, haha).



##########
core/src/main/java/org/apache/calcite/adapter/enumerable/EnumUtils.java:
##########
@@ -406,11 +407,28 @@ public static Expression convert(Expression operand, Type 
fromType,
     if (toPrimitive != null) {
       if (fromPrimitive != null) {
         // E.g. from "float" to "double"
-        return Expressions.convert_(
+        if (toPrimitive == Primitive.BOOLEAN) {
+          return Expressions.convert_(
+              operand, toPrimitive.getPrimitiveClass());
+        }
+        return Expressions.convertChecked(
             operand, toPrimitive.getPrimitiveClass());
       }
-      if (fromNumber || fromBox == Primitive.CHAR) {
-        // Generate "x.shortValue()".
+      if (fromType == BigDecimal.class
+          && (toPrimitive == Primitive.INT

Review Comment:
   could add a method to the `Primitive` class similar to `isNumeric()` in case 
another check like this is needed in the future, also reduces `if` complexity.



##########
core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java:
##########
@@ -3334,20 +3333,11 @@ private void registerOperandSubQueries(
   @Override public void validateLiteral(SqlLiteral literal) {
     switch (literal.getTypeName()) {
     case DECIMAL:
-      // Decimal and long have the same precision (as 64-bit integers), so
-      // the unscaled value of a decimal must fit into a long.
-

Review Comment:
   is there a better comment to leave in its place or no?>



##########
core/src/main/java/org/apache/calcite/adapter/enumerable/EnumUtils.java:
##########
@@ -406,11 +407,28 @@ public static Expression convert(Expression operand, Type 
fromType,
     if (toPrimitive != null) {
       if (fromPrimitive != null) {
         // E.g. from "float" to "double"
-        return Expressions.convert_(
+        if (toPrimitive == Primitive.BOOLEAN) {

Review Comment:
   nit: third level of nested `if` might be a bit much, no worries if 
refactoring isn't simple/easy.



##########
linq4j/src/main/java/org/apache/calcite/linq4j/tree/UnaryExpression.java:
##########
@@ -51,6 +51,24 @@ public class UnaryExpression extends Expression {
         expression.accept(writer, nodeType.rprec, rprec);
       }
       return;
+    case ConvertChecked:
+      // This is ugly, but Java does not seem to have any facilities
+      // to perform checked cast between scalar types!
+      // So we use the existing linq4j Primitive.numberValue method
+      // which does overflow checking.
+      if (!writer.requireParentheses(this, lprec, rprec)) {
+        // Generate e.g.,
+        // ((Number)org.apache.calcite.linq4j.tree.Primitive.of(int.class)
+        //     .numberValue(literal_value)).intValue();
+        writer.append("((Number)")

Review Comment:
   Unfortunate you had to write this yourself.



-- 
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: commits-unsubscr...@calcite.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to