Search code examples
kdb

How to decipher this Q/KDB code


I need to understand this code.

raze(handle1, handle2, handle3)@\:({.myqueries.select_from_all[`some_table] . (a;b;c;();();0b);p1,p2;`})

Basically, the idea is to select from three databases data from the same table.

What does @\: do in this case? Why do we have {} inside ()?

What does the . mean after select_from_all[`some_table]?


Solution

  • Looking at the code you've supplied, looks like there may be something missing - I'll run you through the expected form of what it looks like the code is doing:

    raze(listOfHandles)@\:({[x;y] funcUsingXandY};arg0;arg1)
    
    where:
          listOfHandles - a list of handles to remote processes
          @\: - apply the right hand side to each item in the left hand side list
          ({[x;y] funcUsingXandY};arg0;arg1) - this sends a function definition (lambda) + args for the function, to be executed on each of the handles.
    

    An example (assumes there are processes listening on localhost, port 7070 and 7071):

    q) handle1:hopen 7070;
    q) handle2:hopen 7071;
    q) raze(handle1,handle2)@\:({[x;y] enlist(.z.p; x+y)}; 39;3)
        2014.08.23D14:11:15.452611000 42
        2014.08.23D14:11:15.452847000 42
    

    In the above example, we execute the function {[x;y] enlist(.z.p; x+y)} with args (39;3) on each of the remote handles handle1 and handle2 -- we get a response of current time (.z.p) & sum of the two args in our response for each handle. The raze joins the results returned from each handle into a single list (in your example, it looks like the function is returning a table - in this case, raze would join the list of tables return from the handles, into a single table)

    Edit: I missed the part in your question regarding . -- the . allows you to apply a list of arguments to a function taking multiple args - for example:

    q)plus:{[x;y] x+y}
    q)plus . (4;5)
        9
    

    For more info, see: