Skip to content
Permalink
Browse files
feat: NaNs in Mutations are equal and have the same hashcode (#1554)
* feat: loosen equality check for NaNs in mutations

NaNs in mutations are considered equal

* feat: explain why we relaxed mutation equality
  • Loading branch information
thiagotnunes committed Nov 16, 2021
1 parent 7895bd9 commit 91a18fc09a2034959758d38f1278dc93128c7622
@@ -349,7 +349,7 @@ public boolean equals(Object o) {
return operation == that.operation
&& Objects.equals(table, that.table)
&& Objects.equals(columns, that.columns)
&& Objects.equals(values, that.values)
&& areValuesEqual(values, that.values)
&& Objects.equals(keySet, that.keySet);
}

@@ -358,6 +358,36 @@ public int hashCode() {
return Objects.hash(operation, table, columns, values, keySet);
}

/**
* We are relaxing equality values here, making sure that Double.NaNs and Float.NaNs are equal to
* each other. This is because our Cloud Spanner Import / Export template in Apache Beam uses the
* mutation equality to check for modifications before committing. We noticed that when NaNs where
* used the template would always indicate a modification was present, when it turned out not to
* be the case. For more information see b/206339664.
*/
private boolean areValuesEqual(List<Value> values, List<Value> otherValues) {
if (values == null && otherValues == null) {
return true;
} else if (values == null || otherValues == null) {
return false;
} else if (values.size() != otherValues.size()) {
return false;
} else {
for (int i = 0; i < values.size(); i++) {
final Value value = values.get(i);
final Value otherValue = otherValues.get(i);
if (!value.equals(otherValue) && (!isNaN(value) || !isNaN(otherValue))) {
return false;
}
}
return true;
}
}

private boolean isNaN(Value value) {
return !value.isNull() && value.getType() == Type.float64() && Double.isNaN(value.getFloat64());
}

static void toProto(Iterable<Mutation> mutations, List<com.google.spanner.v1.Mutation> out) {
Mutation last = null;
// The mutation currently being built.
@@ -28,6 +28,7 @@
import com.google.common.testing.EqualsTester;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.hamcrest.Matcher;
import org.hamcrest.MatcherAssert;
@@ -206,6 +207,67 @@ public void equalsAndHashCode() {
tester.addEqualityGroup(
Mutation.delete("T1", KeySet.singleKey(Key.of("k"))), Mutation.delete("T1", Key.of("k")));

// Test NaNs
tester.addEqualityGroup(
Mutation.newInsertBuilder("T1").set("C").to(Double.NaN).build(),
Mutation.newInsertBuilder("T1").set("C").to(Value.float64(Double.NaN)).build(),
Mutation.newInsertBuilder("T1").set("C").to(Float.NaN).build(),
Mutation.newInsertBuilder("T1").set("C").to(Value.float64(Float.NaN)).build());

tester.addEqualityGroup(
Mutation.newInsertBuilder("T1").set("C").toFloat64Array(new double[] {Double.NaN}).build(),
Mutation.newInsertBuilder("T1").set("C").toFloat64Array(new double[] {Float.NaN}).build(),
Mutation.newInsertBuilder("T1")
.set("C")
.toFloat64Array(new double[] {Double.NaN}, 0, 1)
.build(),
Mutation.newInsertBuilder("T1")
.set("C")
.toFloat64Array(new double[] {Float.NaN}, 0, 1)
.build(),
Mutation.newInsertBuilder("T1")
.set("C")
.toFloat64Array(Collections.singletonList(Double.NaN))
.build(),
Mutation.newInsertBuilder("T1")
.set("C")
.toFloat64Array(Collections.singletonList((double) Float.NaN))
.build(),
Mutation.newInsertBuilder("T1")
.set("C")
.to(Value.float64Array(new double[] {Double.NaN}))
.build(),
Mutation.newInsertBuilder("T1")
.set("C")
.to(Value.float64Array(new double[] {Float.NaN}))
.build(),
Mutation.newInsertBuilder("T1")
.set("C")
.to(Value.float64Array(new double[] {Double.NaN}, 0, 1))
.build(),
Mutation.newInsertBuilder("T1")
.set("C")
.to(Value.float64Array(new double[] {Float.NaN}, 0, 1))
.build(),
Mutation.newInsertBuilder("T1")
.set("C")
.to(Value.float64Array(Collections.singletonList(Double.NaN)))
.build(),
Mutation.newInsertBuilder("T1")
.set("C")
.to(Value.float64Array(Collections.singletonList((double) Float.NaN)))
.build());
// Test NaNs and nulls
tester.addEqualityGroup(
Mutation.newInsertBuilder("T1")
.set("C")
.toFloat64Array(Arrays.asList(null, Double.NaN))
.build(),
Mutation.newInsertBuilder("T1")
.set("C")
.toFloat64Array(Arrays.asList(null, (double) Float.NaN))
.build());

tester.testEquals();
}

0 comments on commit 91a18fc

Please sign in to comment.