【发布时间】:2023-01-11 17:51:36
【问题描述】:
给定一个字符串,我想创建一个函数来删除字符串中存在于查找表中的任何单词/词组。
例如,给定一个字符串s1 s2 s3 s4 s5 s6 和一个查找表
| word |
|---|
| s2 |
| s4 s5 |
预期结果:
select fn.remove_substring('s1 s2 s3 s4 s5 s6')
-- Expected output: 's1 s3 s6'
在 PostgreSQL 中,我实际上实现了一个工作函数,但是,我不确定如何在 BigQuery 中重写它,因为 BigQuery UDF 不允许游标和循环。
CREATE OR REPLACE FUNCTION fn.remove_substring(s text)
RETURNS text
LANGUAGE plpgsql
AS $function$
declare
replaced_string text := s;
t_cur cursor for
select word from public.lookup order by word desc;
begin
for row in t_cur loop
replaced_string := regexp_replace(replaced_string, '\y'||row.word||'\y', '', 'gi');
end loop;
return replaced_string;
end;
$function$
;
【问题讨论】:
标签: sql google-bigquery