Python Redshift 中的 UDF 函数总是 return NULL 值

Python UDF function in Redshift always return NULL value

我想在 Redshift 中有一个功能,可以从单词中删除重音。我在 SO(question) 中发现了一个问题,其中的代码在 Python 中用于制作它。我尝试了一些解决方案,其中之一是:

import unicodedata
def remove_accents(accented_string):
    nfkd_form = unicodedata.normalize('NFKD', input_str)
    return u"".join([c for c in nfkd_form if not unicodedata.combining(c)])

然后我在Redshift中创建函数如下:

create function remove_accents(accented_string varchar)
returns varchar
immutable
as $$
import unicodedata
def remove_accents(accented_string):
    nfkd_form = unicodedata.normalize('NFKD', input_str)
    return u"".join([c for c in nfkd_form if not unicodedata.combining(c)])
$$ language plpythonu;

然后我将它应用到一个列:

SELECT remove_accents(city) FROM info_geo

只获取空值。列 city 是 varchar 类型。为什么我得到空值,我该如何解决?

您不需要在 UDF 中创建 Python 函数。添加函数调用或将其写为标量表达式:

create function remove_accents(accented_string varchar)
returns varchar
immutable
as $$
  import unicodedata
  nfkd_form = unicodedata.normalize('NFKD', accented_string)
  return u"".join([c for c in nfkd_form if not unicodedata.combining(c)])
$$ language plpythonu;