|
| 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 | + |
| 18 | +package org.apache.spark.sql.catalyst.expressions |
| 19 | + |
| 20 | +import org.apache.spark.SparkFunSuite |
| 21 | +import org.apache.spark.sql.catalyst.dsl.expressions._ |
| 22 | +import org.apache.spark.sql.types.StringType |
| 23 | + |
| 24 | +/** |
| 25 | + * Unit tests for regular expression (regexp) related SQL expressions. |
| 26 | + */ |
| 27 | +class RegexpExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper { |
| 28 | + |
| 29 | + test("LIKE literal Regular Expression") { |
| 30 | + checkEvaluation(Literal.create(null, StringType).like("a"), null) |
| 31 | + checkEvaluation(Literal.create("a", StringType).like(Literal.create(null, StringType)), null) |
| 32 | + checkEvaluation(Literal.create(null, StringType).like(Literal.create(null, StringType)), null) |
| 33 | + checkEvaluation( |
| 34 | + Literal.create("a", StringType).like(NonFoldableLiteral.create("a", StringType)), true) |
| 35 | + checkEvaluation( |
| 36 | + Literal.create("a", StringType).like(NonFoldableLiteral.create(null, StringType)), null) |
| 37 | + checkEvaluation( |
| 38 | + Literal.create(null, StringType).like(NonFoldableLiteral.create("a", StringType)), null) |
| 39 | + checkEvaluation( |
| 40 | + Literal.create(null, StringType).like(NonFoldableLiteral.create(null, StringType)), null) |
| 41 | + |
| 42 | + checkEvaluation("abdef" like "abdef", true) |
| 43 | + checkEvaluation("a_%b" like "a\\__b", true) |
| 44 | + checkEvaluation("addb" like "a_%b", true) |
| 45 | + checkEvaluation("addb" like "a\\__b", false) |
| 46 | + checkEvaluation("addb" like "a%\\%b", false) |
| 47 | + checkEvaluation("a_%b" like "a%\\%b", true) |
| 48 | + checkEvaluation("addb" like "a%", true) |
| 49 | + checkEvaluation("addb" like "**", false) |
| 50 | + checkEvaluation("abc" like "a%", true) |
| 51 | + checkEvaluation("abc" like "b%", false) |
| 52 | + checkEvaluation("abc" like "bc%", false) |
| 53 | + checkEvaluation("a\nb" like "a_b", true) |
| 54 | + checkEvaluation("ab" like "a%b", true) |
| 55 | + checkEvaluation("a\nb" like "a%b", true) |
| 56 | + } |
| 57 | + |
| 58 | + test("LIKE Non-literal Regular Expression") { |
| 59 | + val regEx = 'a.string.at(0) |
| 60 | + checkEvaluation("abcd" like regEx, null, create_row(null)) |
| 61 | + checkEvaluation("abdef" like regEx, true, create_row("abdef")) |
| 62 | + checkEvaluation("a_%b" like regEx, true, create_row("a\\__b")) |
| 63 | + checkEvaluation("addb" like regEx, true, create_row("a_%b")) |
| 64 | + checkEvaluation("addb" like regEx, false, create_row("a\\__b")) |
| 65 | + checkEvaluation("addb" like regEx, false, create_row("a%\\%b")) |
| 66 | + checkEvaluation("a_%b" like regEx, true, create_row("a%\\%b")) |
| 67 | + checkEvaluation("addb" like regEx, true, create_row("a%")) |
| 68 | + checkEvaluation("addb" like regEx, false, create_row("**")) |
| 69 | + checkEvaluation("abc" like regEx, true, create_row("a%")) |
| 70 | + checkEvaluation("abc" like regEx, false, create_row("b%")) |
| 71 | + checkEvaluation("abc" like regEx, false, create_row("bc%")) |
| 72 | + checkEvaluation("a\nb" like regEx, true, create_row("a_b")) |
| 73 | + checkEvaluation("ab" like regEx, true, create_row("a%b")) |
| 74 | + checkEvaluation("a\nb" like regEx, true, create_row("a%b")) |
| 75 | + |
| 76 | + checkEvaluation(Literal.create(null, StringType) like regEx, null, create_row("bc%")) |
| 77 | + } |
| 78 | + |
| 79 | + test("RLIKE literal Regular Expression") { |
| 80 | + checkEvaluation(Literal.create(null, StringType) rlike "abdef", null) |
| 81 | + checkEvaluation("abdef" rlike Literal.create(null, StringType), null) |
| 82 | + checkEvaluation(Literal.create(null, StringType) rlike Literal.create(null, StringType), null) |
| 83 | + checkEvaluation("abdef" rlike NonFoldableLiteral.create("abdef", StringType), true) |
| 84 | + checkEvaluation("abdef" rlike NonFoldableLiteral.create(null, StringType), null) |
| 85 | + checkEvaluation( |
| 86 | + Literal.create(null, StringType) rlike NonFoldableLiteral.create("abdef", StringType), null) |
| 87 | + checkEvaluation( |
| 88 | + Literal.create(null, StringType) rlike NonFoldableLiteral.create(null, StringType), null) |
| 89 | + |
| 90 | + checkEvaluation("abdef" rlike "abdef", true) |
| 91 | + checkEvaluation("abbbbc" rlike "a.*c", true) |
| 92 | + |
| 93 | + checkEvaluation("fofo" rlike "^fo", true) |
| 94 | + checkEvaluation("fo\no" rlike "^fo\no$", true) |
| 95 | + checkEvaluation("Bn" rlike "^Ba*n", true) |
| 96 | + checkEvaluation("afofo" rlike "fo", true) |
| 97 | + checkEvaluation("afofo" rlike "^fo", false) |
| 98 | + checkEvaluation("Baan" rlike "^Ba?n", false) |
| 99 | + checkEvaluation("axe" rlike "pi|apa", false) |
| 100 | + checkEvaluation("pip" rlike "^(pi)*$", false) |
| 101 | + |
| 102 | + checkEvaluation("abc" rlike "^ab", true) |
| 103 | + checkEvaluation("abc" rlike "^bc", false) |
| 104 | + checkEvaluation("abc" rlike "^ab", true) |
| 105 | + checkEvaluation("abc" rlike "^bc", false) |
| 106 | + |
| 107 | + intercept[java.util.regex.PatternSyntaxException] { |
| 108 | + evaluate("abbbbc" rlike "**") |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + test("RLIKE Non-literal Regular Expression") { |
| 113 | + val regEx = 'a.string.at(0) |
| 114 | + checkEvaluation("abdef" rlike regEx, true, create_row("abdef")) |
| 115 | + checkEvaluation("abbbbc" rlike regEx, true, create_row("a.*c")) |
| 116 | + checkEvaluation("fofo" rlike regEx, true, create_row("^fo")) |
| 117 | + checkEvaluation("fo\no" rlike regEx, true, create_row("^fo\no$")) |
| 118 | + checkEvaluation("Bn" rlike regEx, true, create_row("^Ba*n")) |
| 119 | + |
| 120 | + intercept[java.util.regex.PatternSyntaxException] { |
| 121 | + evaluate("abbbbc" rlike regEx, create_row("**")) |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | + test("RegexReplace") { |
| 127 | + val row1 = create_row("100-200", "(\\d+)", "num") |
| 128 | + val row2 = create_row("100-200", "(\\d+)", "###") |
| 129 | + val row3 = create_row("100-200", "(-)", "###") |
| 130 | + val row4 = create_row(null, "(\\d+)", "###") |
| 131 | + val row5 = create_row("100-200", null, "###") |
| 132 | + val row6 = create_row("100-200", "(-)", null) |
| 133 | + |
| 134 | + val s = 's.string.at(0) |
| 135 | + val p = 'p.string.at(1) |
| 136 | + val r = 'r.string.at(2) |
| 137 | + |
| 138 | + val expr = RegExpReplace(s, p, r) |
| 139 | + checkEvaluation(expr, "num-num", row1) |
| 140 | + checkEvaluation(expr, "###-###", row2) |
| 141 | + checkEvaluation(expr, "100###200", row3) |
| 142 | + checkEvaluation(expr, null, row4) |
| 143 | + checkEvaluation(expr, null, row5) |
| 144 | + checkEvaluation(expr, null, row6) |
| 145 | + |
| 146 | + val nonNullExpr = RegExpReplace(Literal("100-200"), Literal("(\\d+)"), Literal("num")) |
| 147 | + checkEvaluation(nonNullExpr, "num-num", row1) |
| 148 | + } |
| 149 | + |
| 150 | + test("RegexExtract") { |
| 151 | + val row1 = create_row("100-200", "(\\d+)-(\\d+)", 1) |
| 152 | + val row2 = create_row("100-200", "(\\d+)-(\\d+)", 2) |
| 153 | + val row3 = create_row("100-200", "(\\d+).*", 1) |
| 154 | + val row4 = create_row("100-200", "([a-z])", 1) |
| 155 | + val row5 = create_row(null, "([a-z])", 1) |
| 156 | + val row6 = create_row("100-200", null, 1) |
| 157 | + val row7 = create_row("100-200", "([a-z])", null) |
| 158 | + |
| 159 | + val s = 's.string.at(0) |
| 160 | + val p = 'p.string.at(1) |
| 161 | + val r = 'r.int.at(2) |
| 162 | + |
| 163 | + val expr = RegExpExtract(s, p, r) |
| 164 | + checkEvaluation(expr, "100", row1) |
| 165 | + checkEvaluation(expr, "200", row2) |
| 166 | + checkEvaluation(expr, "100", row3) |
| 167 | + checkEvaluation(expr, "", row4) // will not match anything, empty string get |
| 168 | + checkEvaluation(expr, null, row5) |
| 169 | + checkEvaluation(expr, null, row6) |
| 170 | + checkEvaluation(expr, null, row7) |
| 171 | + |
| 172 | + val expr1 = new RegExpExtract(s, p) |
| 173 | + checkEvaluation(expr1, "100", row1) |
| 174 | + |
| 175 | + val nonNullExpr = RegExpExtract(Literal("100-200"), Literal("(\\d+)-(\\d+)"), Literal(1)) |
| 176 | + checkEvaluation(nonNullExpr, "100", row1) |
| 177 | + } |
| 178 | + |
| 179 | + test("SPLIT") { |
| 180 | + val s1 = 'a.string.at(0) |
| 181 | + val s2 = 'b.string.at(1) |
| 182 | + val row1 = create_row("aa2bb3cc", "[1-9]+") |
| 183 | + val row2 = create_row(null, "[1-9]+") |
| 184 | + val row3 = create_row("aa2bb3cc", null) |
| 185 | + |
| 186 | + checkEvaluation( |
| 187 | + StringSplit(Literal("aa2bb3cc"), Literal("[1-9]+")), Seq("aa", "bb", "cc"), row1) |
| 188 | + checkEvaluation( |
| 189 | + StringSplit(s1, s2), Seq("aa", "bb", "cc"), row1) |
| 190 | + checkEvaluation(StringSplit(s1, s2), null, row2) |
| 191 | + checkEvaluation(StringSplit(s1, s2), null, row3) |
| 192 | + } |
| 193 | + |
| 194 | +} |
0 commit comments