在 KX 中从客户端添加一个新列

add a new column from client side in KX

我在这样的服务器中有一个 table

col0  col1  col2  col3  col4  
-----------------------------
42034 64878 62076 3716  51556 
76279 27763 61893 56680 34261
54345 39819 44585 42108 83936 
88150 17032 58679 35727 56002 
22830 77292 56069 58333 31837

我尝试使用现有列之一从客户端添加新列 +1.user 将输入现有列号。

#cat add_cols.q

N:.z.x
h: hopen 5010;
h "update newcol1:N+1 from `t"
res:h "t";
show res
exit 1
#q ./add_cols.q col1 

结果为

col0  col1  col2  col3  col4  newcol1
-------------------------------------
42034 64878 62076 3716  51556 6
76279 27763 61893 56680 34261 6
54345 39819 44585 42108 83936 6
88150 17032 58679 35727 56002 6
22830 77292 56069 58333 31837 6
h "update newcol1:N+1 from `t"

这将不起作用,因为它将使用服务器进程上定义的任何 N,N 必须为 5。

你想要这样的东西:

//so.q
N:.Q.opt[.z.x]; // better to use flags and convert into a dictionary with .Q.opt
h:hopen 5010;
h raze "update newcol1:",N[`myCol],"+1 from `t";
show h"t";
exit 0; // generally anything other than exit 0 indicates a failure in execution

// creilly solution - avoids the user from feeding problematic code to the server process
N:.Q.opt[.z.x]; h:hopen 5010;
col:first`$N`myCol; 
h(!;`t;();0b;enlist[`newCol1]!enlist(+;col;1));
show h"t"; exit 0;

// This could also be defined on the server as a function e.g.:
myFunc:{[col] ![`t;();0b;enlist[`newCol1]!enlist(+;col;1)] }
// And called from the client with h(`myFunc;col)


q so.q -myCol col1
col0 col1 col2 col3 col4 newcol1
--------------------------------
370  45   804  829  660  46
481  851  12   503  926  852
564  487  481  120  418  488
578  94   732  710  126  95
920  392  995  506  77   393

t:([]col0:5?1000;col1:5?1000;col2:5?1000;col3:5?1000;col4:5?1000)