You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@orc.apache.org by do...@apache.org on 2021/08/16 16:02:30 UTC

[orc] branch main updated: ORC-834: Do Not Convert to String in DecimalFromTimestampTreeReader (#743)

This is an automated email from the ASF dual-hosted git repository.

dongjoon pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/orc.git


The following commit(s) were added to refs/heads/main by this push:
     new be3eacd  ORC-834: Do Not Convert to String in DecimalFromTimestampTreeReader (#743)
be3eacd is described below

commit be3eacd53f60a3459a32345dae27de096db8a2a7
Author: belugabehr <12...@users.noreply.github.com>
AuthorDate: Mon Aug 16 12:00:55 2021 -0400

    ORC-834: Do Not Convert to String in DecimalFromTimestampTreeReader (#743)
    
    ### What changes were proposed in this pull request?
    
    When converting Timestamp to Decimal type, do not use String operations.
    
    ### Why are the changes needed?
    
    Performance.
    
    A quick test (hardly definitive, but indicative) shows an order of magnitude (10x) performance improvement:
    
    ```java
        long t1 = System.nanoTime();
        long r = 0;
        for (long i = -100_000L; i < 100_000; i++) {
          BigDecimal secondsBd = new BigDecimal(i);
          BigDecimal nanosBd = new BigDecimal(i).movePointLeft(9);
          BigDecimal resultBd = (i >= 0L) ? secondsBd.add(nanosBd) : secondsBd.subtract(nanosBd);
          r += resultBd.longValue();
        }
        long t2 = System.nanoTime();
    
        System.out.println(r);
        System.out.println(t2 - t1);
    ```
    
    ### How was this patch tested?
    
    No changes to functionality. Use existing unit tests.
---
 .../core/src/java/org/apache/orc/impl/ConvertTreeReaderFactory.java | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/java/core/src/java/org/apache/orc/impl/ConvertTreeReaderFactory.java b/java/core/src/java/org/apache/orc/impl/ConvertTreeReaderFactory.java
index 8f9d02e..e15011f 100644
--- a/java/core/src/java/org/apache/orc/impl/ConvertTreeReaderFactory.java
+++ b/java/core/src/java/org/apache/orc/impl/ConvertTreeReaderFactory.java
@@ -38,6 +38,7 @@ import org.apache.orc.impl.reader.tree.TypeReader;
 import org.threeten.extra.chrono.HybridChronology;
 
 import java.io.IOException;
+import java.math.BigDecimal;
 import java.nio.charset.StandardCharsets;
 import java.sql.Timestamp;
 import java.time.Instant;
@@ -962,7 +963,10 @@ public class ConvertTreeReaderFactory extends TreeReaderFactory {
         seconds += 1;
         nanos = 1_000_000_000 - nanos;
       }
-      HiveDecimal value = HiveDecimal.create(String.format("%d.%09d", seconds, nanos));
+      BigDecimal secondsBd = new BigDecimal(seconds);
+      BigDecimal nanosBd = new BigDecimal(nanos).movePointLeft(9);
+      BigDecimal resultBd = (seconds >= 0L) ? secondsBd.add(nanosBd) : secondsBd.subtract(nanosBd);
+      HiveDecimal value = HiveDecimal.create(resultBd);
       if (value != null) {
         // The DecimalColumnVector will enforce precision and scale and set the entry to null when out of bounds.
         if (decimalColVector instanceof Decimal64ColumnVector) {