"Type error" comes up when using a variable in the map function

A query like this works fine:
from(bucket: “X”)
|> range(start: 2022-01-01T00:00:00Z, stop: 2022-01-02T00:00:00Z)
|> group()
|> map(
fn: (r) => ({r with Errorx: r[“Y”]})
)

But when I replace “Y” with a variable shown below, I get an error (invalid: type error @7:37-7:38: expected int but found string)

vary = “Y”
from(bucket: “X”)
|> range(start: 2022-01-01T00:00:00Z, stop: 2022-01-02T00:00:00Z)
|> group()
|> map(
fn: (r) => ({r with Errorx: r[vary]})
)

Why can’t I use a variable in the place of a column name when mapping? Please let me know how to resolve this error.

Thank you.

Hi @deb

Some time has passed without any replies, so I want to begin the dialogue. Looks like you want the value of Errorx to be the value in another column, but that column’s name isn’t hard-coded, it’s stored in a variable. If I’m understanding correctly, then perhaps map’s if-then-else construct would work for you?

Hi @phill

Thank you for your response. Ok so with the if-then-else, all the possibilities will be included like below?

vary = “Y”
from(bucket: “X”)
|> range(start: 2022-01-01T00:00:00Z, stop: 2022-01-02T00:00:00Z)
|> group()
|> map(
fn: (r) => ({r with Errorx:
if vary == “Y”
then r[“Y”]
else if vary == “Z”
then r[“Z”]
else r[“I”]
})
)

Why didn’t it work with r[vary]?

Thank you,

@deb In this example from the map documentation (linked above)

|> map(fn: (r) => ({time: r._time, source: r.tag, alert: if r._value > 10 then true else false}))

the _value column of the record is used as the test condition. In your case, what is determining whether to choose Y, Z or I? Let’s say it’s also the _value column. Then something like

|> map(fn: (r) => ({r with Errorx: if r._value ==  (select Y column) then r["Y"]}))
|> map(fn: (r) => ({r with Errorx: if r._value == (select Z column)  then r["Z"] else r["I"]}))

might to the job.

A couple disclaimers: (a) I don’t think else if is an allowable construct in map so I’ve broken it into two maps. (b) I’ve made assumptions about your data, so this approach may not be helpful (goes back to what is determining the column choice).

I think vary inside map is not allowed to prevent a runtime error, that is vary gets set to a non-existent column (e.g., “QQ”, “21.6”).

Hope this moves you closer to a solution.
Phill

Hi @phill,

Thank you for your help. I didn’t want it to depend on the _value column, I wanted vary to determine the column name. I’m actually working on something different with a function, where vary is an input and map is used to do something else in the function. Thanks for your help, I’ll use the if-else to avoid the r[vary] error. The else if actually worked for me:

vary = "Y"
from(bucket: “X”)
|> range(start: 2022-01-01T00:00:00Z, stop: 2022-01-02T00:00:00Z)
|> group()
|> map(
    fn: (r) => ({r with Errorx: 
      if vary == "Y"
        then r["Y"]
      else if vary == "Z"
        then r["Z"]
      else r["I"]
    })
)

Thank you,

1 Like

@deb Great news! I figured you gave us a code snippet of something bigger and vary was playing a deeper role in your script. Also, thanks for expanding my knowledge of map.

1 Like