You are viewing a plain text version of this content. The canonical link for it is here.
Posted to gitbox@hive.apache.org by GitBox <gi...@apache.org> on 2020/08/03 05:48:01 UTC

[GitHub] [hive] rbalamohan commented on a change in pull request #1352: HIVE-23975: Reuse evicted keys from aggregation buffers

rbalamohan commented on a change in pull request #1352:
URL: https://github.com/apache/hive/pull/1352#discussion_r464204771



##########
File path: ql/src/java/org/apache/hadoop/hive/ql/exec/vector/VectorGroupByOperator.java
##########
@@ -514,7 +526,8 @@ private void prepareBatchAggregationBufferSets(VectorizedRowBatch batch) throws
           // is very important to clone the keywrapper, the one we have from our
           // keyWrappersBatch is going to be reset/reused on next batch.
           aggregationBuffer = allocateAggregationBuffer();

Review comment:
       allocateAggregationBuffer can be reused with the flushed out entries.

##########
File path: ql/src/java/org/apache/hadoop/hive/ql/exec/vector/wrapper/VectorHashKeyWrapperGeneral.java
##########
@@ -262,6 +255,138 @@ private void duplicateTo(VectorHashKeyWrapperGeneral clone) {
 
     clone.hashcode = hashcode;
     assert clone.equals(this);
+
+    return clone;
+  }
+
+  private long[] copyInPlaceOrAllocate(long[] from, long[] to) {
+    if (from.length > 0) {
+      if (to != null && to.length == from.length) {
+        System.arraycopy(from, 0, to, 0, from.length);
+        return to;
+      } else {
+        return from.clone();
+      }
+    } else {
+      return EMPTY_LONG_ARRAY;
+    }
+  }
+
+  private double[] copyInPlaceOrAllocate(double[] from, double[] to) {
+    if (from.length > 0) {
+      if (to != null && to.length == from.length) {
+        System.arraycopy(from, 0, to, 0, from.length);
+        return to;
+      } else {
+        return from.clone();
+      }
+    } else {
+      return EMPTY_DOUBLE_ARRAY;
+    }
+  }
+
+  private boolean[] copyInPlaceOrAllocate(boolean[] from, boolean[] to) {
+    if (to != null && to.length == from.length) {
+      System.arraycopy(from, 0, to, 0, from.length);
+      return to;
+    } else {
+      return from.clone();
+    }
+  }
+
+  private HiveDecimalWritable[] copyInPlaceOrAllocate(HiveDecimalWritable[] from, HiveDecimalWritable[] to) {
+    if (from.length > 0) {
+      if (to == null || to.length != from.length) {
+        to = new HiveDecimalWritable[from.length];
+      }
+      for (int i = 0; i < from.length; i++) {
+        to[i] = new HiveDecimalWritable(from[i]);
+      }
+      return to;
+    } else {
+      return EMPTY_DECIMAL_ARRAY;
+    }
+  }
+
+  private Timestamp[] copyInPlaceOrAllocate(Timestamp[] from, Timestamp[] to) {
+    if (from.length > 0) {
+      if (to == null || to.length != from.length) {
+        to = new Timestamp[from.length];
+      }
+      for (int i = 0; i < from.length; i++) {
+        to[i] = (Timestamp) from[i].clone();
+      }
+      return to;
+    } else {
+      return EMPTY_TIMESTAMP_ARRAY;
+    }
+  }
+
+  @Override
+  public void copyKey(KeyWrapper oldWrapper) {
+    VectorHashKeyWrapperGeneral clone = (VectorHashKeyWrapperGeneral) oldWrapper;
+    clone.hashCtx = hashCtx;
+    clone.keyCount = keyCount;
+    clone.longValues = copyInPlaceOrAllocate(longValues, clone.longValues);
+    clone.doubleValues = copyInPlaceOrAllocate(doubleValues, clone.doubleValues);
+    clone.isNull = copyInPlaceOrAllocate(isNull, clone.isNull);
+    clone.decimalValues = copyInPlaceOrAllocate(decimalValues, clone.decimalValues);
+
+    if (byteLengths.length > 0) {
+      if (clone.byteLengths == null || clone.byteValues.length != byteValues.length) {
+        // byteValues and byteStarts are always the same length
+        clone.byteValues = new byte[byteValues.length][];
+        clone.byteStarts = new int[byteValues.length];
+        clone.byteLengths = byteLengths.clone();
+        for (int i = 0; i < byteValues.length; ++i) {
+          // avoid allocation/copy of nulls, because it potentially expensive.
+          // branch instead.
+          if (byteLengths[i] != -1) {
+            clone.byteValues[i] = Arrays.copyOfRange(byteValues[i],
+                byteStarts[i], byteStarts[i] + byteLengths[i]);
+          }
+        }
+      } else {
+        System.arraycopy(byteLengths, 0, clone.byteLengths, 0, byteValues.length);
+        Arrays.fill(byteStarts, 0);
+        System.arraycopy(byteStarts, 0, clone.byteStarts, 0, byteValues.length);
+        for (int i = 0; i < byteValues.length; ++i) {
+          // avoid allocation/copy of nulls, because it potentially expensive.
+          // branch instead.
+          if (byteLengths[i] != -1) {
+            if (clone.byteValues[i] != null && clone.byteValues[i].length >= byteValues[i].length) {
+              System.arraycopy(byteValues[i], byteStarts[i], clone.byteValues[i], 0, byteLengths[i]);
+            } else {
+              clone.byteValues[i] = Arrays.copyOfRange(byteValues[i],
+                  byteStarts[i], byteStarts[i] + byteLengths[i]);

Review comment:
       Why is this needed? Wouldn't be it always from 0 - bytelength?




----------------------------------------------------------------
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org