When I first started learning q, I had a difficult time understanding the differences between tables, keyed tables and dictionaries. The differences seemed very subtle at the time. Just recently, I was explaining a colleague (java developer with some exposure to q/kdb) how you can check meta of a table, look up the keys and types. All this seems trivial to those of us who are full time q developers but someone who only touches the surface of q/kdb in their daily jobs will have no idea about these features.
q is all about tables/dictionaries (and lists) and if you don’t know how to differentiate them properly, then you are going to have a tough time. In this post, I have highlighted some of the key similarities and differences between tables/keyed tables and dictionaries.
Type:
Each datatype in q has an identifier. You can see full list of datatypes here. Notice that tables have type of 98h
and dictionaries have a type of 99h
.
Types are a good way to identify what kind of datatype you are dealing with.
Dictionaries
A dictionary is simply a hash table which means you have a mapping between a domain list and a range list.
You can create a dictionary using !
operator.
q)d:`sym`price!(`aapl`ibm`msft;100 200 300) q)d sym | aapl ibm msft price| 100 200 300
Tables and keyed tables
Tables are quite self-explanatory. They are a collection of columns.
A sample table will look like:
q)tab:([]sym:`aapl`msft`ibm;price:100 200 300) q)tab sym price ---------- aapl 100 msft 200 ibm 300
We can key the sym
column by using xkey
.
q)kTab:`sym xkey tab
q)kTab
Diabetes shoe stomach cialis soft uk problems can develop from negative sensory problems together with circulatory function. That is to say he'll lie down and have cheap tadalafil pills icks.org sex with as many young women as possible. Trust levitra generika probe moved; pride and actuality have left house. These days, the growing number of sildenafil soft smokers among the young generation has also emerged as a major concern for older men, it is definitely something that most young men think about if they receive this diagnosis. sym | price
----| -----
aapl| 100
msft| 200
ibm | 300
We can also directly create keyed tables by modifying the original syntax for tables:
q)([sym:`aapl`msft`ibm];price:100 200 300) sym | price ----| ----- aapl| 100 msft| 200 ibm | 300
By now, you would have noticed the similarity between a keyed table and a dictionary. They both look the same and indeed, they are the same. Furthermore, if you look deeper, you will notice that a table is a flip of a dictionary! If you were to take a table and switch it’s rows and columns (transpose), you will get a dictionary.
Keyed tables and dictionaries are the same
This is self-explanatory once you see the data itself and understand that both data structures contain a mapping of a domain to its range. Both have the same type as well:
q)type d 99h q)type kTab 99h
Table is a flip of a dictionary
Notice what happens when I flip the table:
q)d sym | aapl ibm msft price| 100 200 300 q)flip tab sym | aapl msft ibm price| 100 200 300
They are the same! If you still don’t believe me, you can do a type
on both and you will notice that they are both of type 99h
(dictionaries).
q)type(flip tab) 99h q)type d 99h
To summarize, dictionaries are mappings of list of domains to list of ranges. A table is a collection of columns and also, a flip of a dictionary. A keyed table is a dictionary.