You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by "zstan (via GitHub)" <gi...@apache.org> on 2023/08/02 07:32:39 UTC

[GitHub] [ignite-3] zstan opened a new pull request, #2396: IGNITE-19877 Sql. Fix CAST(n AS DECIMAL(precision, scale)) behaviour for scale > precision issues

zstan opened a new pull request, #2396:
URL: https://github.com/apache/ignite-3/pull/2396

   **SELECT CAST (0.0001100011 AS DECIMAL(3, 6));** correct output : **0.000110**
   **SELECT CAST (0.0001110011 AS DECIMAL(2, 6));** not applicable cause after trimming by **scale** : 0.000111 - 3 digits are involved


-- 
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-unsubscribe@ignite.apache.org

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


[GitHub] [ignite-3] zstan commented on a diff in pull request #2396: IGNITE-19947 Sql. Fix CAST(n AS DECIMAL(precision, scale)) behaviour for scale > precision issues

Posted by "zstan (via GitHub)" <gi...@apache.org>.
zstan commented on code in PR #2396:
URL: https://github.com/apache/ignite-3/pull/2396#discussion_r1286694993


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/exp/IgniteSqlFunctions.java:
##########
@@ -246,7 +234,86 @@ public static BigDecimal convertDecimal(BigDecimal value, int precision, int sca
             }
         }
 
