-
Notifications
You must be signed in to change notification settings - Fork 29.2k
[SPARK-47410][SQL] Refactor UTF8String and CollationFactory #45978
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
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e603189
Refactor StringPredicate expressions
uros-db 7e1aa64
scalastyle fix
uros-db c043cc0
Fix contains
uros-db e6b9d2a
Refine refactoring
uros-db e6c3d26
Refactor CollationSupport
uros-db 3c5174d
Add codegen import
uros-db bf01742
Refactor CollationSupport
uros-db 72a8219
Fix UTF8String
uros-db e1f8c99
Fix CollationSupport
uros-db ed5a577
Refine code & add comments
uros-db b11dcbb
Fix string expression tests
uros-db 35db4cd
Fix regexp expression tests
uros-db 8cffecc
Style fix
uros-db cdc53c5
Fix naming
uros-db File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
174 changes: 174 additions & 0 deletions
174
common/unsafe/src/main/java/org/apache/spark/sql/catalyst/util/CollationSupport.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.spark.sql.catalyst.util; | ||
|
|
||
| import com.ibm.icu.text.StringSearch; | ||
|
|
||
| import org.apache.spark.unsafe.types.UTF8String; | ||
|
|
||
| /** | ||
| * Static entry point for collation-aware expressions (StringExpressions, RegexpExpressions, and | ||
| * other expressions that require custom collation support), as well as private utility methods for | ||
| * collation-aware UTF8String operations needed to implement . | ||
| */ | ||
| public final class CollationSupport { | ||
|
uros-db marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Collation-aware string expressions. | ||
| */ | ||
|
|
||
| public static class Contains { | ||
| public static boolean exec(final UTF8String l, final UTF8String r, final int collationId) { | ||
| CollationFactory.Collation collation = CollationFactory.fetchCollation(collationId); | ||
| if (collation.supportsBinaryEquality) { | ||
| return execBinary(l, r); | ||
| } else if (collation.supportsLowercaseEquality) { | ||
| return execLowercase(l, r); | ||
| } else { | ||
| return execICU(l, r, collationId); | ||
| } | ||
| } | ||
| public static String genCode(final String l, final String r, final int collationId) { | ||
| CollationFactory.Collation collation = CollationFactory.fetchCollation(collationId); | ||
| String expr = "CollationSupport.Contains.exec"; | ||
| if (collation.supportsBinaryEquality) { | ||
| return String.format(expr + "Binary(%s, %s)", l, r); | ||
| } else if (collation.supportsLowercaseEquality) { | ||
| return String.format(expr + "Lowercase(%s, %s)", l, r); | ||
| } else { | ||
| return String.format(expr + "ICU(%s, %s, %d)", l, r, collationId); | ||
| } | ||
| } | ||
| public static boolean execBinary(final UTF8String l, final UTF8String r) { | ||
| return l.contains(r); | ||
| } | ||
| public static boolean execLowercase(final UTF8String l, final UTF8String r) { | ||
| return l.toLowerCase().contains(r.toLowerCase()); | ||
|
uros-db marked this conversation as resolved.
|
||
| } | ||
| public static boolean execICU(final UTF8String l, final UTF8String r, | ||
| final int collationId) { | ||
| if (r.numBytes() == 0) return true; | ||
| if (l.numBytes() == 0) return false; | ||
| StringSearch stringSearch = CollationFactory.getStringSearch(l, r, collationId); | ||
| return stringSearch.first() != StringSearch.DONE; | ||
| } | ||
| } | ||
|
|
||
| public static class StartsWith { | ||
| public static boolean exec(final UTF8String l, final UTF8String r, | ||
| final int collationId) { | ||
| CollationFactory.Collation collation = CollationFactory.fetchCollation(collationId); | ||
| if (collation.supportsBinaryEquality) { | ||
| return execBinary(l, r); | ||
| } else if (collation.supportsLowercaseEquality) { | ||
| return execLowercase(l, r); | ||
| } else { | ||
| return execICU(l, r, collationId); | ||
| } | ||
| } | ||
| public static String genCode(final String l, final String r, final int collationId) { | ||
| CollationFactory.Collation collation = CollationFactory.fetchCollation(collationId); | ||
| String expr = "CollationSupport.StartsWith.exec"; | ||
| if (collation.supportsBinaryEquality) { | ||
| return String.format(expr + "Binary(%s, %s)", l, r); | ||
| } else if (collation.supportsLowercaseEquality) { | ||
| return String.format(expr + "Lowercase(%s, %s)", l, r); | ||
| } else { | ||
| return String.format(expr + "ICU(%s, %s, %d)", l, r, collationId); | ||
| } | ||
| } | ||
| public static boolean execBinary(final UTF8String l, final UTF8String r) { | ||
| return l.startsWith(r); | ||
| } | ||
| public static boolean execLowercase(final UTF8String l, final UTF8String r) { | ||
| return l.toLowerCase().startsWith(r.toLowerCase()); | ||
| } | ||
| public static boolean execICU(final UTF8String l, final UTF8String r, | ||
| final int collationId) { | ||
| return CollationAwareUTF8String.matchAt(l, r, 0, collationId); | ||
| } | ||
| } | ||
|
|
||
| public static class EndsWith { | ||
| public static boolean exec(final UTF8String l, final UTF8String r, final int collationId) { | ||
| CollationFactory.Collation collation = CollationFactory.fetchCollation(collationId); | ||
| if (collation.supportsBinaryEquality) { | ||
| return execBinary(l, r); | ||
| } else if (collation.supportsLowercaseEquality) { | ||
| return execLowercase(l, r); | ||
| } else { | ||
| return execICU(l, r, collationId); | ||
| } | ||
| } | ||
| public static String genCode(final String l, final String r, final int collationId) { | ||
| CollationFactory.Collation collation = CollationFactory.fetchCollation(collationId); | ||
| String expr = "CollationSupport.EndsWith.exec"; | ||
| if (collation.supportsBinaryEquality) { | ||
| return String.format(expr + "Binary(%s, %s)", l, r); | ||
| } else if (collation.supportsLowercaseEquality) { | ||
| return String.format(expr + "Lowercase(%s, %s)", l, r); | ||
| } else { | ||
| return String.format(expr + "ICU(%s, %s, %d)", l, r, collationId); | ||
| } | ||
| } | ||
| public static boolean execBinary(final UTF8String l, final UTF8String r) { | ||
| return l.endsWith(r); | ||
| } | ||
| public static boolean execLowercase(final UTF8String l, final UTF8String r) { | ||
| return l.toLowerCase().endsWith(r.toLowerCase()); | ||
| } | ||
| public static boolean execICU(final UTF8String l, final UTF8String r, | ||
| final int collationId) { | ||
| return CollationAwareUTF8String.matchAt(l, r, l.numBytes() - r.numBytes(), collationId); | ||
| } | ||
| } | ||
|
|
||
| // TODO: Add more collation-aware string expressions. | ||
|
|
||
| /** | ||
| * Collation-aware regexp expressions. | ||
| */ | ||
|
|
||
| // TODO: Add more collation-aware regexp expressions. | ||
|
|
||
| /** | ||
| * Other collation-aware expressions. | ||
| */ | ||
|
|
||
| // TODO: Add other collation-aware expressions. | ||
|
|
||
| /** | ||
| * Utility class for collation-aware UTF8String operations. | ||
| */ | ||
|
|
||
| private static class CollationAwareUTF8String { | ||
|
|
||
| private static boolean matchAt(final UTF8String target, final UTF8String pattern, | ||
| final int pos, final int collationId) { | ||
| if (pattern.numChars() + pos > target.numChars() || pos < 0) { | ||
| return false; | ||
| } | ||
| if (pattern.numBytes() == 0 || target.numBytes() == 0) { | ||
| return pattern.numBytes() == 0; | ||
| } | ||
| return CollationFactory.getStringSearch(target.substring( | ||
| pos, pos + pattern.numChars()), pattern, collationId).last() == 0; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.