dbatomic commented on code in PR #45382:
URL: https://github.com/apache/spark/pull/45382#discussion_r1512945586


##########
common/unsafe/src/main/java/org/apache/spark/unsafe/types/UTF8String.java:
##########
@@ -343,19 +346,33 @@ public boolean contains(final UTF8String substring) {
     return false;
   }
 
-  public boolean contains(final UTF8String substring, int collationId) throws 
SparkException {
+  public boolean contains(final UTF8String substring, int collationId) {
     if (CollationFactory.fetchCollation(collationId).isBinaryCollation) {
       return this.contains(substring);
     }
     if (collationId == CollationFactory.LOWERCASE_COLLATION_ID) {
       return this.toLowerCase().contains(substring.toLowerCase());
     }
-    // TODO: enable ICU collation support for "contains" (SPARK-47248)
-    Map<String, String> params = new HashMap<>();
-    params.put("functionName", "contains");
-    params.put("collationName", 
CollationFactory.fetchCollation(collationId).collationName);
-    throw new SparkException("UNSUPPORTED_COLLATION.FOR_FUNCTION",
-            SparkException.constructMessageParams(params), null);
+    return collatedStringSearch(substring, collationId);
+  }
+
+  private boolean collatedStringSearch(final UTF8String substring, int 
collationId) {
+    if (substring.numBytes == 0) {
+      return true;
+    } else if (this.numBytes == 0) {
+      return false;
+    }
+    String pattern = substring.toString();
+    CharacterIterator target = new StringCharacterIterator(this.toString());
+    Collator collator = CollationFactory.fetchCollation(collationId).collator;
+    StringSearch stringSearch = new StringSearch(pattern, target, 
(RuleBasedCollator) collator);

Review Comment:
   With that you can also add local unit tests to `CollationFactorySuite`.



##########
common/unsafe/src/main/java/org/apache/spark/unsafe/types/UTF8String.java:
##########
@@ -343,19 +346,33 @@ public boolean contains(final UTF8String substring) {
     return false;
   }
 
-  public boolean contains(final UTF8String substring, int collationId) throws 
SparkException {
+  public boolean contains(final UTF8String substring, int collationId) {
     if (CollationFactory.fetchCollation(collationId).isBinaryCollation) {
       return this.contains(substring);
     }
     if (collationId == CollationFactory.LOWERCASE_COLLATION_ID) {
       return this.toLowerCase().contains(substring.toLowerCase());
     }
-    // TODO: enable ICU collation support for "contains" (SPARK-47248)
-    Map<String, String> params = new HashMap<>();
-    params.put("functionName", "contains");
-    params.put("collationName", 
CollationFactory.fetchCollation(collationId).collationName);
-    throw new SparkException("UNSUPPORTED_COLLATION.FOR_FUNCTION",
-            SparkException.constructMessageParams(params), null);
+    return collatedStringSearch(substring, collationId);
+  }
+
+  private boolean collatedStringSearch(final UTF8String substring, int 
collationId) {
+    if (substring.numBytes == 0) {
+      return true;
+    } else if (this.numBytes == 0) {
+      return false;
+    }
+    String pattern = substring.toString();
+    CharacterIterator target = new StringCharacterIterator(this.toString());
+    Collator collator = CollationFactory.fetchCollation(collationId).collator;
+    StringSearch stringSearch = new StringSearch(pattern, target, 
(RuleBasedCollator) collator);

Review Comment:
   My preference would be to keep `StringSearch` as a field in 
`CollationFactory`. That way we will also avoid object creation on every 
function invocation. Plus I think that it is cleaner to keep collation specific 
objects local to collation factory.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to