select [] syntax

In playing around with the select syntax with a table such as:

t:(a:10 20 30;b:40 50 60)

I notice that the following syntaxes are supported as opposed to generating the expected exception though I don’t know if a third parameter is planned or supported by q.  Any ideas on what’s going on here?

q)select [;;] from t
a  b

10 40
20 50
30 60

q)select [;;>a] from t
a  b

30 60
20 50
10 40

q)select [;;;] from t
'length

You can find more info about this syntax here:
https://code.kx.com/q4m3/9\_Queries\_q-sql/#9324-select

Basically, there are four options:

  1. select [n] from table

  2. select [n m] from table… where m is starting row number and n is number of rows

  3. select [> col] from table 

  4. select [n;>col] from table

Thank you Himanshu,

I am aware of these 4 standard cases from the Borror book, however I was curious why the parser does not signal an error on the following cases (note the double semicolons).  Is it simply a parser error in the template?

q)select [;;] from t
a  b

10 40
20 50
30 60

q)select [;;>a] from t
a  b

30 60
20 50
10 40

I’m seeing similar behaviour - the projected form with 2 or 3 arguments does something I hadn’t expected. Where does the number 11 come from?

$ q

KDB+ 3.6 2018.05.17 Copyright (C) 1993-2018 Kx Systems

q)-3!parse"select[;;>a] from t"

“(?;t;();0b;();11;,(\>:;a))”

q)-3!parse"select[;>a] from t"

“(?;t;();0b;();(\>;11;a))”

This could be pure tinfoil-hat speculation but:

q)“x”$“11”

0x3131

q)

q)“c”$3131

“;”

Coincidence?? 

Terry

Ha!  Terry I like your tinfoil hat speculation! :)

I believe the template parser is not strict enough and passes a number of cases that would otherwise be errors.

I have found:

select [>a,distinct a] from t

select [>(distinct a),a] from t

select [>a,a] from t

to execute and I thought the first case returned a useful, though strange result, but I cannot make sense of the result from the second expression and decided not to document these anomalies in my book.

I’ve also found that placing the from clause after the where clause in a template changes the behavior of the statement, but again they just seem like idiosyncrasies and not terribly useful behavior.

Regards, Erik