superset: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd4 in position 6: invalid continuation byte
Bug description
I encountered the following error when querying select * from table in SQL Lab. This issue occurred after upgrading from Superset version 2.1.3 to version 4.0.1.
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd4 in position 6: invalid continuation byte
How to reproduce the bug
Any table column with a TIMESTAMP datatype generates this error.
Screenshots/recordings
Superset version
4.0.1
Python version
3.11
Node version
I don’t know
Browser
Chrome
Additional context
No response
Checklist
- I have searched Superset docs and Slack and didn’t find a solution to my problem.
- I have searched the GitHub issue tracker and didn’t find a similar bug report.
- I have checked Superset’s logs for errors and if I found a relevant Python stacktrace, I included it here as text in the “additional context” section.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 19 (15 by maintainers)
I agree that databases and their drivers should handle UTF-8 conversion natively to avoid such errors. Superset should not attempt to convert binary-like data to text. For older systems, exposing views that force UTF-8 conversion upfront could prevent similar issues in modern tools like Superset.
@OleksandrDikov I think it will be published in
v4.1.xwhich is still in pre-release. We’re waiting for4.1.0rc2to test.Oh it appears 4.0.2 does not include the large json refactor that centralized all calls to
superset/utils/json.pyhere -> https://github.com/apache/superset/pull/28702This should make 4.1.x I believe, I don’t recommend brining in this large refactor as a cherry as it’ll merge-conflict heavily
To resolve the
UnicodeDecodeErrorwhen querying a table with aTIMESTAMPdatatype in SQL Lab after upgrading from Superset version 2.1.3 to version 4.0.1, ensure that the timestamp columns are properly normalized to a pandas-supported datetime format. This can be done using theutils.normalize_dttm_colfunction in theget_dfmethod of theBaseVizclass.Here is the relevant code snippet from the
get_dfmethod:Ensure that this normalization step is correctly applied to handle the timestamp columns and avoid
UnicodeDecodeError[1].Additionally, the Pinot database connector is known to cause issues with
TIMESTAMPcolumns in Superset. The Pinot driver infersTIMESTAMPcolumns asLONG, which can lead to problems such asUnicodeDecodeError. Here is a quick fix for the Pinot driver:This fix ensures that
TIMESTAMPcolumns are correctly interpreted and processed [2].To continue talking to Dosu, mention @dosu.
Fixed by
4.1.1This issue is related to Matomo (formerly Piwik). For troubleshooting, I set up a new instance of Matomo with a fresh database and encountered the same error.
The error particularly occurs with the matomo_log_visit table. When I run the query SELECT * from matomo_log_visit;
I receive the following error message: Failed to execute query ‘548’ - ‘SELECT * from matomo_log_visit;’: ‘utf-8’ codec can’t decode byte 0xd0 in position 0: invalid continuation byte
After some investigation, I found that the problem is related to binary fields. If I exclude these binary fields from the query, everything works fine.
I agree with you. I’m not exactly sure about the business logic here since I’m a DBA focused on database support for analytical tools. They encountered the error because of a
SELECT * FROM tablequery, and they might not need that column, or it could reference something within the application — I’m not sure.Overall, it’s good that we can skip this error now when using
SELECT *.But what’s in there? Some other language/character set? Guessing these bytes represents something intelligible (?)
Having worked with SQL Server a long time ago, I’m guessing this has to do with “collation” and MSFT SQL SERVER deep support for different character sets. From my understanding, all this is pretty much obsolete with the rise of the utf-8 / utf-16 standards.
Given that, Apache Superset probably shouldn’t go out of its way to support the intricacies of how different databases support different character sets, and just tell people to convert to
utf-x(either physically in your tables or using casting in views) in order to get Superset to deal with non ASCII characters.This is where the
[bytes]come from: https://github.com/apache/superset/blob/master/superset/utils/json.py#L102The chinese characters would show if/when your binary blob are decodable to utf-8 or utf-16.
What is in your binary blob? What do you expect to see?
Maybe you’re using some funky other encoding or “collation”. At this point if you’re using something else than utf-N in this day and age you may want to standardize, or wrap the column with some database function that brings things to a modern encoding.
@mistercrunch, I have some good news and bad news. The good news is that I think I have successfully pushed to the
masterbranch, and the query is running fine. However, the bad news is that the output is incorrectly formatted with Chinese characters.I’m not sure if this is a bug or if my push was incorrect and missed something.
https://github.com/apache/superset/pull/28266 was first cherry-picked in 4.0.2
Please try upgrading and confirm the issue is fixed