deniskuzZ commented on code in PR #6878:
URL: https://github.com/apache/hive/pull/6878#discussion_r4294551129


##########
ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/TruncDecimal.java:
##########
@@ -45,26 +45,9 @@ public TruncDecimal(int colNum, int scale, int 
outputColumnNum) {
 
   @Override
   protected void trunc(ColumnVector inputColVector, ColumnVector 
outputColVector, int i) {
-    HiveDecimal input = ((DecimalColumnVector) 
inputColVector).vector[i].getHiveDecimal();
-
-    HiveDecimal output = trunc(input);
-    ((DecimalColumnVector) outputColVector).vector[i] = new 
HiveDecimalWritable(output);
-  }
-
-  protected HiveDecimal trunc(HiveDecimal input) {
-    HiveDecimal pow = HiveDecimal.create(Math.pow(10, Math.abs(scale)));
-
-    if (scale >= 0) {
-      if (scale != 0) {
-        long longValue = input.multiply(pow).longValue();
-        return HiveDecimal.create(longValue).divide(pow);
-      } else {
-        return HiveDecimal.create(input.longValue());
-      }
-    } else {
-      long longValue2 = input.divide(pow).longValue();
-      return HiveDecimal.create(longValue2).multiply(pow);
-    }
+    HiveDecimalWritable output = ((DecimalColumnVector) 
outputColVector).vector[i];

Review Comment:
   Fair points, thanks — agreed on keeping it allocation-free. To make the 
in-place semantics explicit and keep callers to one line, could the decimal 
overload take the output explicitly, like DecimalUtil.round(...) does?
   
   ````
   public static void trunc(HiveDecimalWritable input, int scale, 
HiveDecimalWritable output) {
     output.set(input);
     output.mutateSetScale(Math.max(scale, MIN_DECIMAL_SCALE), 
DECIMAL_ROUND_DOWN);
   }
   ````
   Then `TruncDecimal` is `TruncUtils.trunc(inV.vector[i], scale, 
outV.vector[i])` and `GenericUDFTrunc` is `TruncUtils.trunc(inputWritable, 
scale, decimalOutput)`, and the input writable is never mutated.



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to