Uploaded image for project: 'Hive'
  1. Hive
  2. HIVE-5520

Use factory methods to instantiate HiveDecimal instead of constructors

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 0.11.0
    • 0.13.0
    • Types
    • None

    Description

      Currently HiveDecimal class provided a bunch of constructors that unfortunately also throws a runtime exception. For example,

       public HiveDecimal(BigInteger unscaled, int scale) {
          bd = this.normalize(new BigDecimal(unscaled, scale), MAX_PRECISION, false);
          if (bd == null) {
           throw new NumberFormatException("Assignment would result in truncation");
         }
      

      As a result, it's hard for the caller to detect error occurrences and the error handling is also complicated. In many cases, the error handling is omitted or missed. For instance,

               HiveDecimalWritable result = new HiveDecimalWritable(HiveDecimal.ZERO);
              try {
                result.set(aggregation.sum.divide(new HiveDecimal(aggregation.count)));
              } catch (NumberFormatException e) {
                result = null;
              }
      

      Throwing runtime exception while expecting caller to catch seems anti-pattern. In the case of constructor, factory class or methods seem more appropriate. With such a change, the apis are cleaner, and the error handling is simplified.

      Attachments

        1. HIVE-5520.1.patch
          39 kB
          Xuefu Zhang
        2. HIVE-5520.patch
          27 kB
          Xuefu Zhang

        Issue Links

          Activity

            People

              xuefuz Xuefu Zhang
              xuefuz Xuefu Zhang
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: