Entries in xcols (2)

How can I reorder column names of a splayed table on disk?

Modify the file .d in the splayed table directory. The contents are editable via operators get and set.

$ rlwrap q sp.q
KDB+ 2.7 2011.02.16 Copyright © 1993-2011 Kx Systems
+`p`city!(`p$`p1`p2`p3`p4`p5`p6`p1`p2;`london`london`london..
(+(,`color)!,`blue`green`red)!+(,`qty)!,900 1000 1200
+`s`p`qty!(`s$`s1`s1`s1`s2`s3`s4;`p$`p1`p4`p6`p2`p2`p4;300 ..
q)sp
s p qty
---------
s1 p1 300
s1 p2 200
s1 p3 400
q)`:splaydir/ set sp
`:splaydir/
q)\ls -a splaydir
,"."
".."
".d" // this is where the col names live
,"p"
"qty"
,"s"
q)get `:splaydir/.d
`s`p`qty
q)`:splaydir/.d set reverse get `:splaydir/.d
`:splaydir/.d
q)get `:splaydir/.d
`qty`p`s
q)

Verify with a fresh kdb instance:

$ rlwrap q splaydir
KDB+ 2.7 2011.02.16 Copyright © 1993-2011 Kx Systems
q)\v
,`splaydir
q)splaydir
qty p s
-------
300 0 0
200 1 0
400 2 0
q)

See also: xcol and xcols faq

What is the difference between the functions xcol and xcols?

Use xcol to rename columns. Note that it doesn't require a complete list of columns.

$ rlwrap q sp.q
KDB+ 2.7 2011.02.16 Copyright © 1993-2011 Kx Systems
+`p`city!(`p$`p1`p2`p3`p4`p5`p6`p1`p2;`london`london`london`l..
(+(,`color)!,`blue`green`red)!+(,`qty)!,900 1000 1200
+`s`p`qty!(`s$`s1`s1`s1`s2`s3`s4;`p$`p1`p4`p6`p2`p2`p4;300 200 100 400 200 300)
q)sp
s p qty
---------
s1 p1 300
s1 p2 200
s1 p3 400
s1 p4 200
q)`p`s xcol sp // rename the first two cols
p s qty
---------
s1 p1 300
s1 p2 200
s1 p3 400
s1 p4 200
q)

Use xcols to reorder columns:

q)(reverse cols sp) xcols sp
qty p s
---------
300 p1 s1
200 p2 s1
400 p3 s1
200 p4 s1

One way to remember which is which is that, just as rename precedes reorder (in alphabetical order), so does xcol precede xcols.