Datatypes

The datatype of an object is given as a short int.

q)type 5                      / integer atom
-6h
q)type 2 3 5                  / integer list
6h
q)type (2;3 5f;"hello")       / mixed list
0h
q)type each (2;3 5f;"hello")
-6 9 10h
q)type (+)                    /not just data
102h

type, .Q.ty (type), Casting

Primitive datatypes

Primitive datatypes are in the range ± 1h to 19h: positive for a vector, negative for an atom. (A general list has type 0h.)
type

ncnameszliteralnullinfSQLJava.Net
0*list
1bboolean10bBooleanboolean
2gguid160NgUUIDGUID
4xbyte10x00Bytebyte
5hshort20h0Nh0WhsmallintShortint16
6iint40i0Ni0WiintIntegerint32
7jlong80j or 00Nj
or 0N
0Wj
or 0W
bigintLongint64
8ereal40e0Ne0WerealFloatsingle
9ffloat80.0 or 0f0n0wfloatDoubledouble
10cchar1" "" "Characterchar
11ssymbol.``varcharStringstring
12ptimestamp8dateDtimespan0Np0WpTimestampDateTime (RW)
13mmonth42000.01m0Nm
14ddate42000.01.010Nd0WddateDate
15zdatetime8dateTtime0Nz0wztimestampTimestampDateTime (RO)
16ntimespan800:00:00.0000000000Nn0WnTimespanTimeSpan
17uminute400:000Nu0Wu
18vsecond400:00:000Nv0Nv
19ttime400:00:00.0000Nt0WttimeTimeTimeSpan
20-76enums
77(unused)
78-9677+t – mapped list of lists of type t
97nested sym enum
98table
99dictionary
100lambda
101unary primitive
102operator
103adverb
104projection
105composition
106f'
107f/
108f\
109f':
110f/:
111f\:
112dynamic load

n: short int returned by type and used for casting, e.g. 9h$3
c: character used lower-case for casting and upper-case for load-csv
sz: size in bytes
inf: infinity (no math on temporal types); 0Wh is 32767h
RO: read only; RW: read-write

Strings

There is no string datatype. The nearest equivalent to a string is a symbol, or a char vector. On this site, string is a synonym for character vector.

Temporal

The valid date range for parsing is ​1709.01.01 to 2290.12.31. Date arithmetic is not checked, so you can go out of this range.

q)2290.12.31
2290.12.31
q)2291.01.01        / out of range
'2291.01.01
q)2290.12.31+0 1
2290.12.31 2291.01.01
q)2000.01.01+2000.01.01-1709.01.01
2290.12.31

Valid ranges can be seen by incrementing or decrementing the infinities.

q)-0W 0Wp+1 -1      / limit of timestamp type
1707.09.22D00:12:43.145224194 2292.04.10D23:47:16.854775806

q)0p+ -0W 0Wp+1 -1  / timespan offset of those from 0p
-106751D23:47:16.854775806 106751D23:47:16.854775806

q)-0W 0Wn+1 -1      / coincide with the min/max for timespan

Symbols

A back tick ` followed by a series of characters represents a symbol, which is not the same as a string.

q)`symbol ~ "symbol"
0b

A back tick without characters after it represents the empty symbol: `.

Cast string to symbol

The empty symbol can be used with cast to cast a string into a symbol, creating symbols whose names could not otherwise be written, such as symbols containing spaces. `$x is shorthand for "S"$x.


q)s:`hello world
'world
q)s:`$"hello world"
q)s
`hello world

Q for Mortals: 2.4 Basic Data Types – Atoms

Filepaths

Filepaths are a special form of symbol.

q)count read0 `:path/to/myfile.txt  / count lines in myfile.txt

Infinities

Note that arithmetic for integer infinities (0Wh,0Wi,0Wj) is undefined, and does not retain the concept when cast.

q)0Wi+5
2147483652
q)0Wi+5i
-2147483644i
q)`float$0Wj
9.223372e+18
q)`float$0Wi
2.147484e+09

Arithmetic for float infinities (0we,0w) behaves as expected.

q)0we + 5
0we
q)0w + 5
0w

.Q.M (long infinity)

Guid

The guid type (since V3.0) is a 16-byte type, and can be used for storing arbitrary 16-byte values, typically transaction IDs.

Generation

Use deal to generate a guid (global unique: uses .z.a .z.i .z.p).


q)-2?0Ng
337714f8-3d76-f283-cdc1-33ca89be59e9 0a369037-75d3-b24d-6721-5a1d44d4bed5
If necessary, manipulate the bytes to make the uuid a Version-4 'standard' uuid.

Guids can also be created from strings or byte vectors, using sv or "G"$, e.g.


q)0x0 sv 16?0xff
8c680a01-5a49-5aab-5a65-d4bfddb6a661
q)"G"$"8c680a01-5a49-5aab-5a65-d4bfddb6a661"
8c680a01-5a49-5aab-5a65-d4bfddb6a661

0Ng is null guid.

q)0Ng
00000000-0000-0000-0000-000000000000
q)null 0Ng
1b

There is no literal entry for a guid, it has no conversions, and the only scalar primitives are =, < and > (similar to sym). In general, since V3.0, there should be no need for char vectors for IDs. IDs should be int, sym or guid. Guids are faster (much faster for =) than the 16-byte char vecs and take 2.5 times less storage (16 per instead of 40 per).

Other types

Enumerated types

Enumerated types are numbered from 20h up to 76h. For example, in a new session with no enumerations defined:

q)type `sym$10?sym:`AAPL`AIG`GOOG`IBM
20h
q)type `city$10?city:`london`paris`rome
21h

(Since V3.0, type 20h is reserved for `sym$.)

Nested types

These types are used for mapped lists of lists of the same type. The numbering is 77 + primitive type (e.g. 78 is boolean, 96 is time and 97 is `sym$ enumeration.)

q)`:t1.dat set 2 3#til 6
`:t1.dat
q)a:get `:t1.dat
q)type a            /integer nested type
83h
q)a
0 1 2
3 4 5

Dictionary and table

Dictionary is 99h and table is 98h.

q)type d:`a`b`c!(1 2;3 5;7 11)     / dict
99h
q)type flip d                      / table
98h

Functions, adverbs, derivatives

Functions, lambdas, operators, adverbs, projections, compositions and derivatives have types in the range [100–112].

q)type each({x+y};neg;-;\;+[;1];<>;,';+/;+\;prev;+/:;+\:;`f 2:`f,1)
100 101 102 103 104 105 106 107 108 109 110 111 112h