Making A Copy and Deep Copy of a Table

638 views
Skip to first unread message

Tom Tzad

unread,
Nov 15, 2019, 6:05:58 AM11/15/19
to Kdb+ Personal Developers
Hello,

I'm trying to relate C++ concepts to q. I read that "In q, copy is done on a write operation". I am looking to make a deep and shallow copy of a table. I do not want to incur a write operation, or write my table to disk to make my copy.
I've been playing around below, and I'm wondering if I can make a deep copy without saving my table (t1) to disk. Please see below:

Our base table:
q)n:1000
q
)t2:([id:til n] sym:n?`2; price:n?90.)
q)t1:([] `t2$id:til n; sym:n?`2; price:n?90.)

1. Making a shallow copy without a write operation to the original table:
q)t3:flip `id`sym`price!"jsf"$\:()
q)t3
id sym price
------------
q)insert[`
t3;t1]
q
)meta t3
c    
| t f a
-----| -----
id  
| j    
sym  
| s    
price
| f    
q
)-16!t1 / This is a shallow copy since it has no foreign key relation to t2 (and is not a reference to t1)
1i

2. Making a deep copy (maintain foreign key relationship):
q)`:ourTable set t1 / First save t1 down
`:ourTable
q)t4:get `:ourTable / Then get it as t4.
q)meta t4
c    | t f  a
-----| ------
id   | j t2 
sym  | s    
price| f    
q)-16!t1
1i / Not a reference to t1

Is there any way to create a deep copy without saving my table t1 to disk?

Cheers

Flying

unread,
Nov 15, 2019, 8:02:24 AM11/15/19
to Kdb+ Personal Developers
Try this: -9!-8!t

András Dőtsch

unread,
Nov 15, 2019, 11:54:02 AM11/15/19
to Kdb+ Personal Developers
Hi Tom,

"Copy on write" means that you never have to worry about creating a physical copy of your data in order to protect it from unwanted modifications.
For example:

q)l1:l2:1 2 3 4 5     // l1 and l2 refers to the same data
q)l2[2]: 30           // l2 will be the modified copy of l1
q)l1                  // l1 is the same 
1 2 3 4 5
q)l2                  // only l2 has the change
1 2 30 4 5

The same works with tables and any other data.

Regards,
Andras

Tom Tzad

unread,
Nov 16, 2019, 2:22:14 AM11/16/19
to Kdb+ Personal Developers
This creates a shallow copy, not a deep copy.

Tom Tzad

unread,
Nov 16, 2019, 2:22:14 AM11/16/19
to Kdb+ Personal Developers
Thanks for your response Andras. But if I do want a physical copy (a deep copy), how can I get one without performing a write or a get on a saved table?

András Dőtsch

unread,
Nov 16, 2019, 10:02:28 AM11/16/19
to Kdb+ Personal Developers
See Flying's answer.

Tom Tzad

unread,
Nov 16, 2019, 1:37:44 PM11/16/19
to Kdb+ Personal Developers
Flying's approach seems to create a shallow copy as opposed to a deep copy, as the foreign key relationship after serializing and deserializing vanishes.

I am wondering how I create a deep copy (i.e. keep the foreign key relation).

q)meta t1 / Original table with foreign key relation
c    
| t f  a
-----| ------
id  
| j t2  
sym  
| s    
price
|
f    
q
)meta -9!-8!t1 / Flying's approach, creating a table without a foreign key relation

c    | t f a
-----| -----
id   | j    
sym  | s    
price| f

What am I not understanding here?

Regards,
Tom

On Saturday, November 16, 2019 at 10:02:28 AM UTC-5, András Dőtsch wrote:
See Flying's answer.

András Dőtsch

unread,
Nov 16, 2019, 3:45:09 PM11/16/19
to personal...@googlegroups.com
Hi,

-9!-8!x is the equivalent of getting data over IPC, so it creates a physical copy. Enums are converted back to their values over IPC, this is why you loose the foreign key relation. (Loosing the relation doesn't mean it was a shallow copy.) You can recreate the link by a simple update:

update `t2$id from `t1

If you have a table with simple columns (like in your example), you can also force a physical copy like this:

t1c:t1; t1c[0]:t1c[0]

It will preserve the linkages to foreign tables too.

(Again, all of this is quite pointless, as we usually work against unnecessary RAM consumption.)

Regards,
András

--
You received this message because you are subscribed to the Google Groups "Kdb+ Personal Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to personal-kdbpl...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/personal-kdbplus/8028b2bb-a5dd-4eb6-bb10-553c24299e46%40googlegroups.com.

Tom Tzad

unread,
Nov 17, 2019, 2:34:35 AM11/17/19
to Kdb+ Personal Developers
Thanks for explaining Andras. Makes sense now.

Cheers,
Tom
To unsubscribe from this group and stop receiving emails from it, send an email to personal...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages