我如何在bigquery中使用UDF中的平方根函数

时间:2018-03-20 17:35:10

标签: google-bigquery

为什么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列

1 个答案:

答案 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