|
| 1 | +import logging |
| 2 | +from collections.abc import Generator |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import duckdb |
| 6 | +import pyarrow as pa |
| 7 | +import pyarrow.dataset as ds |
| 8 | + |
| 9 | +from abdiff.config import Config |
| 10 | +from abdiff.core.utils import load_dataset, write_to_dataset |
| 11 | + |
| 12 | +logger = logging.getLogger(__name__) |
| 13 | + |
| 14 | +CONFIG = Config() |
| 15 | + |
| 16 | +READ_BATCH_SIZE = 1_000 |
| 17 | + |
| 18 | + |
| 19 | +def create_final_records( |
| 20 | + run_directory: str, diffs_dataset_path: str, metrics_dataset_path: str |
| 21 | +) -> str: |
| 22 | + """Produce a single, final dataset that contains all records and diff information. |
| 23 | +
|
| 24 | + This dataset is produced by joining the "diffs" dataset (which contains the full |
| 25 | + A and B records, and the JSON diff) with the "metrics" dataset (which is a sparse |
| 26 | + matrix of TIMDEX fields and boolean 1 or 0 if that record has a diff for that field). |
| 27 | + This dataset should be sufficient for supporting any webapp data needs. |
| 28 | +
|
| 29 | + This dataset is partitioned by source and 'has_diff' boolean. |
| 30 | + """ |
| 31 | + logger.info("Creating final records dataset from 'diffs' and 'metrics' datasets.") |
| 32 | + |
| 33 | + diffs_dataset = load_dataset(diffs_dataset_path) |
| 34 | + metrics_dataset = load_dataset(metrics_dataset_path) |
| 35 | + |
| 36 | + # get list of unique columns from metrics dataset, and create final dataset schema |
| 37 | + metrics_timdex_field_columns = [ |
| 38 | + name |
| 39 | + for name in metrics_dataset.schema.names |
| 40 | + if name not in diffs_dataset.schema.names |
| 41 | + ] |
| 42 | + metrics_columns = ( |
| 43 | + pa.field(name, pa.int64()) |
| 44 | + for name in metrics_dataset.schema.names |
| 45 | + if name in metrics_timdex_field_columns |
| 46 | + ) |
| 47 | + final_records_dataset_schema = pa.schema( |
| 48 | + ( |
| 49 | + pa.field("timdex_record_id", pa.string()), |
| 50 | + pa.field("source", pa.string()), |
| 51 | + pa.field("record_a", pa.binary()), |
| 52 | + pa.field("record_b", pa.binary()), |
| 53 | + pa.field("ab_diff", pa.string()), |
| 54 | + pa.field("modified_timdex_fields", pa.list_(pa.string())), |
| 55 | + pa.field("has_diff", pa.string()), |
| 56 | + *metrics_columns, # type: ignore[arg-type] |
| 57 | + ) |
| 58 | + ) |
| 59 | + |
| 60 | + records_dataset_path = str(Path(run_directory) / "records") |
| 61 | + write_to_dataset( |
| 62 | + get_final_records_iter( |
| 63 | + diffs_dataset, metrics_dataset, metrics_timdex_field_columns |
| 64 | + ), |
| 65 | + base_dir=records_dataset_path, |
| 66 | + schema=final_records_dataset_schema, |
| 67 | + partition_columns=["source", "has_diff"], |
| 68 | + ) |
| 69 | + |
| 70 | + return records_dataset_path |
| 71 | + |
| 72 | + |
| 73 | +def get_final_records_iter( |
| 74 | + diffs_dataset: ds.Dataset, |
| 75 | + metrics_dataset: ds.Dataset, |
| 76 | + metrics_timdex_field_columns: list[str], |
| 77 | +) -> Generator[pa.RecordBatch, None, None]: |
| 78 | + |
| 79 | + with duckdb.connect(":memory:") as conn: |
| 80 | + |
| 81 | + # register datasets in DuckDB for use |
| 82 | + conn.register("diffs", diffs_dataset.to_table()) |
| 83 | + conn.register("metrics", metrics_dataset.to_table()) |
| 84 | + |
| 85 | + # prepare select columns |
| 86 | + select_columns = ",".join( |
| 87 | + [ |
| 88 | + "d.timdex_record_id", |
| 89 | + "d.source", |
| 90 | + "d.record_a", |
| 91 | + "d.record_b", |
| 92 | + "d.ab_diff", |
| 93 | + "d.modified_timdex_fields", |
| 94 | + "d.has_diff", |
| 95 | + *[f"m.{name}" for name in metrics_timdex_field_columns], |
| 96 | + ] |
| 97 | + ) |
| 98 | + |
| 99 | + results = conn.execute( |
| 100 | + f""" |
| 101 | + select {select_columns} |
| 102 | + from diffs d |
| 103 | + inner join metrics m on m.timdex_record_id = d.timdex_record_id |
| 104 | + """ |
| 105 | + ).fetch_record_batch(READ_BATCH_SIZE) |
| 106 | + |
| 107 | + count = 0 |
| 108 | + while True: |
| 109 | + try: |
| 110 | + count += 1 |
| 111 | + logger.info(f"Yielding final records dataset batch: {count}") |
| 112 | + yield results.read_next_batch() |
| 113 | + except StopIteration: |
| 114 | + break |
0 commit comments