cancel
Showing results for 
Search instead for 
Did you mean: 

upsert to dictionary of tables?

Angela
New Contributor
Hi,

So it's better/faster to do upsert with the symbol "`t upsert ..." than upsert and assign "t:t upsert ...", as show here, right?

q)t0:t1:([]sym:`a`b;data:1 2)
q)\t do[50000;`t1 upsert ([]sym:`a`b;data:1 2)]
27
q)t1:t0
q)\t do[50000;t1:t1 upsert ([]sym:`a`b;data:1 2)]
1396

Now if I have a dictionary of tables, I can upsert using the "symbol" way like this:
q)t:`1`2`3!3#enlist flip `sym`data!(`$();`int$())
q)t
1| +`sym`data!(`symbol$();`int$())
2| +`sym`data!(`symbol$();`int$())
3| +`sym`data!(`symbol$();`int$())
q)`t.1 upsert ([]sym:`a`b;data:1 2)
`t.1

But if the table dictionary keys like `1 is in a function local variable (ie. x), is there a way to do upsert like with "`t.1"?

thanks
5 REPLIES 5

matthew_clark
New Contributor
Hi Angela,

It seems like you were very close to having something working there. My only suggestion surrounds using the sv keyword in order to generate the symbol name for upsert in place. I've timed this and it doesn't seem to be too impactful on the time taken.

q)\t:50000 sv[`;`t,x] upsert ([]sym:`a`b;data:1 2)
76

Here x is just equal to the symbol 1. And instead of a do loop here I am using the notation \t:X which just performs the operation X times while timing it. 

A suggestion made to me can improve on this further. Instead of using the sv keyword, which takes up more time you can use the @ operator with a symbol input to perform the upsert in place. Seems to save a substantial amount of time (almost half):

q)\t:50000 sv[`;`t,x] upsert ([]sym:`a`b;data:1 2)
76
q)\t:50000 @[`t;x;,;([]sym:`a`b;data:1 2)
42

Hope this helps!



Thank you! I was trying with the @ operator but wasn't quite there, and didn't know it's even possible with the sv function!

thanks

I suggest using the variadic apply '.' as it supports all depths:

t:`1`2`3!3#enlist flip `sym`data!(`$();`int$())


.[`t;enlist `1;upsert;([]sym:`a`b;data:1 2)]

`t


t

1| +`sym`data!(`a`b;1 2i)

2| +`sym`data!(`symbol$();`int$())

3| +`sym`data!(`symbol$();`int$())


t[`4]: enlist[`inner]!enlist flip `sym`data!(`$();`int$())


t

1| +`sym`data!(`a`b;1 2i)

2| +`sym`data!(`symbol$();`int$())

3| +`sym`data!(`symbol$();`int$())

4| (,`inner)!,+`sym`data!(`symbol$();`int$())


.[`t;(`4;`inner);upsert;([]sym:`a`b;data:1 2)]

`t


t

1| +`sym`data!(`a`b;1 2i)

2| +`sym`data!(`symbol$();`int$())

3| +`sym`data!(`symbol$();`int$())

4| (,`inner)!,+`sym`data!(`a`b;1 2i)


Thanks!