You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "uros-db (via GitHub)" <gi...@apache.org> on 2024/03/05 07:47:58 UTC

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

uros-db opened a new pull request, #45382:
URL: https://github.com/apache/spark/pull/45382

   ### What changes were proposed in this pull request?
   Extend built-in string functions to support non-binary, non-lowercase collation for: contains.
   
   
   ### Why are the changes needed?
   Update collation support for built-in string functions in Spark.
   
   
   ### Does this PR introduce _any_ user-facing change?
   Yes, users should now be able to use COLLATE within arguments for built-in string function CONTAINS in Spark SQL queries, using non-binary collations such as UNICODE_CI.
   
   
   ### How was this patch tested?
   Unit tests for queries using "collate" (CollationSuite).
   
   
   ### Was this patch authored or co-authored using generative AI tooling?
   Yes.


-- 
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


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

Posted by "dbatomic (via GitHub)" <gi...@apache.org>.
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


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

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan closed pull request #45382: [SPARK-47248][SQL][COLLATION] Improved string function support: contains
URL: https://github.com/apache/spark/pull/45382


-- 
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


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

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on PR #45382:
URL: https://github.com/apache/spark/pull/45382#issuecomment-1983758947

   The GA jobs all passed: https://github.com/uros-db/spark/actions/runs/8186876833/job/22395549669
   
   merging to master, thanks!


-- 
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


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

Posted by "uros-db (via GitHub)" <gi...@apache.org>.
uros-db commented on code in PR #45382:
URL: https://github.com/apache/spark/pull/45382#discussion_r1515579768


##########
common/unsafe/src/main/java/org/apache/spark/sql/catalyst/util/CollationFactory.java:
##########
@@ -145,6 +149,18 @@ public Collation(
     }
   }
 
+  /**
+   * Auxiliary methods for collation aware string operations.
+   */
+
+  public static StringSearch getStringSearch(final UTF8String left, final UTF8String right,
+                                          final int collationId) {

Review Comment:
   definitely, that's in ([SPARK-47295](https://issues.apache.org/jira/browse/SPARK-47295)) and will serve as an onboarding task for a new eng (of course, I will help them out with this)



-- 
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


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

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #45382:
URL: https://github.com/apache/spark/pull/45382#discussion_r1512896346


##########
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);
+    stringSearch.reset();

Review Comment:
   We need to call `reset` on a fresh instance?



-- 
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


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

Posted by "uros-db (via GitHub)" <gi...@apache.org>.
uros-db commented on code in PR #45382:
URL: https://github.com/apache/spark/pull/45382#discussion_r1513909856


##########
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);
+    stringSearch.reset();

Review Comment:
   indeed, removed



-- 
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


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

Posted by "uros-db (via GitHub)" <gi...@apache.org>.
uros-db commented on code in PR #45382:
URL: https://github.com/apache/spark/pull/45382#discussion_r1514612341


##########
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:
   done!



-- 
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


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

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #45382:
URL: https://github.com/apache/spark/pull/45382#discussion_r1514639021


##########
common/unsafe/src/main/java/org/apache/spark/sql/catalyst/util/CollationFactory.java:
##########
@@ -145,6 +149,18 @@ public Collation(
     }
   }
 
+  /**
+   * Auxiliary methods for collation aware string operations.
+   */
+
+  public static StringSearch getStringSearch(final UTF8String left, final UTF8String right,
+                                          final int collationId) {

Review Comment:
   ```suggestion
     public static StringSearch getStringSearch(
         final UTF8String left,
         final UTF8String right,
         final int collationId) {
   ```



-- 
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


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

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #45382:
URL: https://github.com/apache/spark/pull/45382#discussion_r1514641048


##########
common/unsafe/src/main/java/org/apache/spark/sql/catalyst/util/CollationFactory.java:
##########
@@ -145,6 +149,18 @@ public Collation(
     }
   }
 
+  /**
+   * Auxiliary methods for collation aware string operations.
+   */
+
+  public static StringSearch getStringSearch(final UTF8String left, final UTF8String right,
+                                          final int collationId) {

Review Comment:
   are we going to use it for startsWith and others?



-- 
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