Search code examples
kdb

How to sort columns in a HDB to apply the p attribute


I have a HDB that is date partitioned. I want to apply the p attribute historically to a specific column. As far as I am aware, to do this, I need to first ensure this column is sorted in a way that all common occurrences are adjacent. Currently, this is not the case. How can I sort this HDB so that this column in each partition has common values adjacent to each other.

Thank you!


Solution

  • You can use xasc on disk.

    https://code.kx.com/q/ref/asc/#xasc

    You'd want to sort each partition and apply the parted attribute. Could build up the paths with .Q.PD & .Q.PV as I don't think this is something that exists in dbmaint.q.

    • This is just a general idea, it is untested so use on some test data and modify to meet your hdb structure if needed.
    • You may need to modify the xasc part if you want additional sorting within each part.
    {[tbl;sortPartCol]
      {[sortPartCol;path] sortPartCol xasc path;@[path;sortPartCol;`p#] 
       }[sortPartCol] each distinct ` sv/: (.Q.PD cross `$string .Q.PV) cross tbl
       }
    

    https://code.kx.com/q/ref/dotq/#qpv-partition-values https://code.kx.com/q/ref/dotq/#qpd-partition-locations