You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by mb...@apache.org on 2024/01/11 21:49:55 UTC
(calcite) branch main updated: [CALCITE-6094] Linq4j.ConstantExpression.write crashes on special FP values
This is an automated email from the ASF dual-hosted git repository.
mbudiu pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git
The following commit(s) were added to refs/heads/main by this push:
new ad2a4e6af2 [CALCITE-6094] Linq4j.ConstantExpression.write crashes on special FP values
ad2a4e6af2 is described below
commit ad2a4e6af21ab27952c7152e670783559e49735e
Author: Mihai Budiu <mb...@feldera.com>
AuthorDate: Wed Nov 8 12:08:14 2023 -0800
[CALCITE-6094] Linq4j.ConstantExpression.write crashes on special FP values
Signed-off-by: Mihai Budiu <mb...@feldera.com>
---
.../calcite/linq4j/tree/ConstantExpression.java | 20 ++++++++++++++++++--
.../org/apache/calcite/test/SqlOperatorTest.java | 11 +++++++++++
2 files changed, 29 insertions(+), 2 deletions(-)
diff --git a/linq4j/src/main/java/org/apache/calcite/linq4j/tree/ConstantExpression.java b/linq4j/src/main/java/org/apache/calcite/linq4j/tree/ConstantExpression.java
index 444cd83a78..3f92bc1150 100644
--- a/linq4j/src/main/java/org/apache/calcite/linq4j/tree/ConstantExpression.java
+++ b/linq4j/src/main/java/org/apache/calcite/linq4j/tree/ConstantExpression.java
@@ -106,7 +106,15 @@ public class ConstantExpression extends Expression {
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(")
@@ -118,7 +126,15 @@ public class ConstantExpression extends Expression {
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(")
diff --git a/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java b/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java
index c393a0f848..2eb931d85a 100644
--- a/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java
+++ b/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java
@@ -13357,6 +13357,17 @@ public class SqlOperatorTest {
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