-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbase.py
More file actions
632 lines (594 loc) · 20.2 KB
/
Copy pathbase.py
File metadata and controls
632 lines (594 loc) · 20.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
"""Base MySQL checkpoint saver with VARCHAR(32) hash for cross-db-sync compatibility."""
from __future__ import annotations
import asyncio
import json
import random
from collections.abc import Sequence
from typing import Any, cast
from langchain_core.runnables import RunnableConfig
from langgraph.checkpoint.base import (
WRITES_IDX_MAP,
BaseCheckpointSaver,
ChannelVersions,
Checkpoint,
CheckpointMetadata,
CheckpointTuple,
get_checkpoint_id,
get_serializable_checkpoint_metadata,
)
from langgraph.checkpoint.serde.base import SerializerProtocol
from langgraph.checkpoint.serde.types import TASKS
from src.checkpoint_mysql.serde import (
deserialize_channel_values,
deserialize_pending_sends,
deserialize_pending_writes,
)
"""
MIGRATIONS: Sequential schema migrations.
The position of the migration in the list is the version number.
"""
MIGRATIONS = [
"""CREATE TABLE IF NOT EXISTS checkpoint_migrations (
v INTEGER PRIMARY KEY
);""",
"""CREATE TABLE IF NOT EXISTS checkpoints (
thread_id VARCHAR(150) NOT NULL,
checkpoint_ns VARCHAR(2000) NOT NULL DEFAULT '',
checkpoint_ns_hash VARCHAR(32) NOT NULL DEFAULT '',
checkpoint_id VARCHAR(150) NOT NULL,
parent_checkpoint_id VARCHAR(150),
type VARCHAR(150),
checkpoint JSON NOT NULL,
metadata JSON NOT NULL DEFAULT ('{}'),
PRIMARY KEY (thread_id, checkpoint_ns_hash, checkpoint_id)
);""",
"""CREATE TABLE IF NOT EXISTS checkpoint_blobs (
thread_id VARCHAR(150) NOT NULL,
checkpoint_ns VARCHAR(2000) NOT NULL DEFAULT '',
checkpoint_ns_hash VARCHAR(32) NOT NULL DEFAULT '',
channel VARCHAR(150) NOT NULL,
version VARCHAR(150) NOT NULL,
type VARCHAR(150) NOT NULL,
`blob` LONGBLOB,
PRIMARY KEY (thread_id, checkpoint_ns_hash, channel, version)
);""",
"""CREATE TABLE IF NOT EXISTS checkpoint_writes (
thread_id VARCHAR(150) NOT NULL,
checkpoint_ns VARCHAR(2000) NOT NULL DEFAULT '',
checkpoint_ns_hash VARCHAR(32) NOT NULL DEFAULT '',
checkpoint_id VARCHAR(150) NOT NULL,
task_id VARCHAR(150) NOT NULL,
idx INTEGER NOT NULL,
channel VARCHAR(150) NOT NULL,
type VARCHAR(150),
`blob` LONGBLOB NOT NULL,
PRIMARY KEY (thread_id, checkpoint_ns_hash, checkpoint_id, task_id, idx)
);""",
"""
CREATE INDEX checkpoints_thread_id_idx ON checkpoints (thread_id);
""",
"""
CREATE INDEX checkpoint_blobs_thread_id_idx ON checkpoint_blobs (thread_id);
""",
"""
CREATE INDEX checkpoint_writes_thread_id_idx ON checkpoint_writes (thread_id);
""",
"""
CREATE INDEX checkpoints_checkpoint_id_idx ON checkpoints (checkpoint_id);
""",
"""
ALTER TABLE checkpoint_writes ADD COLUMN task_path VARCHAR(2000) NOT NULL DEFAULT '';
""",
]
SELECT_SQL = """
WITH channel_versions AS (
SELECT thread_id, checkpoint_ns_hash, checkpoint_id, channel,
json_unquote(
json_extract(checkpoint, concat('$.channel_versions.', '"', channel, '"'))
) AS version
FROM checkpoints, JSON_TABLE(
JSON_KEYS(checkpoint, '$.channel_versions'),
'$[*]' COLUMNS (channel VARCHAR(150) CHARACTER SET utf8mb4 PATH '$')
) AS channels
)
SELECT
thread_id,
checkpoint_ns,
checkpoint_ns_hash,
checkpoint_id,
parent_checkpoint_id,
checkpoint,
metadata,
(
SELECT JSON_ARRAYAGG(JSON_ARRAY(
bl.channel,
bl.type,
IFNULL(TO_BASE64(bl.`blob`), '')
))
FROM channel_versions
INNER JOIN checkpoint_blobs bl
ON bl.channel = channel_versions.channel
AND bl.version = channel_versions.version
WHERE bl.thread_id = checkpoints.thread_id
AND bl.checkpoint_ns_hash = checkpoints.checkpoint_ns_hash
AND channel_versions.thread_id = checkpoints.thread_id
AND channel_versions.checkpoint_ns_hash = checkpoints.checkpoint_ns_hash
AND channel_versions.checkpoint_id = checkpoints.checkpoint_id
) AS channel_values,
(
SELECT JSON_ARRAYAGG(JSON_ARRAY(
cw.task_id,
cw.channel,
cw.type,
TO_BASE64(cw.`blob`),
cw.idx
))
FROM checkpoint_writes cw
WHERE cw.thread_id = checkpoints.thread_id
AND cw.checkpoint_ns_hash = checkpoints.checkpoint_ns_hash
AND cw.checkpoint_id = checkpoints.checkpoint_id
) AS pending_writes,
(
SELECT JSON_ARRAYAGG(JSON_ARRAY(
cw.task_path,
cw.task_id,
cw.type,
TO_BASE64(cw.`blob`),
cw.idx
))
FROM checkpoint_writes cw
WHERE cw.thread_id = checkpoints.thread_id
AND cw.checkpoint_ns_hash = checkpoints.checkpoint_ns_hash
AND cw.checkpoint_id = checkpoints.checkpoint_id
AND cw.channel = '__pregel_tasks'
) AS pending_sends
FROM checkpoints {WHERE}
"""
SELECT_SQL_LIGHT = """
WITH channel_versions AS (
SELECT thread_id, checkpoint_ns_hash, checkpoint_id, channel,
json_unquote(
json_extract(checkpoint, concat('$.channel_versions.', '"', channel, '"'))
) AS version
FROM checkpoints, JSON_TABLE(
JSON_KEYS(checkpoint, '$.channel_versions'),
'$[*]' COLUMNS (channel VARCHAR(150) CHARACTER SET utf8mb4 PATH '$')
) AS channels
WHERE checkpoints.thread_id = %s
AND checkpoints.checkpoint_ns_hash = %s
)
SELECT
checkpoints.thread_id,
checkpoints.checkpoint_ns,
checkpoints.checkpoint_ns_hash,
checkpoints.checkpoint_id,
checkpoints.parent_checkpoint_id,
checkpoints.checkpoint,
checkpoints.metadata,
(
SELECT JSON_ARRAYAGG(JSON_ARRAY(
bl.channel,
bl.type,
IFNULL(TO_BASE64(bl.`blob`), '')
))
FROM channel_versions
INNER JOIN checkpoint_blobs bl
ON bl.channel = channel_versions.channel
AND bl.version = channel_versions.version
WHERE bl.thread_id = checkpoints.thread_id
AND bl.checkpoint_ns_hash = checkpoints.checkpoint_ns_hash
) AS channel_values,
(
SELECT JSON_ARRAYAGG(JSON_ARRAY(
cw.task_id,
cw.channel,
cw.type,
TO_BASE64(cw.`blob`),
cw.idx
))
FROM checkpoint_writes cw
WHERE cw.thread_id = checkpoints.thread_id
AND cw.checkpoint_ns_hash = checkpoints.checkpoint_ns_hash
AND cw.checkpoint_id = checkpoints.checkpoint_id
) AS pending_writes
FROM checkpoints
WHERE checkpoints.thread_id = %s
AND checkpoints.checkpoint_ns_hash = %s
"""
SELECT_SQL_NO_NS_HASH = """
SELECT
checkpoints.thread_id,
checkpoints.checkpoint_ns,
checkpoints.checkpoint_ns_hash,
checkpoints.checkpoint_id,
checkpoints.parent_checkpoint_id,
checkpoints.checkpoint,
checkpoints.metadata,
(
SELECT JSON_ARRAYAGG(JSON_ARRAY(
cw.task_id,
cw.channel,
cw.type,
TO_BASE64(cw.`blob`),
cw.idx
))
FROM checkpoint_writes cw
WHERE cw.thread_id = checkpoints.thread_id
AND cw.checkpoint_ns_hash = checkpoints.checkpoint_ns_hash
AND cw.checkpoint_id = checkpoints.checkpoint_id
) AS pending_writes
FROM checkpoints {WHERE}
"""
SELECT_SQL_NO_NS_HASH_LIGHT = """
SELECT
checkpoints.thread_id,
checkpoints.checkpoint_ns,
checkpoints.checkpoint_ns_hash,
checkpoints.checkpoint_id,
checkpoints.parent_checkpoint_id,
checkpoints.checkpoint,
checkpoints.metadata,
(
SELECT JSON_ARRAYAGG(JSON_ARRAY(
cw.task_id,
cw.channel,
cw.type,
TO_BASE64(cw.`blob`),
cw.idx
))
FROM checkpoint_writes cw
WHERE cw.thread_id = checkpoints.thread_id
AND cw.checkpoint_ns_hash = checkpoints.checkpoint_ns_hash
AND cw.checkpoint_id = checkpoints.checkpoint_id
) AS pending_writes
FROM checkpoints
WHERE checkpoints.thread_id = %s
AND checkpoints.checkpoint_ns_hash = %s
"""
SELECT_SQL_MARIADB = """
WITH channel_versions AS (
SELECT thread_id, checkpoint_ns_hash, checkpoint_id, channel,
json_unquote(
json_extract(checkpoint, concat('$.channel_versions.', '"', channel, '"'))
) AS version
FROM checkpoints, JSON_TABLE(
JSON_KEYS(checkpoint, '$.channel_versions'),
'$[*]' COLUMNS (channel VARCHAR(150) CHARACTER SET utf8mb4 PATH '$')
) AS channels
{WHERE}
)
SELECT
thread_id,
checkpoint_ns,
checkpoint_ns_hash,
checkpoint_id,
parent_checkpoint_id,
checkpoint,
metadata,
(
SELECT JSON_ARRAYAGG(JSON_ARRAY(
bl.channel,
bl.type,
IFNULL(bl.`blob`, '')
))
FROM channel_versions
INNER JOIN checkpoint_blobs bl
ON bl.channel = channel_versions.channel
AND bl.version = channel_versions.version
WHERE bl.thread_id = checkpoints.thread_id
AND bl.checkpoint_ns_hash = checkpoints.checkpoint_ns_hash
AND channel_versions.thread_id = checkpoints.thread_id
AND channel_versions.checkpoint_ns_hash = checkpoints.checkpoint_ns_hash
AND channel_versions.checkpoint_id = checkpoints.checkpoint_id
) AS channel_values,
(
SELECT JSON_ARRAYAGG(JSON_ARRAY(
cw.task_id,
cw.channel,
cw.type,
cw.`blob`,
cw.idx
))
FROM checkpoint_writes cw
WHERE cw.thread_id = checkpoints.thread_id
AND cw.checkpoint_ns_hash = checkpoints.checkpoint_ns_hash
AND cw.checkpoint_id = checkpoints.checkpoint_id
) AS pending_writes,
(
SELECT JSON_ARRAYAGG(JSON_ARRAY(
cw.task_path,
cw.task_id,
cw.type,
cw.`blob`,
cw.idx
))
FROM checkpoint_writes cw
WHERE cw.thread_id = checkpoints.thread_id
AND cw.checkpoint_ns_hash = checkpoints.checkpoint_ns_hash
AND cw.checkpoint_id = checkpoints.checkpoint_id
AND cw.channel = '__pregel_tasks'
) AS pending_sends
FROM checkpoints {WHERE}
"""
SELECT_SQL_LIGHT_MARIADB = """
WITH channel_versions AS (
SELECT thread_id, checkpoint_ns_hash, checkpoint_id, channel,
json_unquote(
json_extract(checkpoint, concat('$.channel_versions.', '"', channel, '"'))
) AS version
FROM checkpoints, JSON_TABLE(
JSON_KEYS(checkpoint, '$.channel_versions'),
'$[*]' COLUMNS (channel VARCHAR(150) CHARACTER SET utf8mb4 PATH '$')
) AS channels
WHERE checkpoints.thread_id = %s
AND checkpoints.checkpoint_ns_hash = %s
)
SELECT
checkpoints.thread_id,
checkpoints.checkpoint_ns,
checkpoints.checkpoint_ns_hash,
checkpoints.checkpoint_id,
checkpoints.parent_checkpoint_id,
checkpoints.checkpoint,
checkpoints.metadata,
(
SELECT JSON_ARRAYAGG(JSON_ARRAY(
bl.channel,
bl.type,
IFNULL(bl.`blob`, '')
))
FROM channel_versions
INNER JOIN checkpoint_blobs bl
ON bl.channel = channel_versions.channel
AND bl.version = channel_versions.version
WHERE bl.thread_id = checkpoints.thread_id
AND bl.checkpoint_ns_hash = checkpoints.checkpoint_ns_hash
) AS channel_values,
(
SELECT JSON_ARRAYAGG(JSON_ARRAY(
cw.task_id,
cw.channel,
cw.type,
cw.`blob`,
cw.idx
))
FROM checkpoint_writes cw
WHERE cw.thread_id = checkpoints.thread_id
AND cw.checkpoint_ns_hash = checkpoints.checkpoint_ns_hash
AND cw.checkpoint_id = checkpoints.checkpoint_id
) AS pending_writes
FROM checkpoints
WHERE checkpoints.thread_id = %s
AND checkpoints.checkpoint_ns_hash = %s
"""
SELECT_SQL_NO_NS_HASH_MARIADB = """
SELECT
checkpoints.thread_id,
checkpoints.checkpoint_ns,
checkpoints.checkpoint_ns_hash,
checkpoints.checkpoint_id,
checkpoints.parent_checkpoint_id,
checkpoints.checkpoint,
checkpoints.metadata,
(
SELECT JSON_ARRAYAGG(JSON_ARRAY(
cw.task_id,
cw.channel,
cw.type,
cw.`blob`,
cw.idx
))
FROM checkpoint_writes cw
WHERE cw.thread_id = checkpoints.thread_id
AND cw.checkpoint_ns_hash = checkpoints.checkpoint_ns_hash
AND cw.checkpoint_id = checkpoints.checkpoint_id
) AS pending_writes
FROM checkpoints {WHERE}
"""
SELECT_SQL_NO_NS_HASH_LIGHT_MARIADB = """
SELECT
checkpoints.thread_id,
checkpoints.checkpoint_ns,
checkpoints.checkpoint_ns_hash,
checkpoints.checkpoint_id,
checkpoints.parent_checkpoint_id,
checkpoints.checkpoint,
checkpoints.metadata,
(
SELECT JSON_ARRAYAGG(JSON_ARRAY(
cw.task_id,
cw.channel,
cw.type,
cw.`blob`,
cw.idx
))
FROM checkpoint_writes cw
WHERE cw.thread_id = checkpoints.thread_id
AND cw.checkpoint_ns_hash = checkpoints.checkpoint_ns_hash
AND cw.checkpoint_id = checkpoints.checkpoint_id
) AS pending_writes
FROM checkpoints
WHERE checkpoints.thread_id = %s
AND checkpoints.checkpoint_ns_hash = %s
"""
UPSERT_CHECKPOINT_BLOBS_SQL = """
INSERT INTO checkpoint_blobs (thread_id, checkpoint_ns, checkpoint_ns_hash, channel, version, type, `blob`)
VALUES (%s, %s, MD5(%s), %s, %s, %s, %s)
AS new
ON DUPLICATE KEY UPDATE
type = new.type,
`blob` = new.`blob`;
"""
UPSERT_CHECKPOINTS_SQL = """
INSERT INTO checkpoints (thread_id, checkpoint_ns, checkpoint_ns_hash, checkpoint_id, parent_checkpoint_id, checkpoint, metadata)
VALUES (%s, %s, MD5(%s), %s, %s, %s, %s)
AS new
ON DUPLICATE KEY UPDATE
checkpoint = new.checkpoint,
metadata = new.metadata;
"""
UPSERT_CHECKPOINT_WRITES_SQL = """
INSERT INTO checkpoint_writes (thread_id, checkpoint_ns, checkpoint_ns_hash, checkpoint_id, task_id, task_path, idx, channel, type, `blob`)
VALUES (%s, %s, MD5(%s), %s, %s, %s, %s, %s, %s, %s)
AS new
ON DUPLICATE KEY UPDATE
channel = new.channel,
type = new.type,
`blob` = new.`blob`;
"""
INSERT_CHECKPOINT_WRITES_SQL = """
INSERT IGNORE INTO checkpoint_writes (thread_id, checkpoint_ns, checkpoint_ns_hash, checkpoint_id, task_id, task_path, idx, channel, type, `blob`)
VALUES (%s, %s, MD5(%s), %s, %s, %s, %s, %s, %s, %s)
"""
class BaseMySQLSaver(BaseCheckpointSaver[str]):
MIGRATIONS = MIGRATIONS
UPSERT_CHECKPOINT_BLOBS_SQL = UPSERT_CHECKPOINT_BLOBS_SQL
UPSERT_CHECKPOINTS_SQL = UPSERT_CHECKPOINTS_SQL
UPSERT_CHECKPOINT_WRITES_SQL = UPSERT_CHECKPOINT_WRITES_SQL
INSERT_CHECKPOINT_WRITES_SQL = INSERT_CHECKPOINT_WRITES_SQL
SELECT_SQL: str = SELECT_SQL
SELECT_SQL_LIGHT: str = SELECT_SQL_LIGHT
SELECT_SQL_MARIADB: str = SELECT_SQL_MARIADB
SELECT_SQL_LIGHT_MARIADB: str = SELECT_SQL_LIGHT_MARIADB
SELECT_SQL_NO_NS_HASH: str = SELECT_SQL_NO_NS_HASH
SELECT_SQL_NO_NS_HASH_LIGHT: str = SELECT_SQL_NO_NS_HASH_LIGHT
SELECT_SQL_NO_NS_HASH_MARIADB: str = SELECT_SQL_NO_NS_HASH_MARIADB
SELECT_SQL_NO_NS_HASH_LIGHT_MARIADB: str = SELECT_SQL_NO_NS_HASH_LIGHT_MARIADB
def _migrate_pending_sends(
self,
pending_sends: list[tuple[str, bytes]],
checkpoint: dict[str, Any],
channel_values: list[tuple[str, str, bytes | None]],
) -> None:
if not pending_sends:
return
enc, blob = self.serde.dumps_typed(
[self.serde.loads_typed((c, b)) for c, b in pending_sends],
)
channel_values.append((TASKS, enc, blob))
checkpoint["channel_versions"][TASKS] = (
max(checkpoint["channel_versions"].values())
if checkpoint["channel_versions"]
else self.get_next_version(None, None)
)
def _load_blobs(
self, blob_values: list[tuple[str, str, bytes | None]]
) -> dict[str, Any]:
if not blob_values:
return {}
return {
k: self.serde.loads_typed((t, v))
for k, t, v in blob_values
if t != "empty" and v is not None
}
def _dump_blobs(
self,
thread_id: str,
checkpoint_ns: str,
values: dict[str, Any],
versions: ChannelVersions,
) -> list[tuple[str, str, str, str, str, str, bytes | None]]:
if not versions:
return []
return [
(
thread_id,
checkpoint_ns,
checkpoint_ns,
k,
cast(str, ver),
*(
self.serde.dumps_typed(values[k])
if k in values
else ("empty", None)
),
)
for k, ver in versions.items()
]
def _load_writes(
self, writes: list[tuple[str, str, str, bytes]]
) -> list[tuple[str, str, Any]]:
return (
[
(
tid,
channel,
self.serde.loads_typed((t, v)),
)
for tid, channel, t, v in writes
]
if writes
else []
)
def _dump_writes(
self,
thread_id: str,
checkpoint_ns: str,
checkpoint_id: str,
task_id: str,
task_path: str,
writes: Sequence[tuple[str, Any]],
) -> list[tuple[str, str, str, str, str, str, int, str, str, bytes]]:
return [
(
thread_id,
checkpoint_ns,
checkpoint_ns,
checkpoint_id,
task_id,
task_path,
WRITES_IDX_MAP.get(channel, idx),
channel,
*self.serde.dumps_typed(value),
)
for idx, (channel, value) in enumerate(writes)
]
def get_next_version(self, current: str | None, channel: None) -> str:
if current is None:
current_v = 0
elif isinstance(current, int):
current_v = current
else:
current_v = int(current.split(".")[0])
next_v = current_v + 1
next_h = random.random()
return f"{next_v:032}.{next_h:016}"
def _search_where(
self,
config: RunnableConfig | None,
filter: dict[str, Any] | None = None,
before: RunnableConfig | None = None,
) -> tuple[str, dict[str, Any]]:
wheres = []
param_values: dict[str, Any] = {}
if config:
wheres.append("thread_id = %(thread_id)s ")
param_values["thread_id"] = config["configurable"]["thread_id"]
checkpoint_ns = config["configurable"].get("checkpoint_ns")
if checkpoint_ns is not None:
wheres.append("checkpoint_ns_hash = MD5(%(checkpoint_ns)s)")
param_values["checkpoint_ns"] = checkpoint_ns
if checkpoint_id := get_checkpoint_id(config):
wheres.append("checkpoint_id = %(checkpoint_id)s ")
param_values["checkpoint_id"] = checkpoint_id
if filter:
wheres.append("JSON_CONTAINS(metadata, %(filter)s) ")
param_values["filter"] = json.dumps(filter)
if before is not None:
wheres.append("checkpoint_id < %(before)s ")
param_values["before"] = get_checkpoint_id(before)
return (
"WHERE " + " AND ".join(wheres) if wheres else "",
param_values,
)
@staticmethod
def _select_sql(where: str, *, light: bool = False, mariadb: bool = False, no_ns_hash: bool = False) -> str:
if no_ns_hash:
return (BaseMySQLSaver.SELECT_SQL_NO_NS_HASH_LIGHT_MARIADB if (light and mariadb) else
BaseMySQLSaver.SELECT_SQL_NO_NS_HASH_LIGHT if light else
BaseMySQLSaver.SELECT_SQL_NO_NS_HASH_MARIADB if mariadb else
BaseMySQLSaver.SELECT_SQL_NO_NS_HASH).replace("{WHERE}", where)
elif light:
return BaseMySQLSaver.SELECT_SQL_LIGHT if not mariadb else BaseMySQLSaver.SELECT_SQL_LIGHT_MARIADB
else:
return (BaseMySQLSaver.SELECT_SQL_MARIADB if mariadb else BaseMySQLSaver.SELECT_SQL).replace("{WHERE}", where)