1

Here is a table and I want to update the column with the following script.

init=array(DOUBLE, 0) val
t=table(1..5 as id, `a`a`b`b`c as sym, 2022.01.01 + 1..5 as date, [1.0, 2.0, 3.0, 4.0, 5.0] as val)
update t set v=accumulate(def(a, b){c=a;c.append!(b);return c}, val, init) 

The accumulate function in this script does not process columns directly because they are vectors. So is there any way to convert a column into a tuple and pass them to the accumulate function?

dbaa9948
  • 189
  • 2
  • 10

1 Answers1

0

Method 1: Use loop and asis. The update statement can be rewritten as follows:

update t set v = accumulate(def(a, b){c=a;c.append!(b);return c}, loop(asis, val), init) 

Method 2: Use cast directly. The update statement can be rewritten as follows:

update t set v = accumulate(def(a, b){c=a;c.append!(b);return c}, val$ANY, init)

The val column of the new t table is of ANY type.

Note: cast is more efficient than loop and asis for the conversion. And cast is 50% faster than loop and asis.

This method works for versions prior to 1.30.21/2.00.9. Starting from 1.30.21/2.00.9, the higher-order functions have been enhanced. You can directly run the script in your question.

Shena
  • 341
  • 1
  • 5