Extract scalar value: An internal error has occurred

Hi,
I’m struggling to extract a scalar value, even if I read the docs and had it already working on another table/query.

My my_table looks like this:

Then I want to sum the session_id column and extract that value

tot = my_table 
  |> sum(column: "session_id") 
  |> first(column: "session_id") 
  |> findColumn(fn: (key) => true, column: "session_id") 
tot_scalar = tot[0]

but the findColumn gives me An internal error has occurred.

What am I doing wrong? I don’t get it since it was working for me in another query that looks quite similar to this one.
Thanks in advance

@eloparco, what version of InfluxDB are you using? What version of Flux are you running? To see your Flux version, run the following in your Data Explorer:

import "runtime"
import "csv"

csv.from(csv: "#datatype,string,long,string
#group,false,false,false
#default,,,
,result,table,version
,,0,${runtime.version()}
")

Hi @scott, I’m using version v0.104.0.

I’m using the Flux REPL built from Flux 0.113.0 (using the data from your screenshot) and it works for me:

Sidenote: you don’t need the first() function. sum() reduces the table down to a single row.

import "array"

my_table = array.from(rows: [
 {class: "1-2", session_id: 2},
 {class: "2-3", session_id: 1},
 {class: "3-4", session_id: 1}
])

tot = my_table 
  |> sum(column: "session_id") 
  |> findColumn(fn: (key) => true, column: "session_id") 
  
tot_scalar = tot[0]

// tot_scalar returns 4

One thing to note is that the Data Explorer and dashboard cells cannot display scalar values. They only support streams of tables, so if you’re trying to display this in a cell, it needs to inside a stream of tables. You could do something like:

import "array"

tot = my_table 
  |> sum(column: "session_id") 
  |> findColumn(fn: (key) => true, column: "session_id") 
  
tot_scalar = tot[0]

array.from(rows: [{ _value: int(v: tot_scalar) }])

Thanks, putting array.from(rows: [{ _value: int(v: tot_scalar) }]) works as expected.

What still doesn’t work in my case is:

tot = my_table 
  |> sum(column: "session_id") 
  |> findColumn(fn: (key) => true, column: "session_id") 
  
tot_scalar = tot[0]
my_table  // <-------  An internal error has occurred

Here I get the error just because I added my_table at the end.

Or (what I would like to do in reality):

tot = my_table 
  |> sum(column: "session_id") 
  |> findColumn(fn: (key) => true, column: "session_id") 
  
tot_scalar = tot[0]
my_table |> map(fn: (r) => ({ r with _value: float(v: r.session_id) / float(v: tot_scalar) * 100.0 })).
//  An internal error has occurred

With your minimal example, these two scenarios work fine while in my case they don’t.
I obtain my_table after some transformations (join between a timeseries and SQL data + some processing). But I don’t get why this is happening since the raw data representation is the one I put in the screenshot (and that you reproduced using array).

Any idea is well accepted :slight_smile:

With your query above, my_table doesn’t resolve to a stream of tables; it resolves to an array. findColumn() returns an array of values in the specified column. The Data Explorer can’t display an array. To do what you’re hoping to do, you need to restructure your query a bit:

tot = (
  my_table
    |> sum(column: "session_id")
    |> findColumn(fn: (key) => true, column: "session_id")
  )[0]

my_table
  |> map(fn: (r) => ({r with
    _value: float(v: r.session_id) / float(v: tot) * 100.0
  }))