javascript 错误:Snowflake UDF中未知的用户定义函数L2.“xx_proc_getDqtReport”

xzlaal3s  于 2023-08-02  发布在  Java
关注(0)|答案(2)|浏览(77)

你好,我正在使用下面的代码在Snowflake中创建一个函数(UDF):

create or replace function l2."xx_proc_getDqtReport"(schemaname text,tablename text)
returns table(tablename text,colname text,fillrate float,distvalues variant,dqtupdatetime timestamp)
language javascript
as
$$
    var sql1 = `create or replace table app.dqt_report_${tablename} (tablename text,colname text,fillrate float,distvalues variant,dqtupdatetime timestamp)`;
    var st1 = snowflake.createStatement({sqlText:sql1});
    var rs1 = st1.execute();

    var colquery = `select distinct column_name
                    from information_schema.columns
                    where table_schema ilike '${schemaname}' -- put your schema name here
                        and table_name ilike '${tablename}'  -- put your table name here
                    order by column_name`;

    var colst = snowflake.createStatement({sqlText:colquery});
    var colrs = colst.execute();

    var useschemasql = `use schema ${schemaname}`;
    var useschemast =  snowflake.createStatement({sqlText:useschemasql});
    var useschemars = useschemast.execute();

    while(colrs.next()){
        var colname = colrs.getColumnValue(1);
        var sql2 = `insert into app.dqt_report_${tablename}
                with t1 as (
                    select count(*) from ${tablename}
                ),
                t2 as (
                    select count(*) as fillcount from ${tablename} where nullif(${colname},'') is not null
                )
                select ${tablename}, ${colname}, (fillcount/rowcount)*100 as fillrate,listagg(distinct ${colname},',') as distvalues,current_timestamp() as dqtupdatetime
                from ${tablename},t1,t2`;

        var st2 = snowflake.createStatement({sqlText:sql2});
        var rs2 = st2.execute();
    }

    var fetchreportsql = `select * from app.dqt_report_${tablename}`;
    var fetchreportst =  snowflake.createStatement({sqlText:useschemasql});
    var fetchreportrs = useschemast.execute();

    return table(fetchreportrs);
$$;

字符串
当我尝试使用下面的查询来使用函数时:select l2."xx_proc_getDqtReport"('l1','dtidr_elg_raw');,它显示了这样的错误:Unknown user-defined function L2."xx_proc_getDqtReport"
1.我用了双引号来避免这个案例的问题
1.函数代码的输出:Function xx_proc_getDqtReport successfully created.
1.但使用show user functions;检查时,函数名称不可用

0ve6wy6x

0ve6wy6x1#

Snowflake UTF应该是from子句的一部分
例如。

select * from table(l2."xx_proc_getDqtReport"('l1','dtidr_elg_raw'));

字符串

rnmwe5a2

rnmwe5a22#

你必须创建JavaScript用户定义的表函数的FUNCTION语法,即每行操作符。但是有错误的主体语法,对于这些,您必须定义三个显式函数,在分区批处理的行之前,每行和最后一行之后调用。
代码体是一个JavaScript存储过程(其中也有许多错误)
所以一个简单UTF:

create or replace function simple_udtf(parameter1 text) returns table(coulmn_1 text)
language javascript
as
$$
   var simple_sql = `select ${parameter1} || '_extra_text' as column_1`;
    var simple_sql_statement =  snowflake.createStatement({sqlText:simple_sql});
    var simple_sql_result = simple_sql_statement.execute();

    return table(simple_sql_result);
$$;

字符串
让我们像UDF一样调用它:

select simple_udtf('simple');


的数据
现在像UUTF一样调用它,它会爆炸,因为它是错误的语法:

select * from table(simple_udtf('simple'));



如果我们将其重写为JavaScript Procedure,则无法从这些返回TABLE,但有返回结果集的示例是VARIANT数据(json)。



所以在某种程度上,你目前正在尝试做的事情,不能以你正在尝试的方式完成。

相关问题