Search code examples
kdb

Does distinct function apply unique attribute to list in q kdb?


In output of asc function applied on a list, we can see that the output consists of sorted attribute.

q)asc 1 3 11 10 4
`s#1 3 4 10 11

But in the output of distinct function applied on a list, we cannot see unique attribute applied on the list.

q)distinct 1 3 11 10 4
1 3 11 10 4

But, the time taken to search an element in distinct list and unique list(list on which unique attribute is applied) is almost same.

q)n:1000000?1000000
q)d:distinct n
q)\t:100000 d[1021]
45
q)\t:100000 d[632265]
45
q)u:`u#d
q)\t:100000 u[1021]
44
q)\t:100000 u[632265]
48

So, is the distinct function applying unique attribute internally on the list and converting it to hash table?


Solution

  • distinct does not apply unique attribute to list. You may see this with help of attr function

    attr distinct 10?10
    

    will return nothing `, when

    attr `u#distinct 10?10
    

    returns `u

    Also, I think the experiment may be wrong: d[1021] seems to be usual 0(1) time operation of getting element by index. If you use search instead, you'll see the difference (numbers on my PC):

    q)\t:10000 d?770751
    85
    q)\t:10000 u?770751 
    6