KX Community

Find answers, ask questions, and connect with our KX Community around the world.
KX Community Guidelines

Home Forums kdb+ How to add a column to the Partition table?

  • How to add a column to the Partition table?

    Posted by hzadonis369 on June 17, 2023 at 12:00 am

    Hi, Masters:

    Because a new field should be added to the Partition table. But it seems there’s problem that I met.

    First of all, the trade table was paritioned by:

    q)`:E:/testfolder/pdb/2023.06.01/trade/ set .Q.en[`:E:/testfolder/pdb;] ([] ti:09:30:00 09:31:00; s:`ibm`msft; p:101 33f);
    q)`:E:/testfolder/pdb/2023.06.02/trade/ set .Q.en[`:E:/testfolder/pdb;] ([] ti:09:30:00 09:31:00; s:`ibm`msft; p:101.5 33.5);
    q)l E:/testfolder/pdb

    It works while I select data from the trade table. And secondly, I added a filed(rk) to the trade table:

    q)`:E:/testfolder/pdb/2023.06.05/trade/ set .Q.en[`:E:/testfolder/pdb;] ([] rk:1 2; ti:09:30:00 09:31:00; s:“ibm`msft; p:108.6 26.2);
    q)`:E:/testfolder/pdb/2023.06.06/trade/ set .Q.en[`:E:/testfolder/pdb;] ([] rk:1 2; ti:09:30:00 09:31:00; s:`ibm`msft; p:110.1 25.6);

    The new field(rk) can be input, but once I select from trade table with date specified, there’re different output:

    1. q)select from trade where date within (2023.06.05, 2023.06.06)

    It works fine.

    2. q)select from trade where date = 2023.06.01

    There’s error message:
    ‘./2023.06.01/trade/rk. OS reports: can’t find directory
    [0] select from trade where date = 2023.06.01

     

    Do you konw the reason and how to fix it? Thanks!

    hzadonis369 replied 9 months, 2 weeks ago 2 Members · 1 Reply
  • 1 Reply
  • cillianreilly

    Member
    June 17, 2023 at 12:00 am

    You haven’t added the rk column to the 2023.06.01 partition.

     

    q){x!`rk in/:get each` sv/:(hsym`$string x),:`trade`.d}date 
    2023.06.01| 0 
    2023.06.02| 0 
    2023.06.05| 1 
    2023.06.06| 1

     

    You can reference dbmaint.q – it contains utility functions for working on partitioned databases. For this case, the fixtable function would help you here – adding the rk column into the partitions that don’t currently contain it.

    In general, better practice is to add the empty column to all partitions first (addcol), and then write down the partitions containing data. This keeps the HDB in a good state throughout the operation.

Log in to reply.