[CALCITE-2529] All numbers are in the same type family (Andrew Pilloud)

The linq4j code generator fails to generate calls to functions
when there is a mix between integer and floating point arguments.
This is due to the incorrect assumption that java integer types
can not be assigned to floating point types. This change fixes
the assignment mapping, enabling the generator to call floating
point functions with integer arguments.

Close apache/calcite#818


Project: http://git-wip-us.apache.org/repos/asf/calcite/repo
Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/25c332d6
Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/25c332d6
Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/25c332d6

Branch: refs/heads/master
Commit: 25c332d609372522e1090cb141ab8ae5f1bdceb8
Parents: 03c88b6
Author: Andrew Pilloud <[email protected]>
Authored: Fri Aug 17 15:44:01 2018 -0700
Committer: Julian Hyde <[email protected]>
Committed: Mon Dec 3 10:57:14 2018 -0800

----------------------------------------------------------------------
 .../apache/calcite/runtime/SqlFunctions.java    | 84 ++------------------
 .../calcite/sql/test/SqlOperatorBaseTest.java   |  6 +-
 .../apache/calcite/linq4j/tree/Primitive.java   |  8 +-
 .../calcite/linq4j/test/PrimitiveTest.java      | 16 ++++
 4 files changed, 31 insertions(+), 83 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/calcite/blob/25c332d6/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java 
b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
index e3f54f7..af008fb 100644
--- a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
+++ b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
@@ -863,10 +863,6 @@ public class SqlFunctions {
     return Math.exp(b0.doubleValue());
   }
 
-  public static double exp(long b0) {
-    return Math.exp(b0);
-  }
-
   // POWER
 
   /** SQL <code>POWER</code> operator applied to double values. */
@@ -878,22 +874,14 @@ public class SqlFunctions {
     return Math.pow(b0, b1.doubleValue());
   }
 
