Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-48441][SQL] Fix StringTrim behaviour for non-UTF8_BINARY collations #46762

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

uros-db
Copy link
Contributor

@uros-db uros-db commented May 27, 2024

What changes were proposed in this pull request?

String searching in UTF8_BINARY_LCASE now works on character-level, rather than on byte-level. For example: ltrim("İ", "i") now returns "İ", because there exist no characters in "İ", starting from the left, such that lowercased version of those characters are equal to "i". Note, however, that there is a byte subsequence of "İ" such that lowercased version of that UTF-8 byte sequence equals to "i" (so the new behaviour is different than the old behaviour).

Why are the changes needed?

Fix functions that give unusable results due to one-to-many case mapping when performing string search under UTF8_BINARY_LCASE (see example above).

Does this PR introduce any user-facing change?

Yes, behaviour of trim* expressions is changed for edge cases with one-to-many case mapping.

How was this patch tested?

New unit tests in CollationSupportSuite and new e2e sql tests in CollationStringExpressionsSuite.

Was this patch authored or co-authored using generative AI tooling?

No.

@github-actions github-actions bot added the SQL label May 27, 2024
@uros-db uros-db changed the title [WIP][SQL] Fix StringTrim behaviour for non-UTF8_BINARY collations [SPARK-48441][SQL] Fix StringTrim behaviour for non-UTF8_BINARY collations May 28, 2024
@uros-db uros-db changed the title [SPARK-48441][SQL] Fix StringTrim behaviour for non-UTF8_BINARY collations [WIP][SPARK-48441][SQL] Fix StringTrim behaviour for non-UTF8_BINARY collations May 28, 2024
cloud-fan pushed a commit that referenced this pull request Jun 6, 2024
### What changes were proposed in this pull request?
CollationFactory has been updated to no longer mark UNICODE as collation that supportsBinaryCollation. To reflect these changes, various tests have been updated.

However, some tests have been (temporarily) removed because StringTrim no longer supports UNICODE collation given the new UNICODE definition in CollationFactory. At this time, StringTrim expression only supports UTF8_BINARY & UTF8_BINARY_LCASE, but not ICU collations. This work is in progress (#46762), so we'll ensure appropriate test coverage with those changes.

### Why are the changes needed?
UNICODE collation should not support binary collation. Note: in the future, we may want to consider a collation such as UNICODE_BINARY, which will support binary equality, but also maintain UNICODE ordering.

### Does this PR introduce _any_ user-facing change?
Yes, UNICODE is no longer treated as a binary collation. This affects how equality works for UNICODE, and also which codepath is taken for various collation-aware string expression given UNICODE collated string arguments.

### How was this patch tested?
Updated existing unit and e2e sql test for UNICODE collation.

### Was this patch authored or co-authored using generative AI tooling?
No.

Closes #46772 from uros-db/fix-unicode.

Authored-by: Uros Bojanic <157381213+uros-db@users.noreply.github.com>
Signed-off-by: Wenchen Fan <wenchen@databricks.com>
@uros-db uros-db changed the title [WIP][SPARK-48441][SQL] Fix StringTrim behaviour for non-UTF8_BINARY collations [SPARK-48441][SQL] Fix StringTrim behaviour for non-UTF8_BINARY collations Jun 10, 2024
cloud-fan pushed a commit that referenced this pull request Jun 11, 2024
### What changes were proposed in this pull request?
Renaming `UTF8_BINARY_LCASE` collation to `UTF8_LCASE`.

### Why are the changes needed?
As part of the collation effort in Spark, we've moved away from byte-by-byte logic towards character-by-character logic, so what we used to call `UTF8_BINARY_LCASE` is now more precisely `UTF8_LCASE`. For example, string searching in UTF8_LCASE now works on character-level (rather than on byte-level), which is reflected in this PRs: #46511, #46589, #46682, #46761, #46762. In addition, string comparison also works on character-level now, as per the changes introduced in this PR: #46700.

### Does this PR introduce _any_ user-facing change?
Yes, what was previously named `UTF8_BINARY_LCASE` collation, will from now on be named `UTF8_LCASE`.

### How was this patch tested?
Existing tests.

### Was this patch authored or co-authored using generative AI tooling?
No.

Closes #46924 from uros-db/rename-lcase.

Authored-by: Uros Bojanic <157381213+uros-db@users.noreply.github.com>
Signed-off-by: Wenchen Fan <wenchen@databricks.com>
return trimRight(trimLeft(srcString, trimString, collationId), trimString, collationId);
}

