|
| 1 | +import argparse |
| 2 | +import json |
| 3 | +import logging |
| 4 | +import os |
| 5 | +from datetime import date, datetime |
| 6 | +from pathlib import Path |
| 7 | +from typing import Any, Iterable, List, Optional, Tuple |
| 8 | + |
| 9 | +import numpy as np |
| 10 | +import pandas as pd |
| 11 | +import psycopg2 |
| 12 | +from psycopg2.extensions import connection |
| 13 | +from psycopg2.extras import execute_values |
| 14 | + |
| 15 | +logging.basicConfig(level=logging.INFO, format="%(asctime)s | %(levelname)s | %(message)s") |
| 16 | +logger = logging.getLogger(__name__) |
| 17 | + |
| 18 | +DB_CONFIG: dict[str, Any] = { |
| 19 | + "host": os.getenv("POSTGRES_HOST"), |
| 20 | + "port": int(os.getenv("POSTGRES_PORT") or 5432), |
| 21 | + "user": os.getenv("POSTGRES_USER"), |
| 22 | + "password": os.getenv("POSTGRES_PASSWORD"), |
| 23 | + "dbname": os.getenv("POSTGRES_DB"), |
| 24 | + "sslmode": "require", |
| 25 | +} |
| 26 | + |
| 27 | +TABLE = "paper" |
| 28 | + |
| 29 | + |
| 30 | +def iter_shards(data_dir: Path) -> Iterable[Path]: |
| 31 | + """Return all parquet shard paths in sorted order.""" |
| 32 | + return sorted(data_dir.glob("shard_*.parquet")) |
| 33 | + |
| 34 | + |
| 35 | +def parse_date(d: Optional[str]) -> Optional[date]: |
| 36 | + """Parse YYYY-MM-DD into a date object or return None.""" |
| 37 | + if not d: |
| 38 | + return None |
| 39 | + try: |
| 40 | + return datetime.strptime(d, "%Y-%m-%d").date() |
| 41 | + except ValueError: |
| 42 | + return None |
| 43 | + |
| 44 | + |
| 45 | +def to_plain(x: Any) -> Any: |
| 46 | + """Convert numpy objects recursively to plain Python types.""" |
| 47 | + if isinstance(x, np.ndarray): |
| 48 | + return [to_plain(v) for v in x.tolist()] |
| 49 | + if isinstance(x, (list, tuple)): |
| 50 | + return [to_plain(v) for v in x] |
| 51 | + if isinstance(x, dict): |
| 52 | + return {k: to_plain(v) for k, v in x.items()} |
| 53 | + if isinstance(x, np.generic): |
| 54 | + return x.item() |
| 55 | + return x |
| 56 | + |
| 57 | + |
| 58 | +def authors_to_json(x: Any) -> str: |
| 59 | + """Convert author list structure into JSON string.""" |
| 60 | + try: |
| 61 | + return json.dumps(to_plain(x)) |
| 62 | + except (TypeError, ValueError): |
| 63 | + return "[]" |
| 64 | + |
| 65 | + |
| 66 | +def ensure_doi_index(conn: connection) -> None: |
| 67 | + """Ensure unique index on paper.doi exists.""" |
| 68 | + with conn.cursor() as cur: |
| 69 | + cur.execute("CREATE UNIQUE INDEX IF NOT EXISTS idx_paper_doi ON public.paper(doi);") |
| 70 | + conn.commit() |
| 71 | + |
| 72 | + |
| 73 | +def embedding_to_literal(embedding: Any) -> str: |
| 74 | + """Convert embedding array into PostgreSQL vector literal.""" |
| 75 | + if hasattr(embedding, "tolist"): |
| 76 | + embedding = embedding.tolist() |
| 77 | + return "[" + ",".join(f"{float(v):.6f}" for v in embedding) + "]" |
| 78 | + |
| 79 | + |
| 80 | +def insert_rows(conn: connection, rows: List[Tuple[Any, ...]]) -> None: |
| 81 | + """Insert a batch of rows into the paper table.""" |
| 82 | + if not rows: |
| 83 | + return |
| 84 | + |
| 85 | + query = f""" |
| 86 | + INSERT INTO public.{TABLE} ( |
| 87 | + doi, |
| 88 | + source, |
| 89 | + paper_type, |
| 90 | + title, |
| 91 | + authors, |
| 92 | + abstract, |
| 93 | + published_at, |
| 94 | + paper_id_external, |
| 95 | + embedding |
| 96 | + ) |
| 97 | + VALUES %s |
| 98 | + ON CONFLICT (doi) DO NOTHING |
| 99 | + """ |
| 100 | + |
| 101 | + template = "(" + ",".join(["%s"] * 8) + ", %s::vector)" |
| 102 | + |
| 103 | + with conn.cursor() as cur: |
| 104 | + execute_values(cur, query, rows, template=template) |
| 105 | + |
| 106 | + conn.commit() |
| 107 | + |
| 108 | + |
| 109 | +def build_row(row: Any) -> Optional[Tuple[Any, ...]]: |
| 110 | + """Build a single row tuple for DB insertion.""" |
| 111 | + doi = getattr(row, "doi", None) |
| 112 | + title: str = (getattr(row, "title", "") or "").strip() |
| 113 | + abstract: str = (getattr(row, "abstract", "") or "").strip() |
| 114 | + published_at = parse_date(getattr(row, "update_date", None)) |
| 115 | + raw_authors = getattr(row, "authors_parsed", None) |
| 116 | + authors_json = authors_to_json(raw_authors) |
| 117 | + arxiv_id = getattr(row, "id", None) |
| 118 | + paper_id_external = arxiv_id or None |
| 119 | + embedding = getattr(row, "embedding", None) |
| 120 | + if embedding is None: |
| 121 | + return None |
| 122 | + embedding_literal = embedding_to_literal(embedding) |
| 123 | + |
| 124 | + return ( |
| 125 | + doi, |
| 126 | + "ARXIV", |
| 127 | + "PREPRINT", |
| 128 | + title, |
| 129 | + authors_json, |
| 130 | + abstract, |
| 131 | + published_at, |
| 132 | + paper_id_external, |
| 133 | + embedding_literal, |
| 134 | + ) |
| 135 | + |
| 136 | + |
| 137 | +def ingest_shard(conn: connection, shard_path: Path, batch_size: int) -> None: |
| 138 | + """Ingest a single parquet shard into the database.""" |
| 139 | + logger.info("Ingesting %s", shard_path.name) |
| 140 | + |
| 141 | + df: pd.DataFrame = pd.read_parquet(shard_path) |
| 142 | + rows: List[Tuple[Any, ...]] = [] |
| 143 | + |
| 144 | + for row in df.itertuples(index=False): |
| 145 | + row_tuple = build_row(row) |
| 146 | + if row_tuple is None: |
| 147 | + continue |
| 148 | + rows.append(row_tuple) |
| 149 | + if len(rows) >= batch_size: |
| 150 | + insert_rows(conn, rows) |
| 151 | + rows.clear() |
| 152 | + |
| 153 | + |
| 154 | +def main(data_dir: Path, batch_size: int) -> None: |
| 155 | + """Main entrypoint for bulk ingestion.""" |
| 156 | + conn = psycopg2.connect(**DB_CONFIG) |
| 157 | + try: |
| 158 | + ensure_doi_index(conn) |
| 159 | + for shard in iter_shards(data_dir): |
| 160 | + ingest_shard(conn, shard, batch_size=batch_size) |
| 161 | + finally: |
| 162 | + conn.close() |
| 163 | + |
| 164 | + |
| 165 | +if __name__ == "__main__": |
| 166 | + script_dir = os.path.dirname(os.path.abspath(__file__)) |
| 167 | + default_data_path = os.path.join(script_dir, "arxiv-vector-embeddings") |
| 168 | + |
| 169 | + parser = argparse.ArgumentParser(description="Bulk ingest arXiv parquet shards.") |
| 170 | + parser.add_argument("--data-dir", type=str, default=default_data_path) |
| 171 | + parser.add_argument("--batch-size", type=int, default=1000) |
| 172 | + args = parser.parse_args() |
| 173 | + |
| 174 | + main(Path(args.data_dir), args.batch_size) |
0 commit comments