"""Parallel split of sorted index parquets into parts < 50 GB for HTTP upload to HF. Windows Xet upload stalls on large files; the HTTP fallback caps at 50 GB/file. Parts preserve sorted order (contiguous row-group ranges), so DuckDB zone-map pruning still works across parts. Multiprocessing (N_WORKERS) since pyarrow's writer is mostly single-threaded. Resumable: existing completed parts are skipped. """ import multiprocessing as mp import os import time import pyarrow.parquet as pq BASE = os.path.dirname(os.path.abspath(__file__)) # budget on UNCOMPRESSED size; SNAPPY output lands well under the 50 GB cap TARGET_UNCOMPRESSED = 45 * 1024**3 N_WORKERS = 5 BATCH_GROUPS = 100 # row groups read per batch (~1-3 GiB in RAM) JOBS = ["idx_phone.parquet", "idx_aadhar.parquet"] def plan_partitions(sizes, target): """Split row-group size list into (start, end) buckets each ~target bytes.""" parts = [] acc = 0 start = 0 for i, s in enumerate(sizes): acc += s if acc >= target and i - start > 200: # at least 200 row groups per part parts.append((start, i + 1)) start = i + 1 acc = 0 if start < len(sizes): parts.append((start, len(sizes))) return parts def split_one(src, pi, a, b): out = f"{os.path.splitext(src)[0]}.{pi}.parquet" if os.path.exists(out) and os.path.getsize(out) > 0: print(f" {out} exists, skip", flush=True) return tmp = out + ".tmp" if os.path.exists(tmp): os.remove(tmp) t0 = time.time() pf = pq.ParquetFile(src) with pq.ParquetWriter(tmp, pf.schema_arrow, compression="snappy", use_dictionary=True, data_page_version="2.0", version="2.6") as w: g = a while g < b: g2 = min(g + BATCH_GROUPS, b) w.write_table(pf.read_row_groups(list(range(g, g2)))) g = g2 os.rename(tmp, out) print(f" DONE {out} ({os.path.getsize(out)/1073741824:.1f} GiB) " f"in {time.time()-t0:.0f}s", flush=True) def worker(wid, tasklist): for src, pi, a, b in tasklist: print(f"[w{wid}] start {src} part {pi} (groups {a}-{b})", flush=True) try: split_one(src, pi, a, b) except Exception as e: print(f"[w{wid}] FAIL {src} part {pi}: {e}", flush=True) def main(): jobs = [] # (src, [(a,b), ...]) for src in JOBS: if not os.path.exists(src): print(f"SKIP {src}: not found", flush=True) continue pf = pq.ParquetFile(src) sizes = [pf.metadata.row_group(i).total_byte_size for i in range(pf.num_row_groups)] boundaries = plan_partitions(sizes, TARGET_UNCOMPRESSED) print(f"{src}: {pf.num_row_groups} row groups, " f"{sum(sizes)/1073741824:.1f} GiB uncompressed -> " f"{len(boundaries)} parts", flush=True) jobs.append((src, boundaries)) flat = [(src, i, a, b) for src, bnd in jobs for i, (a, b) in enumerate(bnd)] procs = [] for wi in range(N_WORKERS): mine = flat[wi::N_WORKERS] p = mp.Process(target=worker, args=(wi, mine)) p.start() procs.append(p) for p in procs: p.join() print("ALL DONE", flush=True) if __name__ == "__main__": main()