我正在使用标准SQL和javascript UDF在bigquery中编写查询,我遇到错误“错误:语法错误:预期”<“但得到了”)“;无法在[解析CREATE [TEMP] FUNCTION语句1:47]”。下面是命令行。请有人帮忙。非常感谢。
CREATE TEMPORARY FUNCTION IRRCalc(CArray ARRAY)
RETURNS FLOAT64
LANGUAGE js AS """
function IRRCalc(CArray){
min = 0.0;
max = 1.0;
do {
guess = (min + max) / 2;
NPV = 0.0;
for (var j=0; j<CArray.length; j++){
NPV += CArray[j]/Math.pow((1+guess),j);
}
if (NPV > 0){
min = guess;
}
else {
max = guess;
}
} while (Math.abs(NPV) > 0.00000001);
return guess * 100;
}
""";
WITH Input AS
(SELECT [-100, 100, 100, 100, 100, 100] as CArray
UNION ALL
SELECT [-100, 100, 100, 100, 100] as CArray)
SELECT
CArray,
IRRCalc(CArray) as IRR
FROM Input as t;
答案 0 :(得分:1)
如错误陈述所述:Expected "<" but got ")"
我认为你只是缺少声明中的类型
CREATE TEMPORARY FUNCTION IRRCalc(CArray ARRAY<INT64>)
而不是
CREATE TEMPORARY FUNCTION IRRCalc(CArray ARRAY)
顺便说一句,看起来你需要对JS UDF进行一些清理 - 见下文
CREATE TEMPORARY FUNCTION IRRCalc(CArray ARRAY<INT64>)
RETURNS FLOAT64
LANGUAGE js AS """
min = 0.0;
max = 1.0;
do {
guess = (min + max) / 2;
NPV = 0.0;
for (var j=0; j<CArray.length; j++){
NPV += CArray[j]/Math.pow((1+guess),j);
}
if (NPV > 0){
min = guess;
}
else {
max = guess;
}
} while (Math.abs(NPV) > 0.00000001);
return guess * 100;
""";
WITH Input AS
(SELECT [-100, 100, 100, 100, 100, 100] as CArray
UNION ALL
SELECT [-100, 100, 100, 100, 100] as CArray)
SELECT
CArray,
IRRCalc(CArray) as IRR
FROM Input as t