DolphinDB: Fail to parse function definitions submitted by submitJob

12 views Asked by At

When running the following code directly with run_factor(), it executes successfully. However, when I use the submitJob function, an error occurs:

Syntax Error: [line #1] Cannot recognize the token factor0000.

The following is my script:

def factor0000(){
    return table(1..100 as id);
}
def factor0001(){
    return table(101..200 as id);
}
def run_factor(){
    factor="";
    for(i in 0..1){
        sql0 = "select * from factor0000()"
        sql1 = strReplace(sql0 , "factor0000" , "factor" + lpad(string(i) , 4 , "0"))
        print(sql1);
        res= parseExpr(sql1).eval();
        factor+=string(res);
    }
    return factor;
}
submitJob("test000" , "test000" , run_factor)
getRecentJobs()

How to pass the function name run_factor as a string and dynamically call it inside the submitted function?

1

There are 1 answers

0
jinwandalaohu On BEST ANSWER

You can use makeCall to call the function, generate SQL metacode, and use unionAll to combine query results, as shown in the following script:

def factor0000(){
    return table(1..100 as id);
}
def factor0001(){
    return table(101..200 as id);
}


funlist=dict(STRING,ANY)
f=exec name from defs() where name like "factor%"
for (i in f){
        funlist[i]=funcByName(i)
        }
 
def run_factor(dd){
        factor = table(1:0, [`id], [INT])
        for(i in 0..1){
                fname="factor"+lpad(string(i),4,"0")
                factor=unionAll(factor, sql(select=sqlCol("*"), from=makeCall(dd[fname])).eval())
        }
        return factor 
}
submitJob("test000" , "test000" , run_factor{funlist})