2020年3月19日 星期四

KDB+ and Q

https://www.tutorialspoint.com/

Q Data Types

There are a lot of data types in Q, including
boolean byte short int long
real float
char symbol
date time month minute second
enum

The most common type of data types used is symbol, i.e. type 11, usually 11h indicating atom symbol. The 'h' means it is a short value.

Type in numeric

Frequently check the Atoms: https://code.kx.com/q4m3/2_Basic_Data_Types_Atoms/

All types has a name and can be represented by a short number internally
0 list
1 boolean
2 guid
4 byte
5 short
6 int
7 long
8 real
9 float
10 char
11 symbol
12 timestamp
13 month
14 date
15 datetime
16 timespan
17 minute
18 second
19 time
20-76 enums
77 anymap
78-96 mapped list of lists of type t (what the hell is it?)
97 nested sym enum
98 table
99 dictionary
100 lambda
101 unary primitive
102 operator
103 iterator
104 projection
105 composition
106 f'
107 f/
108 f\
109 f':
110 f/:
111 f\:
112 dynamic load

Some important  types does not only has a name and a short number, but also has a char number.
* list
b boolean
g guid
x byte
h short
i int
j long
e real
f float
c char
s symbol
p timestamp
m month
d date
z datetime
n timespan
u minute
v second
t time

Comment

/ this is a comment
// is more popular

Assignment to variable x

x:0b // boolean 0
x:0x // byte 0
x:0h // short 0
x:0i // int 0
x:0j // long 0
x:2010.01.01

Default literal types

type 0 // default -7h long
type 0.0 // default -9h float
type "" // default 10h empty char list
type ` // default -11h symbol
type () // default 0h empty list
type [] // default 101h unary primitive
type 2000.01.01 // -14h date
type 2000.1.1 // `2010.1.1 error
type 00:00:00.000 // -19h time
type 2000.01.01T00:00:00.000 // -15h datetime

What is symbol?

A symbol is equivalent to SQL varchar, Java String, C++ std::string. But dont call it varchar.

Atoms vs List

All data types are atoms including a single number, character or symbol and more.
A list contains sequence of atoms.

List example

type `appl // -11h, negative means atom
type (`appl;`goog) // 11h, positive means list
type (`appl`goog) // same as above
type `appl`goog // same as above

List Accessing

symbols:(`appl;`goog;`)
symbols[0] / `appl
symbols[1] / `goog
symbols[2] / ` empty
symbols 0 / also `appl, square brackets optional
symbols 0 1
type symbols 0 // -11h, atom
type symbols 0 1 // 11h, list

List with different types

