You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "dbatomic (via GitHub)" <gi...@apache.org> on 2024/03/06 08:59:39 UTC

Re: [PR] [SPARK-47248][SQL][COLLATION] Improved string function support: contains [spark]

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-unsubscribe@spark.apache.org

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


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