我正在将SQL(当前在Python中运行)迁移到Redshift存储过程中,出于性能原因,我希望该proc运行语言列表(例如EN,ES,DE)
看来Redshift存储的过程不支持数组/列表。你们中任何一个可爱的人都知道这样做的最佳方法吗?我已经通过拆分字符串并将其插入到临时表中来工作了,但这有点脏
这不起作用:
CREATE OR REPLACE PROCEDURE sp_del_me(batchid char(36), list_languages char(2)[])
AS $$
BEGIN
TRUNCATE TABLE control.del_me;
INSERT INTO control.del_me
SELECT sentimentbatchid
FROM control.sentiment_batch sb
WHERE batchuuid = batchid
AND ISOLanguageCode = ANY(list_languages);
END;
$$ LANGUAGE plpgsql;
我收到错误
无效的操作:pg_catalog.bpchar []不是函数或过程支持的参数类型;
这确实有效:
CREATE OR REPLACE PROCEDURE sp_del_me(batchid char(36), list_languages varchar(max))
AS $$
DECLARE
num_langs INTEGER;
BEGIN
CREATE TEMP TABLE IF NOT EXISTS tmp_languages(isolanguagecode char(2));
-- GET THE NUMBER OF LANGUAGES WHICH IS EN OR EN,ES OR EN,ES,DE ETC
-- SO WE ARE ASSUMING THERE ARE NO GAPS IN THE STRING AND ITS COMMA SEPARATED
-- I.E. LENGTH OF STRING = 2 + 3 * (1-#LANGUAGES) WHICH, WITH BASIC ALGEBRA EVALUATES TO...
num_langs := 1 + (len(list_languages) - 2)/3;
TRUNCATE TABLE tmp_languages;
FOR counter in 1..num_langs LOOP
INSERT INTO tmp_languages
SELECT split_part(list_languages, ',', counter);
END LOOP;
TRUNCATE TABLE control.del_me;
INSERT INTO control.del_me
SELECT sentimentbatchid
FROM control.sentiment_batch sb
INNER JOIN tmp_languages tl
ON sb.isolanguagecode = tl.isolanguagecode
WHERE batchuuid = batchid;
END;
$$ LANGUAGE plpgsql;