modelop / hadrian

Implementations of the Portable Format for Analytics (PFA)
Apache License 2.0
130 stars 49 forks source link

[aurelius] assigning whole number to a variable of type double #47

Open taunometsalu opened 6 years ago

taunometsalu commented 6 years ago

If I try to assign a constant whole number to a variable of type double, it works with Titus:

library(aurelius)
pfaDocument = pfa_document(
  input = avro_array(avro_double),
  output = avro_array(avro_double),
  action = expression(
    out <- input,
    out[0] <- 5.0,
    out
  )
)

library(jsonlite)
engine = pfa_engine(pfaDocument)
x = 1:2
engine$action(x)

But it gives error 'Assignment conversion not possible from type "int" to type "java.lang.Double"' in Hadrian:

f = "/usr/local/src/gdrive/results/pfa/int_as_double_example_error1.pfa"
write_pfa(pfaDocument, file = f, pretty = TRUE)
library(jsonlite)
tmp1 = tempfile(fileext = ".json")
tmp2 = tempfile(fileext = ".json")
write(minify(toJSON(x, auto_unbox = TRUE)), file = tmp1)
cmd = paste0("cd /usr/local/src/gdrive/; touch ", tmp2, "; ",
             "java -jar scripts/hadrian/hadrian-standalone-0.8.1-jar-with-dependencies.jar -i json -o json ",
             f, " ", tmp1, " > ", tmp2)
system(cmd)

PFA file:

{
  "input": {
    "type": "array",
    "items": "double"
  },
  "output": {
    "type": "array",
    "items": "double"
  },
  "action": [
    {
      "let": {
        "out": "input"
      }
    },
    {
      "do": [
        {
          "let": {
            "tmp_7": 5
          }
        },
        {
          "set": {
            "out": {
              "attr": "out",
              "path": [
                0
              ],
              "to": "tmp_7"
            }
          }
        },
        "tmp_7"
      ]
    },
    "out"
  ]
}

I tried to use always_decimal parameter, but then it gives another error 'path index for an array must resolve to a long or int; item 0 is a "double"' because the path index in out[0] is also converted to double.

f = "/usr/local/src/gdrive/results/pfa/int_as_double_example_error2.pfa"
write_pfa(pfaDocument, file = f, pretty = TRUE, always_decimal = TRUE)
library(jsonlite)
tmp1 = tempfile(fileext = ".json")
tmp2 = tempfile(fileext = ".json")
write(minify(toJSON(x, auto_unbox = TRUE)), file = tmp1)
cmd = paste0("cd /usr/local/src/gdrive/; touch ", tmp2, "; ",
             "java -jar scripts/hadrian/hadrian-standalone-0.8.1-jar-with-dependencies.jar -i json -o json ",
             f, " ", tmp1, " > ", tmp2)
system(cmd)

PFA file:

{
  "input": {
    "type": "array",
    "items": "double"
  },
  "output": {
    "type": "array",
    "items": "double"
  },
  "action": [
    {
      "let": {
        "out": "input"
      }
    },
    {
      "do": [
        {
          "let": {
            "tmp_7": 5.0
          }
        },
        {
          "set": {
            "out": {
              "attr": "out",
              "path": [
                0.0
              ],
              "to": "tmp_7"
            }
          }
        },
        "tmp_7"
      ]
    },
    "out"
  ]
}

How can I force aurelius to use always_decimal only in the places where required?

The only workaround I see at the moment is to manually change the exported .pfa file (make sure that 0 is printed as 0 and 5 as 5.0).