如何创建用于DolphinDB中SQL WHERE子句的UDF?

huangapple go评论56阅读模式
英文:

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)

但是报告了以下错误:

如何创建用于DolphinDB中SQL WHERE子句的UDF?

发生这种情况的原因是什么?在创建这样的子句时,我需要注意什么吗?

英文:

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:

如何创建用于DolphinDB中SQL WHERE子句的UDF?

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)使用iifwhere 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&lt;1.0/pow(10,4) &amp;&amp; openDiff&gt;-1.0/pow(10,4)),true,false) == true

(4) use eqFloat(precision=4)

huangapple
  • 本文由 发表于 2023年6月6日 15:55:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76412507.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定