Search code examples
kdbk

A dictionary with a single value and multiple keys


What are the dictionaries with a single value and multiple keys stands for? What are their purposes?

I've accidentally created one, but can not do anything with it:

q)type (`a`b`c)!(`d)
99h
q)((`a`b`c)!(`d))[`a]
'par

Solution

  • That special form usually denotes the flip of a partitioned table, where the keys represent the column names and the value represents the table name:

    q)/load a database with partitioned table part_tab
    q)flip part_tab
    `ncej`jogn`ciha`hkpb`aeaj`blmj`ooei`jgjm`cflm`bpmc!`part_tab
    

    This dictionary is not intended to be looked up in the usual manner and not in the way that you've attempted.

    It would be completely ill-advised but it is possible to restrict columns of a partitioned table by manipulating this dictionary:

    q)select from part_tab where date=2020.01.02
    date       ncej jogn ciha hkpb aeaj blmj ooei jgjm cflm bpmc
    ------------------------------------------------------------
    2020.01.02 0    0    0    0    0    0    0    0    0    0
    2020.01.02 1    1    1    1    1    1    1    1    1    1
    2020.01.02 2    2    2    2    2    2    2    2    2    2
    2020.01.02 3    3    3    3    3    3    3    3    3    3
    ...
    
    q)part_tab:flip`ncej`jogn`ciha!`part_tab
    q)select from part_tab where date=2020.01.02
    date       ncej jogn ciha
    -------------------------
    2020.01.02 0    0    0
    2020.01.02 1    1    1
    2020.01.02 2    2    2
    ...
    

    Again don't try this on any large/production tables, it's an undocumented quirk.

    Splay table have a similar dictionary when flipped:

    q)flip splay
    `ncej`jogn`ciha`hkpb`aeaj`blmj`ooei`jgjm`cflm`bpmc!`:splay/
    

    The difference being that the table name has a "/" at the end and is hsym'd. This is how .Q.qp determines if a table is partitioned or splayed.