Can't Bear with Encoding Any Longer

How much precious time has been wasted?!

Xianying Tan

2018/05/07

Tech / #complaint #note #tech #R #encoding

Translated with AI assistance, originally written in Chinese · 阅读中文原文

The RSQLServer Package Is Officially Retired

RSQLServer was officially archived on April 10, 2018. The author recommends using r-dbi/odbc as a replacement.

This package is archived as there is now an excellent, much better supported package odbc.

Switching to the odbc Package

As a developer who has been lurking on GitHub for years, I noticed the odbc package quite early on. Indeed, odbc is faster and lighter (no Java installation required), and is under active development. RSQLServer, on the other hand, had been somewhat lacking in maintenance. On top of that, it introduced some dplyr features that occasionally caused conflicts, so I had been itching to make the switch. Although I ran some comparisons and tests and didn’t find obvious issues, I was secretly worried about running into unexpected problems.

But since the switch was inevitable sooner or later, better sooner than later. I recall a classic statement from “The Pragmatic Programmer”:

Refactor Early, Refactor Often.

The Pitfall

Sure enough, it wasn’t long before I noticed some scripts producing odd results. After half a day of debugging, I finally found the problem: odbc does not pre-convert the SQL string encoding to match the database’s encoding, unlike RODBC or RJDBC. What made it especially frustrating was that I rarely use Chinese characters in SQL. The few Chinese characters that were there got converted to garbled text but didn’t throw an error – they just returned different results 😡.

That morning I filed a report on GitHub r-dbi/odbc#179. I thought forcibly converting the SQL to the database’s encoding should work (that’s what RODBC::odbcQuery() does), but given that I’m really not a database expert, I decided not to submit a PR and embarrass myself. I’ll leave it to the author Jim Hester to solve…

Wasted Time

In the world of computing, character encoding is truly a nightmare, especially on Windows (oh, and let’s not forget that other nightmare: timezones). Looking back on my years as a developer, I’ve wasted countless precious hours on these two issues. Here are some of the more memorable ones:

I imagine everyone has been tormented by these issues to some extent. But after all the struggling, how much of it was truly meaningful? The saddest part is that no matter how many pitfalls you’ve fallen into before, there are always more waiting ahead 😂.

(A little bragging about PRs I submitted to fix these issues)

The Future

According to these two articles by Tomas Kalibera, starting from Windows 10 (versions after November 2019), applications are allowed to use UTF-8 encoding to interact with the operating system. Therefore, after switching R and related packages’ build tools to UCRT, most encoding issues on Windows could potentially be resolved at the root. The articles don’t explicitly lay out R’s future roadmap, but at least we can see a glimmer of hope.