KDB文档 - Basic Data Types - Atoms

407 阅读3分钟

只是摘抄学习笔记

Overview

Integer Data

Long

q)42
42
q)42j
42

Short 

q)-123h
-123h

Int

q)1234567890i
1234567890i

Floating Point Data

float

q)1f
1f
q)1.234e-7
1.234e-07

real

q)1.234e7e
12340000e

Floating Point Display

You can change this by using the \P command (note upper case) to specify a display width up to 16 digits. If you issue \P 0 the console will display all 17 decimal digits of the underlying binary representation, although the last digit is unreliable.

q)f12:1.23456789012
q)\P 12
q)f12
1.23456789012

Binary Data

boolean

q)0b
0b
q)42+1b
43

byte

0x followed by two hexadecimal digits

q)1+0x29
42

GUID

q)2?0Ng
2d948578-e9d6-79a2-8207-9df7a71f0b3b 409031f3-b19c-6770-ee84-6e9369c98697

Text Data

char

q)"q"
"q"
q)"\\"
"\"
q)"\142"
"b"

symbol

A symbol is an atom holding text. It is denoted by a leading back-quote, read "back tick" in q-speak.

q)`q
`q

A symbol is akin to a SQL VARCHAR, in that it can hold an arbitrary number of characters, but is different in that it is atomic. The char "q" and the symbol `kdb are both atomic entities. A symbol is irreducible, meaning that the individual characters that comprise it are not directly accessible.

A symbol is not a string.

q)`a~"a"
0b

Temporal Data

date

q)2015.01.01
2015.01.01
q)2000.01.01=0
1b
q)2000.01.02=1
1b
q)1999.12.31=-1
1b
q)`int$2000.02.01
31i

Time Types

There are two versions of time, depending on the resolution required. If milliseconds are sufficient, use the time type, which stores the count of milliseconds from midnight in a 32-bit signed integer. It is denoted by hh:mm:ss.uuu where hh represents hours on the 24-hour clock, mm represents minutes, ss represents seconds, and uuu represents milliseconds.

q)12:34:56.789
12:34:56.789t
q)12:00:00.000=12*60*60*1000
1b
q)`int$12:00:00.000
43200000i

It is denoted by 0Dhh:mm:ss.nnnnnnnnn where hh represents hours on the 24-hour clock, mm represents minutes, ss represents seconds, and nnnnnnnnn represents nanoseconds. Observe that the leading 0D is optional.

q)12:34:56.123456789
0D12:34:56.123456789
q)12:34:56.123456 / microseconds become nanos
0D12:34:56.123456000

Date-Time Types

A datetime (deprecated) is the lexical combination of a date and a time, separated by T as in the ISO standard format. A datetime value stores in a float the fractional day count from midnight Jan 1, 2000.

q)2000.01.01T12:00:00.000
2000.01.01T12:00:00.000
q)2000.01.02T12:00:00.000=1.5
1b
q)`float$2000.01.02T12:00:00.000
1.5
q)`date$2000.01.02T12:00:00.000
2000.01.02
q)`time$2000.01.02T12:00:00.000
12:00:00.000

The preferred type is timestamp, which is the lexical combination of a date and a timespan, separated by D. The underlying timestamp value is a long representing the count of nanoseconds since the millennium. Post-millennium is positive and pre- is negative.

q)2014.11.22D17:43:40.123456789
2014.11.22D17:43:40.123456789
q)`timespan$2014.11.22D17:43:40.123456789
0D17:43:40.123456789

month

q)2015.11m
2015.11m
q)2001.01m=12
1b
q)`int$2015.01m
180i
q)2015.07m=2015.07.01
1b

minute

q)12:30
12:30
q)12:00=12*60
1b
q)`int$12:00
720i
q)12:00=12:00:00.000
1b

second

q)23:59:59
23:59:59
q)23:59:59=-1+24*60*60
1b
q)12:34:56=12:34:56.000
1b

Constituents and Dot Notation

q)dt:2014.01.01
q)dt.year
2014i
q)dt.mm
1i
q)dt.dd
1i
q)ti:12:34:56.789
q)ti.hh
12i
q)ti.mm
34i
q)ti.ss
56i

q)`month$dt
2014.01m
q)(`int$12:34:56.789) mod 1000
789

Arithmetic Infinities and Nulls

q)42<0W
1b
q)-0W<42
1b
q)9223372036854775806+1
0W
q)-0W-1
0N
q)-0W+1
-9223372036854775806
q)0W+1
0N
q)0W+2
-0W
q)0W+3
-9223372036854775806

Nulls

The q situation is more interesting. There are no references or pointers, so the notion of an unallocated entity does not arise. Most types have null values that are distinct from "normal" values and occupy the same amount of storage. Some types do not designate a distinct null value because there is no available bit pattern – i.e., for boolean, byte and char all underlying bit patterns are meaningfully employed. In this case, the value with no information content serves as a proxy for null.

Binary Nulls

The binary types have no null values. 

Numeric and Temporal Nulls

The numeric and temporal types have their own designated null values. Here the situation is similar to SQL, in that you can distinguish missing data from data whose underlying value is zero. In contrast, there is no universal null value and q nulls take the same space as non-nulls.

An advantage of the q approach is that the null values act like other values in expressions. The tradeoff is that you must use the correct null value in type-checked situations.

Text Nulls

Considering a symbol as variable length text justifies that the symbol null is the empty symbol, designated by a naked back-tick `.

The null value for the char type is the blank character " ".

The value "" is not a null char. It is an empty list of char.

Testing for Null

Always use the unary null to test a value for null, as opposed to =, as it provides a type-independent check. 

q)null 42
0b
q)null `
1b
q)null " "
1b
q)null ""
`boolean$()

出处:code.kx.com/q4m3/2\_Bas…

作者:Jeffry A. Borror