Github user paul-rogers commented on a diff in the pull request:

    https://github.com/apache/drill/pull/819#discussion_r113606681
  
    --- Diff: common/src/main/java/org/apache/drill/common/types/Types.java ---
    @@ -636,43 +658,63 @@ public static String toString(final MajorType type) {
     
       /**
        * Get the <code>precision</code> of given type.
    -   * @param majorType
    -   * @return
    +   *
    +   * @param majorType major type
    +   * @return precision value
        */
       public static int getPrecision(MajorType majorType) {
    -    MinorType type = majorType.getMinorType();
    -
    -    if (type == MinorType.VARBINARY || type == MinorType.VARCHAR) {
    -      return 65536;
    -    }
    -
         if (majorType.hasPrecision()) {
           return majorType.getPrecision();
         }
     
    -    return 0;
    +    return isScalarStringType(majorType) ? MAX_VARCHAR_LENGTH : UNDEFINED;
       }
     
       /**
        * Get the <code>scale</code> of given type.
    -   * @param majorType
    -   * @return
    +   *
    +   * @param majorType major type
    +   * @return scale value
        */
       public static int getScale(MajorType majorType) {
         if (majorType.hasScale()) {
           return majorType.getScale();
         }
     
    -    return 0;
    +    return UNDEFINED;
       }
     
       /**
    -   * Is the given type column be used in ORDER BY clause?
    -   * @param type
    -   * @return
    +   * Checks if the given type column be used in ORDER BY clause.
    +   *
    +   * @param type minor type
    +   * @return true if type can be used in ORDER BY clause
        */
       public static boolean isSortable(MinorType type) {
         // Currently only map and list columns are not sortable.
         return type != MinorType.MAP && type != MinorType.LIST;
       }
    +
    +  /**
    +   * Sets max precision from both types if these types are string scalar 
types.
    +   * Sets max precision and scale from both types if these types are 
decimal types.
    +   *
    +   * @param leftType type from left side
    +   * @param rightType type from right side
    +   * @param typeBuilder type builder
    +   * @return type builder
    +   */
    +  public static MajorType.Builder calculateTypePrecisionAndScale(MajorType 
leftType, MajorType rightType, MajorType.Builder typeBuilder) {
    +    boolean isScalarString = Types.isScalarStringType(leftType) && 
Types.isScalarStringType(rightType);
    +    boolean isDecimal = CoreDecimalUtility.isDecimalType(leftType);
    +
    +    if ((isScalarString || isDecimal) && leftType.hasPrecision() && 
rightType.hasPrecision()) {
    +      typeBuilder.setPrecision(Math.max(leftType.getPrecision(), 
rightType.getPrecision()));
    +    }
    +
    +    if (isDecimal && leftType.hasScale() && rightType.hasScale()) {
    +      typeBuilder.setScale(Math.max(leftType.getScale(), 
rightType.getScale()));
    --- End diff --
    
    Not sure this is right. From 
[here](https://docs.microsoft.com/en-us/sql/t-sql/data-types/precision-scale-and-length-transact-sql):
 "Precision is the number of digits in a number. Scale is the number of digits 
to the right of the decimal point in a number. For example, the number 123.45 
has a precision of 5 and a scale of 2."
    
    So, if we have:
    ```
    Decimal9(9,0)
    Decimal9(9,9)
    ```
    The result should be:
    ```
    Decimal(18,9)
    ```
    Or since this is out of range:
    ```
    Decimal18(9,9)
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to