Search code examples
kdb+

How to write a functional form in KDB+/Q with a present wildcard inside where clause?


As an example in a normal qSQL query we would write:

select from table where sym like "temp*"

To select any elements in the `sym column that start with "temp*" followed by any sequence of characters.

How would we write that in a functional form? It is purely for an assignment task that I'm completing which it requires me to re-write a function using functional form.

?[trade; enlist(like;`newCol;($:;enlist`ABC)); 0b; ()] this works well in the KX Academy Jupyter Notebook however, this does not take a wildcard into consideration.

My parsed select statement returns me this:

?
`table
,,(';(like;`sym);(*;`temp))
0b
()

However, I'm failing to understand how to write this, there's nothing I could find online. Please help.


Solution

  • When building your query you can use backslashes to escape the quote characters to allow your query to be parsed:

    q)parse "select from table where sym like \"temp*\""
    ?
    `table
    ,,(like;`sym;"temp*")
    0b
    ()
    

    which would be formed into:

    ?[table;enlist (like;`sym;"temp*");0b;()]
    

    There is a nice script by kx which helps in forming functional selects:

    https://code.kx.com/q/wp/parse-trees/#appendix