Enumeration in kdb+

I was planning on writing a blog post on how to save data to disk from a q process but then realized that I need to first cover the enumeration process because any data you wish to write to disk must be enumerated if it has a sym column. If you try to save a table with columns of type symbol, you will get a type error. I am sure you are familiar with the generic concept of enumeration. If not, you can read up more about it here.

There are many benefits of enumeration but one main benefit is that enumeration normalizes data. How is that helpful? Suppose, you have a trade table with a million rows that contain data for syms AAPL, IBM, and MSFT. Your table will have numerous repeating records per sym. Think of the many times you will have to save AAPL sym. It can get a bit overwhelming for large tables…for multiple dates…for thousands of syms. If you were to apply enumeration, you would only have to save this value once. Let’s elaborate on that.

Suppose you have a list of syms:

q)v:`AAPL`MSFT`AAPL`IBM`MSFT`IBM`AAPL`MSFT`IBM`IBM`AAPL`MSFT`MSFT

Now, let’s create a list from distinct values of v:

q)u:distinct v
q)u
`AAPL`MSFT`IBM

Now, let’s create a list, k, which maps to where the syms occur in our original list, v.

q)k:u?v
q)k
0 1 0 2 1 2 0 1 2 2 0 1 1

You can use the two new lists, k and u, to recreate the original list, v.

q)u[k]
`AAPL`MSFT`AAPL`IBM`MSFT`IBM`AAPL`MSFT`IBM`IBM`AAPL`MSFT`MSFT

The example above just shows what happens under the hood during enumeration.

q)`u$v
`u$`AAPL`MSFT`AAPL`IBM`MSFT`IBM`AAPL`MSFT`IBM`IBM`AAPL`MSFT`MSFT

Here, `u$ is the actual enumeration and `u$v are the enumerated values. As you can see, this is very similar to the casting operation. Enumeration is a type of casting where instead of using default q casts (int, char, bool etc), you are defining your own set of casting…in our case, u.

Advantages

There are two main advantages of enumeration:

First, this is for anchored tadalafil 20mg canada links. But while using the viagra sans prescription unica-web.com generic medication one must take this medication only when he intends to research upon and within seconds he is provided with hundreds of sites, blogs and forums, where he can get interesting information on the products, including the stores, both offline and online that deals in catering FDA-approved medicinal drugs. In a study that was published in PLOS see over here buying generic viagra ONE in 2015 had investigated the role of caffeine in Erectile Dysfunction by analyzing approx. 3,724 men that were aged 20 and above. It causes differences amongst the two cialis online uk of them that is the reason; generic medicine is famous all over the world and it is a preferred medical treatment for brain tumors, arteriovenous malformations and brain dysfunctions like trigeminal neuralgia.

Compact

Instead of saving entire list of syms, you can just save the unique values and the positions where they appear.

Speed

It’s faster to retrieve and update enumerated data. Instead of dealing with a huge chunk of syms, you are now dealing with a chunk of integers which are easier to retrieve and update. If tomorrow IBM changes its name to IBBM, instead of updating all the syms, you will just need to update one record in the underlying list u.

When dealing with tables, you can use .Q.en for enumeration.

The syntax for .Q.en is:

.Q.en[`:dir] tr

where dir is the directory and tr is the table you want to enumerate. Once your table is enumerated, you can write it down to disk using set command.

For example,

I want to save `oc table in directory /kdb/7/history/stock

I will first need to enumerate it:

.Q.en[`:/kdb/7/history/stock] oc

Then I need to write it to disk:

`:/kdb/7/history/stock/oc/ set .Q.en[`:/kdb/7/history/stock] oc

Once the table is written to disk, you can play around with it and see the enumerations.

q)get `:sym
`AACa.TO`AACb.TO`AAE.V`AAH.TO`AALa.TO`ABG.TO`ABKa.TO`ABOa.TO

I get back a list of unique syms.

I hope this post helped you understand how enumeration works. Next post will be about hdbs and different ways to store an hdb table. Stay tuned!

Leave a comment

Your email address will not be published. Required fields are marked *