Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@
* Utility class for collation-aware UTF8String operations.
*/
public class CollationAwareUTF8String {

/**
* The constant value to indicate that the match is not found when searching for a pattern
* string in a target string.
*/
private static final int MATCH_NOT_FOUND = -1;

public static UTF8String replace(final UTF8String src, final UTF8String search,
final UTF8String replace, final int collationId) {
// This collation aware implementation is based on existing implementation on UTF8String
Expand Down Expand Up @@ -185,9 +192,8 @@ public static int findInSet(final UTF8String match, final UTF8String set, int co

public static int indexOf(final UTF8String target, final UTF8String pattern,
final int start, final int collationId) {
if (pattern.numBytes() == 0) {
return 0;
}
if (pattern.numBytes() == 0) return 0;
if (target.numBytes() == 0) return MATCH_NOT_FOUND;

StringSearch stringSearch = CollationFactory.getStringSearch(target, pattern, collationId);
stringSearch.setIndex(start);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,12 @@ public CollationIdentifier identifier() {
PROVIDER_ICU,
Collator.getInstance(ULocale.ROOT),
"153.120.0.0",
true,
false,
Copy link
Contributor Author

@uros-db uros-db May 28, 2024

Choose a reason for hiding this comment

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

note for reviewers: this is the place we say UNICODE does not supportBinaryEquality

false,
false
);

collationTable[2].collator.setStrength(Collator.TERTIARY);
collationTable[2].collator.setStrength(Collator.IDENTICAL);
collationTable[2].collator.freeze();

// UNICODE case-insensitive comparison (ROOT locale, in ICU + Secondary strength).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,8 @@ public void testStringInstr() throws SparkException {
assertStringInstr("", "xxxx", "UNICODE", 0);
assertStringInstr("test大千世界X大千世界", "界x", "UNICODE", 0);
assertStringInstr("test大千世界X大千世界", "界X", "UNICODE", 8);
assertStringInstr("xxxx", "", "UNICODE_CI", 1);
assertStringInstr("", "xxxx", "UNICODE_CI", 0);
assertStringInstr("aaads", "AD", "UNICODE_CI", 3);
assertStringInstr("aaads", "dS", "UNICODE_CI", 4);
assertStringInstr("test大千世界X大千世界", "界y", "UNICODE_CI", 0);
Expand Down Expand Up @@ -914,20 +916,6 @@ public void testStringTrim() throws SparkException {
assertStringTrim("UTF8_BINARY_LCASE", "xxasdxx", "x", "asd");
assertStringTrim("UTF8_BINARY_LCASE", "xa世ax", "x", "a世a");

assertStringTrimLeft("UNICODE", "asd", null, "asd");
assertStringTrimLeft("UNICODE", " asd ", null, "asd ");
assertStringTrimLeft("UNICODE", " a世a ", null, "a世a ");
assertStringTrimLeft("UNICODE", "asd", "x", "asd");
assertStringTrimLeft("UNICODE", "xxasdxx", "x", "asdxx");
assertStringTrimLeft("UNICODE", "xa世ax", "x", "a世ax");

assertStringTrimRight("UNICODE", "asd", null, "asd");
assertStringTrimRight("UNICODE", " asd ", null, " asd");
assertStringTrimRight("UNICODE", " a世a ", null, " a世a");
assertStringTrimRight("UNICODE", "asd", "x", "asd");
assertStringTrimRight("UNICODE", "xxasdxx", "x", "xxasd");
assertStringTrimRight("UNICODE", "xa世ax", "x", "xa世a");

// Test cases where trimString has more than one character
assertStringTrim("UTF8_BINARY", "ddsXXXaa", "asd", "XXX");
assertStringTrimLeft("UTF8_BINARY", "ddsXXXaa", "asd", "XXXaa");
Expand All @@ -937,22 +925,14 @@ public void testStringTrim() throws SparkException {
assertStringTrimLeft("UTF8_BINARY_LCASE", "ddsXXXaa", "asd", "XXXaa");
assertStringTrimRight("UTF8_BINARY_LCASE", "ddsXXXaa", "asd", "ddsXXX");

assertStringTrim("UNICODE", "ddsXXXaa", "asd", "XXX");
assertStringTrimLeft("UNICODE", "ddsXXXaa", "asd", "XXXaa");
assertStringTrimRight("UNICODE", "ddsXXXaa", "asd", "ddsXXX");

// Test cases specific to collation type
// uppercase trim, lowercase src
assertStringTrim("UTF8_BINARY", "asd", "A", "asd");
assertStringTrim("UTF8_BINARY_LCASE", "asd", "A", "sd");
assertStringTrim("UNICODE", "asd", "A", "asd");
assertStringTrim("UNICODE_CI", "asd", "A", "sd");

// lowercase trim, uppercase src
assertStringTrim("UTF8_BINARY", "ASD", "a", "ASD");
assertStringTrim("UTF8_BINARY_LCASE", "ASD", "a", "SD");
assertStringTrim("UNICODE", "ASD", "a", "ASD");
assertStringTrim("UNICODE_CI", "ASD", "a", "SD");

// uppercase and lowercase chars of different byte-length (utf8)
assertStringTrim("UTF8_BINARY", "ẞaaaẞ", "ß", "ẞaaaẞ");
Expand All @@ -963,10 +943,6 @@ public void testStringTrim() throws SparkException {
assertStringTrimLeft("UTF8_BINARY_LCASE", "ẞaaaẞ", "ß", "aaaẞ");
assertStringTrimRight("UTF8_BINARY_LCASE", "ẞaaaẞ", "ß", "ẞaaa");

assertStringTrim("UNICODE", "ẞaaaẞ", "ß", "ẞaaaẞ");
assertStringTrimLeft("UNICODE", "ẞaaaẞ", "ß", "ẞaaaẞ");
assertStringTrimRight("UNICODE", "ẞaaaẞ", "ß", "ẞaaaẞ");

assertStringTrim("UTF8_BINARY", "ßaaaß", "ẞ", "ßaaaß");
assertStringTrimLeft("UTF8_BINARY", "ßaaaß", "ẞ", "ßaaaß");
assertStringTrimRight("UTF8_BINARY", "ßaaaß", "ẞ", "ßaaaß");
Expand All @@ -975,10 +951,6 @@ public void testStringTrim() throws SparkException {
assertStringTrimLeft("UTF8_BINARY_LCASE", "ßaaaß", "ẞ", "aaaß");
assertStringTrimRight("UTF8_BINARY_LCASE", "ßaaaß", "ẞ", "ßaaa");

assertStringTrim("UNICODE", "ßaaaß", "ẞ", "ßaaaß");
assertStringTrimLeft("UNICODE", "ßaaaß", "ẞ", "ßaaaß");
assertStringTrimRight("UNICODE", "ßaaaß", "ẞ", "ßaaaß");

// different byte-length (utf8) chars trimmed
assertStringTrim("UTF8_BINARY", "Ëaaaẞ", "Ëẞ", "aaa");
assertStringTrimLeft("UTF8_BINARY", "Ëaaaẞ", "Ëẞ", "aaaẞ");
Expand All @@ -987,10 +959,6 @@ public void testStringTrim() throws SparkException {
assertStringTrim("UTF8_BINARY_LCASE", "Ëaaaẞ", "Ëẞ", "aaa");
assertStringTrimLeft("UTF8_BINARY_LCASE", "Ëaaaẞ", "Ëẞ", "aaaẞ");
assertStringTrimRight("UTF8_BINARY_LCASE", "Ëaaaẞ", "Ëẞ", "Ëaaa");

assertStringTrim("UNICODE", "Ëaaaẞ", "Ëẞ", "aaa");
assertStringTrimLeft("UNICODE", "Ëaaaẞ", "Ëẞ", "aaaẞ");
assertStringTrimRight("UNICODE", "Ëaaaẞ", "Ëẞ", "Ëaaa");
}

// TODO: Test more collation-aware string expressions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,23 @@ class CollationFactorySuite extends AnyFunSuite with Matchers { // scalastyle:ig
CollationTestCase("UTF8_BINARY", "aaa", "aaa", true),
CollationTestCase("UTF8_BINARY", "aaa", "AAA", false),
CollationTestCase("UTF8_BINARY", "aaa", "bbb", false),
CollationTestCase("UTF8_BINARY", "å", "a\u030A", false),
CollationTestCase("UTF8_BINARY_LCASE", "aaa", "aaa", true),
CollationTestCase("UTF8_BINARY_LCASE", "aaa", "AAA", true),
CollationTestCase("UTF8_BINARY_LCASE", "aaa", "AaA", true),
CollationTestCase("UTF8_BINARY_LCASE", "aaa", "AaA", true),
CollationTestCase("UTF8_BINARY_LCASE", "aaa", "aa", false),
CollationTestCase("UTF8_BINARY_LCASE", "aaa", "bbb", false),
CollationTestCase("UTF8_BINARY_LCASE", "å", "a\u030A", false),
CollationTestCase("UNICODE", "aaa", "aaa", true),
CollationTestCase("UNICODE", "aaa", "AAA", false),
CollationTestCase("UNICODE", "aaa", "bbb", false),
CollationTestCase("UNICODE", "å", "a\u030A", true),
CollationTestCase("UNICODE_CI", "aaa", "aaa", true),
CollationTestCase("UNICODE_CI", "aaa", "AAA", true),
CollationTestCase("UNICODE_CI", "aaa", "bbb", false))
CollationTestCase("UNICODE_CI", "aaa", "bbb", false),
CollationTestCase("UNICODE_CI", "å", "a\u030A", true)
)

checks.foreach(testCase => {
val collation = fetchCollation(testCase.collationName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -804,10 +804,12 @@ class CollationStringExpressionsSuite

test("StringTrim* functions - unit tests for both paths (codegen and eval)") {
// Without trimString param.
checkEvaluation(StringTrim(Literal.create( " asd ", StringType("UTF8_BINARY"))), "asd")
checkEvaluation(
StringTrim(Literal.create( " asd ", StringType("UTF8_BINARY"))), "asd")
checkEvaluation(
StringTrimLeft(Literal.create(" asd ", StringType("UTF8_BINARY_LCASE"))), "asd ")
checkEvaluation(StringTrimRight(Literal.create(" asd ", StringType("UNICODE"))), " asd")
checkEvaluation(StringTrimRight(
Literal.create(" asd ", StringType("UTF8_BINARY"))), " asd")

// With trimString param.
checkEvaluation(
Expand All @@ -822,8 +824,8 @@ class CollationStringExpressionsSuite
"asd ")
checkEvaluation(
StringTrimRight(
Literal.create(" asd ", StringType("UNICODE")),
Literal.create(" ", StringType("UNICODE"))),
Literal.create(" asd ", StringType("UTF8_BINARY")),
Literal.create(" ", StringType("UTF8_BINARY"))),
" asd")

checkEvaluation(
Expand All @@ -838,8 +840,8 @@ class CollationStringExpressionsSuite
"asdxx")
checkEvaluation(
StringTrimRight(
Literal.create("xxasdxx", StringType("UNICODE")),
Literal.create("x", StringType("UNICODE"))),
Literal.create("xxasdxx", StringType("UTF8_BINARY")),
Literal.create("x", StringType("UTF8_BINARY"))),
"xxasd")
}

Expand All @@ -863,10 +865,10 @@ class CollationStringExpressionsSuite
StringTrimTestCase("UTF8_BINARY_LCASE", "LTRIM", "xxasdxx", true, "x", "asdxx"),
StringTrimTestCase("UTF8_BINARY_LCASE", "RTRIM", " asd ", false, null, " asd"),

StringTrimTestCase("UNICODE", "TRIM", "xxasdxx", true, "x", "asd"),
StringTrimTestCase("UNICODE", "BTRIM", "xxasdxx", true, "x", "asd"),
StringTrimTestCase("UNICODE", "LTRIM", " asd ", false, null, "asd "),
StringTrimTestCase("UNICODE", "RTRIM", " asd ", true, null, null)
StringTrimTestCase("UTF8_BINARY", "TRIM", "xxasdxx", true, "x", "asd"),
StringTrimTestCase("UTF8_BINARY", "BTRIM", "xxasdxx", true, "x", "asd"),
StringTrimTestCase("UTF8_BINARY", "LTRIM", " asd ", false, null, "asd "),
StringTrimTestCase("UTF8_BINARY", "RTRIM", " asd ", true, null, null)

// Other more complex cases can be found in unit tests in CollationSupportSuite.java.
)
Expand Down Expand Up @@ -906,7 +908,7 @@ class CollationStringExpressionsSuite
+ "COLLATE('x', 'UTF8_BINARY_LCASE'))"),
expectedAnswer = Row("a"))
checkAnswer(
df = sql("SELECT LTRIM(COLLATE('x', 'UNICODE'), COLLATE('xax', 'UNICODE'))"),
df = sql("SELECT LTRIM(COLLATE('x', 'UTF8_BINARY'), COLLATE('xax', 'UTF8_BINARY'))"),
expectedAnswer = Row("ax"))

checkAnswer(
Expand All @@ -916,7 +918,7 @@ class CollationStringExpressionsSuite
df = sql("SELECT TRIM('x', COLLATE('xax', 'UTF8_BINARY_LCASE'))"),
expectedAnswer = Row("a"))
checkAnswer(
df = sql("SELECT BTRIM('xax', COLLATE('x', 'UNICODE'))"),
df = sql("SELECT BTRIM('xax', COLLATE('x', 'UTF8_BINARY'))"),
expectedAnswer = Row("a"))

checkAnswer(
Expand All @@ -926,21 +928,21 @@ class CollationStringExpressionsSuite
df = sql("SELECT RTRIM(COLLATE('x', 'UTF8_BINARY_LCASE'), 'xax')"),
expectedAnswer = Row("xa"))
checkAnswer(
df = sql("SELECT TRIM(COLLATE('x', 'UNICODE'), 'xax')"),
df = sql("SELECT TRIM(COLLATE('x', 'UTF8_BINARY'), 'xax')"),
expectedAnswer = Row("a"))
}

test("StringTrim* functions - collation type mismatch") {
List("TRIM", "LTRIM", "RTRIM").foreach(func => {
val collationMismatch = intercept[AnalysisException] {
sql("SELECT " + func + "(COLLATE('x', 'UTF8_BINARY_LCASE'), "
+ "COLLATE('xxaaaxx', 'UNICODE'))")
+ "COLLATE('xxaaaxx', 'UTF8_BINARY'))")
}
assert(collationMismatch.getErrorClass === "COLLATION_MISMATCH.EXPLICIT")
})

val collationMismatch = intercept[AnalysisException] {
sql("SELECT BTRIM(COLLATE('xxaaaxx', 'UNICODE'), COLLATE('x', 'UTF8_BINARY_LCASE'))")
sql("SELECT BTRIM(COLLATE('xxaaaxx', 'UTF8_BINARY'), COLLATE('x', 'UTF8_BINARY_LCASE'))")
}
assert(collationMismatch.getErrorClass === "COLLATION_MISMATCH.EXPLICIT")
}
Expand Down