KX Community

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

Home Forums kdb+ help understanding a function

  • help understanding a function

    Posted by fav_pa on May 29, 2024 at 9:07 pm

    hello everyone!
    i am not sure to really understand the operations order in this function .
    .quantQ.io.saveSplayedTab:{[tabPath;pvar;table]

    // tabPath — path where to store the table

    // pvar — variable to sort and index on for fast querying

    // table — name of the table to save

    :@[;pvar;`p#] pvar xasc (` sv (tabPath;`;table;`)) set .Q.en[tabPath] get table;

    };

     

    i am under the impression that because of left to right evaluation, it is saved before it is ordered. can someone explain ?
    thank you!!

     

    unterrainer_ale replied 2 weeks, 1 day ago 3 Members · 4 Replies
  • 4 Replies
  • fav_pa

    Member
    May 29, 2024 at 9:27 pm

    and also would this not make more sense: (` sv (tabPath;`;table;`)) set @[; pvar;`p#] pvar xasc .Q.en[tabPath] get table

  • unterrainer_ale

    Member
    May 30, 2024 at 8:09 am

    Hey fav_pa,

    I believe there are a few more mistakes in the code you posted, can you share the source where you have it from?

    First, the part to create the path to save down is missing some code

    q) ` sv (`$":path/toTable";`tableName;`)
    `:path/toTable/tableName/

    Then you also miss the `p in

    @[;pvar;`p#]

    Other than that, the second code you posted makes sense

    `(` sv (tabPath;; table; )) set @[; pvar;`p#] pvar xasc .Q.en[tabPath] get table

     

  • rocuinneagain

    Member
    May 30, 2024 at 9:12 am

    Yes with right to left evaluation it is being saved then sorted.

    The reason this may have been done is that sorting in memory will temporarily use extra memory to perform the operation.

    https://code.kx.com/q/ref/asc/#sorting-data-on-disk

    Sorting on disk is very memory efficient as it does not bring the full table in to memory.

    The trade off is it is slower as each column gets written twice.

    q)tab:flip (`$/:.Q.an)!{1000000?10000.0}each .Q.an
    q)\ts `a xasc `:tab/ set tab
    q)
    1824 25168800
    q)\ts `:tab/ set `a xasc tab
    1165 536874192
    q)536874192%25168800
    21.33094 //Sorting in memory uses 21x memory than sorting on disk
    • fav_pa

      Member
      May 30, 2024 at 9:29 am

      Extremely interesting, thank you very much for your answer!

Log in to reply.