Search code examples
kdb+k

Upsert list of lists to the table


Let's see the definition of upsert:

q)upsert
.[;();,;]

So it is just an Amend Entire:

.[d;();v;y]   <=>   v[d;y]

q).[1 2; (); ,; 3 4 5]
1 2 3 4 5

It looks like join , under the hood. But applying the same approach to adding some rows to a table gives different results:

q)show t:((`a`b!1 2);(`a`b!3 4))
a b
---
1 2
3 4
q).[t;();,;(5 6;7 8)]
a b
---
1 2
3 4
5 6
7 8
q),[t;(5 6;7 8)]
`a`b!1 2
`a`b!3 4
5 6
7 8

For some reason q doesn’t want to join , list of lists to the table in the same way as in Amend Entire. I’m wondering why.

Could you give me some directions please?


Solution

  • One should draw an analogy between amend entire and assign through operator, not join:

    q)show t:((`a`b!1 2);(`a`b!3 4))
    a b
    ---
    1 2
    3 4
    q),:[t;(5 6;7 8)]
    q)t
    a b
    ---
    1 2
    3 4
    5 6
    7 8
    

    As you can see, the latter is not the same as

    q)t:((`a`b!1 2);(`a`b!3 4))
    q)show t:t,(5 6;7 8)
    `a`b!1 2
    `a`b!3 4
    5 6
    7 8
    

    The section I linked even mentions that ,: "is syntactic sugar for Amend At."