Search code examples
kdb

Apply parted attribute on a column without loosing sorted attribute on another column after splay in kdb


We have a table trade which is sorted on time column

q)trade:([] date:2020.04.05; time:asc 1000000?09:30:00.000 + til 21600000; sym:1000000?`GOOG`AMZN`FB; price:1000000?10.; size:1000000?1000000);

We are trying to partition it on date and apply sorted attribute on time column and parted attribute on sym column.

But, if we use .Q.en and set to partition the table then sorted attribute has to be lost.

trade:`sym xasc trade;
update `p#sym from `trade;
`:/parpath/2020.04.05/trade/ set .Q.en[`:/sympath/;trade];

And, if we use .Q.dpft then also sorted attribute is lost.

.Q.dpft[`:/parpath;2020.04.05;`sym;`trade];

How can we apply parted attribute on sym column with loosing sorted attribute of time column?


Solution

  • You can't have both `p on sym and `s on time in the general case. For example, this table

    q)show t:([]sym:`AMZN`GOOG`AMZN;time:09:00:00 10:00:00 11:00:00)
    sym  time
    -------------
    AMZN 09:00:00
    GOOG 10:00:00
    AMZN 11:00:00
    

    is sorted by time already, but you can't apply `p on sym. If you rearrange the rows so that `p is applicable on sym you will lose ascending order of the time values:

    sym  time
    -------------
    AMZN 09:00:00
    AMZN 11:00:00
    GOOG 10:00:00
    

    or

    sym  time
    -------------
    GOOG 10:00:00
    AMZN 09:00:00
    AMZN 11:00:00
    

    Having said that, parted and sorted at the same time is never a requirement if all you want is save a table as part of a partitioned table on disk, for example. You just need to make sure sym is parted and time is sorted within each sym. In other words `sym`time xasc t is enough.