kdb+k

About xkey implementation


What is the difference between a functional apply approach .[0!y;();xcols x], seen in xkey:

xkey
k){(#x)!.[0!y;();xcols x]}

and a simple function calling: x xcols 0!y.

Why is it the function apply preferred in xkey?

And a second question about a call to xcols from the inside of k) context - how is it ever works? I could not do the same for some reason:

t:([]a:`a`s`d;b:1 2 3;c:4 5 6)
k).[0!t;();xcols `a`b]
ERROR: 'xcols 
(attempt to use variable xcols without defining/assigning first (or user-defined signal))

Solution

  • I think the functional apply may be for when passing the table by name

    t:([]a:`a`s`d;b:1 2 3;c:4 5 6)
    
    .[0!t;();xcols[`a`b]]  /this works
    
    `a`b xcols 0!t         /equivalent non functional form works
    
    q)`a`b xcols 0!`t      /this fails when passing `t
    'type
      [0]  `a`b xcols 0!`t
    
    
    q).[0!`t;();xcols[`a`b]]  /though functional form still works with `t
    
    
    q)`a`b xkey `t            /hence you can use xkey in place
    `t
    

    Edit: To include my comment in the answer for completeness, when inside k you should prefix q commands with .q to access them, as this is the namesapce they reside in

    q)xcols[`a`b]
    k){(x,f@&~(f:cols y)in x)#y}[`a`b]
    q)\
      .q.xcols`a`b
    k){(x,f@&~(f:cols y)in x)#y}[`a`b]
    

    Edit 2: To address your comment, xcols for your example comes down to

    `a`b`c#t
    

    And

    `a`b`c#`t 
    

    doesn't work. I don't have much of an explanation for why it doesn't work, except for that is how it is designed. The functional form is telling q to modify the table by applying the take function. But the example above is not an accepted syntax for redefining a table as a re-ordering of its columns.