Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@
import org.apache.calcite.sql.JoinType;
import org.apache.calcite.sql.SqlAggFunction;
import org.apache.calcite.sql.SqlBasicCall;
import org.apache.calcite.sql.SqlBasicTypeNameSpec;
import org.apache.calcite.sql.SqlBinaryOperator;
import org.apache.calcite.sql.SqlCall;
import org.apache.calcite.sql.SqlDataTypeSpec;
import org.apache.calcite.sql.SqlDialect;
import org.apache.calcite.sql.SqlDynamicParam;
import org.apache.calcite.sql.SqlIdentifier;
Expand Down Expand Up @@ -1493,7 +1495,7 @@
}

/** Converts a {@link RexLiteral} to a {@link SqlLiteral}. */
public static SqlNode toSql(RexLiteral literal) {

Check failure on line 1498 in core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=apache_calcite&issues=AZz0APf9WT_PiSiGD54F&open=AZz0APf9WT_PiSiGD54F&pullRequest=4838
SqlTypeName typeName = literal.getTypeName();
switch (typeName) {
case SYMBOL:
Expand Down Expand Up @@ -1536,8 +1538,16 @@
case NUMERIC:
case EXACT_NUMERIC: {
if (SqlTypeName.APPROX_TYPES.contains(typeName)) {
return SqlLiteral.createApproxNumeric(
castNonNull(literal.getValueAs(Double.class)).toString(), POS);
final Double d = castNonNull(literal.getValueAs(Double.class));
// BigDecimal cannot represent IEEE 754 special values (NaN, ±Infinity).
if (!Double.isFinite(d)) {
final SqlNode strLiteral =
SqlLiteral.createCharString(d.toString(), POS);
final SqlDataTypeSpec typeSpec =
new SqlDataTypeSpec(new SqlBasicTypeNameSpec(typeName, POS), POS);
return SqlStdOperatorTable.CAST.createCall(POS, strLiteral, typeSpec);
}
return SqlLiteral.createApproxNumeric(d.toString(), POS);
} else {
return SqlLiteral.createExactNumeric(
castNonNull(literal.getValueAs(BigDecimal.class)).toPlainString(), POS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11913,5 +11913,39 @@ public Sql schema(CalciteAssert.SchemaSpec schemaSpec) {
.schema(CalciteAssert.SchemaSpec.JDBC_SCOTT)
.ok(expected2);
}
/**
* Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-7432">[CALCITE-7432]
* NumberFormatException when convert `NaN` literal to sql</a>.
*
* <p>Tests support for all IEEE 754 floating-point special values:
Comment thread
Dwrite marked this conversation as resolved.
* NaN, positive/negative infinity, signed zero, and subnormal values.
*/
@Test void testCastFloatingPointSpecialValuesToDouble() {
// Test NaN
sql("select cast('NaN' as DOUBLE)")
.ok("SELECT *\n"
+ "FROM (VALUES (CAST('NaN' AS DOUBLE))) AS \"t\" (\"EXPR$0\")");

// Test Positive Infinity
sql("select cast('Infinity' as DOUBLE)")
.ok("SELECT *\n"
+ "FROM (VALUES (CAST('Infinity' AS DOUBLE))) AS \"t\" (\"EXPR$0\")");

// Test Negative Infinity
sql("select cast('-Infinity' as DOUBLE)")
.ok("SELECT *\n"
+ "FROM (VALUES (CAST('-Infinity' AS DOUBLE))) AS \"t\" (\"EXPR$0\")");

// Test Negative Zero
sql("select cast('-0.0' as DOUBLE)")
.ok("SELECT *\n"
+ "FROM (VALUES (0E0)) AS \"t\" (\"EXPR$0\")");

// Test Subnormal values
sql("select cast('1e-310' as DOUBLE)")
.ok("SELECT *\n"
+ "FROM (VALUES (1.0E-310)) AS \"t\" (\"EXPR$0\")");
}

}
Loading