|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from collections.abc import Sequence |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +from sqlalchemy.engine import Engine |
| 7 | + |
| 8 | + |
| 9 | +def _exec_and_collect(conn, sql: str, params: list[Any]) -> list[dict[str, Any]]: |
| 10 | + # exec_driver_sql requires a tuple for single-row execution, or None for no params. |
| 11 | + result = conn.exec_driver_sql(sql, tuple(params) if params else None) |
| 12 | + columns = list(result.keys()) |
| 13 | + return [dict(zip(columns, row, strict=False)) for row in result.fetchall()] |
| 14 | + |
| 15 | + |
| 16 | +def paradedb_indexes(engine: Engine) -> list[dict[str, Any]]: |
| 17 | + """Return metadata for all BM25 indexes from ``pdb.indexes()``.""" |
| 18 | + with engine.connect() as conn: |
| 19 | + return _exec_and_collect(conn, "SELECT * FROM pdb.indexes()", []) |
| 20 | + |
| 21 | + |
| 22 | +def paradedb_index_segments(engine: Engine, index: str) -> list[dict[str, Any]]: |
| 23 | + """Return segment metadata for a BM25 index from ``pdb.index_segments()``.""" |
| 24 | + with engine.connect() as conn: |
| 25 | + return _exec_and_collect( |
| 26 | + conn, "SELECT * FROM pdb.index_segments(%s::regclass)", [index] |
| 27 | + ) |
| 28 | + |
| 29 | + |
| 30 | +def paradedb_verify_index( |
| 31 | + engine: Engine, |
| 32 | + index: str, |
| 33 | + *, |
| 34 | + heapallindexed: bool = False, |
| 35 | + sample_rate: float | None = None, |
| 36 | + report_progress: bool = False, |
| 37 | + verbose: bool = False, |
| 38 | + on_error_stop: bool = False, |
| 39 | + segment_ids: Sequence[int] | None = None, |
| 40 | +) -> list[dict[str, Any]]: |
| 41 | + """Run ``pdb.verify_index()`` for one BM25 index.""" |
| 42 | + sql_parts = ["SELECT * FROM pdb.verify_index(%s::regclass"] |
| 43 | + params: list[Any] = [index] |
| 44 | + if heapallindexed: |
| 45 | + sql_parts.append(", heapallindexed => %s::boolean") |
| 46 | + params.append(heapallindexed) |
| 47 | + if sample_rate is not None: |
| 48 | + sql_parts.append(", sample_rate => %s::double precision") |
| 49 | + params.append(sample_rate) |
| 50 | + if report_progress: |
| 51 | + sql_parts.append(", report_progress => %s::boolean") |
| 52 | + params.append(report_progress) |
| 53 | + if verbose: |
| 54 | + sql_parts.append(", verbose => %s::boolean") |
| 55 | + params.append(verbose) |
| 56 | + if on_error_stop: |
| 57 | + sql_parts.append(", on_error_stop => %s::boolean") |
| 58 | + params.append(on_error_stop) |
| 59 | + if segment_ids is not None: |
| 60 | + sql_parts.append(", segment_ids => %s::int[]") |
| 61 | + params.append(list(segment_ids)) |
| 62 | + sql_parts.append(")") |
| 63 | + with engine.connect() as conn: |
| 64 | + return _exec_and_collect(conn, "".join(sql_parts), params) |
| 65 | + |
| 66 | + |
| 67 | +def paradedb_verify_all_indexes( |
| 68 | + engine: Engine, |
| 69 | + *, |
| 70 | + schema_pattern: str | None = None, |
| 71 | + index_pattern: str | None = None, |
| 72 | + heapallindexed: bool = False, |
| 73 | + sample_rate: float | None = None, |
| 74 | + report_progress: bool = False, |
| 75 | + on_error_stop: bool = False, |
| 76 | +) -> list[dict[str, Any]]: |
| 77 | + """Run ``pdb.verify_all_indexes()`` across BM25 indexes.""" |
| 78 | + named_params: list[tuple[str, str, Any]] = [] |
| 79 | + if schema_pattern is not None: |
| 80 | + named_params.append(("schema_pattern", "text", schema_pattern)) |
| 81 | + if index_pattern is not None: |
| 82 | + named_params.append(("index_pattern", "text", index_pattern)) |
| 83 | + if heapallindexed: |
| 84 | + named_params.append(("heapallindexed", "boolean", heapallindexed)) |
| 85 | + if sample_rate is not None: |
| 86 | + named_params.append(("sample_rate", "double precision", sample_rate)) |
| 87 | + if report_progress: |
| 88 | + named_params.append(("report_progress", "boolean", report_progress)) |
| 89 | + if on_error_stop: |
| 90 | + named_params.append(("on_error_stop", "boolean", on_error_stop)) |
| 91 | + |
| 92 | + sql_parts = ["SELECT * FROM pdb.verify_all_indexes("] |
| 93 | + params: list[Any] = [] |
| 94 | + if named_params: |
| 95 | + sql_parts.append( |
| 96 | + ", ".join( |
| 97 | + f"{name} => %s::{pg_type}" |
| 98 | + for name, pg_type, _ in named_params |
| 99 | + ) |
| 100 | + ) |
| 101 | + params.extend(value for _, _, value in named_params) |
| 102 | + sql_parts.append(")") |
| 103 | + |
| 104 | + with engine.connect() as conn: |
| 105 | + return _exec_and_collect(conn, "".join(sql_parts), params) |
| 106 | + |
| 107 | + |
| 108 | +__all__ = [ |
| 109 | + "paradedb_index_segments", |
| 110 | + "paradedb_indexes", |
| 111 | + "paradedb_verify_all_indexes", |
| 112 | + "paradedb_verify_index", |
| 113 | +] |
0 commit comments