Skip to content

Conversation

@wangyum
Copy link
Member

@wangyum wangyum commented Jun 25, 2018

Fix PrimitiveType.equals throw NPE.

@Override
protected boolean equals(Type other) {
if (!other.isPrimitive()) {
if (other == null || !other.isPrimitive()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come the other is null? Adding a null check sounds reasonable for me, though I wonder in what situation does one call equals with a null parameter.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's part of the method contract: https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#equals(java.lang.Object)

This basically eliminates the need to add two checks when you want to check equality and the other object may be null. There are lots of cases where equals is used and the code doesn't need to handle null checks in addition.


@Override
public boolean equals(Object other) {
if (!(other instanceof Type) || other == null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this required? The test you wrote fails when I don't apply the change on PrimitiveType, but doesn't fail if I don't apply this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I think null check is not needed here at all: https://docs.oracle.com/javase/specs/jls/se10/html/jls-15.html#jls-15.20.2

@wangyum
Copy link
Member Author

wangyum commented Jun 30, 2018

It seems that my usage is wrong.

@wangyum wangyum closed this Jun 30, 2018
@wangyum wangyum deleted the PARQUET-1338 branch June 30, 2018 02:05
@rdblue
Copy link
Contributor

rdblue commented Jul 3, 2018

@wangyum, the PrimitiveType's equals method still seems wrong. I think the Type implementation that uses instanceof is fine (because null instanceof Anything is always false) but PrimitiveType is definitely wrong.

@wangyum wangyum restored the PARQUET-1338 branch July 4, 2018 08:13
@wangyum wangyum reopened this Jul 4, 2018
@wangyum
Copy link
Member Author

wangyum commented Jul 4, 2018

Thanks @rdblue. I reopened this.

@nandorKollar
Copy link
Contributor

It seems to me that this equals method is called from the public equals method of Type, where the null check is already present. Since null check is already done before (the 'real' equals), what's the advantage of doing it here again?

import static org.apache.parquet.schema.Type.Repetition.*;

public class TestTypeBuilders {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove this? It is a non-functional change.

@rdblue
Copy link
Contributor

rdblue commented Jul 4, 2018

@nandorKollar, any equals implementation should fulfill the documented contract, regardless of how it is used at the moment. Code changes over time and you don't want NPEs thrown because you didn't foresee how something would be used.

@nandorKollar
Copy link
Contributor

@rdblue yes, and a null check is absolutely fine for me! I just wanted to point out, that the method what we just modify is actually not the overridden version of Object#equals (it is protected, not public, therefore we have a method with more restricted scope which happen to have the same name as Object#equals), and Type already has an equals method, which already does null check before casting to Type and calling the protected method.

Anyway, I'm fine with this. One minor thing: if we add the null check here, should we consider adding it to GroupType#equals too?

@wangyum wangyum closed this Nov 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants