// sql parser \d .tree \l tree \d .k \l show / tokenizer (atw/sa) / class: ;*aq/: (punc op alphanum quote backslash :) / state: ;*aq/:br abbb qrrr continuation states (i.e. first 4 are starts. last 4 continuation) / t:&4>M\A@ there are 4 start states C:";*aq/:" A:(&33),C?/:"*q****q;;****a*aaaaaaaaaa:;*****aaaaaaaaaaaaaaaaaaaaaaaaaa;/;*aaaaaaaaaaaaaaaaaaaaaaaaaaaa;*;*" M:(C,"br")?/:/:M:1_'M:(";;*aq**" "*;*aq*:" "a;*bq**" "qrrr:/r" "/rrrrrr" ":;*aq**" "b;*bq**" "rrrr:/r") t:{{@[x;&x _lin,"and";:[;,","]]}((&4>M\A _ic x)_ x:@[x;&x _lin"\n\t\r";:;" "])_dv," "} / shift-reduce transitions + valence + operators N:{(1_ x;y;x[,0],z)} / noun S:{(1_ x;x[,0],y;z)} / shift R:{(x;1_ y;(,y[,0],|n#z),(n:v[y;z])_ z)} / reduce F:{(x;v[y;y]_ y;z)} / function Z:{(x;y;z)} / accept Y:{'"left"} / missing left X:{'"right"} / missing right / od,nac|&=+*^_DEI;()ZN T:("RSSSSSSSSSSSSSSSRSRRN 2 on using inner left right full" "RRSSSSSSSSSSSSSSRSRRN 1 distinct" "RRRSSSSSSSSSSSSSRSRRN 2 , or" "RRRRSSSSSSSSSSSSRSRRN 1 not" "RRRRRSSSSSSSSSSSRSRRN 2 as" "RRRRRRSSSSSSSSSSRSRRN 1 count first last sum min max avg var dev med" "RRRRRRRSSSSSSSSSRSRRN 2 |" "RRRRRRRRSSSSSSSSRSRRN 2 &" "RRRRRRRRRSSSSSSSRSRRN 2 = < > ~= ~< ~> ~= <= >=" "RRRRRRRRRRSSSSSSRSRRN 2 + -" "RRRRRRRRRRRSSSSSRSRRN 2 * /" "RRRRRRRRRRRRSSSSRSRRN 2 ^" "RRRRRRRRRRRRRSSSRSRRN 1 ? ~" "RRRRRRRRRRRRRRSSRSRRN 1 date" "RRRRRRRRRRRRRRRSRSRRN 2 extract" "RRRRRRRRRRRRRRRRRSRRN 3 if between" "FFFFFFFFFFFFFFFFFFFFN 1 ;" "SSSSSSSSSSSSSSSSSSSXN 0 (" "FFFFFFFFFFFFFFFFFFFFN 2 )" "SSSSSSSSSSSSSSSSSSYZN 0") T:{1_'(&x=" ")_ x:" ",x}'T / split table O:1_'T;V:(.*:)'O;O:(-1_1_'O),,E:,," ";T:.:''T[;0] / O V E T / digits, noise words, keywords D:".0123456789" J:$`by`join`outer K:$`select`from`where`group`having`order`limit p:{:[~#x;x;1=#x;*x;q m@**|c/(n r x,E;E;())]} / expression parser n:{@[x;&{(x~,"-")&y _in,/O _dv,,")"}':E,x;:[;,"?"]]} / v - n -> v ? n m:{:[-3=4:x;(x;,"-")x~,"?";m'x]} / ? -> - q:{:[4:x;x;~2=#x;q'x;((*x)~,"-")&(*x 1)_in D;,/x;x]} / - D -> -D r:{,/x{y;:[y;("as";x);,x]}'0&':~x _lin,/O} / n n -> n as n c:{(T .|o'x[!2;0]). x} / continue o:{(x _in/:O)?1} / operator v:{:[(#y)':K?/:x;x;'"order"]} / list order w:{(x;:[1=#y;y;a y])} / flatten , a:{:[3>#r:b[x]x;x;(,,","),r]} / , if list b:{:["or"~*y;();(,",")~*y;b[x;y 1],,y 2;,y]} / recur on , f:e@g/ / across abc[`f`g`h`i`j]:(0 1 2 10 20 30;101 102 103 104 3 4;`f`e`d`c`b`a;2 3 8 2 9 0;1 2 1 2 2 2) eval:{ t:table of[x]"from" g:group of[x]"group" f:field[g]of[x]"select" w:where of[x]"where" o:order of[x]"order" l:limit of[x]"limit" f::[#f;f;2#,!t] i::[#w;t w;_n] g::[#g;=+t[g]i;_n] r:@[_n;*f;:;{:[@g;@[t;x]i;x[0]'@[t;x 1][i][g]]}'f 1] i::[#o;{x y z x}/[_n;o 1;r@*o];_n] r:@[r;_n;@[;i]] i::[#l;l;0I]&*|^r[] r:@[r;_n;i#] r} table:{:[4:x;`$x;'"table"]} field:{:[~#y;y;(,"*")~y;();4:y;(`$y;y);"as"~*y;(`$y 2;:[#x;(`$*y 1;unparse y[1;1]);unparse y 1]);+field[x]'1_ y]} where:{:[~#x;x;(,",")~*x;,/where'1_ x;"&",unparse x]} group:{:[~#x;x;(,",")~*x;group'1_ x;unparse x]} order:{:[~#x;x;4:x;+,(`$x;<:);(,",")~*x;,/'+order'1_ x;+,(`$x 1;(>:;<:)("desc";"asc")?x 2)]} limit:{:[~#x;x;. x]} unparse:{:[4:x;x;(*x),1!"][",1_,/(";",unparse@)'1_ x]} of:{:[(#x[;0])>i:x[;0]?y;x[i;1];()]} sum:+/ / show eval parse"select sum f+1 as fsum,sum g-1 as gsum from abc group by j having fsum>4 order by fsum desc" show eval parse"select sum f+1 as fsum,sum g-1 as gsum from abc group by j order by fsum desc" show eval parse"select f as ff,g+1 as gg,h from abc where f<30,g>100 order by f asc,g desc limit 5" show eval parse"select f as ff,g+1 as gg,h from abc order by h as asc,i as desc limit 5" show eval parse"select f as ff,g+1 as gg,h from abc limit 5" show eval parse"select f as ff,g+1 as gg,h from abc" show eval parse"select * from abc" show eval parse"select from abc" \ .tree.show parse"select o_year,sum(if(nation = 'BRAZIL';revenue;0)) / sum(revenue) as mkt_share from (select extract(year;o_orderdate) as o_year, revenue, n2.n_name as nation from t,part,supplier,orders,customer,nation n1,nation n2,region where p_partkey = l_partkey and s_suppkey = l_suppkey and l_orderkey = o_orderkey and o_custkey = c_custkey and c_nationkey = n1.n_nationkey and n1.n_regionkey = r_regionkey and r_name = 'AMERICA' and s_nationkey = n2.n_nationkey and between(o_orderdate;date '1995-01-01';date '1996-12-31') and p_type = 'STEEL') as all_nations group by o_year order by o_year" \ "select sum f+1 as fsum,sum g-1 as gsum from abc group by j" fsum gsum ---- ---- 4 202 65 209 "select f as ff,g+1 as gg,h from abc where f<30,g>100 order by h asc,i desc limit 5" ff gg h -- --- - 2 104 d 1 103 e 10 105 c 0 102 f "select f as ff,g+1 as gg,h from abc order by h as asc,i as desc limit 5" ff gg h -- --- - 20 4 b 2 104 d 1 103 e 10 105 c 0 102 f "select f as ff,g+1 as gg,h from abc limit 5" ff gg h -- --- - 0 102 f 1 103 e 2 104 d 10 105 c 20 4 b "select f as ff,g+1 as gg,h from abc" ff gg h -- --- - 0 102 f 1 103 e 2 104 d 10 105 c 20 4 b 30 5 a "select * from abc" f g h i j -- --- - - - 0 101 f 2 1 1 102 e 3 2 2 103 d 8 1 10 104 c 2 2 20 3 b 9 2 30 4 a 0 2 "select from abc" f g h i j -- --- - - - 0 101 f 2 1 1 102 e 3 2 2 103 d 8 1 10 104 c 2 2 20 3 b 9 2 30 4 a 0 2 \ .tree.show parse"select distinct x,sum y from z" .tree.show parse"select x,sum y from t left outer join u group by x order by x" .tree.show parse"select x from (3+(select y from (select z from (1+-t)))) where (select a from u)=10" .tree.show parse"select e.ename, d.dname from employees,e left join departments,d on e.dept=d.dept" .tree.show parse"select o.o, o.i, o.c, o.r, p.p, c.c from orders as o inner join people as p on o.r = p.r inner join customers as c on o.i=c.i" .tree.show parse"select o.o, o.i, o.c, o.r, p.p, c.c from o inner join p on o.r = p.r inner join c on o.i=c.i" \ "select distinct x,sum y from z" +---0 | +---0:"select" | +---1 | +---0:"distinct" | +---1 | +---0:"sum" | +---1:,"y" +---1 +---0:"from" +---1:,"z" "select x,sum y from t left outer join u group by x order by x" +---0 | +---0:"select" | +---1 | +---0:"sum" | +---1:,"y" +---1 | +---0:"from" | +---1 | +---0:"left" | +---1:,"t" | +---2:,"u" +---2 | +---0:"group" | +---1:,"x" +---3 +---0:"order" +---1:,"x" "select x from (3+(select y from (select z from (1+-t)))) where (select a from u)=10" +---0 | +---0:"select" | +---1:,"x" +---1 | +---0:"from" | +---1 | +---0:,"+" | +---1:,"3" | +---2 | +---0 | | +---0:"select" | | +---1:,"y" | +---1 | +---0:"from" | +---1 | +---0 | | +---0:"select" | | +---1:,"z" | +---1 | +---0:"from" | +---1 | +---0:,"+" | +---1:,"1" | +---2 | +---0:,"-" | +---1:,"t" +---2 +---0:"where" +---1 +---0:,"=" +---1 | +---0 | | +---0:"select" | | +---1:,"a" | +---1 | +---0:"from" | +---1:,"u" +---2:"10" "select e.ename, d.dname from employees,e left join departments,d on e.dept=d.dept" +---0 | +---0:"select" | +---1:"d.dname" +---1 +---0:"from" +---1 +---0:"on" +---1 | +---0:"left" | +---1:,"," | +---2:,"d" +---2 +---0:,"=" +---1:"e.dept" +---2:"d.dept" "select o.o, o.i, o.c, o.r, p.p, c.c from orders as o inner join people as p on o.r = p.r inner join customers as c on o.i=c.i" +---0 | +---0:"select" | +---1:"c.c" +---1 +---0:"from" +---1 +---0:"on" +---1 | +---0:"inner" | +---1 | | +---0:"on" | | +---1 | | | +---0:"inner" | | | +---1 | | | | +---0:"as" | | | | +---1:"orders" | | | | +---2:,"o" | | | +---2 | | | +---0:"as" | | | +---1:"people" | | | +---2:,"p" | | +---2 | | +---0:,"=" | | +---1:"o.r" | | +---2:"p.r" | +---2 | +---0:"as" | +---1:"customers" | +---2:,"c" +---2 +---0:,"=" +---1:"o.i" +---2:"c.i" "select o.o, o.i, o.c, o.r, p.p, c.c from o inner join p on o.r = p.r inner join c on o.i=c.i" +---0 | +---0:"select" | +---1:"c.c" +---1 +---0:"from" +---1 +---0:"on" +---1 | +---0:"inner" | +---1 | | +---0:"on" | | +---1 | | | +---0:"inner" | | | +---1:,"o" | | | +---2:,"p" | | +---2 | | +---0:,"=" | | +---1:"o.r" | | +---2:"p.r" | +---2:,"c" +---2 +---0:,"=" +---1:"o.i" +---2:"c.i"