public static UTF8String lowercaseTrimLeft(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add some document about what the method does:

  1. Create a hash set where we store the lowercase versions of the characters in the trim string.
  2. Iterate over the characters of the input UTF8 string from left to right and stop when we find a character of the input string that does not belong the trim set.
  3. Return the appropriate substring of the input string.

++searchIndex;
}

return srcString.substring(searchIndex, srcString.numChars());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we optimize this to return srcString if searchIndex is 0?

Comment on lines +680 to +682
HashSet<Integer> trimChars = new HashSet<>();
Iterator<Integer> trimIter = trimString.codePointIterator();
while (trimIter.hasNext()) trimChars.add(UCharacter.toLowerCase(trimIter.next()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach is a bit problematic because UCharacter.toLowerCase(codepoint) returns a code point which means that İ maps to i. The second problem that we have is that UCharacter.toLowerCase will maps the Greek final sigma to itself, whereas we want it to be mapped to the Greek small sigma.

Here are two simple test cases and the expected results here, that can help verify the correctness of the implementation:

  • string: Iİa, trim string: İ, expected result: a
  • string ςσa, trim string: Σ, expected result: a.

These test cases can be natually extended to right trimming and bidirectional trimming of course.

Comment on lines +710 to +716
// Iterate over srcString from the left and find the first character that is not in trimChars.
String input = srcString.toString();
int i = 0;
while (i < input.length()) {
String key = CollationFactory.getCollationKey(String.valueOf(input.charAt(i)), collationId);
if (!trimChars.contains(key)) break;
++i;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with this approach is that it focuses on matching single code points. Instead I think we need to think of trimming as a prefix-match kind of problem. Essentially if you have input string str for which we have already trimmed k characters, and some trim string trimStr, we want for each UTF8 character in trimStr to check if it is a prefix of substring(str, k+1). If this is the case then we need to trim str, otherwise stop trimming. This is essential if we want to keep Unicode semantics, and specifically character normalization.

Here are two interesting examples/test cases:

  • string: a\u030Aβγδ, trim string: å, expected result: βγδ.
  • string: a\u030Aβγδ, trim string: , expected result: βγδ.

The second case brings up another interesting issue which having a match against multiple characters in the input string. I think we should follow a greedy approach here and use the match with the longest byte length in the input string.

Comment on lines +732 to +741
while (trimIter.hasNext()) trimChars.add(UCharacter.toLowerCase(trimIter.next()));

int searchIndex = srcString.numChars();
Iterator<Integer> srcIter = srcString.reverseCodePointIterator();
while (srcIter.hasNext()) {
if (!trimChars.contains(UCharacter.toLowerCase(srcIter.next()))) {
break;
}
--searchIndex;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obviously the comments for left trim are applicable to right trim as well.

}

public static UTF8String lowercaseTrimLeft(
public static UTF8String trimLeft(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please add documentation for this method as well?
Does this method need to be public? Same for the lowercase version of the algorithm.

/**
* Returns the code point starting from the byte at position `index`.
*/
public int codePointFrom(int index) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this (given other comments for the PR)?

};
}

public int getChar(int index) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question here.
Also, whatever remains, we need to add documentation for the methods we add.

public Iterator<Integer> codePointIterator() {
return new CodePointIterator();
}
private class CodePointIterator implements Iterator<Integer> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need an additional comment here that the iterator works correctly only for valid UTF8 strings (because of how it jumps from one character to the next).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add some interesting test cases along the lines of those posted in the comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants