Skip to content

Commit

Permalink
Fix incorrect comparison for negative zero
Browse files Browse the repository at this point in the history
  • Loading branch information
martint committed Nov 23, 2023
1 parent 93c0040 commit ee27de4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
14 changes: 12 additions & 2 deletions core/trino-spi/src/main/java/io/trino/spi/type/DoubleType.java
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ private static boolean distinctFromOperator(double left, @IsNull boolean leftNul
@ScalarOperator(COMPARISON_UNORDERED_LAST)
private static long comparisonUnorderedLastOperator(double left, double right)
{
return Double.compare(left, right);
return compare(left, right);
}

@ScalarOperator(COMPARISON_UNORDERED_FIRST)
Expand All @@ -258,7 +258,8 @@ private static long comparisonUnorderedFirstOperator(double left, double right)
if (Double.isNaN(right)) {
return 1;
}
return Double.compare(left, right);

return compare(left, right);
}

@ScalarOperator(LESS_THAN)
Expand All @@ -272,4 +273,13 @@ private static boolean lessThanOrEqualOperator(double left, double right)
{
return left <= right;
}

private static int compare(double left, double right)
{
if (left == right) { // Double.compare considers 0.0 and -0.0 different from each other
return 0;
}

return Double.compare(left, right);
}
}
13 changes: 11 additions & 2 deletions core/trino-spi/src/main/java/io/trino/spi/type/RealType.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ private static boolean distinctFromOperator(long left, @IsNull boolean leftNull,
@ScalarOperator(COMPARISON_UNORDERED_LAST)
private static long comparisonUnorderedLastOperator(long left, long right)
{
return Float.compare(intBitsToFloat((int) left), intBitsToFloat((int) right));
return compare(intBitsToFloat((int) left), intBitsToFloat((int) right));
}

@ScalarOperator(COMPARISON_UNORDERED_FIRST)
Expand All @@ -201,7 +201,7 @@ private static long comparisonUnorderedFirstOperator(long leftBits, long rightBi
if (Float.isNaN(right)) {
return 1;
}
return Float.compare(left, right);
return compare(left, right);
}

@ScalarOperator(LESS_THAN)
Expand All @@ -215,4 +215,13 @@ private static boolean lessThanOrEqualOperator(long left, long right)
{
return intBitsToFloat((int) left) <= intBitsToFloat((int) right);
}

private static int compare(float left, float right)
{
if (left == right) { // Float.compare considers 0.0 and -0.0 different from each other
return 0;
}

return Float.compare(left, right);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,16 @@ public void testContains()
ImmutableMap.of(
A, Domain.singleValue(BIGINT, 0L),
B, Domain.none(VARCHAR)))).isTrue();

assertThat(contains(
ImmutableMap.of(A, Domain.singleValue(DOUBLE, 0.0)),
ImmutableMap.of(A, Domain.singleValue(DOUBLE, -0.0))))
.isTrue();

assertThat(contains(
ImmutableMap.of(A, Domain.singleValue(DOUBLE, -0.0)),
ImmutableMap.of(A, Domain.singleValue(DOUBLE, 0.0))))
.isTrue();
}

@Test
Expand Down Expand Up @@ -575,6 +585,11 @@ public void testEquals()
ImmutableMap.of(
A, Domain.singleValue(BIGINT, 0L),
C, Domain.singleValue(DOUBLE, 0.0)))).isFalse();

assertThat(equals(
ImmutableMap.of(A, Domain.singleValue(DOUBLE, 0.0)),
ImmutableMap.of(A, Domain.singleValue(DOUBLE, -0.0))))
.isTrue();
}

@Test
Expand Down

0 comments on commit ee27de4

Please sign in to comment.