Skip to content

Commit

Permalink
API: Override equals and hashCode methods for primitive types (#6305)
Browse files Browse the repository at this point in the history
  • Loading branch information
smallx authored Dec 4, 2022
1 parent 577867e commit 574a55b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
18 changes: 18 additions & 0 deletions api/src/main/java/org/apache/iceberg/types/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.nio.ByteBuffer;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.apache.iceberg.StructLike;

public interface Type extends Serializable {
Expand Down Expand Up @@ -112,6 +113,23 @@ public PrimitiveType asPrimitiveType() {
Object writeReplace() throws ObjectStreamException {
return new PrimitiveHolder(toString());
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
} else if (!(o instanceof PrimitiveType)) {
return false;
}

PrimitiveType that = (PrimitiveType) o;
return typeId() == that.typeId();
}

@Override
public int hashCode() {
return Objects.hash(PrimitiveType.class, typeId());
}
}

abstract class NestedType implements Type {
Expand Down
4 changes: 2 additions & 2 deletions api/src/main/java/org/apache/iceberg/types/TypeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,10 @@ public static boolean isPromotionAllowed(Type from, Type.PrimitiveType to) {

switch (from.typeId()) {
case INTEGER:
return to == Types.LongType.get();
return to.typeId() == Type.TypeID.LONG;

case FLOAT:
return to == Types.DoubleType.get();
return to.typeId() == Type.TypeID.DOUBLE;

case DECIMAL:
Types.DecimalType fromDecimal = (Types.DecimalType) from;
Expand Down

0 comments on commit 574a55b

Please sign in to comment.