|
| 1 | +/* |
| 2 | + * Copyright 2026-present Alibaba Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "paimon/common/predicate/like.h" |
| 18 | + |
| 19 | +namespace paimon { |
| 20 | + |
| 21 | +Result<bool> Like::TestString(const std::string& field, const std::string& pattern) const { |
| 22 | + if (pattern.empty()) { |
| 23 | + return Result<bool>(field.empty()); |
| 24 | + } |
| 25 | + std::vector<char> pat; |
| 26 | + std::vector<bool> is_wild; |
| 27 | + for (size_t i = 0; i < pattern.size(); ++i) { |
| 28 | + if (pattern[i] == '\\' && i + 1 < pattern.size()) { |
| 29 | + pat.push_back(pattern[i + 1]); |
| 30 | + is_wild.push_back(false); |
| 31 | + ++i; |
| 32 | + } else { |
| 33 | + char c = pattern[i]; |
| 34 | + pat.push_back(c); |
| 35 | + is_wild.push_back(c == '_' || c == '%'); |
| 36 | + } |
| 37 | + } |
| 38 | + std::vector<char> simp_pat; |
| 39 | + std::vector<bool> simp_wild; |
| 40 | + for (size_t i = 0; i < pat.size(); ++i) { |
| 41 | + if (is_wild[i] && pat[i] == '%' && !simp_pat.empty() && simp_wild.back() && |
| 42 | + simp_pat.back() == '%') { |
| 43 | + continue; |
| 44 | + } |
| 45 | + simp_pat.push_back(pat[i]); |
| 46 | + simp_wild.push_back(is_wild[i]); |
| 47 | + } |
| 48 | + const size_t m = field.size(); |
| 49 | + const size_t n = simp_pat.size(); |
| 50 | + if (field.empty()) { |
| 51 | + return Result<bool>(n == 1 && simp_wild[0] && simp_pat[0] == '%'); |
| 52 | + } |
| 53 | + size_t min_len = 0; |
| 54 | + for (size_t i = 0; i < n; ++i) { |
| 55 | + if (!simp_wild[i]) { |
| 56 | + min_len++; |
| 57 | + } |
| 58 | + } |
| 59 | + if (min_len > m) { |
| 60 | + return Result<bool>(false); |
| 61 | + } |
| 62 | + constexpr size_t STACK_LIMIT = 128; |
| 63 | + std::unique_ptr<bool[]> dp_storage; |
| 64 | + bool* dp; |
| 65 | + if (n <= STACK_LIMIT) { |
| 66 | + dp = static_cast<bool*>(alloca((n + 1) * sizeof(bool))); |
| 67 | + } else { |
| 68 | + dp_storage = std::make_unique<bool[]>(n + 1); |
| 69 | + dp = dp_storage.get(); |
| 70 | + } |
| 71 | + std::fill_n(dp, n + 1, false); |
| 72 | + dp[0] = true; |
| 73 | + for (size_t j = 1; j <= n && simp_wild[j - 1] && simp_pat[j - 1] == '%'; ++j) { |
| 74 | + dp[j] = true; |
| 75 | + } |
| 76 | + const char* f = field.data(); |
| 77 | + for (size_t i = 0; i < m; ++i) { |
| 78 | + const char sc = f[i]; |
| 79 | + bool prev = dp[0]; |
| 80 | + dp[0] = false; |
| 81 | + bool has_match = false; |
| 82 | + for (size_t j = 1; j <= n; ++j) { |
| 83 | + const bool temp = dp[j]; |
| 84 | + const char pc = simp_pat[j - 1]; |
| 85 | + const bool wild = simp_wild[j - 1]; |
| 86 | + if (wild && pc == '%') { |
| 87 | + dp[j] = dp[j - 1] || dp[j]; |
| 88 | + } else if (wild && pc == '_') { |
| 89 | + dp[j] = prev; |
| 90 | + } else { |
| 91 | + dp[j] = (pc == sc) ? prev : false; |
| 92 | + } |
| 93 | + has_match |= dp[j]; |
| 94 | + prev = temp; |
| 95 | + } |
| 96 | + if (!has_match) { |
| 97 | + return Result<bool>(false); |
| 98 | + } |
| 99 | + } |
| 100 | + return Result<bool>(dp[n]); |
| 101 | +} |
| 102 | +} // namespace paimon |
0 commit comments