-  public static double power(long b0, long b1) {
-    return Math.pow(b0, b1);
+  public static double power(BigDecimal b0, double b1) {
+    return Math.pow(b0.doubleValue(), b1);
   }
 
   public static double power(BigDecimal b0, BigDecimal b1) {
     return Math.pow(b0.doubleValue(), b1.doubleValue());
   }
 
-  public static double power(long b0, BigDecimal b1) {
-    return Math.pow(b0, b1.doubleValue());
-  }
-
-  public static double power(BigDecimal b0, long b1) {
-    return Math.pow(b0.doubleValue(), b1);
-  }
-
   // LN
 
   /** SQL {@code LN(number)} function applied to double values. */
@@ -901,11 +889,6 @@ public class SqlFunctions {
     return Math.log(d);
   }
 
-  /** SQL {@code LN(number)} function applied to long values. */
-  public static double ln(long b0) {
-    return Math.log(b0);
-  }
-
   /** SQL {@code LN(number)} function applied to BigDecimal values. */
   public static double ln(BigDecimal d) {
     return Math.log(d.doubleValue());
@@ -918,11 +901,6 @@ public class SqlFunctions {
     return Math.log10(b0);
   }
 
-  /** SQL {@code LOG10(number)} function applied to long values. */
-  public static double log10(long b0) {
-    return Math.log10(b0);
-  }
-
   /** SQL {@code LOG10(number)} function applied to BigDecimal values. */
   public static double log10(BigDecimal d) {
     return Math.log10(d.doubleValue());
@@ -1121,11 +1099,6 @@ public class SqlFunctions {
   }
 
   // ACOS
-  /** SQL <code>ACOS</code> operator applied to long values. */
-  public static double acos(long b0) {
-    return Math.acos(b0);
-  }
-
   /** SQL <code>ACOS</code> operator applied to BigDecimal values. */
   public static double acos(BigDecimal b0) {
     return Math.acos(b0.doubleValue());
@@ -1137,11 +1110,6 @@ public class SqlFunctions {
   }
 
   // ASIN
-  /** SQL <code>ASIN</code> operator applied to long values. */
-  public static double asin(long b0) {
-    return Math.asin(b0);
-  }
-
   /** SQL <code>ASIN</code> operator applied to BigDecimal values. */
   public static double asin(BigDecimal b0) {
     return Math.asin(b0.doubleValue());
@@ -1153,11 +1121,6 @@ public class SqlFunctions {
   }
 
   // ATAN
-  /** SQL <code>ATAN</code> operator applied to long values. */
-  public static double atan(long b0) {
-    return Math.atan(b0);
-  }
-
   /** SQL <code>ATAN</code> operator applied to BigDecimal values. */
   public static double atan(BigDecimal b0) {
     return Math.atan(b0.doubleValue());
@@ -1169,18 +1132,13 @@ public class SqlFunctions {
   }
 
   // ATAN2
-  /** SQL <code>ATAN2</code> operator applied to long values. */
-  public static double atan2(long b0, long b1) {
-    return Math.atan2(b0, b1);
-  }
-
-  /** SQL <code>ATAN2</code> operator applied to long/BigDecimal values. */
-  public static double atan2(long b0, BigDecimal b1) {
+  /** SQL <code>ATAN2</code> operator applied to double/BigDecimal values. */
+  public static double atan2(double b0, BigDecimal b1) {
     return Math.atan2(b0, b1.doubleValue());
   }
 
-  /** SQL <code>ATAN2</code> operator applied to BigDecimal/long values. */
-  public static double atan2(BigDecimal b0, long b1) {
+  /** SQL <code>ATAN2</code> operator applied to BigDecimal/double values. */
+  public static double atan2(BigDecimal b0, double b1) {
     return Math.atan2(b0.doubleValue(), b1);
   }
 
@@ -1195,11 +1153,6 @@ public class SqlFunctions {
   }
 
   // COS
-  /** SQL <code>COS</code> operator applied to long values. */
-  public static double cos(long b0) {
-    return Math.cos(b0);
-  }
-
   /** SQL <code>COS</code> operator applied to BigDecimal values. */
   public static double cos(BigDecimal b0) {
     return Math.cos(b0.doubleValue());
@@ -1211,11 +1164,6 @@ public class SqlFunctions {
   }
 
   // COT
-  /** SQL <code>COT</code> operator applied to long values. */
-  public static double cot(long b0) {
-    return 1.0d / Math.tan(b0);
-  }
-
   /** SQL <code>COT</code> operator applied to BigDecimal values. */
   public static double cot(BigDecimal b0) {
     return 1.0d / Math.tan(b0.doubleValue());
@@ -1227,11 +1175,6 @@ public class SqlFunctions {
   }
 
   // DEGREES
-  /** SQL <code>DEGREES</code> operator applied to long values. */
-  public static double degrees(long b0) {
-    return Math.toDegrees(b0);
-  }
-
   /** SQL <code>DEGREES</code> operator applied to BigDecimal values. */
   public static double degrees(BigDecimal b0) {
     return Math.toDegrees(b0.doubleValue());
@@ -1243,11 +1186,6 @@ public class SqlFunctions {
   }
 
   // RADIANS
-  /** SQL <code>RADIANS</code> operator applied to long values. */
-  public static double radians(long b0) {
-    return Math.toRadians(b0);
-  }
-
   /** SQL <code>RADIANS</code> operator applied to BigDecimal values. */
   public static double radians(BigDecimal b0) {
     return Math.toRadians(b0.doubleValue());
@@ -1360,11 +1298,6 @@ public class SqlFunctions {
   }
 
   // SIN
-  /** SQL <code>SIN</code> operator applied to long values. */
-  public static double sin(long b0) {
-    return Math.sin(b0);
-  }
-
   /** SQL <code>SIN</code> operator applied to BigDecimal values. */
   public static double sin(BigDecimal b0) {
     return Math.sin(b0.doubleValue());
@@ -1376,11 +1309,6 @@ public class SqlFunctions {
   }
 
   // TAN
-  /** SQL <code>TAN</code> operator applied to long values. */
-  public static double tan(long b0) {
-    return Math.tan(b0);
-  }
-
   /** SQL <code>TAN</code> operator applied to BigDecimal values. */
   public static double tan(BigDecimal b0) {
     return Math.tan(b0.doubleValue());

http://git-wip-us.apache.org/repos/asf/calcite/blob/25c332d6/core/src/test/java/org/apache/calcite/sql/test/SqlOperatorBaseTest.java
----------------------------------------------------------------------
diff --git 
a/core/src/test/java/org/apache/calcite/sql/test/SqlOperatorBaseTest.java 
b/core/src/test/java/org/apache/calcite/sql/test/SqlOperatorBaseTest.java
index 1a1b750..90e2372 100644
--- a/core/src/test/java/org/apache/calcite/sql/test/SqlOperatorBaseTest.java
+++ b/core/src/test/java/org/apache/calcite/sql/test/SqlOperatorBaseTest.java
@@ -4902,7 +4902,11 @@ public abstract class SqlOperatorBaseTest {
     tester.setFor(
         SqlStdOperatorTable.ATAN2);
     tester.checkType("atan2(2, -2)", "DOUBLE NOT NULL");
-    tester.checkType("atan2(cast(1 as float), -1)", "DOUBLE NOT NULL");
+    tester.checkScalarApprox(
+        "atan2(cast(1 as float), -1)",
+        "DOUBLE NOT NULL",
+        2.3562d,
+        0.0001d);
     tester.checkType(
         "atan2(case when false then 0.5 else null end, -1)", "DOUBLE");
     tester.checkFails(

http://git-wip-us.apache.org/repos/asf/calcite/blob/25c332d6/linq4j/src/main/java/org/apache/calcite/linq4j/tree/Primitive.java
----------------------------------------------------------------------
diff --git a/linq4j/src/main/java/org/apache/calcite/linq4j/tree/Primitive.java 
b/linq4j/src/main/java/org/apache/calcite/linq4j/tree/Primitive.java
index f2e42d3..db4a282 100644
--- a/linq4j/src/main/java/org/apache/calcite/linq4j/tree/Primitive.java
+++ b/linq4j/src/main/java/org/apache/calcite/linq4j/tree/Primitive.java
@@ -47,12 +47,12 @@ public enum Primitive {
       Integer.MAX_VALUE, Integer.SIZE),
   LONG(Long.TYPE, Long.class, 2, 0L, Long.MIN_VALUE, null, null,
       Long.MAX_VALUE, Long.SIZE),
-  FLOAT(Float.TYPE, Float.class, 3, 0F, -Float.MAX_VALUE, -Float.MIN_VALUE,
+  FLOAT(Float.TYPE, Float.class, 2, 0F, -Float.MAX_VALUE, -Float.MIN_VALUE,
       Float.MIN_VALUE, Float.MAX_VALUE, Float.SIZE),
-  DOUBLE(Double.TYPE, Double.class, 3, 0D, -Double.MAX_VALUE, 
-Double.MIN_VALUE,
+  DOUBLE(Double.TYPE, Double.class, 2, 0D, -Double.MAX_VALUE, 
-Double.MIN_VALUE,
       Double.MIN_VALUE, Double.MAX_VALUE, Double.SIZE),
-  VOID(Void.TYPE, Void.class, 4, null, null, null, null, null, -1),
-  OTHER(null, null, 5, null, null, null, null, null, -1);
+  VOID(Void.TYPE, Void.class, 3, null, null, null, null, null, -1),
+  OTHER(null, null, 4, null, null, null, null, null, -1);
 
   public final Class primitiveClass;
   public final Class boxClass;

http://git-wip-us.apache.org/repos/asf/calcite/blob/25c332d6/linq4j/src/test/java/org/apache/calcite/linq4j/test/PrimitiveTest.java
----------------------------------------------------------------------
diff --git 
a/linq4j/src/test/java/org/apache/calcite/linq4j/test/PrimitiveTest.java 
b/linq4j/src/test/java/org/apache/calcite/linq4j/test/PrimitiveTest.java
index d195bc0..45bddcc 100644
--- a/linq4j/src/test/java/org/apache/calcite/linq4j/test/PrimitiveTest.java
+++ b/linq4j/src/test/java/org/apache/calcite/linq4j/test/PrimitiveTest.java
@@ -42,12 +42,16 @@ public class PrimitiveTest {
     assertTrue(Primitive.INT.assignableFrom(Primitive.INT));
     assertTrue(Primitive.INT.assignableFrom(Primitive.SHORT));
     assertFalse(Primitive.INT.assignableFrom(Primitive.LONG));
+    assertFalse(Primitive.INT.assignableFrom(Primitive.FLOAT));
+    assertFalse(Primitive.INT.assignableFrom(Primitive.DOUBLE));
 
     assertTrue(Primitive.LONG.assignableFrom(Primitive.BYTE));
     assertTrue(Primitive.LONG.assignableFrom(Primitive.SHORT));
     assertTrue(Primitive.LONG.assignableFrom(Primitive.CHAR));
     assertTrue(Primitive.LONG.assignableFrom(Primitive.INT));
     assertTrue(Primitive.LONG.assignableFrom(Primitive.LONG));
+    assertFalse(Primitive.LONG.assignableFrom(Primitive.FLOAT));
+    assertFalse(Primitive.LONG.assignableFrom(Primitive.DOUBLE));
 
     // SHORT and CHAR cannot be assigned to each other
 
@@ -56,12 +60,24 @@ public class PrimitiveTest {
     assertFalse(Primitive.SHORT.assignableFrom(Primitive.CHAR));
     assertFalse(Primitive.SHORT.assignableFrom(Primitive.INT));
     assertFalse(Primitive.SHORT.assignableFrom(Primitive.LONG));
+    assertFalse(Primitive.SHORT.assignableFrom(Primitive.FLOAT));
+    assertFalse(Primitive.SHORT.assignableFrom(Primitive.DOUBLE));
 
     assertFalse(Primitive.CHAR.assignableFrom(Primitive.BYTE));
     assertFalse(Primitive.CHAR.assignableFrom(Primitive.SHORT));
     assertTrue(Primitive.CHAR.assignableFrom(Primitive.CHAR));
     assertFalse(Primitive.CHAR.assignableFrom(Primitive.INT));
     assertFalse(Primitive.CHAR.assignableFrom(Primitive.LONG));
+    assertFalse(Primitive.CHAR.assignableFrom(Primitive.FLOAT));
+    assertFalse(Primitive.CHAR.assignableFrom(Primitive.DOUBLE));
+
+    assertTrue(Primitive.DOUBLE.assignableFrom(Primitive.BYTE));
+    assertTrue(Primitive.DOUBLE.assignableFrom(Primitive.SHORT));
+    assertTrue(Primitive.DOUBLE.assignableFrom(Primitive.CHAR));
+    assertTrue(Primitive.DOUBLE.assignableFrom(Primitive.INT));
+    assertTrue(Primitive.DOUBLE.assignableFrom(Primitive.LONG));
+    assertTrue(Primitive.DOUBLE.assignableFrom(Primitive.FLOAT));
+    assertTrue(Primitive.DOUBLE.assignableFrom(Primitive.DOUBLE));
 
     // cross-family assignments
 

Reply via email to