Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.4k views
in Technique[技术] by (71.8m points)

为什么在函数内用parseExpr执行会报SQL context is not initialized yet

在DolphinDB GUI内执行下面代码:

def yoy_continuous_validation(X, threshold, validation_way, continuous_len){
 if (not(string(validation_way) in ['>','>=','<','<=','='])){
  return NULL
 }
 m = parseExpr("iif(msum(iif(yoy(X)" + iif(string(validation_way) == "=","==",validation_way) + "threshold,1,0),continuous_len) == continuous_len,1,0)").eval()
 return m
}

报错SQL context is not initialized yet,parseExpr拼接的语句为

iif(msum(iif(yoy(X)>threshold,1,0),continuous_len) == continuous_len,1,0)

直接执行不会报错,是什么原因?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

如DolphinDB用户手册中所述,parseExpr函数解析变量时,首先搜索会话中的局部变量,再搜索共享变量,但不会搜索函数体内定义的局部变量。这里是因为parseExpr中用到了函数参数变量X的缘故。代码优化可如下所示:

def yoy_continuous_validation(X, threshold, validation_way, continuous_len){
 cmps = [gt, ge, lt, le, eq]
 index = ['>','>=','<','<=','='].find(string(validation_way))
 if(index < 0)  return NULL
 return  int(msum(cmps[index](yoy(X), threshold), continuous_len) == continuous_len)
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...