Next: , Previous: , Up: Top  


13 Tables

This chapter introduces the different types of tables available in k9. Table, xtable and ktable are very similar and as you’ll see in the kSQL chapter are easy to query. In the benchmark chapter tables were shown to be fast to save, read, and query.

t:[[]i:2 3;f:3 4.;s:`a`b]
ktable [[b:..]a:..]      
xtable `..![[]a:..]      

13.1 Table

The table is the most basic of the three types. A table consists of columns and rows of information where each column has a name. Tables can be created in three different ways (1) specification via table format, (2) flipping a dictionary, or (3) reading in from a file.

13.1.1 Table format

Tables can be created with the table square bracket notation.

As an example let’s create a table with two columns named “a” and “col2” with three rows. The syntax is to surround the defintion with square brackeets and then have a first element of empty square brackets. In general this in square bracket pair will contain any keys but more on this will happen in ktable. After that it’s first column name, colon, and the list of values, then second column, and continuing for all the columns.

 [[] a:1 20 3; col2: 3.6 4.8 0.1]
a  col2
-- ----
 1 3.6 
20 4.8 
 3 0.1

 [[] a:1; col2:3.6]   / will error :class as values but be a list
[[] a:1; col2:3.6]

:class

 [[] a:,1; col2:,3.6] / using enlist will succeed
[[]a:,1;col2:,3.6]

13.1.2 Dictionary format

Tables can also be created by flipping a dictionary into a table.

 +`a`col2!(1 20 3; col2: 3.6 4.8 0.1)
a  col2
-- ----
 1 3.6 
20 4.8 
 3 0.1 

13.1.3 File import

Tables can also be created by reading in a file.

t.csv

a, col2
1, 3.6
20, 4.8
3, 0.1

Use load file 2:x which returns a table.

 2:`t.csv
a   col2
-- -----
 1  3.6 
20  4.8 
 3  0.1 

13.2 xtable

An cross tab (xtable) is a collection of tables stored in a dictionary where the keys are symbols and the values are tables. Below is an example where the keys are symbols and the values are end-of-day prices.

 x1:+`d`p!(2020.09.08 2020.09.09 2020.09.10;140 139 150)
 x2:+`d`p!(2020.09.08 2020.09.10;202 208)
 eod:`AB`ZY!(x1;x2)
 eod`AB
d          p  
---------- ---
2020.09.08 140
2020.09.09 139
2020.09.10 150

13.3 ktable

ktable (or key table) is a table where some of the columns are keyed and thus should not have duplicate values.

 [[d:2020.09.08 2020.09.09 2020.09.10]p:140 139 150]
d         |p  
----------|---
2020.09.08|140
2020.09.09|139
2020.09.10|150

 `d^ [[]d:2020.09.08 2020.09.09 2020.09.10;p:140 139 150]
d         |p  
----------|---
2020.09.08|140
2020.09.09|139
2020.09.10|150

Next: , Previous: , Up: Top