英文:
How to create a UDF used for a SQL where clause in DolphinDB?
问题
我已经创建了一个UDF并在where
子句中使用它,如下图所示。
def isZero(number, precision=4):
unit = 1 \ pow(10, precision)
ngUnit = -unit
if (ngUnit < number && number < unit):
return true
return false
}
res = select * from diff where isZero(openDiff)
但是报告了以下错误:
发生这种情况的原因是什么?在创建这样的子句时,我需要注意什么吗?
英文:
I have created a UDF and used it in a where
clause, which is shown in the following figure.
def isZero(number,precision=4){
unit =1\pow(10,precision)
ngUnit =-unit
if(ngUnit < number && number < unit){
return true
}
return false
}
res= select * from diff where isZero(openDiff)
But an error is reported as follows:
Why is that happening? Is there anything I should be aware of when creating a UDF in such a clause?
答案1
得分: 1
DolphinDB支持向量运算。在where
子句中,您UDF的参数openDiff是一列(即向量),因此需要确保您的函数返回相同长度的向量。但您为UDF设计的输出是布尔标量。要解决这个问题,请参考以下方法:
(1)将您的UDF更改为向量函数
(2)使用each(isZero, openDiff)
(3)使用iif
:where iif((openDiff < 1.0/pow(10,4) && openDiff > -1.0/pow(10,4)), true, false) == true
(4)使用eqFloat(precision=4)
英文:
DolphinDB supports vector operation. In the where
clause, the parameter openDiff of your UDF is a column (i.e., a vector), so you need to ensure that your function returns a vector of the same length. But the output you designed for your UDF is a Boolean scalar. To solve your problem, refer to the following methods:
(1) change your UDF to a vector function
(2) use each(isZero, openDiff)
(3) Use iif
: where iif((openDiff<1.0/pow(10,4) && openDiff>-1.0/pow(10,4)),true,false) == true
(4) use eqFloat(precision=4)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论