kdb - how does the 'where' function work

317 views Asked by At

I want to understand what is happening with the below statement:

sum(til 100) where 000011110000b

That line evaluates to 22, and I can't figure out why. sum(til 100) is 4950. where 000011110000b returns the list 4 5 6 7. The kdb reference page doesn't seem to explain this use case. Why does the above line evaluate to 22?

Also, why does the below line result in error

4950 where 000011110000b
1

There are 1 answers

0
James Little On BEST ANSWER

Square brackets are used for function call arguments, rather than parentheses. So the above is being interpreted as:

sum[(til 100) where 000011110000b]

And you can probably see now why that would evaluate to 22, i.e. it is the sum of the 5th,6th,7th,8th values of the list til 100, i.e. (0...99), because where is indexing into til 100 using the boolean list 000011110000b

q)til[100] where 000011110000b
4 5 6 7

As you will have noticed, you can omit square brackets when calling functions; but when doing so you need to be sure it will be parsed/interpreted as intended. In general, code is evaluated right-to-left so in this case (til 100) where 000011110000b was evaluated first, and the result passed to the sum function.

Regarding 4950 where 000011110000b - again if we think right to left, the interpreter is trying to pass the result of where 000011110000b to the function 4590. Although 4590 isn't a named function in kdb - this is the format used for IPC, i.e. executing commands on other kdb processes over TCP. So you are telling kdb to use the OS file descriptor number 4590 (which almost certainly won't correspond to a valid TCP connection) to run the command where 000011110000b. See the "message formats" section here for more details: http://code.kx.com/q/tutorials/startingq/ipc/