kdb+ Casting and Parsing
Kdb+ provides a large number of data types, below is shown how to convert between types:
kdb+ Data Types
For reference the kdb+ data types table is reproduced on this page:
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 |
Casting Types
Casting in kdb+ allows converting from one builtin type to another. Casting uses the $ symbol and specifies the type on the left and those values to be converted on the right. There are three variations for specifying castings and specifying the type to be converted to, using:
- Name.
- Lower Case Character.
- Type Number.
Casting Booleans, Chars...
When casting to some types the behaviour may not be intuitive, some rules to take note of are:
- When casting to boolean, Zero/0 is false, everything else is true.
- When casting from complex types (dates, characters, time..) to numeric values, the numeric value is simply the underlying value of the bytes.
Cast Time to Hours, Minutes
A common use of casting is when performing aggregations, often we will want to bucket data stored with millisecond accurate timestamps by minute or second for example: To get the avg price each minute.
Casting Lists
Casting a list of values can be carried out in two ways. By either specifying the same type for all items or specifying a specific type for each item, this behaviour is identical to applying most dyadic functions to a list.
Parsing from Strings
Parsing is the term used for reading strings and converting them to other types. There is only one method of specifying parsing, using the uppercase letter together with the dollar $ operator. If a string is invalid and can't be interpreted as a given type, this will not throw an error, instead the value will be interpreted as null. Parsing a list of strings behaves similar to casting, you can specify one type to use for all parsing or specify individually for each item.
As shown above, casting and parsing in kdb+ is extremely easy.
- Casting is mainly used for storing down values as the most suitable type.
- Parsing is used when loading data into kdb+ from file.