[ASTERIXDB-2877] Fix multi-byte character handling in CSV output#37
Open
ongdisheng wants to merge 2 commits intoapache:masterfrom
Open
[ASTERIXDB-2877] Fix multi-byte character handling in CSV output#37ongdisheng wants to merge 2 commits intoapache:masterfrom
ongdisheng wants to merge 2 commits intoapache:masterfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
There are two bugs in
writeUTF8StringAsCSVin PrintTools.java:Incorrect loop step in the quoting scan:
The loop that checks whether a string needs quoting advanced one byte at a time (
i++). Since multi-byte characters span 2 to 4 bytes, this might actually causecharAt()to be called at offsets pointing to the middle of a character.Incorrect character writing:
Characters were written using
PrintStream.print(char), which converts thecharthrough the platform default charset before writing. For multi-byte characters such aséand CJK characters (e.g.中), this re-encoding could produce incorrect bytes if the platform default charset is not UTF-8. Writing the raw UTF-8 bytes directly from the array is always correct regardless of platform.Fix
Added a fix for the quoting scan loop so that it now advances by
UTF8StringUtil.charSize()per iteration andcharAt()is always called at a valid character boundary. Characters are now written as raw UTF-8 bytes directly, which is also consistent with howwriteUTF8StringAsJSONalready handles the same data.How to Reproduce and Verify
Setup
Before fix
After fix
JIRA Issue
https://issues.apache.org/jira/browse/ASTERIXDB-2877