paul8263 commented on a change in pull request #16740:
URL: https://github.com/apache/flink/pull/16740#discussion_r685726680



##########
File path: 
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/MathFunctionsITCase.java
##########
@@ -115,6 +115,14 @@
                                 $("f0").round(2),
                                 "ROUND(f0, 2)",
                                 new BigDecimal("12345.12"),
-                                DataTypes.DECIMAL(8, 2).notNull()));
+                                DataTypes.DECIMAL(8, 2).notNull()),
+                TestSpec.forFunction(BuiltInFunctionDefinitions.TRUNCATE)
+                        .onFieldsWithData(new BigDecimal("123.456"))
+                        // TRUNCATE(DECIMAL(6, 3) NOT NULL, 2) => DECIMAL(6, 
2) NOT NULL
+                        .testResult(
+                                $("f0").truncate(2),
+                                "TRUNCATE(f0, 2)",
+                                new BigDecimal("123.45"),
+                                DataTypes.DECIMAL(6, 2).notNull()));

Review comment:
       Hi @tsreaper ,
   Thank you for your advice. 
   In 
`flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/SqlFunctionUtils.java`
 there is an implementation of truncating method designed for `DecimalData`:
   ```
       public static DecimalData struncate(DecimalData b0, int b1) {
           if (b1 >= b0.scale()) {
               return b0;
           }
   
           BigDecimal b2 =
                   b0.toBigDecimal()
                           .movePointRight(b1)
                           .setScale(0, RoundingMode.DOWN)
                           .movePointLeft(b1);
           int p = b0.precision();
           int s = b0.scale();
   
           if (b1 < 0) {
               return DecimalData.fromBigDecimal(b2, Math.min(38, 1 + p - s), 
0);
           } else {
               return DecimalData.fromBigDecimal(b2, 1 + p - s + b1, b1);
           }
       }
   ```
   It uses the same logic as the round method.
   
   After I thought over this issue, I suggest that we should add 1 on precision 
(same logic as round method). If we did not do that, for example, given a 
number f1 0.333 with the type of DECIMAL(3, 3), if we call truncate(f1, 0), the 
precision of the result would be 0, which would trigger an exception of 
'Decimal precision must be between 1 and 38 (both inclusive)'.
   
   Those info above is my point of view. If you have any suggestion, please 
comment below. Thanks a lot.




-- 
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