danny0405 commented on a change in pull request #1859: [CALCITE-3863] Make 
Truncate/Round return type inference overridable through rel data …
URL: https://github.com/apache/calcite/pull/1859#discussion_r396948372
 
 

 ##########
 File path: 
core/src/main/java/org/apache/calcite/rel/type/RelDataTypeSystem.java
 ##########
 @@ -405,4 +405,103 @@ default RelDataType 
deriveDecimalModType(RelDataTypeFactory typeFactory,
     return null;
   }
 
+  /**
+   * Infers the return type of a decimal truncate operation. Decimal truncate
+   * involves at least one decimal operand.
+   *
+   * <p>The default implementation is SQL:2003 compliant: the declared type of
+   * the result is the declared type of the first operand (decimal to be 
truncated).
+   *
+   * @see Glossary#SQL2003 SQL:2003 Part 2 Section 6.27
+   *
+   * <p>Rules:
+   *
+   * <ul>
+   * <li>Let p1, s1 be the precision and scale of the first operand</li>
+   * <li>Let s2 be the scale value to truncate</li>
+   * <li>Let p, s be the precision and scale of the result</li>
+   * <li>Let d be the number of whole digits in the result</li>
+   * <li>Then the result type is a decimal with:
+   *   <ul>
+   *   <li>s = s2</li>
+   *   <li>p = p1</li>
+   *   </ul>
+   * </li>
+   * <li>p and s are capped at their maximum values</li>
+   * </ul>
+   *
+   * @param typeFactory TypeFactory used to create output type
+   * @param type1       Type of the first operand
+   * @param scale2      Scale value to truncate to
+   * @return Result type for a decimal truncate
+   */
+  default RelDataType deriveDecimalTruncateType(RelDataTypeFactory typeFactory,
+      RelDataType type1, Integer scale2) {
+    if (SqlTypeUtil.isExactNumeric(type1)) {
+      if (SqlTypeUtil.isDecimal(type1)) {
+        // Java numeric will always have invalid precision/scale,
 
 Review comment:
   you just return a decimal with same precision and scale of `type1`, so why 
we need these methods in `RelDataTypeSystem`, just return the `type1` seems 
okey ?

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to