简体   繁体   中英

Conditional evaluation bigquery UDF

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

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
}
""";    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM