KX Community

Find answers, ask questions, and connect with our KX Community around the world.
KX Community Guidelines

Home Forums kdb+ Why Q SQL doesn’t work with "in"?

  • Why Q SQL doesn’t work with "in"?

    Posted by vivo on December 6, 2022 at 12:00 am

    I have codes as below. Why the last line would fail? How to make it work?

    Thanks!

     

    t:([] c1:`a`b`a`c`b`c; c2:1 1 1 2 2 2; c3:10 20 30 40 50 60);

    select from t where c1 in enlist `a; // works
    select from t where c1 in `a`b; // works
    ?[t;enlist (in;`c1;enlist `a);0b;()]; // works
    ?[t;enlist (in;`c1;`a`b);0b;()]; // failed

     

    vivo replied 8 months, 2 weeks ago 2 Members · 1 Reply
  • 1 Reply
  • gyorokpeter-kx

    Member
    December 6, 2022 at 12:00 am

    The parameters to functional select are parse trees. These have a special rule for symbols and lists because a symbol can also represent a variable/column name, and a list can mean function invocation.

    The rule is that enlisting a value acts as an “escape” so the symbol or list is taken at face value and not evaluated. In your 3rd example, you are enlisting the symbol so it acts as a literal symbol and not a column named “a”. In the 4rd example, there is no enlist, so the list `a`b is taken as a function application, which then looks for variables or columns named a and b to perform the application. It works if you enlist the symbol list.

    ?[t;enlist (in;`c1;enlist`a`b);0b;()]

Log in to reply.