简体   繁体   English

Redshift中的Python UDF函数总是返回NULL值

[英]Python UDF function in Redshift always return NULL value

I want to have a function in Redshift that removes accents from words.我想在 Redshift 中有一个功能可以从单词中删除重音符号。 I have found a question in SO( question ) with the code in Python for making it.我在 SO( question ) 中发现了一个问题,用 Python 中的代码来制作它。 I have tried a few solutions, one of them being:我尝试了一些解决方案,其中之一是:

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)])

Then I create the function in Redshift as follows:然后我在 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;

And I apply it to a column with:我将它应用到一列:

SELECT remove_accents(city) FROM info_geo

Getting just null values.只获取空值。 The column city is of varchar type.列城市是 varchar 类型。 Why am I getting null values and how could I solve it?为什么我得到空值,我该如何解决?

You don't need to create a Python function inside the UDF.您不需要在 UDF 中创建 Python 函数。 Either add a call of the function or write it as a scalar expression:添加函数调用或将其写为标量表达式:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM