|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -import re |
4 | | - |
5 | 3 | from alembic.autogenerate import comparators, renderers |
6 | 4 | from alembic.operations import Operations |
7 | 5 | from alembic.operations.ops import MigrateOperation |
@@ -210,12 +208,47 @@ def _normalize_bm25_expression(expr: str) -> str: |
210 | 208 | normalized = "".join(expr.split()) |
211 | 209 | normalized = normalized.replace('"', "") |
212 | 210 | normalized = normalized.replace("::text", "") |
213 | | - # Ignore schema/table qualification differences, but keep tokenizer namespaces like `pdb.simple`. |
214 | | - previous = None |
215 | | - while previous != normalized: |
216 | | - previous = normalized |
217 | | - normalized = re.sub(r"(?<![A-Za-z0-9_])(?!pdb\b)[A-Za-z_][A-Za-z0-9_]*\.", "", normalized) |
218 | | - return normalized |
| 211 | + return _strip_non_pdb_qualifiers(normalized) |
| 212 | + |
| 213 | + |
| 214 | +def _strip_non_pdb_qualifiers(expr: str) -> str: |
| 215 | + """Strip relation qualifiers outside SQL string literals. |
| 216 | +
|
| 217 | + Preserves tokenizer namespaces like ``pdb.simple`` and leaves quoted literal |
| 218 | + content untouched (for example regex patterns like ``'run.*'``). |
| 219 | + """ |
| 220 | + out: list[str] = [] |
| 221 | + i = 0 |
| 222 | + in_single = False |
| 223 | + while i < len(expr): |
| 224 | + ch = expr[i] |
| 225 | + |
| 226 | + if ch == "'": |
| 227 | + out.append(ch) |
| 228 | + # Escaped quote inside a string literal: ''. |
| 229 | + if in_single and i + 1 < len(expr) and expr[i + 1] == "'": |
| 230 | + out.append("'") |
| 231 | + i += 2 |
| 232 | + continue |
| 233 | + in_single = not in_single |
| 234 | + i += 1 |
| 235 | + continue |
| 236 | + |
| 237 | + if not in_single and (ch.isalpha() or ch == "_"): |
| 238 | + j = i + 1 |
| 239 | + while j < len(expr) and (expr[j].isalnum() or expr[j] == "_"): |
| 240 | + j += 1 |
| 241 | + |
| 242 | + if j < len(expr) and expr[j] == ".": |
| 243 | + token = expr[i:j] |
| 244 | + if token.lower() != "pdb": |
| 245 | + i = j + 1 |
| 246 | + continue |
| 247 | + |
| 248 | + out.append(ch) |
| 249 | + i += 1 |
| 250 | + |
| 251 | + return "".join(out) |
219 | 252 |
|
220 | 253 |
|
221 | 254 | def _normalized_expression_list(expressions: list[str]) -> list[str]: |
|
0 commit comments