[Fix](multi-catalog) optimize hashcode for PartitionKey. (#21307)
This commit is contained in:
@ -56,7 +56,6 @@ import java.time.temporal.TemporalAccessor;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.TimeZone;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@ -1086,7 +1085,10 @@ public class DateLiteral extends LiteralExpr {
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return 31 * super.hashCode() + Objects.hashCode(unixTimestamp(TimeZone.getDefault()));
|
||||
// do not invoke the super.hashCode(), just use the return value of getLongValue()
|
||||
// a DateV2 DateLiteral obj may be equal to a Date DateLiteral
|
||||
// if the return value of getLongValue() is the same
|
||||
return Long.hashCode(getLongValue());
|
||||
}
|
||||
|
||||
// parse the date string value in 'value' by 'format' pattern.
|
||||
|
||||
@ -434,11 +434,15 @@ public class PartitionKey implements Comparable<PartitionKey>, Writable {
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int hashCode = 1;
|
||||
for (LiteralExpr key : keys) {
|
||||
hashCode = 31 * hashCode + (key == null ? 0 : key.hashCode());
|
||||
}
|
||||
int ret = types.size() * 1000;
|
||||
for (PrimitiveType type : types) {
|
||||
ret += type.ordinal();
|
||||
}
|
||||
return ret;
|
||||
return hashCode + ret;
|
||||
}
|
||||
|
||||
public static class PartitionKeySerializer implements JsonSerializer<PartitionKey> {
|
||||
|
||||
@ -79,6 +79,7 @@ public class PartitionKeyTest {
|
||||
pk1 = PartitionKey.createPartitionKey(Arrays.asList(new PartitionValue("127"), new PartitionValue("32767")),
|
||||
Arrays.asList(tinyInt, smallInt));
|
||||
pk2 = PartitionKey.createInfinityPartitionKey(Arrays.asList(tinyInt, smallInt), true);
|
||||
Assert.assertTrue(pk1.hashCode() != pk2.hashCode());
|
||||
Assert.assertTrue(!pk1.equals(pk2) && pk1.compareTo(pk2) == -1);
|
||||
|
||||
// case2
|
||||
@ -86,6 +87,7 @@ public class PartitionKeyTest {
|
||||
Arrays.asList(tinyInt, smallInt));
|
||||
pk2 = PartitionKey.createPartitionKey(Arrays.asList(new PartitionValue("127"), new PartitionValue("-32768")),
|
||||
Arrays.asList(tinyInt, smallInt));
|
||||
Assert.assertTrue(pk1.hashCode() == pk2.hashCode());
|
||||
Assert.assertTrue(pk1.equals(pk2) && pk1.compareTo(pk2) == 0);
|
||||
|
||||
// case3
|
||||
@ -93,6 +95,7 @@ public class PartitionKeyTest {
|
||||
Arrays.asList(int32, bigInt));
|
||||
pk2 = PartitionKey.createPartitionKey(Arrays.asList(new PartitionValue("128"), new PartitionValue("-32768")),
|
||||
Arrays.asList(int32, bigInt));
|
||||
Assert.assertTrue(pk1.hashCode() != pk2.hashCode());
|
||||
Assert.assertTrue(!pk1.equals(pk2) && pk1.compareTo(pk2) == -1);
|
||||
|
||||
// case4
|
||||
@ -100,6 +103,7 @@ public class PartitionKeyTest {
|
||||
Arrays.asList(largeInt, bigInt));
|
||||
pk2 = PartitionKey.createPartitionKey(Arrays.asList(new PartitionValue("127"), new PartitionValue("12346")),
|
||||
Arrays.asList(largeInt, bigInt));
|
||||
Assert.assertTrue(pk1.hashCode() != pk2.hashCode());
|
||||
Assert.assertTrue(!pk1.equals(pk2) && pk1.compareTo(pk2) == -1);
|
||||
|
||||
// case5
|
||||
@ -107,24 +111,28 @@ public class PartitionKeyTest {
|
||||
Arrays.asList(date, datetime));
|
||||
pk2 = PartitionKey.createPartitionKey(Arrays.asList(new PartitionValue("2014-12-12"), new PartitionValue("2014-12-12 10:00:01")),
|
||||
Arrays.asList(date, datetime));
|
||||
Assert.assertTrue(pk1.hashCode() != pk2.hashCode());
|
||||
Assert.assertTrue(!pk1.equals(pk2) && pk1.compareTo(pk2) == -1);
|
||||
|
||||
// case6
|
||||
pk1 = PartitionKey.createPartitionKey(Arrays.asList(new PartitionValue("-128")),
|
||||
Arrays.asList(tinyInt, smallInt));
|
||||
pk2 = PartitionKey.createInfinityPartitionKey(Arrays.asList(tinyInt, smallInt), false);
|
||||
Assert.assertTrue(pk1.hashCode() == pk2.hashCode());
|
||||
Assert.assertTrue(pk1.equals(pk2) && pk1.compareTo(pk2) == 0);
|
||||
|
||||
// case7
|
||||
pk1 = PartitionKey.createPartitionKey(Arrays.asList(new PartitionValue("127")),
|
||||
Arrays.asList(tinyInt, smallInt));
|
||||
pk2 = PartitionKey.createInfinityPartitionKey(Arrays.asList(tinyInt, smallInt), true);
|
||||
Assert.assertTrue(pk1.hashCode() != pk2.hashCode());
|
||||
Assert.assertTrue(!pk1.equals(pk2) && pk1.compareTo(pk2) == -1);
|
||||
|
||||
// case7
|
||||
pk1 = PartitionKey.createPartitionKey(Arrays.asList(new PartitionValue("127"), new PartitionValue("32767")),
|
||||
Arrays.asList(tinyInt, smallInt));
|
||||
pk2 = PartitionKey.createInfinityPartitionKey(Arrays.asList(tinyInt, smallInt), true);
|
||||
Assert.assertTrue(pk1.hashCode() != pk2.hashCode());
|
||||
Assert.assertTrue(!pk1.equals(pk2) && pk1.compareTo(pk2) == -1);
|
||||
|
||||
// case8
|
||||
@ -134,6 +142,7 @@ public class PartitionKeyTest {
|
||||
new PartitionValue("9999-12-31"), new PartitionValue("9999-12-31 23:59:59")),
|
||||
allColumns);
|
||||
pk2 = PartitionKey.createInfinityPartitionKey(allColumns, true);
|
||||
Assert.assertTrue(pk1.hashCode() != pk2.hashCode());
|
||||
Assert.assertTrue(!pk1.equals(pk2) && pk1.compareTo(pk2) == -1);
|
||||
|
||||
// case9
|
||||
@ -143,6 +152,7 @@ public class PartitionKeyTest {
|
||||
new PartitionValue("0000-01-01"), new PartitionValue("0000-01-01 00:00:00")),
|
||||
allColumns);
|
||||
pk2 = PartitionKey.createInfinityPartitionKey(allColumns, false);
|
||||
Assert.assertTrue(pk1.hashCode() == pk2.hashCode());
|
||||
Assert.assertTrue(pk1.equals(pk2) && pk1.compareTo(pk2) == 0);
|
||||
|
||||
// case10
|
||||
@ -151,6 +161,7 @@ public class PartitionKeyTest {
|
||||
new PartitionValue("0"), new PartitionValue("1970-01-01"), new PartitionValue("1970-01-01 00:00:00")),
|
||||
allColumns);
|
||||
pk2 = PartitionKey.createInfinityPartitionKey(allColumns, false);
|
||||
Assert.assertTrue(pk1.hashCode() != pk2.hashCode());
|
||||
Assert.assertTrue(!pk1.equals(pk2) && pk1.compareTo(pk2) == 1);
|
||||
|
||||
// case11
|
||||
@ -158,6 +169,7 @@ public class PartitionKeyTest {
|
||||
Arrays.asList(charString, varchar));
|
||||
pk2 = PartitionKey.createPartitionKey(Arrays.asList(new PartitionValue("beijing"), new PartitionValue("shanghai")),
|
||||
Arrays.asList(charString, varchar));
|
||||
Assert.assertTrue(pk1.hashCode() == pk2.hashCode());
|
||||
Assert.assertTrue(pk1.equals(pk2) && pk1.compareTo(pk2) == 0);
|
||||
|
||||
// case12
|
||||
@ -165,6 +177,7 @@ public class PartitionKeyTest {
|
||||
Arrays.asList(charString, varchar));
|
||||
pk2 = PartitionKey.createPartitionKey(Arrays.asList(new PartitionValue("shijiazhuang"), new PartitionValue("tianjin")),
|
||||
Arrays.asList(charString, varchar));
|
||||
Assert.assertTrue(pk1.hashCode() != pk2.hashCode());
|
||||
Assert.assertTrue(!pk1.equals(pk2) && pk1.compareTo(pk2) == -1);
|
||||
|
||||
// case13
|
||||
@ -172,6 +185,7 @@ public class PartitionKeyTest {
|
||||
Arrays.asList(charString, varchar));
|
||||
pk2 = PartitionKey.createPartitionKey(Arrays.asList(new PartitionValue("beijing"), new PartitionValue("tianjin")),
|
||||
Arrays.asList(charString, varchar));
|
||||
Assert.assertTrue(pk1.hashCode() != pk2.hashCode());
|
||||
Assert.assertTrue(!pk1.equals(pk2) && pk1.compareTo(pk2) == -1);
|
||||
|
||||
// case14
|
||||
@ -179,6 +193,7 @@ public class PartitionKeyTest {
|
||||
Arrays.asList(bool));
|
||||
pk2 = PartitionKey.createPartitionKey(Arrays.asList(new PartitionValue("false")),
|
||||
Arrays.asList(bool));
|
||||
Assert.assertTrue(pk1.hashCode() != pk2.hashCode());
|
||||
Assert.assertTrue(!pk1.equals(pk2) && pk1.compareTo(pk2) == 1);
|
||||
|
||||
// case15
|
||||
@ -186,6 +201,7 @@ public class PartitionKeyTest {
|
||||
Arrays.asList(bool));
|
||||
pk2 = PartitionKey.createPartitionKey(Arrays.asList(new PartitionValue("true")),
|
||||
Arrays.asList(bool));
|
||||
Assert.assertTrue(pk1.hashCode() == pk2.hashCode());
|
||||
Assert.assertTrue(pk1.equals(pk2) && pk1.compareTo(pk2) == 0);
|
||||
|
||||
// case16
|
||||
@ -193,6 +209,7 @@ public class PartitionKeyTest {
|
||||
Arrays.asList(bool));
|
||||
pk2 = PartitionKey.createPartitionKey(Arrays.asList(new PartitionValue("false")),
|
||||
Arrays.asList(bool));
|
||||
Assert.assertTrue(pk1.hashCode() == pk2.hashCode());
|
||||
Assert.assertTrue(pk1.equals(pk2) && pk1.compareTo(pk2) == 0);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user