为什么JS sqrt()函数不起作用?
CREATE TEMPORARY FUNCTION sqt(x int64)
RETURNS int64
LANGUAGE js AS """
return sqrt(x);
""";
with table1 as(
select 25 as x union all
select 100 as x union all
select 625 as x
)
select x,sqt(x)square_root from table1
错误:ReferenceError:sqt未定义为sqt(INT64)第2行,第2-3列
答案 0 :(得分:3)
JavaScript函数是Math.sqrt
。试试这个:
CREATE TEMPORARY FUNCTION sqt(x int64)
RETURNS int64
LANGUAGE js AS """
return Math.sqrt(x);
""";
with table1 as(
select 25 as x union all
select 100 as x union all
select 625 as x
)
select x,sqt(x)square_root from table1
请注意,INT64
不是JavaScript UDF的官方支持类型(因为没有等效的JavaScript类型)。最好使用FLOAT64
。