Connect

How can I slice a vector into a series of vectors of length n?

By passing the # (take) operator a pair as its first (i.e., left) argument:

q)2 5 # til 10
0 1 2 3 4
5 6 7 8 9

When # is used in this way, it is referred to as "reshape." We like to call this, "taking a box." Just like when you apply # with an integer left argument, taking a box stops when you have taken as many items as requested, leaving the rest behind:

q)3 3 # til 10
0 1 2
3 4 5
6 7 8

Similarly, when you over-take a box, you start over from the beginning of the source:

q)4 3 # til 10
0 1 2
3 4 5
6 7 8
9 0 1

If you want to take all the items from the source, pass 0N as the first element of the box:

q)0N 2 # til 10
0 1
2 3
4 5
6 7
8 9

If you take a whole box, but the elements from the source don't fit, you end up with a short list in the last row:

q)0N 3 # til 10
0 1 2
3 4 5
6 7 8
,9
q)

Can I retrieve kdb data from my web browser and save the query results to a csv file for viewing in a spreadsheet?

Yes. Just enter a URL in your browser whose host and port is followed by the characters .csv?. For example,

http://server:5001/.csv?select from tablename where date=2011.02.25,fname=`JOE

Is the q language simply syntactic sugar on top of k?

Not really. There's been a meaningful expansion in the language to support tables and such. If you are curious, take a look at the code in \$QHOME/q.k, it will help you understand what portions of q are implemented natively vs in k.

I noticed a list with a `s# prefix attached. What does this mean?

`s is one of q's data attributes, and indicates that the data in the list is sorted. When q knows that a list is sorted, it can perform some operations much faster. In contrast to the other attribute types, the sorted attribute has zero memory overhead (no data structure needs to be created to support it):

q)d: (-10000 ? `4) ¡ til 10000
q)key d
`namk`eefb`dnkg`pfme`ndpk`bhck`bdga`hkdk`gpja`oiof..
q)value d
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ..
q)\t do[100000; d key[d] 5000]
443
q)d: (asc key d) ! value[d] iasc key d
q)key d
q)\t do[100000; d key[d] 5000]
89
q)

Notice that value d above did not have the `s attribute; that is, the til function does not return a list with the `s attribute applied. In general, for a list to have the `s attribute, you must either sort the list (with asc as above) or let q know explicitly that the list is sorted by applying the `s attribute to a list that you know is already sorted:

q)x: `s # til 10
q)x
`s#0 1 2 3 4 5 6 7 8 9
q)

If you try to apply `s to a list that isn't actually sorted, you'll get an 's-fail error:

q)`s # 3 2 1
's-fail
q)

You can add elements to the end of a sorted list without losing the sorted attribute if the new elements do not violate the sorted-ness of the result:

q)x: `s # 1 2 3
q)x
`s#1 2 3
q)x ,: 4 5 6
q)x
`s#1 2 3 4 5 6
q)x ,: 7 7 7
q)x
`s#1 2 3 4 5 6 7 7 7

However, other modifications to a sorted list will cause the list to lose its attribute:

q)x: `s # 1 2 3
q)x
`s#1 2 3
q)x, 0
1 2 3 0
q)reverse x / reverse function removes it
3 2 1
q)

This is true even if the modifications result in a sorted list:

q) x: `s # 1 2 3
q)1 _ x
2 3
q)2 # x
1 2
q)

You can remove the sorted attribute explicitly by applying a null attribute to the list:

q)`#1 2 3 / application of a null attribute
1 2 3

For even more detail, see Section 42 of the Abridged Q Language Manual.

What does '\l .' or 'system “l ."' do?

Typically, it's used to reinitialize and alert an existing kdb process of a new partition. Note that command triggers the execution of any q scripts existing in the current working directory of the running kdb process.

This command allows kdb to continue running and servicing requests while a new partition is constructed. Running kdb remains unware of the new partition until the \l . command is issued.

How do I comment q code?

A till-end-of-line comment begins with a single slash that appears either -

1. at the start of a non-empty line,

/ this line is ignored
/so is this one

or

2. is preceded by a space:

foo: 47; / this is also a comment
42+/1 2 3 / The first / does NOT start a comment!

In the last example, the first slash functions as the adverb over.

Begin a block comment with a lone backslash at the start of a line, and finish it with sole slash -

\
All of this is a block comment.
Even /\ this is OK.
/
x: 47 / this line (before the slash) is code

or the reverse:

/
The opposite works, too.
Using block comments to comment out code can be
confusing unless you pick one way and stick to it.
\

A \ does not have to be matched; it then serves as an end-of-file indicator:

\
all lines below are
i
g
n
o
r
e
d

Lastly, if a script's first line begins with '#!', that line is skipped, e.g.:

#! blah.. blah..

Can a historical database be partitioned on multiple levels - say, by date, then symbol?

No. Only one level of partitioning (by date, month, year or integer) is supported.

'exit' fails to terminate kdb console. How do I gracefully exit a kdb process?

exit 0 or \\ or simply control+d from a console.

Inside a function, the double backslash requires escaping. Use value "\\\\" or system "\\\\"

Note: kdb process exits can be trapped via .z.exit.

How do i interpret the output of function .Q.w[]?

An example explains it best:

\$ rlwrap q
q).Q.w[]
used| 108432   / bytes malloced
heap| 67108864 / heap bytes available
peak| 67108864 / heap high-watermark in bytes
wmax| 0        / workspace limit from -w param
mmap| 0        / amount of memory mapped
syms| 537      / number of symbols interned
symw| 15616    / bytes used by 537 symbols
q)

Note how .Q.w is simply a pretty print output of \w and the workspace memory footprint from \w 0

q).Q.w
k){`used`heap`peak`wmax`mmap`syms`symw!(."\\w"),."\\w 0"}
q)\w
108304 67108864 67108864 0 0j
q)

I’ve setup many views based on a single large table. How much of a performance penalty will I pay?

A view is syntactic sugar for a canned query. Unless the view is referenced, it does not expend computational resources.