KDB:为什么在更新时出现类型错误?

KDB: why am I getting a type error when upserting?

我将列指定为字符串类型。为什么会出现以下错误:

q)test: ([key1:"s"$()] col1:"s"$();col2:"s"$();col3:"s"$())
q)`test upsert(`key1`col1`col2`col3)!(string "999"; string "693"; string "943"; 
string "249")

'type
[0]  `test upsert(`key1`col1`col2`col3)!(string "999"; string "693"; string "9
43"; string "249")

您有几个问题:

  • 引号中的字符数组一个字符串,所以不需要写string "abc"
  • string "aaa" 将字符串拆分为字符串
  • 您最初定义的类型是符号 "s" 而不是字符串

这将允许您作为符号插入:

q)test: ([key1:"s"$()] col1:"s"$();col2:"s"$();col3:"s"$())
q)`test upsert(`key1`col1`col2`col3)!`$("999"; "693"; "943"; "249")
`test

这会将它们保留为字符串:

q)test: ([key1:()] col1:();col2:();col3:())
q)`test upsert(`key1`col1`col2`col3)!("999"; "693"; "943"; "249")
`test

看看两者的版本差异

HTH, 肖恩

要做到这一点,您可以删除您在测试中定义的列表类型:

q)test: ([key1:()] col1:();col2:();col3:())
q)test upsert (`key1`col1`col2`col3)!("999";"693";"943";"249")
key1 | col1  col2  col3
-----| -----------------
"999"| "693" "943" "249"

您收到类型错误的原因是 "s" 对应于符号列表,而不是字符列表。您可以使用 .Q.ty:

进行检查
q).Q.ty `symbol$()
"s"
q).Q.ty `char$()
"c"

将键设置为嵌套的字符列表(通常)不是一个好主意,您可能会发现将它们设置为整数 ("i") 或长整数 ("j") 会更好如:

test: ([key1:"j"$()] col1:"j"$();col2:"j"$();col3:"j"$())

将键设置为 integers/longs 将使 upsert 函数表现良好。另请注意,table 是字典列表,因此每个字典都可以单独更新,也可以更新 table:

q)`test upsert (`key1`col1`col2`col3)!(9;4;6;2)
`test
q)test
key1| col1 col2 col3
----| --------------
9   | 4    6    2
q)`test upsert (`key1`col1`col2`col3)!(8;6;2;3)
`test
q)test
key1| col1 col2 col3
----| --------------
9   | 4    6    2
8   | 6    2    3
q)`test upsert (`key1`col1`col2`col3)!(9;1;7;4)
`test
q)test
key1| col1 col2 col3
----| --------------
9   | 1    7    4
8   | 6    2    3
q)`test upsert ([key1: 8 7] col1:2 4; col2:9 3; col3:1 9)
`test
q)test
key1| col1 col2 col3
----| --------------
9   | 1    7    4
8   | 2    9    1
7   | 4    3    9