Search code examples
kdb

Different results of flip on select and on index-accessed table in kdb+


In a q session I've made a keyed table t:

q)/KDB+ 3.6 2018.05.17
q)f:flip (`a`b)!(1 2 3;4 5 6)
q)k:flip (enlist `k)!(enlist 101 102 103)
q)t:k!f;t
k  | a b
---| ---
101| 1 4
102| 2 5
103| 3 6

Then I've tried to make a query and it gives a nice results:

q)select a,b from t where k=101
a b
---
1 4
q)flip select a,b from t where k=101
a| 1
b| 4
q)flip flip select a,b from t where k=101
a b
---
1 4

But without select-syntax this gives an error:

q)t[101]
a| 1
b| 4
q)flip t[101]
'rank
  [0]  flip t[101]
       ^

Why can't I just make a simple flip on the same result as from select of the same data types?

q)type flip select a,b from t where k=101
99h
q)type t[101]
99h

Solution

  • Because the elements of dictionary t[101] aren't lists, but atoms. So flip on a list of atoms fails.

    Appending each element to an empty list first will work.

    q)(),/:t[101]
    a| 1
    b| 4
    

    Not necessarily something you want to do. For a given dictionary output, the solution you probably want is enlist

    q)enlist t[101]
    a b
    ---
    1 4