What companion tools to kdb are useful?

How does 'protected execution' (or exception handling) work in q?

Protected execution is q's model of exception handling. If you are familiar with exception models (such as try, catch, and throw) from another language, you will notice a resemblance.

In any exception handling model, there is a block of code to attempt to execute and one or more blocks of code to invoke should the attempted block fail. Moreover, the error handling blocks have a proscribed form. Lastly, there is a mechanism to raise an exception.

Let's look at a simple example in q and break it down:

LaidBackLoad: {[file]
    @[{system "l ", x; 1b}; / code to attempt
      file;                 / argument
      {[err] 0N! err; 0b}]} / error handler

In q, there is only one error handling block, and both the try block and the error handling block must be functions. In addition, the error handler takes a single parameter, which - if the error handler is called - will be a string.

In our example, the function to try takes only a single parameter, and that's why the protected execution expression starts with an @. If your try function takes more than one parameter, use . instead:

LaidBackEqual: {
    .[{x = y};              / code to attempt
      (x; y);               / arguments
      {[err] 0N! err; 0b}]} / error handler

You signal an error (which may be a symbol or a string) using the ' (signal) operator:

CantBeNegative: {if [x < 0; ' "Must be >= 0"]; x}

This is exactly the same mechanism that q uses to let us know when we have done something wrong:

q)(1 2 3) = 1 2
'length
q)

There is one last detail to note when reviewing the above examples. Protected execution expressions are expressions, and they have a value. If the try function does not signal, then the expression's value is the try function's return value. Otherwise, the protected execution expression's value is the value returned by the error handler.

How can I find a listing of tables in a given namespace?

To list the tables in the current namespace, use \a; to list the tables from a specific namespace, both \a namespace and tables `namespace work:

$ q sp.q
KDB+ 2.7 2011.02.16 Copyright © 1993-2011 Kx Systems
+`p`city!(`p$`p1`p2`p3`p4`p5`p6`p1`p2;`london`london`london`l..
(+(,`color)!,`blue`green`red)!+(,`qty)!,900 1000 1200
+`s`p`qty!(`s$`s1`s1`s1`s2`s3`s4;`p$`p1`p4`p6`p2`p2`p4;300 200 100 400 200 300)
q)\a
`p`s`sp
q)\a .
`p`s`sp
q)tables `.
`p`s`sp
q)

What is a dyadic function?

A function that takes two arguments. Dyadic is to binary as Aphrodite is to Venus.

See wikipedia's definition.

I understand white space matters in q code. Where does it matter?

  • The first character before a '/' that begins a comment must be whitespace (newline is OK).
  • Every line after the first in a multiline expressions must be indented.

The latter rule applies, for example, to functions. The following is not valid q:

f: {[x]
x}

Even a function's closing brace must be indented if it appears on a line by itself:

f: {[x]
x
}

Function definition is not the only time you have to be careful to follow this rule, however. In the following example, g is not the list (47; 48); rather, g is a projection of the comma operator with its first argument set to 47:

g: 47,
48

How do I delete a column from a table?

The delete command works for that, too. To remove a column named xyz from table t, use

delete xyz from `t // in place delete (backquote)
delete xyz from t  // returns a copy minus column xyz

I know SQL and don’t care to learn q. Can I still get my work done while benefiting from kdb’s speed?

Yes and no. q supports SQL out of the box. In many cases, you will see an improvement in performance compared to a similar traditional RDBMS. However, q does not optimize your queries for you, so some queries may actually take more time.

James Gosling stated that he purposely made Java syntax C-like to facilitate the adoption of the JVM platform. q's query language was similarly designed to resemble SQL. Begin with what you know, and incrementally refine your queries using q-specific features to get the best performance possible.

How do I delete the nth row from a table?

Use delete and the virtual column i:

delete from `t where i = n

Don't forget the backquote if you intend to delete-in-place.

What does it mean for a q function or table to be a ‘first class’ type?

Like any other value, you can do the following with functions:

  • pass them as arguments to other functions
  • return them from functions
  • create new functions at run time

In fact, since the syntax of q is consistent with the semantic similarity between calling a function and indexing a data structure, sometimes you won't even care whether a particular value is a function or data.

Tables can be treated as ordinary data as well. In a typical database, tables can only be manipulated and queried using a special language via a specific access mechanism (.NET's LINQ is Microsoft's effort to bridge the gap). In q, although you can use a query language when convenient, you can also use any other feature of the q language that you might want. For example, sometime it's much simpler or faster to express a table update using . (apply).

What kdb connection libraries exist?