🔍
Upsert
Triad acts similarly to SQL "UPDATE OR INSERT ..." expression. It also supports creating new fields in addition to updating and inserting existing fields.
First argument is expected to be either a table or a symbol of a table for in-place modification.
Second argument is expected to be a table.
Third argument must be a two-element list of key symbol vectors. Key field vectors must match in length. The first field vector corresponds to the first table, the second one - to the second table.
It is recommended to have (composite) an index for the keys of the left table. It will speed-up processing.
Let's have a look at some examples:
o)a:(+:)`a`b`c`d!(!10;!10;!10;!10);
o)b:(+:)`a`b`c!(8+!5;8+!5;10+!5);
o)upsert[a;b;(`a`b;`a`b)]
a b c d
-----------
0 0 0 0
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
6 6 6 6
7 7 7 7
8 8 10 8
9 9 11 9
10 10 12 0N
11 11 13 0N
12 12 14 0N
o)
And in-place modification example:
o)a:(+:)`a`b`c`d!(!10;!10;!10;!10);
o)b:(+:)`a`b`c!(8+!5;8+!5;10+!5);
o)upsert[`a;b;(`a`b;`a`b)]
`a
o)a
a b c d
-----------
0 0 0 0
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
6 6 6 6
7 7 7 7
8 8 10 8
9 9 11 9
10 10 12 0N
11 11 13 0N
12 12 14 0N
o)
See creating new fields:
o)a:(+:)`a`b`c!(!10;!10;!10); b:(+:)`a`b`c`d`e!(2+!5;2+!5;10+!5;20+!5;5#,"123"); upsert[a;b;(`a`b;`a`b)]
a b c d e
---------------
0 0 0 0N 0N0
1 1 1 0N 0N0
2 2 10 20 "123"
3 3 11 21 "123"
4 4 12 22 "123"
5 5 13 23 "123"
6 6 14 24 "123"
7 7 7 0N 0N0
8 8 8 0N 0N0
9 9 9 0N 0N0
o)