hive-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ekoif...@apache.org
Subject hive git commit: HIVE-7517 RecordIdentifier overrides equals() but not hashCode() (Eugene Koifman, reviewed by Wei Zheng)
Date Wed, 01 Mar 2017 00:56:24 GMT
Repository: hive
Updated Branches:
  refs/heads/master 10c4c963a -> 63ffa3c72


HIVE-7517 RecordIdentifier overrides equals() but not hashCode() (Eugene Koifman, reviewed
by Wei Zheng)


Project: http://git-wip-us.apache.org/repos/asf/hive/repo
Commit: http://git-wip-us.apache.org/repos/asf/hive/commit/63ffa3c7
Tree: http://git-wip-us.apache.org/repos/asf/hive/tree/63ffa3c7
Diff: http://git-wip-us.apache.org/repos/asf/hive/diff/63ffa3c7

Branch: refs/heads/master
Commit: 63ffa3c721600864bd6003e6571fe1c57c8e026e
Parents: 10c4c96
Author: Eugene Koifman <ekoifman@hortonworks.com>
Authored: Tue Feb 28 16:56:20 2017 -0800
Committer: Eugene Koifman <ekoifman@hortonworks.com>
Committed: Tue Feb 28 16:56:20 2017 -0800

----------------------------------------------------------------------
 .../hadoop/hive/ql/io/RecordIdentifier.java     | 13 ++++++++-
 .../hive/ql/io/orc/OrcRawRecordMerger.java      | 11 +++++++-
 .../hadoop/hive/ql/io/TestRecordIdentifier.java | 28 ++++++++++++++++++++
 3 files changed, 50 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hive/blob/63ffa3c7/ql/src/java/org/apache/hadoop/hive/ql/io/RecordIdentifier.java
----------------------------------------------------------------------
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/io/RecordIdentifier.java b/ql/src/java/org/apache/hadoop/hive/ql/io/RecordIdentifier.java
index cdde3dc..7f2c169 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/io/RecordIdentifier.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/io/RecordIdentifier.java
@@ -43,7 +43,7 @@ public class RecordIdentifier implements WritableComparable<RecordIdentifier>
{
    * Each field of RecordIdentifier which should be part of ROWID should be in this enum...
which 
    * really means that it should be part of VirtualColumn (so make a subclass for rowid).
    */
-  public static enum Field {
+  public enum Field {
     //note the enum names match field names in the struct
     transactionId(TypeInfoFactory.longTypeInfo,
       PrimitiveObjectInspectorFactory.javaLongObjectInspector),
@@ -197,6 +197,9 @@ public class RecordIdentifier implements WritableComparable<RecordIdentifier>
{
 
   @Override
   public boolean equals(Object other) {
+    if(other == this) {
+      return true;
+    }
     if (other == null || other.getClass() != getClass()) {
       return false;
     }
@@ -205,6 +208,14 @@ public class RecordIdentifier implements WritableComparable<RecordIdentifier>
{
         oth.bucketId == bucketId &&
         oth.rowId == rowId;
   }
+  @Override
+  public int hashCode() {
+    int result = 17;
+    result = 31 * result + (int)(transactionId ^ (transactionId >>> 32));
+    result = 31 * result + bucketId;
+    result = 31 * result + (int)(rowId ^ (rowId >>> 32));
+    return result;
+  }
 
   @Override
   public String toString() {

http://git-wip-us.apache.org/repos/asf/hive/blob/63ffa3c7/ql/src/java/org/apache/hadoop/hive/ql/io/orc/OrcRawRecordMerger.java
----------------------------------------------------------------------
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/io/orc/OrcRawRecordMerger.java b/ql/src/java/org/apache/hadoop/hive/ql/io/orc/OrcRawRecordMerger.java
index dd53afa..95b8806 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/io/orc/OrcRawRecordMerger.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/io/orc/OrcRawRecordMerger.java
@@ -74,7 +74,8 @@ public class OrcRawRecordMerger implements AcidInputFormat.RawReader<OrcStruct>{
    * reader is collapsing events to just the last update, just the first
    * instance of each record is required.
    */
-  final static class ReaderKey extends RecordIdentifier{
+  @VisibleForTesting
+  public final static class ReaderKey extends RecordIdentifier{
     private long currentTransactionId;
     private int statementId;//sort on this descending, like currentTransactionId
 
@@ -120,6 +121,14 @@ public class OrcRawRecordMerger implements AcidInputFormat.RawReader<OrcStruct>{
             && statementId == ((ReaderKey) other).statementId//consistent with compareTo()
           ;
     }
+    @Override
+    public int hashCode() {
+      int result = super.hashCode();
+      result = 31 * result + (int)(currentTransactionId ^ (currentTransactionId >>>
32));
+      result = 31 * result + statementId;
+      return result;
+    }
+
 
     @Override
     public int compareTo(RecordIdentifier other) {

http://git-wip-us.apache.org/repos/asf/hive/blob/63ffa3c7/ql/src/test/org/apache/hadoop/hive/ql/io/TestRecordIdentifier.java
----------------------------------------------------------------------
diff --git a/ql/src/test/org/apache/hadoop/hive/ql/io/TestRecordIdentifier.java b/ql/src/test/org/apache/hadoop/hive/ql/io/TestRecordIdentifier.java
index 6d83f70..a2bd8da 100644
--- a/ql/src/test/org/apache/hadoop/hive/ql/io/TestRecordIdentifier.java
+++ b/ql/src/test/org/apache/hadoop/hive/ql/io/TestRecordIdentifier.java
@@ -17,8 +17,13 @@
  */
 package org.apache.hadoop.hive.ql.io;
 
+import org.apache.hadoop.hive.ql.io.orc.OrcRawRecordMerger;
 import org.junit.Test;
 
+import java.util.concurrent.ThreadLocalRandom;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
 import static org.junit.Assert.assertTrue;
 
 public class TestRecordIdentifier {
@@ -42,4 +47,27 @@ public class TestRecordIdentifier {
     assertTrue(left.compareTo(right) < 0);
     assertTrue(right.compareTo(left) > 0);
   }
+
+  @Test
+  public void testHashEquals() throws Exception {
+    long origTxn = ThreadLocalRandom.current().nextLong(1, 10000000000L);
+    int bucketId = ThreadLocalRandom.current().nextInt(1, 512);
+    long rowId = ThreadLocalRandom.current().nextLong(1, 10000000000L);
+    long currTxn = origTxn + ThreadLocalRandom.current().nextLong(0, 10000000000L);
+    int stmtId = ThreadLocalRandom.current().nextInt(1, 512);
+
+    RecordIdentifier left = new RecordIdentifier(origTxn, bucketId, rowId);
+    RecordIdentifier right = new RecordIdentifier(origTxn, bucketId, rowId);
+    OrcRawRecordMerger.ReaderKey rkLeft = new OrcRawRecordMerger.ReaderKey(origTxn, bucketId,
rowId, currTxn, stmtId);
+    OrcRawRecordMerger.ReaderKey rkRight = new OrcRawRecordMerger.ReaderKey(origTxn, bucketId,
rowId, currTxn, stmtId);
+
+    assertEquals("RecordIdentifier.equals", left, right);
+    assertEquals("RecordIdentifier.hashCode", left.hashCode(), right.hashCode());
+
+    assertEquals("ReaderKey", rkLeft, rkLeft);
+    assertEquals("ReaderKey.hashCode", rkLeft.hashCode(), rkRight.hashCode());
+
+    //debatable if this is correct, but that's how it's implemented
+    assertNotEquals("RecordIdentifier <> ReaderKey", left, rkRight);
+  }
 }


Mime
View raw message