如何将此 select 语句转换为函数形式?
How can I convert this select statement to functional form?
我遇到了几个问题,无法将其转换为函数格式。
select from tableName where i=fby[(last;i);([]column_one;column_two)]
这是我得到的:
?[tableName;fby;enlist(=;`i;(enlist;last;`i);(+:;(!;enlist`column_one`column_two;(enlist;`column_one;`column_two))));0b;()]
但我收到 type
错误。
有什么建议吗?
其实我自己搞定了 ->
?[tableName;((=;`i;(fby;(enlist;last;`i);(+:;(!;enlist`column_one`column_two;(enlist;`column_one;`column_two)))));(in;`venue;enlist`venueone`venuetwo));0b;()]
声明中缺少 ()
问题。现在工作正常。
**如果有人想添加有关如何构建手动解析树以及如何将通用 (k){}
函数替换为 q
中的实际函数的更详细说明,请随时添加你的回答,我会接受并点赞
很高兴您能够解决将查询转换为函数形式的问题。
通常情况下,当您在语句中使用带有 fby
的 parse 时,q 会将此函数转换为它的 k 定义。通常你应该能够用 q 函数本身替换这个 k 代码(即将 (k){stuff}
更改为 fby
)并且在将查询转换为函数形式时这应该 运行 正确。
此外,如果您查看 https://code.kx.com/v2/wp/parse-trees/,它会更详细地介绍解析树和函数形式。此外,它包含一个名为 buildQuery
的脚本,它将 return 感兴趣的查询的函数形式作为字符串,这在函数形式复杂时非常方便并节省时间。
考虑使用以下函数,根据Parse Trees白皮书中给出的buildQuery
函数进行调整。这是一个在 q 中快速开发的非常有用的工具,这个版本是对链接白皮书中给出的版本的改进,已经扩展到通过引用处理更新(即update x:3 from `tab
)
\c 30 200
tidy:{ssr/[;("\"~~";"~~\"");("";"")] $[","=first x;1_x;x]};
strBrk:{y,(";" sv x),z};
//replace k representation with equivalent q keyword
kreplace:{[x] $[`=qval:.q?x;x;"~~",string[qval],"~~"]};
funcK:{$[0=t:type x;.z.s each x;t<100h;x;kreplace x]};
//replace eg ,`FD`ABC`DEF with "enlist`FD`ABC`DEF"
ereplace:{"~~enlist",(.Q.s1 first x),"~~"};
ereptest:{((0=type x) & (1=count x) & (11=type first x)) | ((11=type x)&(1=count x))};
funcEn:{$[ereptest x;ereplace x;0=type x;.z.s each x;x]};
basic:{tidy .Q.s1 funcK funcEn x};
addbraks:{"(",x,")"};
//where clause needs to be a list of where clauses, so if only one whereclause need to enlist.
stringify:{$[(0=type x) & 1=count x;"enlist ";""],basic x};
//if a dictionary apply to both, keys and values
ab:{$[(0=count x) | -1=type x;.Q.s1 x;99=type x;(addbraks stringify key x),"!",stringify value x;stringify x]};
inner:{[x]
idxs:2 3 4 5 6 inter ainds:til count x;
x:@[x;idxs;'[ab;eval]];
if[6 in idxs;x[6]:ssr/[;("hopen";"hclose");("iasc";"idesc")] x[6]];
//for select statements within select statements
//This line has been adjusted
x[1]:$[-11=type x 1;x 1;$[11h=type x 1;[idxs,:1;"`",string first x 1];[idxs,:1;.z.s x 1]]];
x:@[x;ainds except idxs;string];
x[0],strBrk[1_x;"[";"]"]
};
buildSelect:{[x]
inner parse x
};
我们可以使用它来创建有效的功能查询
q)n:1000
q)tab:([]sym:n?`3;col1:n?100.0;col2:n?10.0)
q)buildSelect "select from tab where i=fby[(last;i);([]col1;col2)]"
"?[tab;enlist (=;`i;(fby;(enlist;last;`i);(flip;(lsq;enlist`col1`col2;(enlist;`col1;`col2)))));0b;()]"
所以我们有下面的函数形式
?[tab;enlist (=;`i;(fby;(enlist;last;`i);(flip;(lsq;enlist`col1`col2;(enlist;`col1;`col2)))));0b;()]
// Applying this
q)?[tab;enlist (=;`i;(fby;(enlist;last;`i);(flip;(lsq;enlist`col1`col2;(enlist;`col1;`col2)))));0b;()]
sym col1 col2
----------------------
bah 18.70281 3.927524
jjb 35.95293 5.170911
ihm 48.09078 5.159796
...
我遇到了几个问题,无法将其转换为函数格式。
select from tableName where i=fby[(last;i);([]column_one;column_two)]
这是我得到的:
?[tableName;fby;enlist(=;`i;(enlist;last;`i);(+:;(!;enlist`column_one`column_two;(enlist;`column_one;`column_two))));0b;()]
但我收到 type
错误。
有什么建议吗?
其实我自己搞定了 ->
?[tableName;((=;`i;(fby;(enlist;last;`i);(+:;(!;enlist`column_one`column_two;(enlist;`column_one;`column_two)))));(in;`venue;enlist`venueone`venuetwo));0b;()]
声明中缺少 ()
问题。现在工作正常。
**如果有人想添加有关如何构建手动解析树以及如何将通用 (k){}
函数替换为 q
中的实际函数的更详细说明,请随时添加你的回答,我会接受并点赞
很高兴您能够解决将查询转换为函数形式的问题。
通常情况下,当您在语句中使用带有 fby
的 parse 时,q 会将此函数转换为它的 k 定义。通常你应该能够用 q 函数本身替换这个 k 代码(即将 (k){stuff}
更改为 fby
)并且在将查询转换为函数形式时这应该 运行 正确。
此外,如果您查看 https://code.kx.com/v2/wp/parse-trees/,它会更详细地介绍解析树和函数形式。此外,它包含一个名为 buildQuery
的脚本,它将 return 感兴趣的查询的函数形式作为字符串,这在函数形式复杂时非常方便并节省时间。
考虑使用以下函数,根据Parse Trees白皮书中给出的buildQuery
函数进行调整。这是一个在 q 中快速开发的非常有用的工具,这个版本是对链接白皮书中给出的版本的改进,已经扩展到通过引用处理更新(即update x:3 from `tab
)
\c 30 200
tidy:{ssr/[;("\"~~";"~~\"");("";"")] $[","=first x;1_x;x]};
strBrk:{y,(";" sv x),z};
//replace k representation with equivalent q keyword
kreplace:{[x] $[`=qval:.q?x;x;"~~",string[qval],"~~"]};
funcK:{$[0=t:type x;.z.s each x;t<100h;x;kreplace x]};
//replace eg ,`FD`ABC`DEF with "enlist`FD`ABC`DEF"
ereplace:{"~~enlist",(.Q.s1 first x),"~~"};
ereptest:{((0=type x) & (1=count x) & (11=type first x)) | ((11=type x)&(1=count x))};
funcEn:{$[ereptest x;ereplace x;0=type x;.z.s each x;x]};
basic:{tidy .Q.s1 funcK funcEn x};
addbraks:{"(",x,")"};
//where clause needs to be a list of where clauses, so if only one whereclause need to enlist.
stringify:{$[(0=type x) & 1=count x;"enlist ";""],basic x};
//if a dictionary apply to both, keys and values
ab:{$[(0=count x) | -1=type x;.Q.s1 x;99=type x;(addbraks stringify key x),"!",stringify value x;stringify x]};
inner:{[x]
idxs:2 3 4 5 6 inter ainds:til count x;
x:@[x;idxs;'[ab;eval]];
if[6 in idxs;x[6]:ssr/[;("hopen";"hclose");("iasc";"idesc")] x[6]];
//for select statements within select statements
//This line has been adjusted
x[1]:$[-11=type x 1;x 1;$[11h=type x 1;[idxs,:1;"`",string first x 1];[idxs,:1;.z.s x 1]]];
x:@[x;ainds except idxs;string];
x[0],strBrk[1_x;"[";"]"]
};
buildSelect:{[x]
inner parse x
};
我们可以使用它来创建有效的功能查询
q)n:1000
q)tab:([]sym:n?`3;col1:n?100.0;col2:n?10.0)
q)buildSelect "select from tab where i=fby[(last;i);([]col1;col2)]"
"?[tab;enlist (=;`i;(fby;(enlist;last;`i);(flip;(lsq;enlist`col1`col2;(enlist;`col1;`col2)))));0b;()]"
所以我们有下面的函数形式
?[tab;enlist (=;`i;(fby;(enlist;last;`i);(flip;(lsq;enlist`col1`col2;(enlist;`col1;`col2)))));0b;()]
// Applying this
q)?[tab;enlist (=;`i;(fby;(enlist;last;`i);(flip;(lsq;enlist`col1`col2;(enlist;`col1;`col2)))));0b;()]
sym col1 col2
----------------------
bah 18.70281 3.927524
jjb 35.95293 5.170911
ihm 48.09078 5.159796
...