You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@drill.apache.org by dz...@apache.org on 2021/12/06 07:42:29 UTC

[drill] branch master updated: DRILL-8066: Convert non-finite floating point literals to string RexLiterals (#2393)

This is an automated email from the ASF dual-hosted git repository.

dzamo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/drill.git


The following commit(s) were added to refs/heads/master by this push:
     new 58d8484  DRILL-8066: Convert non-finite floating point literals to string RexLiterals (#2393)
58d8484 is described below

commit 58d848426d373c9aacfc499cdd65473705cfcee6
Author: James Turton <91...@users.noreply.github.com>
AuthorDate: Mon Dec 6 09:41:37 2021 +0200

    DRILL-8066: Convert non-finite floating point literals to string RexLiterals (#2393)
---
 .../exec/planner/logical/DrillConstExecutor.java   | 14 ++++++++++
 .../exec/fn/impl/TestMathFunctionsWithNanInf.java  | 30 ++++++++++++++++++++++
 2 files changed, 44 insertions(+)

diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillConstExecutor.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillConstExecutor.java
index dd5b62e..2e1c854 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillConstExecutor.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillConstExecutor.java
@@ -184,12 +184,26 @@ public class DrillConstExecutor implements RexExecutor {
           case FLOAT4: {
             float value = (materializedExpr.getMajorType().getMode() == TypeProtos.DataMode.OPTIONAL) ?
                 ((NullableFloat4Holder) valueHolder).value : ((Float4Holder) valueHolder).value;
+
+            // +Infinity, -Infinity and NaN must be represented as strings since
+            // BigDecimal cannot represent them.
+            if (!Float.isFinite(value)) {
+              return rexBuilder.makeLiteral(Float.toString(value));
+            }
+
             return rexBuilder.makeLiteral(new BigDecimal(value),
                 TypeInferenceUtils.createCalciteTypeWithNullability(typeFactory, SqlTypeName.FLOAT, newCall.getType().isNullable()), false);
           }
           case FLOAT8: {
             double value = (materializedExpr.getMajorType().getMode() == TypeProtos.DataMode.OPTIONAL) ?
                 ((NullableFloat8Holder) valueHolder).value : ((Float8Holder) valueHolder).value;
+
+            // +Infinity, -Infinity and NaN must be represented as strings since
+            // BigDecimal cannot represent them.
+            if (!Double.isFinite(value)) {
+              return rexBuilder.makeLiteral(Double.toString(value));
+            }
+
             return rexBuilder.makeLiteral(new BigDecimal(value),
                 TypeInferenceUtils.createCalciteTypeWithNullability(typeFactory, SqlTypeName.DOUBLE, newCall.getType().isNullable()), false);
           }
diff --git a/exec/java-exec/src/test/java/org/apache/drill/exec/fn/impl/TestMathFunctionsWithNanInf.java b/exec/java-exec/src/test/java/org/apache/drill/exec/fn/impl/TestMathFunctionsWithNanInf.java
index d2b6c3f..d6bb811 100644
--- a/exec/java-exec/src/test/java/org/apache/drill/exec/fn/impl/TestMathFunctionsWithNanInf.java
+++ b/exec/java-exec/src/test/java/org/apache/drill/exec/fn/impl/TestMathFunctionsWithNanInf.java
@@ -543,6 +543,36 @@ public class TestMathFunctionsWithNanInf extends BaseTestQuery {
       evalTest(table_name, json, query, columns, values);
     }
 
+  @Test
+  public void testNanInfLiteralConversion() throws Exception {
+    String query =
+      "select " +
+      " cast('Infinity' as float) float_inf, " +
+      " cast('-Infinity' as float) float_ninf, " +
+      " cast('NaN' as float) float_nan, " +
+      " cast('Infinity' as double) double_inf, " +
+      " cast('-Infinity' as double) double_ninf, " +
+      " cast('NaN' as double) double_nan";
+
+    String[] columns = {
+      "float_inf", "float_ninf", "float_nan",
+      "double_inf", "double_ninf", "double_nan"
+    };
+
+    Object[] values = {
+      Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY, Float.NaN,
+      Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NaN
+    };
+
+    testBuilder()
+      .sqlQuery(query)
+      .ordered()
+      .baselineColumns(columns)
+      .baselineValues(values)
+      .build()
+      .run();
+  }
+
     private void evalTest(String table_name, String json, String query, String[] columns, Object[] values) throws Exception {
       File file = new File(dirTestWatcher.getRootDir(), table_name);
       try {