问题描述
查询SNowFLAKE_SAMPLE_DATA
数据库中的一个表时出现了UTF-8解码错误,这是SNowflake在注册试用帐户时提供的。
import sNowflake.connector
c = sNowflake.connector.connect(...,database='SNowFLAKE_SAMPLE_DATA')
r = c.cursor().execute("""
SELECT tpcds_sf100tcl.customer.c_birth_country
FROM tpcds_sf100tcl.customer WHERE c_birth_country LIKE 'R%'
LIMIT 25 OFFSET 50
""")
list(r)
例外:
arrow_result.pyx in sNowflake.connector.arrow_result.ArrowResult.__next__()
arrow_iterator.pyx in sNowflake.connector.arrow_iterator.PyArrowIterator.__next__()
~/[...]/lib/python3.7/site-packages/sNowflake/connector/errors.py in errorhandler_wrapper(connection,cursor,error_class,error_value)
121 if cursor is not None:
122 cursor.messages.append((error_class,error_value))
--> 123 cursor.errorhandler(connection,error_value)
124 return
125 elif connection is not None:
~/[...]/lib/python3.7/site-packages/sNowflake/connector/errors.py in default_errorhandler(connection,error_value)
86 sqlstate=error_value.get('sqlstate'),87 sfqid=error_value.get('sfqid'),---> 88 done_format_msg=error_value.get('done_format_msg'))
89
90 @staticmethod
InterfaceError: 252005: Failed to convert current row,cause: 'utf-8' codec can't decode byte 0xc9 in position 1: invalid continuation byte
仅当结果包含带有重音字符的值时,这种情况才会发生。
据我所知,SNowflake仅支持UTF-8,所以我觉得很奇怪。
解决方法
我们在下游应用程序中遇到了类似的问题。由于非UTF8字符,他们无法加载数据。我们已经在雪花中创建了一个JavaScript UDF,以用用户提供的替换字符替换字符串中的Non-UTF8字符,并在我们的Load脚本中使用此UDF函数。这样,我们就可以摆脱雪花表中的所有Non-UTF8字符,而且下游应用程序也可以毫无问题地加载此数据。
您可以通过使用以下sql语句排除Non-UTF8记录来避免此问题:
SELECT tpcds_sf100tcl.customer.c_birth_country
FROM tpcds_sf100tcl.customer WHERE c_birth_country LIKE 'R%'
AND TRY_HEX_DECODE_STRING(HEX_ENCODE(c_birth_country)) IS NOT NULL
LIMIT 25 OFFSET 50;