Datatypes in q

In this post, we are going to cover all the datatypes available in the programming language, q. These datatypes are known as atoms in q because you cannot reduce them to a smaller datatype. These datatypes are also referred to as nouns and we will discuss why that is in another post when we discuss q grammar.

This post is available as a video on our YouTube Channel.


Here is a list of q datatypes taken from Kx’s reference page.

The fact is online viagra that people come to know about disadvantages of using modern toilets. Polyphenois can be found mainly in the free viagra 100mg skin may become deeper skin sores or ulcers. Taking a real life situation is always more sildenafil tablets india effective. Try to confide in your partner to get the required order cheap cialis support and care.

The table above shows all the available datatypes along with their number (n), character (c) and literal representation as well as the equivalent null and inf representation. Each datatype in q can be represented in multiple ways. For example, long has a numerical representation of 7 and character representation of j. The table also shows how many bytes, sz, each datatype takes.

A useful operator to determine type of a q object is type. When called, it returns the numerical representation. We will be using it extensively today. Here is an example of how to use it:

q)type(1)
-7h
q)type(1 2 3)
7h

As you can see above, when we checked for type of a long, 7, we got back -7h. However, when we checked the type of a simple list, 1 2 3, we got back the same value but with a positive sign.

Let’s discuss this.

First of all, 7, is a long in this case. To define an integer, we must specifically tell q so. If we don’t, it defaults to a long which is represented by ‘7’. Now, a negative result means it’s an atom and a positive result means that it’s a simple list consisting of that datatype. For example, 1 2 3, is a list containing three longs. Because the datatypes of each atom in the list is the same, long, it is considered a simple list as opposed to a general list.

Let’s go through each datatype.

boolean (1, b)

We can specify a boolean by using 0b or 1b.

q)type(0b)
-1h
q)type(1b)
-1h

Here is a simple list consisting of two booleans:

q)type(1b,0b)
1h

byte (4, x)

q)type(0x00)
-4h

short (5,h), int (6,i), long (7,j), real (8,e), float (9,f)

/ to specify the type, add character representation at the end
/ this is a short
q)type(6h)
-5h

/ this is a long
q)type(6j)
-7h

/ this is a float
q)type(6f)
-9h

char (10,c)

In q, a character is an atom such as c or m or n. If you have multiple characters, then it’s known as a list of characters. This is similar to strings in other languages.

/ here is a character
q)type("m")
-10h

/ now here is a list of character, which is why we get a positive value
q)type("enlist[q]")
10h

symbol (11,s)

In q, there is a datatype known as symbol which is not available in most other languages. Symbols are a more efficient way of storing strings under the hood. This is discussed in a previous post about enumeration.

/ you can create a symbol with a back tick ` q)type(`AAPL)
-11h

Everything time related

timestamp (12,p), month (13,m), date (14,d), datetime (15,z), timespan (16,n), minute (17,u), second (18,v), time (19,t)

As you can tell, time is important for a timeseries database.

/ .z.p is a quick way to get the current timestamp
q)show .z.p
2020.06.25D19:05:35.032940000
q)type(.z.p)
-12h

/ .m is the month datatype
q)type(2018.11m)
-13h

/ Without 'm' in the end, it's just a float
q)type(2018.11)
-9h

/ .z.d gives you current date
q).z.d
2020.06.25
q)type(.z.d)
-14h

/ .z.z is similar to .z.p but with less precision
q).z.z
2020.06.25T19:06:09.938
q)type(.z.z)
-15h

/ .z.n gives you the timespan
q).z.n
0D19:06:18.873750000
q)type(.z.n)
-16h

/ Here is how to get minute datatype
q)type(15:30)
-17h

/ here you have seconds datatype
q)type(15:30:20)
-18h

/ .z.t gives you the current time
q).z.t
19:06:38.544
q)type(.z.t)
-19h

So these were all the basic datatypes that you should know. In this tutorial, we covered:

  1. basic datatypes and their various representations
  2. type operator
  3. positive types vs negative types
  4. simple lists
  5. important internal time-related variables in .z namespace

In a future post, we will discuss how to cast one value from one type to another. Hope you enjoyed this post. Please leave your feedback in the comment section.

Leave a comment

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