Reputation: 63
I'm passing some values to UDF and checking to see if they are between a range, I pass that range as well. My two parameters: P5->1 , P10->2 . I'm applying this function on a column of integer. I'm getting an odd outcome from this snippet below:
(INPUT > P5 && INPUT <= P10)
INPUT: 117
P5: 1
P10:2
RETURNED: TRUE (ALL ABOVE VALUES WERE CONFIRMED BY OUTPUTTING THEM DIRECTLY)
So instead I attempted with the parameters:
(INPUT > 1 && INPUT <= 2)
INPUT: 117
RETURNED: FALSE
FULL CODE:
CREATE TEMP FUNCTION label2(INPUT INT64, P5 INT64, P10 INT64)
RETURNS STRING
LANGUAGE js
"""
if (INPUT > P5 && INPUT <= P10){
return 'B1'
}else{
return false
}
""";
SELECT A.COLUMN , LABEL2(A.COLUMN,1,2)
FROM ... AS A
AS
Upvotes: 0
Views: 903
Reputation: 172974
The problem you have is because INT64 is unsupported as an input type for JavaScript UDFs. So your input values are treated as STRINGs which leads to result you see
Instead, use FLOAT64 to represent integer values as a number
So, your UDF should look as below
CREATE TEMP FUNCTION label2(INPUT FLOAT64, P5 FLOAT64, P10 FLOAT64)
RETURNS STRING
LANGUAGE js AS
"""
if (INPUT > P5 && INPUT <= P10){
return 'B1'
} else {
return false
}
""";
Upvotes: 2