我有一个表volatilitysurface和一个详细表volatilitysurface_smile作为详细表的一部分,我为主表定义了一个外键,即
volatilitysurface::([date:`datetime$(); ccypair:`symbol$()] atm_convention:`symbol$(); ...);
volatilitysurface_smile::([...] volatilitysurface:`volatilitysurface$(); ...);当我尝试使用AquaQ的TorQ .loader.loadallfiles加载详细表volatilitysurface_smile时,需要将其作为"dataprocessfunc“函数的一部分来动态构建外键字段,即
rawdatadir:hsym `$("" sv (getenv[`KDBRAWDATA]; "volatilitysurface_smile"));
.loader.loadallfiles[`headers`types`separator`tablename`dbdir`partitioncol`partitiontype`dataprocessfunc!(`x`ccypair...;"ZS...";enlist ",";`volatilitysurface_smile;target;`date;`month;{[p;t] select date,ccypair,volatilitysurface,... from update date:x,volatilitysurface:`volatilitysurface$(x,'ccypair) from t}); rawdatadir];请注意以下部分:
update date:x,volatilitysurface:`volatilitysurface$(x,'ccypair) from t强制转换错误指向volatilitysurface键的构造。但是,这在.loader.loadallfiles之外工作,并且表是全局::,并且在调用.loader.loadallfiles函数之前完全定义。
有什么办法处理这个用例吗?如果未初始化详细表外键,则插入将失败。
发布于 2017-11-21 09:43:54
错误可能是由于更新中的作用域造成的。当您在.loader命名空间中运行cast/update时,表名需要是完全限定作用域的(`..volatilitysurface)。
例如:update date:x,volatilitysurface:`..volatilitysurface$(x,'ccypair) from t
致以敬意,
史考特
发布于 2017-11-22 10:00:40
您确定所有可能的x和ccypair组合都在挥发表中吗?'cast错误似乎表明情况并非如此。
q)t:([a:1 2 3;b:`a`b`c] c:"ghi")
q)update t:`t$(a,'b) from ([] a:2 3 1;b:`b`c`a)
a b t
-----
2 b 1
3 c 2
1 a 0
q)update t:`t$(a,'b) from ([] a:2 3 1 5;b:`b`c`a`d)
'cast
[0] update t:`t$(a,'b) from ([] a:2 3 1 5;b:`b`c`a`d)
^注意,在第二个例子中,我有a-b对(5;`d),它不存在于表t中,因此我得到了'cast错误。
您可以确定是否有丢失的密钥,以及它们是哪一个,如下所示:
q)all (exec (a,'b) from ([] a:2 3 1;b:`b`c`a)) in key t //check for presence, all present
1b
q)all (exec (a,'b) from ([] a:2 3 1 5;b:`b`c`a`d)) in key t //check for presence, not all present
0b
q)k where not (k:exec (a,'b) from ([] a:2 3 1 5;b:`b`c`a`d)) in key t //check which keys AREN'T present
5 `d如果是这样的话,我想你有两个选择:
第二种选择可能是这样的:
q.test){if[count k:k where not (k:exec (a,'b) from x) in key `..t;@[`..t;;:;value[`..t](0N;`)]'[k]];update t:`t$(a,'b) from x}([] a:2 3 1;b:`b`c`a)
a b t
-----
2 b 1
3 c 2
1 a 0
q.test){if[count k:k where not (k:exec (a,'b) from x) in key `..t;@[`..t;;:;value[`..t](0N;`)]'[k]];update t:`t$(a,'b) from x}([] a:2 3 1 5 6;b:`b`c`a`d`e)
a b t
-----
2 b 1
3 c 2
1 a 0
5 d 3
6 e 4
q.test)value `..t //check table t, new dummy records added by previous call
a b| c
---| -
1 a| g
2 b| h
3 c| i
5 d|
6 e|我在名称空间中进行了这些测试,因为数据处理函数将在TorQ中运行(即在某些地方需要使用`..t来访问根命名空间中的t )。用于设置的此函数的类似版本(具有比上面的一行更好的格式)如下所示:
{
if[count k:k where not (k:exec (x,'ccypair from volatilitysurface_smile) in key `..volatilitysurface; //check for missing keys
@[`..volatilitysurface;;:;value[`..volatilitysurface](0Nz;`)]'[k]]; //index into null key of table to get dummy record and upsert to global volatilitysurface table
update volatilitysurface:`volatilitysurface$(x,'ccypair) from x //create foreign key
}https://stackoverflow.com/questions/47395333
复制相似问题