/ API BAG:`bag / fake bs loadbs:{Bag::2:BAG} / load bs savebs:{BAG 1:Bag} / save bs initbs:{Bag::.+(`t`o`d`p;(();();();()));savebs`} / init bs loadbs` getbs:{[t]r:+Bag[`o`d`p;&t=Bag`t];:[#r;r@>#:'r[;0];r]} / get bag server setbs:{[t;o;p;d]:[(#Bag`t)>i:keybs[t]o;repbs[p;d]i;appbs[t;o;p]d];savebs`} / set bag server keybs:{[t;o](+Bag`t`o)?(t;o)} / keys appbs:{[t;o;p;d]Bag[],:+,(t;o;d;p)} / append repbs:{[p;d;i]Bag[`p`d;i]:(p;d)} / replace Cache:(();()) / ((t;q);(u;n)) / API: set query set:{[t;o] q:rebase[t]o;t:*q;o:q 1 / re-base if[0 r=o^p b:b{?x,cs y}'(O[;1],.())Q?/:q / breaks + canselects if[cm[i;(!j)_dvl i;j#b](j:1+*|i)#s / can match? :(.dbu.H P;match_ o _di i)]]]} / (path;memoized q) / recursive match match_:{[o].[o;(no o;1);{@[x;`table2`ops;:;@[memoize . x`table2`ops;0;$:]]}]} / memoize ops / i = &O _lin o, j = &~O _lin o, b = breaks, s = selects cm:{[i;j;b;s]:[~#,/b;1;&/{:[x;y;(,0#`),y]}[*i;cp[s;i]j]{&/x _lin y}'cp[b;j]i]} / can match? / recursive (t;..base u..q) -> (u;q) rebase:{[t;o]:[0>k:lb o;(t;rebase_ o);(o[k;1;`table];rebase_ (1+k)_ o)]} / re-base ops rebase_:{[o].[o;(no o;1);{@[x;`table2`ops;:;rebase . x`table2`ops]}]} / re-base link/merge w/ops / query type: 0 = run, -1 = fail, n>0 store type:{:[punt[x]y;-1;~#m:ma x;0;~m~,-1+#x;-1;(-1,#x)"1"~*(*|y)`memoize]} / query type / punt conditions punt:{ :[`transpose _in x ;1 / ops not memoizable |/{(~_n~x)&0<#x}'y[&x=`tabu;`cbreaks] ;1 / crosstab not memoizable |/1~'y[&x _lin`willbe`link`amend`merge;`materialize] ;1 / materialize not memoizable 0]} / ok / auxilliary functions sy:`$$: cs:{:[`canselect _in!x;`$x`canselect;0#`]} / canselect values cn:{:[(~"_"=*|x)&.parse.a[`nam]x;(),`$x;0#`]} / 1010 column name cp:{(?,)\x[z]@&1>':y _binl z} / cumulative partition lo:{:[_sm[x:sy x]"*default.lonely";0#`;x]} / default.lonely? lb:{:[#i:&`base=*:'x;*|i;-1]} / last base op ds:{x[;1]:x[;1]di'`SOURCE_;x} / ops - `SOURCE_ dc:{x[;1]:x[;1]di'`canselect;x} / ops - `canselect mi:{:[(#x)_in i:x?/:y;!0;i]} / if all y in x, x?/:y di:{:[y _in!x;x _di y;x]} / tolerant _di ma:{&`materialize=x} / materialize no:{i:&x[;0]_lin`link`amend`merge;i@&0<#:'x[i;1;`ops]} / nested ops \ / disqualifiers tt:{"_"=*$x} / temp table? bm:{|/`dynamic`loop`do`widget _lin*:'x} / block language macros? / column references col:{:[~-3=4:x;?(,/_f'1_ x)_dv`;.parse.a[`nam]x;,`$x;()]} / column token or ` rcol:{:[rfun y;x;()]} / column state if r_(;.. rfun:{:[4:x;0;~_sm["",*x;"r_*"];|/_f'1_ x;"_n"~x 1;1;|/_f'1_ x]} / r_ function w/ null C / dispersed match: sel:{""} willbe:{""} link:{""} linksel:{(-2_ link[x;y]z)," type=select/>"} table:{"\n",(nl@co'$+x[]),"
"} join:{1_,/x,'y};co:join[","];nl:join["\n"] memoize:{(-2_ materialize x)," memoize=1/>"} materialize:{""} colord:{""} t:.+(`state`city`sel`order`stat (`CA`CA`CA`CA`TX`TX`TX`TX`TX `LA`LA`SF`SF`DA`DS`DS`HO`AS 1 1 0 1 1 1 1 1 0 1 2 2 1 1 2 3 4 5 4 10 0 2 5 0N 5 0N 8)) u:.+(`state`pop;(`CA`TX;1000 2000)) v:.+(`state`pop;(,`CA;,1000)) / store the memoization template in the bag server: d:nl(table t;willbe[`r;"g_sum(city;;stat)";"canselect=state"];memoize`sticky) Q[`default.lonely;d] / memoize: t -> `sticky, o -> select state='CA' r:nl(table t;sel"state='CA'";willbe[`r;"g_sum(city;;stat)";"canselect=state"]) Q[`default.lonely;r] / recursive memoization stored queries: qry:"" query[h;ENV.TAB`autosales;qry;_n] qry:"" query[h;ENV.TAB`autosales;qry;_n] qry:" " query[h;ENV.TAB`auto3;qry;_n] qry:" " query[h;ENV.TAB`auto2;qry;_n] qry:" " query[h;ENV.TAB`autosales;qry;_n] input query: qry:" 1993\"/> " query[h;ENV.TAB`autosales;qry;_n] memoized executable form (abbreviated): (`.db.mdb_regression_tester.mdb_regression_test_folder.abc ((`sel : (`link .((`table2;`.db.mdb_regression_tester.mdb_regression_test_folder.ott;) : (`ops ((`sel : (`link .((`table2;`.db.mdb_regression_tester.mdb_regression_test_folder.xyz;) : (`ops ,(`sel : (`willbe .((`name;`abc;) : from before accum call: (`.db.mdb_regression_tester.mdb_regression_test_folder.auto3 ((`sel .((`value;"year<1996";) (`SOURCE_;"3 1 0";))) (`willbe .((`name;`xx;) (`SOURCE_;"3 1 1";))) (`willbe .((`name;`xy;) (`SOURCE_;"3 1 2";))) (`willbe .((`name;`xz;) (`SOURCE_;"3 1 3";))))) -> (`.db.mdb_regression_tester.mdb_regression_test_folder.xyz ,(`sel .((`value;"year<1996";) (`SOURCE_;"3 1 0";)))) ====== (`.db.mdb_regression_tester.mdb_regression_test_folder.auto2 ((`sel .((`value;"year>1993";) (`SOURCE_;"3 0";))) (`link .((`table2;`.db.temp_tables.t0;) (`SOURCE_;"3 1";))) (`willbe .((`name;`xone;) (`SOURCE_;"3 2";))) (`willbe .((`name;`xtwo;) (`SOURCE_;"3 3";))) (`willbe .((`name;`xthree;) (`SOURCE_;"3 4";))))) -> Cache[1;1] (`.db.mdb_regression_tester.mdb_regression_test_folder.ott ((`sel .((`value;"year>1993";) (`SOURCE_;"3 0";))) (`link .((`table2;`.db.temp_tables.t0;) (`SOURCE_;"3 1";))))) ======== (`.db.mdb_regression_tester.mdb_regression_test_folder.autosales ((`willbe .((`name;`xc;) (`SOURCE_;",0";))) (`willbe .((`name;`xb;) (`SOURCE_;",1";))) (`willbe .((`name;`xa;) (`SOURCE_;",2";))) (`link .((`table2;`.db.temp_tables.t1;) (`SOURCE_;",3";))))) (`willbe .((`name;`abc;) (`SOURCE_;",4";)))))) -> (`.db.mdb_regression_tester.mdb_regression_test_folder.abc ((`link .((`table2;`.db.temp_tables.t1;) (`SOURCE_;",3";))) (`willbe .((`name;`abc;) (`SOURCE_;",4";)))))