deephaven / deephaven-core

Deephaven Community Core
Other
255 stars 80 forks source link

Move manually added code in QueryLanguageFunctionUtils to QueryLanguageFunctionGenerator #2184

Open jcferretti opened 2 years ago

jcferretti commented 2 years ago

QueryLanguageFunctionGenerator is supposed to be the generator for QueryLanguageFunctionUtils.java; in a couple of PRs of late that went unnoticed and changes were made directly to the generated file.

jmao-denver commented 2 years ago

Below is the stashed changes to the generator for #2044

--- a/engine/table/src/main/java/io/deephaven/engine/table/impl/lang/QueryLanguageFunctionGenerator.java
+++ b/engine/table/src/main/java/io/deephaven/engine/table/impl/lang/QueryLanguageFunctionGenerator.java
@@ -10,6 +10,7 @@ import com.github.javaparser.ast.expr.BinaryExpr;
 import java.io.*;
 import java.text.*;
 import java.time.LocalDate;

 public class QueryLanguageFunctionGenerator {

@@ -295,6 +296,11 @@ public class QueryLanguageFunctionGenerator {
          *
          * See the language specification, or comments in the parser, for more details.
          */
+        MessageFormat castFromNumberObjFormatter = new MessageFormat("" +
+                "    public static {1} {1}Cast(Object a)'{'\n" +
+                "        return a==null ? QueryConstants.NULL_{4} : ((Number)a).{1}Value();\n" +
+                "    '}'");
+
         MessageFormat castFromObjFormatter = new MessageFormat("" +
                 "    public static {1} {1}Cast(Object a)'{'\n" +
                 "        return a==null ? QueryConstants.NULL_{4} : ({1})a;\n" +
@@ -319,6 +325,7 @@ public class QueryLanguageFunctionGenerator {

         buf.append("package io.deephaven.engine.table.impl.lang;\n\n");

+        buf.append("import java.lang.Number;\n");
         buf.append("import io.deephaven.util.QueryConstants;\n");
         buf.append("import org.jpy.PyObject;\n\n");

@@ -556,7 +563,11 @@ public class QueryLanguageFunctionGenerator {
         // Functions for null-safe casts from Object to primitive types
         for (Class<?> c : classes) {
             // the plus and Object are just so we don't get a npe
-            append(buf, castFromObjFormatter, BinaryExpr.Operator.PLUS, c, Object.class);
+            if (c == char.class) {
+                append(buf, castFromObjFormatter, BinaryExpr.Operator.PLUS, c, Object.class);
+            } else {
+                append(buf, castFromNumberObjFormatter, BinaryExpr.Operator.PLUS, c, Object.class);
+            }
         }

         // Special casts for PyObject to primitive
@@ -686,9 +697,9 @@ public class QueryLanguageFunctionGenerator {
         testBuf.append("}\n");

         String fileName =
-                "./engine/table/src/main/java/io/deephaven/engine/tables/lang/QueryLanguageFunctionUtils.java";
+                "./engine/table/src/main/java/io/deephaven/engine/table/impl/lang/QueryLanguageFunctionUtils.java";
         String testFileName =
-                "./engine/table/src/test/java/io/deephaven/engine/tables/lang/TestLanguageFunctionUtil.java";
+                "./engine/table/src/test/java/io/deephaven/engine/table/impl/lang/TestLanguageFunctionUtil.java";
         try {
             try (BufferedWriter out = new BufferedWriter(new FileWriter(fileName))) {
                 out.write(buf.toString());