z.rubi
z.rubi

Reputation: 327

BigQuery User Defined Function Error - Unable to Call UDF In Query

I am trying to use a user defined function that I have created in my BQ dataset.

Here is the UDF:

CREATE OR REPLACE FUNCTION `project.dataset.CountHolidays`(START_DATE DATETIME, 
SLA INT64, OPERATION STRING) AS (
(
        SELECT
            SUM(CASE WHEN OPERATION = 'OPTION 1' THEN OPTION_1
                WHEN OPERATION = 'OPTION 2' THEN OPTION_2
                WHEN OPERATION = 'OPTION 3' THEN OPTION_3
                WHEN OPERATION = 'OPTION 4' THEN OPTION_4
                WHEN OPERATION = 'OPTION 5' THEN OPTION_5
                WHEN OPERATION = 'OPTION 6' THEN OPTION_6
            END)
    FROM
        `project.dataset.calendar` cal
    WHERE
        cal.CALENDAR_DATE BETWEEN START_DATE AND DATE_ADD(START_DATE, INTERVAL SLA DAY)
)

);

I am trying to call the UDF within a query like this:

SELECT 
ORDER_NUMBER,
 `project.dataset.CountHolidays`(DATE(PICK_RELEASE_DATE), EBS_SERVICE_DAYS, WAREHOUSE_ID) as CountHolidays
FROM `different_project.different_dataset.view_name`

I get the following error and am unable to run the query:

LEFT OUTER JOIN cannot be used without a condition that is an equality of fields from both sides of the join.

Does anybody know what the error is here? Been scratching my head for a few hours now. Thanks.

Upvotes: 1

Views: 835

Answers (1)

Mikhail Berlyant
Mikhail Berlyant

Reputation: 172994

Your select references UDF which gets "translated" into left join that is not supported - thus the error!

Try below version of your UDF - should work I think

CREATE OR REPLACE FUNCTION `project.dataset.CountHolidays`(START_DATE DATE, 
SLA INT64, OPERATION STRING) AS (
(
        SELECT
            SUM(IF(cal.CALENDAR_DATE BETWEEN START_DATE AND DATE_ADD(START_DATE, INTERVAL SLA DAY),
            CASE WHEN OPERATION = 'OPTION 1' THEN OPTION_1
                WHEN OPERATION = 'OPTION 2' THEN OPTION_2
                WHEN OPERATION = 'OPTION 3' THEN OPTION_3
                WHEN OPERATION = 'OPTION 4' THEN OPTION_4
                WHEN OPERATION = 'OPTION 5' THEN OPTION_5
                WHEN OPERATION = 'OPTION 6' THEN OPTION_6
            END, 0))
    FROM
        `project.dataset.calendar` cal
));

Upvotes: 1

Related Questions