Skip to content

Commit

Permalink
Override hashcode and equal functions for block builder
Browse files Browse the repository at this point in the history
Custom hashcode and equal functions are introduced for xxxBlock in PR prestodb#17720, where content in the corresponding
block rather than the object itself are used in hashcode and equal function computation. However, xxxBlockBuilder
which also extends from Block interface is left behind. In this commit, we override such functions for block builder.
  • Loading branch information
feilong-liu committed Sep 1, 2022
1 parent 09e50f3 commit e543518
Show file tree
Hide file tree
Showing 10 changed files with 313 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import javax.annotation.Nullable;

import java.util.Arrays;
import java.util.Objects;
import java.util.function.ObjLongConsumer;

import static com.facebook.presto.common.block.ArrayBlock.createArrayBlockInternal;
Expand Down Expand Up @@ -337,4 +338,39 @@ public String toString()
{
return format("ArrayBlockBuilder(%d){positionCount=%d}", hashCode(), getPositionCount());
}

@Override
public boolean equals(Object obj)
{
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
if (currentEntryOpened) {
return false;
}
ArrayBlockBuilder other = (ArrayBlockBuilder) obj;
return this.positionCount == other.positionCount &&
Arrays.equals(this.valueIsNull, other.valueIsNull) &&
Objects.equals(this.values, other.values) &&
Arrays.equals(this.offsets, other.offsets) &&
this.hasNullValue == other.hasNullValue &&
this.retainedSizeInBytes == other.retainedSizeInBytes;
}

@Override
public int hashCode()
{
if (currentEntryOpened) {
return super.hashCode();
}
return Objects.hash(positionCount,
Arrays.hashCode(valueIsNull),
values,
Arrays.hashCode(offsets),
hasNullValue,
retainedSizeInBytes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import javax.annotation.Nullable;

import java.util.Arrays;
import java.util.Objects;
import java.util.OptionalInt;
import java.util.function.ObjLongConsumer;

Expand Down Expand Up @@ -350,4 +351,34 @@ public int getOffsetBase()
{
return 0;
}

@Override
public boolean equals(Object obj)
{
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
ByteArrayBlockBuilder other = (ByteArrayBlockBuilder) obj;
return this.positionCount == other.positionCount &&
this.hasNullValue == other.hasNullValue &&
this.hasNonNullValue == other.hasNonNullValue &&
Arrays.equals(this.valueIsNull, other.valueIsNull) &&
Arrays.equals(this.values, other.values) &&
this.retainedSizeInBytes == other.retainedSizeInBytes;
}

@Override
public int hashCode()
{
return Objects.hash(
positionCount,
hasNullValue,
hasNonNullValue,
Arrays.hashCode(valueIsNull),
Arrays.hashCode(values),
retainedSizeInBytes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import javax.annotation.Nullable;

import java.util.Arrays;
import java.util.Objects;
import java.util.OptionalInt;
import java.util.function.ObjLongConsumer;

Expand Down Expand Up @@ -230,10 +231,10 @@ public long getLong(int position, int offset)
* Get the Slice starting at {@code this.positionOffset + offset} in the value at {@code position} with {@code length} bytes.
*
* @param position The logical position of the 128-bit integer in the values array.
* For example, position = 0 refers to the 128-bit integer at values[2] and values[3] if this.positionOffset = 1.
* For example, position = 0 refers to the 128-bit integer at values[2] and values[3] if this.positionOffset = 1.
* @param offset The offset to the position in the unit of 128-bit integers.
* For example, offset = 1 means the next position (one 128-bit integer or 16 bytes) to the specified position.
* This means we always compare bytes starting at 128-bit integer boundaries.
* For example, offset = 1 means the next position (one 128-bit integer or 16 bytes) to the specified position.
* This means we always compare bytes starting at 128-bit integer boundaries.
* @param length The length in bytes. It has to be a multiple of 16.
*/
@Override
Expand All @@ -253,10 +254,10 @@ public int getSliceLength(int position)
* Get the Slice starting at {@code offset} in the value at {@code internalPosition} with {@code length} bytes.
*
* @param internalPosition The physical position of the 128-bit integer in the values array.
* For example, internalPosition = 1 refers to the 128-bit integer at values[2] and values[3]
* For example, internalPosition = 1 refers to the 128-bit integer at values[2] and values[3]
* @param offset The offset to the position in the unit of 128-bit integers.
* For example, offset = 1 means the next position (one 128-bit integer or 16 bytes) to the specified position.
* This means we always compare bytes starting at 128-bit integer boundaries.
* For example, offset = 1 means the next position (one 128-bit integer or 16 bytes) to the specified position.
* This means we always compare bytes starting at 128-bit integer boundaries.
* @param length The length in bytes. It has to be a multiple of 16.
*/
@Override
Expand Down Expand Up @@ -462,4 +463,34 @@ public int getOffsetBase()
{
return 0;
}

@Override
public boolean equals(Object obj)
{
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
Int128ArrayBlockBuilder other = (Int128ArrayBlockBuilder) obj;
return this.positionCount == other.positionCount &&
hasNullValue == other.hasNullValue &&
hasNonNullValue == other.hasNonNullValue &&
Arrays.equals(this.valueIsNull, other.valueIsNull) &&
Arrays.equals(this.values, other.values) &&
this.retainedSizeInBytes == other.retainedSizeInBytes;
}

@Override
public int hashCode()
{
return Objects.hash(
positionCount,
hasNullValue,
hasNonNullValue,
Arrays.hashCode(valueIsNull),
Arrays.hashCode(values),
retainedSizeInBytes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import javax.annotation.Nullable;

import java.util.Arrays;
import java.util.Objects;
import java.util.OptionalInt;
import java.util.function.ObjLongConsumer;

Expand Down Expand Up @@ -350,4 +351,31 @@ public int getOffsetBase()
{
return 0;
}

@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
IntArrayBlockBuilder other = (IntArrayBlockBuilder) o;
return positionCount == other.positionCount &&
hasNullValue == other.hasNullValue &&
hasNonNullValue == other.hasNonNullValue &&
Arrays.equals(valueIsNull, other.valueIsNull) &&
Arrays.equals(values, other.values) &&
retainedSizeInBytes == other.retainedSizeInBytes;
}

@Override
public int hashCode()
{
int result = Objects.hash(positionCount, hasNullValue, hasNonNullValue, retainedSizeInBytes);
result = 31 * result + Arrays.hashCode(valueIsNull);
result = 31 * result + Arrays.hashCode(values);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import javax.annotation.Nullable;

import java.util.Arrays;
import java.util.Objects;
import java.util.OptionalInt;
import java.util.function.ObjLongConsumer;

Expand Down Expand Up @@ -387,4 +388,34 @@ public int getOffsetBase()
{
return 0;
}

@Override
public boolean equals(Object obj)
{
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
LongArrayBlockBuilder other = (LongArrayBlockBuilder) obj;
return this.positionCount == other.positionCount &&
hasNullValue == other.hasNullValue &&
hasNonNullValue == other.hasNonNullValue &&
Arrays.equals(this.valueIsNull, other.valueIsNull) &&
Arrays.equals(this.values, other.values) &&
this.retainedSizeInBytes == other.retainedSizeInBytes;
}

@Override
public int hashCode()
{
return Objects.hash(
positionCount,
hasNullValue,
hasNonNullValue,
Arrays.hashCode(valueIsNull),
Arrays.hashCode(values),
retainedSizeInBytes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.lang.invoke.MethodHandle;
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;
import java.util.function.ObjLongConsumer;

Expand Down Expand Up @@ -633,4 +634,38 @@ static void verify(boolean assertion, String message)
throw new AssertionError(message);
}
}

@Override
public boolean equals(Object obj)
{
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
if (currentEntryOpened) {
return false;
}
MapBlockBuilder other = (MapBlockBuilder) obj;
return this.positionCount == other.positionCount &&
Arrays.equals(this.mapIsNull, other.mapIsNull) &&
Arrays.equals(this.offsets, other.offsets) &&
Objects.equals(this.keyBlockBuilder, other.keyBlockBuilder) &&
Objects.equals(this.valueBlockBuilder, other.valueBlockBuilder);
}

@Override
public int hashCode()
{
if (currentEntryOpened) {
return super.hashCode();
}
return Objects.hash(
positionCount,
Arrays.hashCode(mapIsNull),
Arrays.hashCode(offsets),
keyBlockBuilder,
valueBlockBuilder);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.function.ObjLongConsumer;

import static com.facebook.presto.common.block.BlockUtil.calculateBlockResetSize;
Expand Down Expand Up @@ -339,4 +340,38 @@ public BlockBuilder newBlockBuilderLike(BlockBuilderStatus blockBuilderStatus, i
}
return new RowBlockBuilder(blockBuilderStatus, newBlockBuilders, new int[newSize + 1], new boolean[newSize]);
}

@Override
public boolean equals(Object obj)
{
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
if (currentEntryOpened) {
return false;
}
RowBlockBuilder other = (RowBlockBuilder) obj;
return this.positionCount == other.positionCount &&
this.hasNullRow == other.hasNullRow &&
Arrays.equals(this.rowIsNull, other.rowIsNull) &&
Arrays.equals(this.fieldBlockOffsets, other.fieldBlockOffsets) &&
Arrays.equals(this.fieldBlockBuilders, other.fieldBlockBuilders);
}

@Override
public int hashCode()
{
if (currentEntryOpened) {
return super.hashCode();
}
return Objects.hash(
positionCount,
hasNullRow,
Arrays.hashCode(rowIsNull),
Arrays.hashCode(fieldBlockOffsets),
Arrays.hashCode(fieldBlockBuilders));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import javax.annotation.Nullable;

import java.util.Arrays;
import java.util.Objects;
import java.util.OptionalInt;
import java.util.function.ObjLongConsumer;

Expand Down Expand Up @@ -350,4 +351,34 @@ public int getOffsetBase()
{
return 0;
}

@Override
public boolean equals(Object obj)
{
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
ShortArrayBlockBuilder other = (ShortArrayBlockBuilder) obj;
return this.positionCount == other.positionCount &&
hasNullValue == other.hasNullValue &&
hasNonNullValue == other.hasNonNullValue &&
Arrays.equals(this.valueIsNull, other.valueIsNull) &&
Arrays.equals(this.values, other.values) &&
this.retainedSizeInBytes == other.retainedSizeInBytes;
}

@Override
public int hashCode()
{
return Objects.hash(
positionCount,
hasNullValue,
hasNonNullValue,
Arrays.hashCode(valueIsNull),
Arrays.hashCode(values),
retainedSizeInBytes);
}
}
Loading

0 comments on commit e543518

Please sign in to comment.