KDB 通过 kdb 字符串解析来识别日期

问题描述 投票:0回答:2

我正在尝试解析以字符串格式记录用户查询的列。例如

"select from table where date=2023.01.01"
"select from table2 where date = 2024.01.01"
"select from table3 where date >= 2022.01.01"

我的目标是提取日期并将其以日期格式保存在不同的列中。我正在按照以下方法进行操作,该方法适用于一种

update queryDate:first each "," vs/: last each "date=" vs/: colName from tableName;
update "D"$queryDate from tableName;

如果查询字符串具有“date=”,则效果很好,但如果“date=”或“date>=”或“date>=”,则效果很好,如果查询具有“日期内”,则绝对不行

有没有一个好的方法可以将以上所有的可能性都包含在上面的条件中?

谢谢!

kdb
2个回答
1
投票

我认为更好的方法是从字符串查询创建解析树,然后从 where 子句中提取日期约束。请参阅 [https://code.kx.com/q/wp/parse-trees/][1] 了解更多信息。

q)f:{wc:(parse x)[2;0]; wc[;2] where `date=wc[;1]}
q)raze f each ("select from table where date=2023.01.01"; "select from table2 where date = 2024.01.01"; "select from table3 where date >= 2022.01.01")
q)2023.01.01 2024.01.01 2022.01.01

0
投票

另一种方法是使用

ss
和索引进行通配符搜索,例如

q)show t:([] qry:("select from table where date=2023.01.01";"select from table2 where date = 2024.01.01";"select from table3 where date >= 2022.01.01";"select from table4 where date within 1993.01.01 2000.01.01"))
qry
------------------------------------------------------------
"select from table where date=2023.01.01"
"select from table2 where date = 2024.01.01"
"select from table3 where date >= 2022.01.01"
"select from table4 where date within 1993.01.01 2000.01.01"

q)wc:"[0-9][0-9][0-9][0-9].[0-9][0-9].[0-9][0-9]";

q)update dt:qry@'idx+\:\:til 10 from update idx:ss[;wc]each qry from t
qry                                                          idx   dt
----------------------------------------------------------------------------------------------
"select from table where date=2023.01.01"                    ,29   ,"2023.01.01"
"select from table2 where date = 2024.01.01"                 ,32   ,"2024.01.01"
"select from table3 where date >= 2022.01.01"                ,33   ,"2022.01.01"
"select from table4 where date within 1993.01.01 2000.01.01" 37 48 ("1993.01.01";"2000.01.01")
© www.soinside.com 2019 - 2024. All rights reserved.