|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.spark.sql.catalyst.util; |
| 18 | + |
| 19 | +import com.ibm.icu.text.StringSearch; |
| 20 | + |
| 21 | +import org.apache.spark.unsafe.types.UTF8String; |
| 22 | + |
| 23 | +/** |
| 24 | + * Static entry point for collation-aware expressions (StringExpressions, RegexpExpressions, and |
| 25 | + * other expressions that require custom collation support), as well as private utility methods for |
| 26 | + * collation-aware UTF8String operations needed to implement . |
| 27 | + */ |
| 28 | +public final class CollationSupport { |
| 29 | + |
| 30 | + /** |
| 31 | + * Collation-aware string expressions. |
| 32 | + */ |
| 33 | + |
| 34 | + public static class Contains { |
| 35 | + public static boolean exec(final UTF8String l, final UTF8String r, final int collationId) { |
| 36 | + CollationFactory.Collation collation = CollationFactory.fetchCollation(collationId); |
| 37 | + if (collation.supportsBinaryEquality) { |
| 38 | + return execBinary(l, r); |
| 39 | + } else if (collation.supportsLowercaseEquality) { |
| 40 | + return execLowercase(l, r); |
| 41 | + } else { |
| 42 | + return execICU(l, r, collationId); |
| 43 | + } |
| 44 | + } |
| 45 | + public static String genCode(final String l, final String r, final int collationId) { |
| 46 | + CollationFactory.Collation collation = CollationFactory.fetchCollation(collationId); |
| 47 | + String expr = "CollationSupport.Contains.exec"; |
| 48 | + if (collation.supportsBinaryEquality) { |
| 49 | + return String.format(expr + "Binary(%s, %s)", l, r); |
| 50 | + } else if (collation.supportsLowercaseEquality) { |
| 51 | + return String.format(expr + "Lowercase(%s, %s)", l, r); |
| 52 | + } else { |
| 53 | + return String.format(expr + "ICU(%s, %s, %d)", l, r, collationId); |
| 54 | + } |
| 55 | + } |
| 56 | + public static boolean execBinary(final UTF8String l, final UTF8String r) { |
| 57 | + return l.contains(r); |
| 58 | + } |
| 59 | + public static boolean execLowercase(final UTF8String l, final UTF8String r) { |
| 60 | + return l.toLowerCase().contains(r.toLowerCase()); |
| 61 | + } |
| 62 | + public static boolean execICU(final UTF8String l, final UTF8String r, |
| 63 | + final int collationId) { |
| 64 | + if (r.numBytes() == 0) return true; |
| 65 | + if (l.numBytes() == 0) return false; |
| 66 | + StringSearch stringSearch = CollationFactory.getStringSearch(l, r, collationId); |
| 67 | + return stringSearch.first() != StringSearch.DONE; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public static class StartsWith { |
| 72 | + public static boolean exec(final UTF8String l, final UTF8String r, |
| 73 | + final int collationId) { |
| 74 | + CollationFactory.Collation collation = CollationFactory.fetchCollation(collationId); |
| 75 | + if (collation.supportsBinaryEquality) { |
| 76 | + return execBinary(l, r); |
| 77 | + } else if (collation.supportsLowercaseEquality) { |
| 78 | + return execLowercase(l, r); |
| 79 | + } else { |
| 80 | + return execICU(l, r, collationId); |
| 81 | + } |
| 82 | + } |
| 83 | + public static String genCode(final String l, final String r, final int collationId) { |
| 84 | + CollationFactory.Collation collation = CollationFactory.fetchCollation(collationId); |
| 85 | + String expr = "CollationSupport.StartsWith.exec"; |
| 86 | + if (collation.supportsBinaryEquality) { |
| 87 | + return String.format(expr + "Binary(%s, %s)", l, r); |
| 88 | + } else if (collation.supportsLowercaseEquality) { |
| 89 | + return String.format(expr + "Lowercase(%s, %s)", l, r); |
| 90 | + } else { |
| 91 | + return String.format(expr + "ICU(%s, %s, %d)", l, r, collationId); |
| 92 | + } |
| 93 | + } |
| 94 | + public static boolean execBinary(final UTF8String l, final UTF8String r) { |
| 95 | + return l.startsWith(r); |
| 96 | + } |
| 97 | + public static boolean execLowercase(final UTF8String l, final UTF8String r) { |
| 98 | + return l.toLowerCase().startsWith(r.toLowerCase()); |
| 99 | + } |
| 100 | + public static boolean execICU(final UTF8String l, final UTF8String r, |
| 101 | + final int collationId) { |
| 102 | + return CollationAwareUTF8String.matchAt(l, r, 0, collationId); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + public static class EndsWith { |
| 107 | + public static boolean exec(final UTF8String l, final UTF8String r, final int collationId) { |
| 108 | + CollationFactory.Collation collation = CollationFactory.fetchCollation(collationId); |
| 109 | + if (collation.supportsBinaryEquality) { |
| 110 | + return execBinary(l, r); |
| 111 | + } else if (collation.supportsLowercaseEquality) { |
| 112 | + return execLowercase(l, r); |
| 113 | + } else { |
| 114 | + return execICU(l, r, collationId); |
| 115 | + } |
| 116 | + } |
| 117 | + public static String genCode(final String l, final String r, final int collationId) { |
| 118 | + CollationFactory.Collation collation = CollationFactory.fetchCollation(collationId); |
| 119 | + String expr = "CollationSupport.EndsWith.exec"; |
| 120 | + if (collation.supportsBinaryEquality) { |
| 121 | + return String.format(expr + "Binary(%s, %s)", l, r); |
| 122 | + } else if (collation.supportsLowercaseEquality) { |
| 123 | + return String.format(expr + "Lowercase(%s, %s)", l, r); |
| 124 | + } else { |
| 125 | + return String.format(expr + "ICU(%s, %s, %d)", l, r, collationId); |
| 126 | + } |
| 127 | + } |
| 128 | + public static boolean execBinary(final UTF8String l, final UTF8String r) { |
| 129 | + return l.endsWith(r); |
| 130 | + } |
| 131 | + public static boolean execLowercase(final UTF8String l, final UTF8String r) { |
| 132 | + return l.toLowerCase().endsWith(r.toLowerCase()); |
| 133 | + } |
| 134 | + public static boolean execICU(final UTF8String l, final UTF8String r, |
| 135 | + final int collationId) { |
| 136 | + return CollationAwareUTF8String.matchAt(l, r, l.numBytes() - r.numBytes(), collationId); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + // TODO: Add more collation-aware string expressions. |
| 141 | + |
| 142 | + /** |
| 143 | + * Collation-aware regexp expressions. |
| 144 | + */ |
| 145 | + |
| 146 | + // TODO: Add more collation-aware regexp expressions. |
| 147 | + |
| 148 | + /** |
| 149 | + * Other collation-aware expressions. |
| 150 | + */ |
| 151 | + |
| 152 | + // TODO: Add other collation-aware expressions. |
| 153 | + |
| 154 | + /** |
| 155 | + * Utility class for collation-aware UTF8String operations. |
| 156 | + */ |
| 157 | + |
| 158 | + private static class CollationAwareUTF8String { |
| 159 | + |
| 160 | + private static boolean matchAt(final UTF8String target, final UTF8String pattern, |
| 161 | + final int pos, final int collationId) { |
| 162 | + if (pattern.numChars() + pos > target.numChars() || pos < 0) { |
| 163 | + return false; |
| 164 | + } |
| 165 | + if (pattern.numBytes() == 0 || target.numBytes() == 0) { |
| 166 | + return pattern.numBytes() == 0; |
| 167 | + } |
| 168 | + return CollationFactory.getStringSearch(target.substring( |
| 169 | + pos, pos + pattern.numChars()), pattern, collationId).last() == 0; |
| 170 | + } |
| 171 | + |
| 172 | + } |
| 173 | + |
| 174 | +} |
0 commit comments