so i have an mysql function that i need to convert to a procedure or function that is compatible with aws redshift.
This is the mysql function:
CREATE DEFINER=`software`@`%` FUNCTION `redeemed_qty`(ticketID bigint) RETURNS bigint
BEGIN
DECLARE redeemed_qty bigint;
SELECT
SUM(quantity) INTO redeemed_qty
FROM redemption
WHERE ticket_id = ticketID
AND (ISNULL(remark)
OR remark <> 'Cancelled');
IF redeemed_qty IS NULL THEN
SELECT IFNULL(CASE WHEN SUM(quantity_total) > quantity_total THEN quantity_total END,0) INTO redeemed_qty FROM ticket WHERE parent_id = ticketID AND status = 2;
END IF;
RETURN redeemed_qty;
END
When I tried transforming it into a redshift function:
CREATE FUNCTION fn_redeemedqty (ticketID bigint,remark character varying,quantity bigint)
RETURNS bigint
STABLE
AS $$
if (ticketID is not None) & (remark <> 'Cancelled') | (remark is None):
return sum(quantity)
return 0
$$ LANGUAGE plpythonu
;
But how do i insert the IF statement from mysql.
I also sometimes get an error:
global name 'plpy' is not defined.
I would appreciate your answers.