|
5 | 5 | import logging |
6 | 6 | import os |
7 | 7 | import re |
| 8 | +import string |
8 | 9 | import threading |
9 | 10 | import time |
10 | 11 | import uuid |
@@ -220,6 +221,43 @@ def wrap(arg): |
220 | 221 | return SQLStr(sql.SQL(query).format(*args, **kwargs).as_string(cr._cnx)) |
221 | 222 |
|
222 | 223 |
|
| 224 | +class _ExplodeFormatter(string.Formatter): |
| 225 | + """ |
| 226 | + Retro-compatible parallel filter formatter. |
| 227 | +
|
| 228 | + Any input that didn't fail before satisfies: |
| 229 | + 1. There is no replacement in the string other than `{parallel_filter}`. |
| 230 | + 2. Any literal brace was escaped --by doubling them. |
| 231 | +
|
| 232 | + For any input that didn't fail before this new implementation returns the same output |
| 233 | + as `str.format`. |
| 234 | +
|
| 235 | + The main change here, and the goal of this class, is to now make former invalid input |
| 236 | + valid. Thus this formatter will _only_ replace `{parallel_filter}` while keeping any |
| 237 | + other `{str}` or `{int}` elements. Double braces will still be formatted into |
| 238 | + single ones. |
| 239 | +
|
| 240 | + :meta private: exclude from online docs |
| 241 | + """ |
| 242 | + |
| 243 | + def parse(self, format_string): |
| 244 | + for literal_text, field_name, format_spec, conversion in super(_ExplodeFormatter, self).parse(format_string): |
| 245 | + if field_name is not None and field_name != "parallel_filter": |
| 246 | + yield literal_text + "{", None, None, None |
| 247 | + composed = ( |
| 248 | + field_name |
| 249 | + + (("!" + conversion) if conversion else "") |
| 250 | + + ((":" + format_spec) if format_spec else "") |
| 251 | + + "}" |
| 252 | + ) |
| 253 | + yield composed, None, None, None |
| 254 | + else: |
| 255 | + yield literal_text, field_name, format_spec, conversion |
| 256 | + |
| 257 | + |
| 258 | +_explode_format = _ExplodeFormatter().format |
| 259 | + |
| 260 | + |
223 | 261 | def explode_query(cr, query, alias=None, num_buckets=8, prefix=None): |
224 | 262 | """ |
225 | 263 | Explode a query to multiple queries that can be executed in parallel. |
@@ -256,7 +294,7 @@ def explode_query(cr, query, alias=None, num_buckets=8, prefix=None): |
256 | 294 | if num_buckets < 1: |
257 | 295 | raise ValueError("num_buckets should be greater than zero") |
258 | 296 | parallel_filter = "mod(abs({prefix}id), %s) = %s".format(prefix=prefix) |
259 | | - query = query.replace("%", "%%").format(parallel_filter=parallel_filter) |
| 297 | + query = _explode_format(query.replace("%", "%%"), parallel_filter=parallel_filter) |
260 | 298 | return [cr.mogrify(query, [num_buckets, index]).decode() for index in range(num_buckets)] |
261 | 299 |
|
262 | 300 |
|
@@ -294,7 +332,7 @@ def explode_query_range(cr, query, table, alias=None, bucket_size=DEFAULT_BUCKET |
294 | 332 | # Even if there are any records, return one query to be executed to validate its correctness and avoid |
295 | 333 | # scripts that pass the CI but fail in production. |
296 | 334 | parallel_filter = "{alias}.id IS NOT NULL".format(alias=alias) |
297 | | - return [query.format(parallel_filter=parallel_filter)] |
| 335 | + return [_explode_format(query, parallel_filter=parallel_filter)] |
298 | 336 | else: |
299 | 337 | return [] |
300 | 338 |
|
@@ -335,10 +373,11 @@ def explode_query_range(cr, query, table, alias=None, bucket_size=DEFAULT_BUCKET |
335 | 373 | # Still, since the query may only be valid if there is no split, we force the usage of `prefix` in the query to |
336 | 374 | # validate its correctness and avoid scripts that pass the CI but fail in production. |
337 | 375 | parallel_filter = "{alias}.id IS NOT NULL".format(alias=alias) |
338 | | - return [query.format(parallel_filter=parallel_filter)] |
| 376 | + return [_explode_format(query, parallel_filter=parallel_filter)] |
339 | 377 |
|
340 | 378 | parallel_filter = "{alias}.id BETWEEN %(lower-bound)s AND %(upper-bound)s".format(alias=alias) |
341 | | - query = query.replace("%", "%%").format(parallel_filter=parallel_filter) |
| 379 | + query = _explode_format(query.replace("%", "%%"), parallel_filter=parallel_filter) |
| 380 | + |
342 | 381 | return [ |
343 | 382 | cr.mogrify(query, {"lower-bound": ids[i], "upper-bound": ids[i + 1] - 1}).decode() for i in range(len(ids) - 1) |
344 | 383 | ] |
|
0 commit comments