kdb+ Data Types
Kdb+ provides a large number of data types, in this tutorial we will consider defining the various types. This tutorial forms part of the online kdb+ training course
kdb+ Data Types
For reference the data types available in kdb+ together with their space usage etc. are shown below.
You may also want to download and print our kdb+ cheat sheet that shows the data types and other useful snippets.
type | size | char | num | notation | Null Value | Positive Infinity |
---|---|---|---|---|---|---|
Mixed List | 0 | |||||
boolean | 1 | b | 1 | 1b | 0b | |
byte | 1 | x | 4 | 0x26 | 0x00 | |
short | 2 | h | 5 | 42h | 0Nh | 0Wh |
int | 4 | i | 6 | 42 | 0N | 0W |
long | 8 | j | 7 | 42j | 0Nj | 0Wj |
real | 4 | e | 8 | 4.2e | 0Ne | 0We |
float | 8 | f | 9 | 4.2 | 0n | 0w |
char | 1 | c | 10 | "z" | " " | |
symbol | s | 11 | `zaphod | ` | ||
timestamp | 8 | p | 12 | 2011.07.08D21:48:48.703125000 | 0Np | 0Wp |
month | 4 | m | 13 | 2006.07m | 0Nm | 0Wm |
date | 4 | d | 14 | 2006.07.21 | 0Nd | 0Wd |
datetime | 4 | z | 15 | 2006.07.21T09:13:39 | 0Nz | 0Wz |
timespan | 8 | n | 16 | 0D21:56:26.421875000 | 0Nn | 0Wn |
minute | 4 | u | 17 | 12:11 | 0Nu | 0Wu |
second | 4 | v | 18 | 12:11:17 | 0Nv | 0Wv |
time | 4 | t | 19 | 09:01:02:042 | 0Nt | 0Wt |
enum | 4 | * | 20-77 | `u$v | ||
table | 98 | ([] c1:ab`c; c2:10 20 30) | ||||
dictionary | 99 | `a`b`c!!10 20 30 |
Defining Data Types
Defining Bytes / Booleans
The "char" for the given type is placed after the value to force it to be that type. Then when we call the type function it returns the type "num" for its argument. In the code above you can see booleans are type num 1 and bytes are type nums 4, this corresponds with what is shown in the data type table above.
Defining Numeric Types
Similar to before, write the "char" at end to force type. Notice that when you do not explicitly specify a type long/float is assumed depending on if your number had decimal places. This behaviour depends on your kdb version, older 2.x defaulted to integers instead of longs.
For floating point numbers, the IEEE standard is used for reals (4 bytes) and floats (8 bytes). Therefore the standard accuracy you would expect is stored. The accuracy shown in the console itself depends on the system P setting.
Defining Character Types
Character data can be stored as char or symbols. Char stores unicode-8/ascii values. Symbols store a sequence of characters as a single entity.
Date Time kdb+ Data Types
Define DateTime TimeStamps etc.
Dot Notation for Accessing Datetime Components or Casting
Using the shorter dot notations dt.dd, dt.mm, dt.ss
returns that component
Using the longer dot notations dt.minute, dt.second
casts the value to that type, truncating it's value.
Getting the Current Date, Time, TimeStamp
To access the current date, time, timestamp etc. type '.z.' followed by the temporal type's char in the table above; for example .z.t will give the current time, .z.d the current date and so forth.
0N Nulls and 0W Infinities
- kdb+ provides values for most types to repsent Positive Infinity and Negative Infinity
- Infinities are useful in relational operations.
- Null represents a piece of unknown missing data.
- The
null
function returns true, if it's argument is null. However since most types in kdb+ have a null value, we can use equality to test for null, in contrast to standard SQL.
Summary
The important information to take from this tutorial is
- How to define:
- Numeric Data
- booleans / bytes
- Characters
- Date, Times, Timestamps, Months, Hours
- Accessing / Casting datetime values using dot notation
- That most types in kdb+ have nulls and infinities