-        return value.setScale(scale, RoundingMode.HALF_UP);
+        return dec.setScale(scale, RoundingMode.HALF_UP);
+    }
+
+    /** Check precision scale for fraction numbers. */
+    private static boolean checkPrecisionScaleFractionPart(Number num, int precision, int scale) {
+        if (num.longValue() != 0) {
+            return false;
+        }
+
+        if (!(num instanceof Double) && !(num instanceof Float) && !(num instanceof BigDecimal)) {
+            return false;
+        }
+
+        boolean canProcess = true;
+
+        if (num instanceof Double) {
+            Double num0 = (Double) num;
+            canProcess = !num0.isInfinite() && !num0.isNaN();
+        }
+
+        if (num instanceof Float) {
+            Float num0 = (Float) num;
+            canProcess = !num0.isInfinite() && !num0.isNaN();
+        }
+
+        if (canProcess) {
+            int lastSignificantDigit = 0;
+
+            String strRepr = num.toString();
+            int pos = strRepr.indexOf('.');
+
+            if (pos == -1) {
+                return false;
+            }
+
+            // fractional part
+            strRepr = strRepr.substring(pos + 1);
+
+            // length of fractional part
+            int processingDigits = strRepr.length();
+
+            // cut if scale is less than processing digits
+            if (scale < processingDigits) {
+                strRepr = strRepr.substring(0, scale);
+            }
+
+            // calculate overall processing digits
+            int digit = scale > processingDigits ? scale - processingDigits : 0;
+            for (int i = strRepr.length() - 1; i >= 0; i--) {
+                digit++;
+                if (strRepr.charAt(i) != '0') {
+                    lastSignificantDigit = digit;
+                }
+            }
+
+            if (lastSignificantDigit > precision) {

Review Comment:
   not suitable ... (0.0101) BigDeciman.ulp().scale() == 4, but 3 is avoid here ..



-- 
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-unsubscribe@ignite.apache.org

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


[GitHub] [ignite-3] lowka commented on a diff in pull request #2396: IGNITE-19947 Sql. Fix CAST(n AS DECIMAL(precision, scale)) behaviour for scale > precision issues

Posted by "lowka (via GitHub)" <gi...@apache.org>.
lowka commented on code in PR #2396:
URL: https://github.com/apache/ignite-3/pull/2396#discussion_r1285450582


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/exp/IgniteSqlFunctions.java:
##########
@@ -213,31 +191,38 @@ public static BigDecimal toBigDecimal(Object o, int precision, int scale) {
         }
 
         return o instanceof Number ? toBigDecimal((Number) o, precision, scale)
-               : toBigDecimal(o.toString(), precision, scale);
+                : 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) {
+    /** Convert and validate input. */
+    public static BigDecimal toBigDecimal(Number value, int precision, int scale) {

Review Comment:
   What is wrong with existing comment?



-- 
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-unsubscribe@ignite.apache.org

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


[GitHub] [ignite-3] zstan commented on a diff in pull request #2396: IGNITE-19947 Sql. Fix CAST(n AS DECIMAL(precision, scale)) behaviour for scale > precision issues

Posted by "zstan (via GitHub)" <gi...@apache.org>.
zstan commented on code in PR #2396:
URL: https://github.com/apache/ignite-3/pull/2396#discussion_r1285475218


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/exp/IgniteSqlFunctions.java:
##########
@@ -213,31 +191,38 @@ public static BigDecimal toBigDecimal(Object o, int precision, int scale) {
         }
 
         return o instanceof Number ? toBigDecimal((Number) o, precision, scale)
-               : toBigDecimal(o.toString(), precision, scale);
+                : 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) {
+    /** Convert and validate input. */
+    public static BigDecimal toBigDecimal(Number value, int precision, int scale) {

Review Comment:
   fixed



-- 
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-unsubscribe@ignite.apache.org

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


[GitHub] [ignite-3] AMashenkov commented on a diff in pull request #2396: IGNITE-19947 Sql. Fix CAST(n AS DECIMAL(precision, scale)) behaviour for scale > precision issues

Posted by "AMashenkov (via GitHub)" <gi...@apache.org>.
AMashenkov commented on code in PR #2396:
URL: https://github.com/apache/ignite-3/pull/2396#discussion_r1286911260


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/exp/IgniteSqlFunctions.java:
##########
@@ -246,7 +234,86 @@ public static BigDecimal convertDecimal(BigDecimal value, int precision, int sca
             }
         }
 
-        return value.setScale(scale, RoundingMode.HALF_UP);
+        return dec.setScale(scale, RoundingMode.HALF_UP);
+    }
+
+    /** Check precision scale for fraction numbers. */
+    private static boolean checkPrecisionScaleFractionPart(Number num, int precision, int scale) {
+        if (num.longValue() != 0) {
+            return false;
+        }
+
+        if (!(num instanceof Double) && !(num instanceof Float) && !(num instanceof BigDecimal)) {
+            return false;
+        }
+
+        boolean canProcess = true;
+
+        if (num instanceof Double) {
+            Double num0 = (Double) num;
+            canProcess = !num0.isInfinite() && !num0.isNaN();
+        }
+
+        if (num instanceof Float) {
+            Float num0 = (Float) num;
+            canProcess = !num0.isInfinite() && !num0.isNaN();
+        }
+
+        if (canProcess) {
+            int lastSignificantDigit = 0;
+
+            String strRepr = num.toString();
+            int pos = strRepr.indexOf('.');
+
+            if (pos == -1) {
+                return false;
+            }
+
+            // fractional part
+            strRepr = strRepr.substring(pos + 1);
+
+            // length of fractional part
+            int processingDigits = strRepr.length();
+
+            // cut if scale is less than processing digits
+            if (scale < processingDigits) {
+                strRepr = strRepr.substring(0, scale);
+            }

Review Comment:
   Let's avoid new string creation.



-- 
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-unsubscribe@ignite.apache.org

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


[GitHub] [ignite-3] zstan commented on a diff in pull request #2396: IGNITE-19947 Sql. Fix CAST(n AS DECIMAL(precision, scale)) behaviour for scale > precision issues

Posted by "zstan (via GitHub)" <gi...@apache.org>.
zstan commented on code in PR #2396:
URL: https://github.com/apache/ignite-3/pull/2396#discussion_r1287246945


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/exp/IgniteSqlFunctions.java:
##########
@@ -246,7 +234,86 @@ public static BigDecimal convertDecimal(BigDecimal value, int precision, int sca
             }
         }
 
-        return value.setScale(scale, RoundingMode.HALF_UP);
+        return dec.setScale(scale, RoundingMode.HALF_UP);
+    }
+
+    /** Check precision scale for fraction numbers. */
+    private static boolean checkPrecisionScaleFractionPart(Number num, int precision, int scale) {
+        if (num.longValue() != 0) {
+            return false;
+        }
+
+        if (!(num instanceof Double) && !(num instanceof Float) && !(num instanceof BigDecimal)) {
+            return false;
+        }
+
+        boolean canProcess = true;
+
+        if (num instanceof Double) {
+            Double num0 = (Double) num;
+            canProcess = !num0.isInfinite() && !num0.isNaN();
+        }
+
+        if (num instanceof Float) {
+            Float num0 = (Float) num;
+            canProcess = !num0.isInfinite() && !num0.isNaN();
+        }
+
+        if (canProcess) {
+            int lastSignificantDigit = 0;
+
+            String strRepr = num.toString();
+            int pos = strRepr.indexOf('.');
+
+            if (pos == -1) {
+                return false;
+            }
+
+            // fractional part
+            strRepr = strRepr.substring(pos + 1);
+
+            // length of fractional part
+            int processingDigits = strRepr.length();
+
+            // cut if scale is less than processing digits
+            if (scale < processingDigits) {
+                strRepr = strRepr.substring(0, scale);
+            }

Review Comment:
   fixed



-- 
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-unsubscribe@ignite.apache.org

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


[GitHub] [ignite-3] AMashenkov commented on a diff in pull request #2396: IGNITE-19947 Sql. Fix CAST(n AS DECIMAL(precision, scale)) behaviour for scale > precision issues

Posted by "AMashenkov (via GitHub)" <gi...@apache.org>.
AMashenkov commented on code in PR #2396:
URL: https://github.com/apache/ignite-3/pull/2396#discussion_r1288214793


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/exp/IgniteSqlFunctions.java:
##########
@@ -213,40 +192,90 @@ public static BigDecimal toBigDecimal(Object o, int precision, int scale) {
         }
 
         return o instanceof Number ? toBigDecimal((Number) o, precision, scale)
-               : toBigDecimal(o.toString(), precision, scale);
+                : toBigDecimal(o.toString(), precision, scale);
     }
 
     /**
-     * Converts the given {@code BigDecimal} to a decimal with the given {@code precision} and {@code scale}
+     * Converts the given {@code Number} 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) {
+    public static BigDecimal toBigDecimal(Number value, int precision, int scale) {
         assert precision > 0 : "Invalid precision: " + precision;
 
+        if (value == null) {
+            return null;
+        }
+
         int defaultPrecision = IgniteTypeSystem.INSTANCE.getDefaultPrecision(SqlTypeName.DECIMAL);
+
         if (precision == defaultPrecision) {
+            BigDecimal dec = convertToBigDecimal(value);
             // 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;
+            return dec;
         }
 
-        boolean nonZero = !value.unscaledValue().equals(BigInteger.ZERO);
+        BigDecimal dec = processFractionData(value, precision, scale);
 
-        if (nonZero) {
-            if (scale > precision) {
-                throw new SqlException(RUNTIME_ERR, NUMERIC_FIELD_OVERFLOW_ERROR);
-            } else {
-                int currentSignificantDigits = value.precision() - value.scale();
-                int expectedSignificantDigits = precision - scale;
+        if (dec != null) {
+            return dec;
+        } else {
+            dec = convertToBigDecimal(value);
+        }

Review Comment:
   Actually, there are 2 cases:
   numbers, which contains fractional part only, (when `value.longValue() == 0`)
   and numbers with integral part.
   ```suggestion
           if (value.longValue() == 0) {
               return processFractionalValue(...);
           } else {
               return processValueWithIntegralPart(..);
           }
   ```



-- 
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-unsubscribe@ignite.apache.org

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


[GitHub] [ignite-3] AMashenkov commented on a diff in pull request #2396: IGNITE-19947 Sql. Fix CAST(n AS DECIMAL(precision, scale)) behaviour for scale > precision issues

Posted by "AMashenkov (via GitHub)" <gi...@apache.org>.
AMashenkov commented on code in PR #2396:
URL: https://github.com/apache/ignite-3/pull/2396#discussion_r1285838232


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/exp/IgniteSqlFunctions.java:
##########
@@ -246,7 +234,86 @@ public static BigDecimal convertDecimal(BigDecimal value, int precision, int sca
             }
         }
 
-        return value.setScale(scale, RoundingMode.HALF_UP);
+        return dec.setScale(scale, RoundingMode.HALF_UP);
+    }
+
+    /** Check precision scale for fraction numbers. */
+    private static boolean checkPrecisionScaleFractionPart(Number num, int precision, int scale) {
+        if (num.longValue() != 0) {
+            return false;
+        }
+
+        if (!(num instanceof Double) && !(num instanceof Float) && !(num instanceof BigDecimal)) {
+            return false;
+        }
+
+        boolean canProcess = true;
+
+        if (num instanceof Double) {
+            Double num0 = (Double) num;
+            canProcess = !num0.isInfinite() && !num0.isNaN();
+        }
+
+        if (num instanceof Float) {
+            Float num0 = (Float) num;
+            canProcess = !num0.isInfinite() && !num0.isNaN();
+        }
+
+        if (canProcess) {
+            int lastSignificantDigit = 0;
+
+            String strRepr = num.toString();
+            int pos = strRepr.indexOf('.');
+
+            if (pos == -1) {
+                return false;
+            }
+
+            // fractional part
+            strRepr = strRepr.substring(pos + 1);
+
+            // length of fractional part
+            int processingDigits = strRepr.length();
+
+            // cut if scale is less than processing digits
+            if (scale < processingDigits) {
+                strRepr = strRepr.substring(0, scale);
+            }
+
+            // calculate overall processing digits
+            int digit = scale > processingDigits ? scale - processingDigits : 0;
+            for (int i = strRepr.length() - 1; i >= 0; i--) {
+                digit++;
+                if (strRepr.charAt(i) != '0') {
+                    lastSignificantDigit = digit;
+                }
+            }
+
+            if (lastSignificantDigit > precision) {

Review Comment:
   Does `lastSignificant` value equals to BigDeciman.ulp().scale() ?



-- 
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-unsubscribe@ignite.apache.org

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


[GitHub] [ignite-3] zstan commented on a diff in pull request #2396: IGNITE-19947 Sql. Fix CAST(n AS DECIMAL(precision, scale)) behaviour for scale > precision issues

Posted by "zstan (via GitHub)" <gi...@apache.org>.
zstan commented on code in PR #2396:
URL: https://github.com/apache/ignite-3/pull/2396#discussion_r1286679522


##########
modules/runner/src/integrationTest/java/org/apache/ignite/internal/sql/engine/ItDataTypesTest.java:
##########
@@ -371,8 +372,7 @@ private static Stream<Arguments> decimalCastFromLiterals() {
                 // TODO Uncomment these test cases after https://issues.apache.org/jira/browse/IGNITE-19822 is fixed.
                 arguments(CaseStatus.SKIP, numeric, "100.12", decimalType(5, 1), bigDecimalVal("100.1")),

Review Comment:
   done



-- 
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-unsubscribe@ignite.apache.org

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


[GitHub] [ignite-3] AMashenkov commented on a diff in pull request #2396: IGNITE-19947 Sql. Fix CAST(n AS DECIMAL(precision, scale)) behaviour for scale > precision issues

Posted by "AMashenkov (via GitHub)" <gi...@apache.org>.
AMashenkov commented on code in PR #2396:
URL: https://github.com/apache/ignite-3/pull/2396#discussion_r1285822349


##########
modules/runner/src/integrationTest/java/org/apache/ignite/internal/sql/engine/ItDataTypesTest.java:
##########
@@ -371,8 +372,7 @@ private static Stream<Arguments> decimalCastFromLiterals() {
                 // TODO Uncomment these test cases after https://issues.apache.org/jira/browse/IGNITE-19822 is fixed.
                 arguments(CaseStatus.SKIP, numeric, "100.12", decimalType(5, 1), bigDecimalVal("100.1")),

Review Comment:
   Let's also validate RoundingMode, it we don't have such tests.
   `arguments(CaseStatus.SKIP, numeric, "100.15", decimalType(5, 1), bigDecimalVal("100.2")),`



-- 
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-unsubscribe@ignite.apache.org

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


[GitHub] [ignite-3] zstan commented on a diff in pull request #2396: IGNITE-19947 Sql. Fix CAST(n AS DECIMAL(precision, scale)) behaviour for scale > precision issues

Posted by "zstan (via GitHub)" <gi...@apache.org>.
zstan commented on code in PR #2396:
URL: https://github.com/apache/ignite-3/pull/2396#discussion_r1286694993


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/exp/IgniteSqlFunctions.java:
##########
@@ -246,7 +234,86 @@ public static BigDecimal convertDecimal(BigDecimal value, int precision, int sca
             }
         }
 
-        return value.setScale(scale, RoundingMode.HALF_UP);
+        return dec.setScale(scale, RoundingMode.HALF_UP);
+    }
+
+    /** Check precision scale for fraction numbers. */
+    private static boolean checkPrecisionScaleFractionPart(Number num, int precision, int scale) {
+        if (num.longValue() != 0) {
+            return false;
+        }
+
+        if (!(num instanceof Double) && !(num instanceof Float) && !(num instanceof BigDecimal)) {
+            return false;
+        }
+
+        boolean canProcess = true;
+
+        if (num instanceof Double) {
+            Double num0 = (Double) num;
+            canProcess = !num0.isInfinite() && !num0.isNaN();
+        }
+
+        if (num instanceof Float) {
+            Float num0 = (Float) num;
+            canProcess = !num0.isInfinite() && !num0.isNaN();
+        }
+
+        if (canProcess) {
+            int lastSignificantDigit = 0;
+
+            String strRepr = num.toString();
+            int pos = strRepr.indexOf('.');
+
+            if (pos == -1) {
+                return false;
+            }
+
+            // fractional part
+            strRepr = strRepr.substring(pos + 1);
+
+            // length of fractional part
+            int processingDigits = strRepr.length();
+
+            // cut if scale is less than processing digits
+            if (scale < processingDigits) {
+                strRepr = strRepr.substring(0, scale);
+            }
+
+            // calculate overall processing digits
+            int digit = scale > processingDigits ? scale - processingDigits : 0;
+            for (int i = strRepr.length() - 1; i >= 0; i--) {
+                digit++;
+                if (strRepr.charAt(i) != '0') {
+                    lastSignificantDigit = digit;
+                }
+            }
+
+            if (lastSignificantDigit > precision) {

Review Comment:
   not suitable ... (0.0101) BigDeciman.ulp().scale() == 4, but 3 is expect here ..



-- 
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-unsubscribe@ignite.apache.org

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


[GitHub] [ignite-3] AMashenkov commented on a diff in pull request #2396: IGNITE-19947 Sql. Fix CAST(n AS DECIMAL(precision, scale)) behaviour for scale > precision issues

Posted by "AMashenkov (via GitHub)" <gi...@apache.org>.
AMashenkov commented on code in PR #2396:
URL: https://github.com/apache/ignite-3/pull/2396#discussion_r1288215887


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/exp/IgniteSqlFunctions.java:
##########
@@ -213,40 +192,90 @@ public static BigDecimal toBigDecimal(Object o, int precision, int scale) {
         }
 
         return o instanceof Number ? toBigDecimal((Number) o, precision, scale)
-               : toBigDecimal(o.toString(), precision, scale);
+                : toBigDecimal(o.toString(), precision, scale);
     }
 
     /**
-     * Converts the given {@code BigDecimal} to a decimal with the given {@code precision} and {@code scale}
+     * Converts the given {@code Number} 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) {
+    public static BigDecimal toBigDecimal(Number value, int precision, int scale) {
         assert precision > 0 : "Invalid precision: " + precision;
 
+        if (value == null) {
+            return null;
+        }
+
         int defaultPrecision = IgniteTypeSystem.INSTANCE.getDefaultPrecision(SqlTypeName.DECIMAL);
+
         if (precision == defaultPrecision) {
+            BigDecimal dec = convertToBigDecimal(value);
             // 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;
+            return dec;
         }
 
-        boolean nonZero = !value.unscaledValue().equals(BigInteger.ZERO);
+        BigDecimal dec = processFractionData(value, precision, scale);
 
-        if (nonZero) {
-            if (scale > precision) {
-                throw new SqlException(RUNTIME_ERR, NUMERIC_FIELD_OVERFLOW_ERROR);
-            } else {
-                int currentSignificantDigits = value.precision() - value.scale();
-                int expectedSignificantDigits = precision - scale;
+        if (dec != null) {
+            return dec;
+        } else {
+            dec = convertToBigDecimal(value);
+        }
 
-                if (currentSignificantDigits > expectedSignificantDigits) {
-                    throw new SqlException(RUNTIME_ERR, NUMERIC_FIELD_OVERFLOW_ERROR);
-                }
+        if (scale > precision) {
+            throw new SqlException(RUNTIME_ERR, NUMERIC_FIELD_OVERFLOW_ERROR);
+        } else {
+            int currentSignificantDigits = dec.precision() - dec.scale();
+            int expectedSignificantDigits = precision - scale;
+
+            if (currentSignificantDigits > expectedSignificantDigits) {
+                throw new SqlException(RUNTIME_ERR, NUMERIC_FIELD_OVERFLOW_ERROR);
             }
         }
 
-        return value.setScale(scale, RoundingMode.HALF_UP);
+        return dec.setScale(scale, roundingMode);
+    }
+
+    private static @Nullable BigDecimal processFractionData(Number value, int precision, int scale) {
+        if (value.longValue() != 0) {
+            return null;
+        }

Review Comment:
   Let's move this check to outside of this method.



-- 
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-unsubscribe@ignite.apache.org

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


[GitHub] [ignite-3] zstan merged pull request #2396: IGNITE-19947 Sql. Fix CAST(n AS DECIMAL(precision, scale)) behaviour for scale > precision issues

Posted by "zstan (via GitHub)" <gi...@apache.org>.
zstan merged PR #2396:
URL: https://github.com/apache/ignite-3/pull/2396


-- 
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-unsubscribe@ignite.apache.org

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


[GitHub] [ignite-3] AMashenkov commented on a diff in pull request #2396: IGNITE-19947 Sql. Fix CAST(n AS DECIMAL(precision, scale)) behaviour for scale > precision issues

Posted by "AMashenkov (via GitHub)" <gi...@apache.org>.
AMashenkov commented on code in PR #2396:
URL: https://github.com/apache/ignite-3/pull/2396#discussion_r1285822349


##########
modules/runner/src/integrationTest/java/org/apache/ignite/internal/sql/engine/ItDataTypesTest.java:
##########
@@ -371,8 +372,7 @@ private static Stream<Arguments> decimalCastFromLiterals() {
                 // TODO Uncomment these test cases after https://issues.apache.org/jira/browse/IGNITE-19822 is fixed.
                 arguments(CaseStatus.SKIP, numeric, "100.12", decimalType(5, 1), bigDecimalVal("100.1")),

Review Comment:
   Let's also validate RoundingMode.
   `arguments(CaseStatus.SKIP, numeric, "100.15", decimalType(5, 1), bigDecimalVal("100.2")),`



-- 
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-unsubscribe@ignite.apache.org

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