data:(1;2;3;`a;`b;`c;(`d;`e);(5;6;7))
type data // 0h, list
data
1
2
3
`a
`b
`c
`d`e
5 6 7

String (list of char)

A list of char is loosely called string. Same idea in C char array.

s:"hello" // 10h, list of char
`$"string with spaces" // $"" for string to symbol
type `$"string with spaces" // -11h
"hello"[0] // h, array like
type "hello"[0] // -10h
"01234" 2 4 // "2 4" 10h

The $ dyadic casting operator

How to cast from integer list to float list?

a:1 2 3
$[`float;a] // use type name to cast
$["f";a] // use char number to cast
$[9h;a] // use short number to cast

Cast String to Symbols

`$("apple";"banana";"orange")

Cast interger to String

string 0 // "0" 10h list of char

Cast String to Non-Symbols

`int$"123" // 49 50 51i, list of ASCII integerr
6h$"123" // same as above
"i"$"123" // same as above
"I"$"123" // 123i

Date

KDB+ date is stored as integer number of days since reference date of 2000-01-01 (Sat), allowing negatives. Literal value is YYYY.MM.DD

`int$2000.01.01 // 0i means 0 day from 2000-01-01
`year$2000.01.01 // 2000i means year 2000
`mm$2000.01.01 // 1i means January
`dd$2000.01.01 // 1i means day 1
x:2000.01.01
x.year // 2000i
x.mm // 1i
x.dd // 1i
x+1 // add 1 day 2000.01.02
x-1 // minus 1 day 1999.12.31
x%7 // weekday, starting on Saturday 0, Sunday 1, Monday 2 ... Friday 6

Time

KDB+ time is stored as the integer number of milliseconds since midnight. literal value is HH:MM:SS.MSS

`int$00:00:00.000 // 0i means 0ms since midnight
`int$00:00:01.000 // 1000i means 1000ms since midnight
`hh$01:02:03.004 // 1i means 1 hour
`mm$01:02:03.004 // 2i means 2 minutes
`ss$01:02:03.004 // 3i means 3 seconds
x:01:02:03.004
x.hh // 1i
x.mm // 2i
x.ss // 3i

Datetime

KDB+ datetime is stored as the fractional day since 2000.01.01T00:00:00.000

`float$2000.01.01T00:00:00.000 // 0f
`float$2000.01.01T00:06:00.000 // 0.25 means a quarter of day

List

KDB+ list is an ordered atoms and other lists

KBD+ list format is (;;;;;;;) equivalent to JSON [,,,,,,]

General list

General list means uniform list means list of atoms of same type.The term homogeneous best describes it.

count (1;2;3) // 3

(1h;2h;3h) // short list
(1.1;2.2;3.3) // float list
(0b;1b;1b;0b) // boolean list
(`appl;`goog) // symbol list
("a";"b";"c") // char list, i.e. string, printed in "abc"

Single Item List (singleton)

enlist 0 // ,0
type enlist 0 // 7h long list
(0) // 0
type (0) // -7h long atom
type 0 // -7h atom

Distinguish an atom with singleton

signum type 0 // -1i atom
signum type enlist 0 // 1i list

List Operation

Indexing

(0,1,2,3)[0] // 0
(0,1,2,3)[1] // 1
(0,1,2,3)[2] // 2

Indexed Assignment

L:1 2 3
L[0]:9 // no return value
L // 9 2 3

List from variables

q)a:1
q)b:(2 3)
q)c:(a b c)
q)d:(a;b;c)
q)d
1
2 3
`a`b`c

List joining by , operator

The , operator append the right operand to the end of left operand, accepts an atom or atoms.

(1;2;3),(4;5;6) // 1 2 3 4 5 6
(1;2;3),4 // 1 2 3 4
1,2,3,4 //  1 2 3 4

Nesting: list of list

count (1;2;(3,4,5)) // 3 items
(1;2;(3,4,5))[2][0] // 3

Elided Indices (TBD: understand it more later)

q)L:((1 2 3; 4 5 6 7); (`a`b`c;`d`e`f`g;`0`1`2);("good";"morning"))

q)L
(1 2 3;4 5 6 7)
(`a`b`c;`d`e`f`g;`0`1`2)
("good";"morning")

q)L[;1;]
4 5 6 7
`d`e`f`g
"morning"

q)L[;;2]
3 6
`c`f`2
"or"

Interpret L[;1;] as, Retrieve all items in the second position of each list at the top level.
Interpret L[;;2] as, Retrieve the items in the third position for each list at the second level.



Quick Browse (Revisit deeply later)

Dictionary

Create

q)d:`Name`Age`Sex`Weight!(`John;36;"M";60.3)
q)d
Name  | `John
Age   | 36
Sex   | "M"
Weight| 60.3

Operation

count d // 4
key d // `Name`Age`Sex`Weight
value d // list of `John 36 "M" 60.3
cols d // same as key d

Lookup

d[`Name] // `John
d[`Name`Sex] // list of `John "M"

Lookup with Verb @

d@`Name // `John

Update and Upsert

d[`Age]:35
d[`Height]:"170cm"

Reverse Lookup with the find operator ?

d?60.3 // `Weight first item in the domain

Delete entry with the delete operator _

d _`Height
Name  | `John
Age   | 36
Sex   | "M"
Weight| 60.3

Column Dictionary (Table)




End

沒有留言:

張貼留言

2023 Promox on Morefine N6000 16GB 512GB

2023 Promox on Morefine N6000 16GB 512GB Software Etcher 100MB (not but can be rufus-4.3.exe 1.4MB) Proxmox VE 7.4 ISO Installer (1st ISO re...