cancel
Showing results for 
Search instead for 
Did you mean: 

parse tree not working for simple select

newstudent2017
New Contributor II

 

 

tab:([] c1: til 10 ; c2:10 + til 10)

/Both do not work
?[tab;til 10;(>;`c2;16)]

?[tab;til 10;(last `c2)]

 

 

9 REPLIES 9

mchbrn-q
New Contributor II

Simply prepend where to the former

q)where ?[tab;til 10;(>;`c2;16)]
7 8 9

and insert a semicolon to the latter

q)?[tab;til 10;(last;`c2)]
19

Where in the doc does it say a "where" is needed ?

Similarly adding ";" is not necessary in the examples shown in docs.

 

@SJT, Are these both cases of incomplete documentation ?

It's not entirely clear what you are trying to achieve when you only supply two broken queries.

Adding where to your query was a suggestion based on the return type being a list of booleans. Using where with your query returns the indexes of tab where c2>16. This could then be fed back into the table to return the desired data.

q)tab where ?[tab;til 10;(>;`c2;16)]
c1 c2
-----
7  17
8  18
9  19

You'll need to use semicolons to form a valid parse tree as shown here https://code.kx.com/q/wp/parse-trees/#eval-and-value like you have used in your first query but not your second.

This was all based off the assumption that you were attempting to use simple execs in functional form https://code.kx.com/q/basics/funsql/#simple-exec

You can also include where in the parse tree passed as the last argument.

q)?[tab;til 10;(where;(>;`c2;16))]
7 8 9

In response to @cillianreilly and @mchbrn-q

It seems, simple exec parse tree has a dual nature : It can act as a "c" and an "a" in  ?[t;c;b;a] . 

It seems like a "c" here:

?[tab;til 10;(where;(>;`c2;16))]

/Equivalent
?[tab;til 10;((&:);(>;`c2;16))]

It seems like an "a" here:

?[tab;til 10;(last;`c2)]


Thoughts ?

Yeah that's interesting. And since "a", the select phrase, can take a parse tree, you could translate the functional simple exec

q)?[tab;til 10;(last;`c2)]
19

to the functional exec

q)exec last c2 from tab
19
q)parse "exec last c2 from tab"
?
`tab
()
()
,(last;`c2)
q)?[tab;();();(last;`c2)]
19

I can't find any examples online of standard qSQL simple execs though? I don't know if it exists because https://code.kx.com/q/ref/exec/#syntax doesn't reference any phrases that allows for indexes . . .

Lets us  ask the all knowing wizard , the mighty @SJT 😀 about the dual nature mentioned in my post above.

davidcrossey
Moderator Moderator
Moderator

As you've mentioned parse trees in your post title, I'll limit the scope of the following examples here using parse on a select statement*;

If you want to carry out of a functional select for both of the queries above, I suggest using the parse keyword to determine the components of the phrase:

// c2>16 example
q)parse "select from tab where c2>16"
?
`tab
,,(>;`c2;16)
0b
()

q)?[tab;enlist (>;`c2;16);0b;()]
c1 c2
-----
7  17
8  18
9  19


// last c2 example
q)parse "select last c2 from tab"
?
`tab
()
0b
(,`c2)!,(last;`c2)

q)?[tab;();0b;enlist[`c2]!enlist (last;`c2)]
c2
--
19

You can read more about parse here and parse trees here

*There are other ways of retrieving specific values for the queries you've listed. For example, you could also write last tab[`c2] to fetch the last value in the c2 column

Hope this helps.

David

Would just like to add https://code.kx.com/q/basics/funsql/#simple-exec

Judging by the number of args in OP's functional query, a simple exec takes table name, list of indexes and parse tree.