Skip to content

Commit

Permalink
[CALCITE-6094] Linq4j.ConstantExpression.write crashes on special FP …
Browse files Browse the repository at this point in the history
…values

Signed-off-by: Mihai Budiu <mbudiu@feldera.com>
  • Loading branch information
mihaibudiu committed Jan 11, 2024
1 parent 135e963 commit ad2a4e6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,15 @@ private static ExpressionWriter write(ExpressionWriter writer,
if (value instanceof BigDecimal) {
bigDecimal = (BigDecimal) value;
} else {
bigDecimal = BigDecimal.valueOf((Float) value);
float f = (Float) value;
if (f == Float.POSITIVE_INFINITY) {
return writer.append("Float.POSITIVE_INFINITY");
} else if (f == Float.NEGATIVE_INFINITY) {
return writer.append("Float.NEGATIVE_INFINITY");
} else if (Float.isNaN(f)) {
return writer.append("Float.NaN");
}
bigDecimal = BigDecimal.valueOf(f);
}
if (bigDecimal.precision() > 6) {
return writer.append("Float.intBitsToFloat(")
Expand All @@ -118,7 +126,15 @@ private static ExpressionWriter write(ExpressionWriter writer,
if (value instanceof BigDecimal) {
bigDecimal = (BigDecimal) value;
} else {
bigDecimal = BigDecimal.valueOf((Double) value);
double d = (Double) value;
if (d == Double.POSITIVE_INFINITY) {
return writer.append("Double.POSITIVE_INFINITY");
} else if (d == Double.NEGATIVE_INFINITY) {
return writer.append("Double.NEGATIVE_INFINITY");
} else if (Double.isNaN(d)) {
return writer.append("Double.NaN");
}
bigDecimal = BigDecimal.valueOf(d);
}
if (bigDecimal.precision() > 10) {
return writer.append("Double.longBitsToDouble(")
Expand Down
11 changes: 11 additions & 0 deletions testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13357,6 +13357,17 @@ private static void checkBoolAndFunc(SqlOperatorFixture f) {
f.checkAgg("bool_and(x)", values4, isNullValue());
}

/** Test case for
* <a href="https://issues.apache.org/jira/projects/CALCITE/issues/CALCITE-6094">
* Linq4j.ConstantExpression.write crashes on special FP values</a>. */
@Test void testInfinityExpression() {
final SqlOperatorFixture f = fixture();
f.check("SELECT CAST(10e70 AS REAL)", "REAL NOT NULL", "Infinity");
f.check("SELECT CAST(-10e70 AS REAL)", "REAL NOT NULL", "-Infinity");
// I could not write a test that generates NaN and triggers this issue.
// I could not write tests with double that trigger this issue.
}

@Test void testBoolOrFunc() {
final SqlOperatorFixture f = fixture();
// not in standard dialect
Expand Down

0 comments on commit ad2a4e6

Please sign in to comment.