代码之家  ›  专栏  ›  技术社区  ›  Nathan T Alexander

雪花UDTF(用户定义表函数)SQL编译错误:无法计算不支持的子查询类型

  •  0
  • Nathan T Alexander  · 技术社区  · 5 年前

    似乎我应该能够将用户定义的表函数连接到select语句。不幸的是,我犯了一个错误。在回顾其他类似的问题时,有人说这是雪花中的一个错误已经修复,但我不知道这个错误修复是否涵盖了这个确切的案例。

    请注意,对于我的应用程序——假设我可以克服这个错误,我需要向函数中添加大约10个输入参数和10多个输出值,因此单输出标量函数解决方案将不起作用。

    create function tmp_fn(
    dd float
    ,ostrat float
    )
    returns table (
    urounded_soq float
    ) as
    '
    select 
    cast(dd * ostrat / cast(7 as float) as float) as unrounded_soq
    '
    
    create or replace temporary table test_fn as
    select 6 dd, 9 ostrat
    union all
    select 12 dd,38 ostrat
    ;
    
    --this works, but of course because the argument values are hardcoded, this is useless to my application.
    select 
        t.*
        ,f.*
    from test_fn t
        join table(tmp_fn(cast(6 as float),cast(9 as float))) f
    ;
    
    --this does not work, this is the error "SQL compilation error: Unsupported subquery type cannot be evaluated"
    select 
        t.*
        ,f.*
    from test_fn t
        join table(tmp_fn(cast(t.dd as float),cast(t.ostrat as float))) f
    ;
    

    注意到根据:

    select current_version();
    

    我的雪花版本是4.19.2

    0 回复  |  直到 5 年前
        1
  •  0
  •   Greg Pavlik    5 年前

    我还不知道为什么会出现不受支持的子查询类型错误,但如果将其转换为Javascript UDTF,则效果良好:

    create or replace function tmp_fn(
    DD float
    ,OSTRAT float
    )
    returns table (UROUNDED_SOQ float)
    language javascript
    as
    $$
    {
        processRow: function (row, rowWriter, context){
            rowWriter.writeRow({UROUNDED_SOQ: (row.DD * row.OSTRAT) / 7});
        }
    }
    $$;
    
    create or replace temporary table test_fn as
    select 6 dd, 9 ostrat
    union all
    select 12 dd,38 ostrat
    ;
    
    --this works, but of course because the argument values are hardcoded, this is useless to my application.
    select 
        t.DD, t.OSTRAT
        ,f.UROUNDED_SOQ
    from test_fn t
        join table(tmp_fn(cast(6 as float),cast(9 as float))) f
    ;
    
    --this does not work, this is the error "SQL compilation error: Unsupported subquery type cannot be evaluated"
    select 
        t.DD, t.OSTRAT
        ,urounded_soq
    from test_fn t, lateral(table(tmp_fn(t.dd::float,t.ostrat::float)))
    ;
    
    推荐文章