|
| 1 | +# Reading data from TIMDEXDataset |
| 2 | + |
| 3 | +This guide explains how `TIMDEXDataset` read methods work and how to use them effectively. |
| 4 | + |
| 5 | +- `TIMDEXDataset` and `TIMDEXDatasetMetadata` both maintain an in-memory DuckDB context. You can issue DuckDB SQL against the views/tables they create. |
| 6 | +- Read methods use a two-step query flow for performance: |
| 7 | + 1) a metadata query determines which Parquet files and row offsets are relevant |
| 8 | + 2) a data query reads just those rows and returns the requested columns |
| 9 | +- Prefer simple key/value `DatasetFilters` for most use cases; add a `where=` SQL predicate when you need more advanced logic (e.g., ranges, `BETWEEN`, `>`, `<`, `IN`). |
| 10 | + |
| 11 | +## Available read methods |
| 12 | + |
| 13 | +- `read_batches_iter(...)`: yields `pyarrow.RecordBatch` |
| 14 | +- `read_dicts_iter(...)`: yields Python `dict` per row |
| 15 | +- `read_dataframe(...)`: returns a pandas `DataFrame` |
| 16 | +- `read_dataframes_iter(...)`: yields pandas `DataFrame` batches |
| 17 | +- `read_transformed_records_iter(...)`: yields `transformed_record` dictionaries only |
| 18 | + |
| 19 | +All accept the same `DatasetFilters` and the optional `where=` SQL predicate. |
| 20 | + |
| 21 | +## Filters vs. where= |
| 22 | + |
| 23 | +- `DatasetFilters` are key/value arguments on read methods. They are validated and translated into SQL and will cover most queries. |
| 24 | + - Examples: `source="alma"`, `run_date="2024-12-01"`, `run_type="daily"`, `action="index"` |
| 25 | +- `where=` is an optional raw SQL WHERE predicate string, combined with `DatasetFilters` using `AND`. Use it for: |
| 26 | + - date/time ranges (BETWEEN, >, <) |
| 27 | + - set membership (IN (...)) |
| 28 | + - complex boolean logic (AND/OR grouping) |
| 29 | + |
| 30 | +Important: `where=` must be only a WHERE predicate (no `SELECT`/`FROM`/`;`). The library plugs it into generated SQL. |
| 31 | + |
| 32 | +## How reading works (two-step process) |
| 33 | + |
| 34 | +1) Metadata query |
| 35 | + - Runs against `TIMDEXDatasetMetadata` views (e.g., `metadata.records`, `metadata.current_records`) |
| 36 | + - Produces a small result set with identifiers: `filename`, row group/offsets, and primary keys |
| 37 | + - Greatly reduces how much data must be scanned |
| 38 | + |
| 39 | +2) Data query |
| 40 | + - Uses DuckDB to read only relevant Parquet fragments based on metadata results |
| 41 | + - Joins the metadata identifiers to return the exact rows requested |
| 42 | + - Returns batches, dicts, or a `DataFrame` depending on the method |
| 43 | + |
| 44 | +This pattern keeps reads fast and memory-efficient even for large datasets. |
| 45 | + |
| 46 | +The following diagram shows the flow for an example query: |
| 47 | + |
| 48 | +```python |
| 49 | +for record_dict in td.read_dicts_iter( |
| 50 | + table="records", |
| 51 | + source="dspace", |
| 52 | + run_date="2025-09-01", |
| 53 | + run_id="abc123" |
| 54 | +): |
| 55 | + # process record... |
| 56 | +``` |
| 57 | + |
| 58 | +```mermaid |
| 59 | +sequenceDiagram |
| 60 | + autonumber |
| 61 | + participant U as User |
| 62 | + participant TD as TIMDEXDataset |
| 63 | + participant TDM as TIMDEXDatasetMetadata |
| 64 | + participant D as DuckDB Context |
| 65 | + participant P as Parquet files |
| 66 | +
|
| 67 | + U->>TD: Perform query |
| 68 | + Note left of TD: read_dicts_iter(<br>table="records",<br>source="dspace",<br>run_date="2025-09-01",<br>run_id="abc123") |
| 69 | + TD->>TDM: build_meta_query(table, filters, where=None) |
| 70 | + Note right of TDM: (Metadata Query)<br><br>SELECT r.timdex_record_id, r.run_id, r.filename, r.run_record_offset<br>FROM metadata.records r<br>WHERE r.source = 'dspace'<br>AND r.run_date = '2025-09-01'<br>AND r.run_id = 'abc123'<br>ORDER BY r.filename, r.run_record_offset |
| 71 | +
|
| 72 | + TDM->>D: Execute metadata query |
| 73 | + D-->>TD: lightweight result set (file + offsets) |
| 74 | +
|
| 75 | + TD->>D: Build and run data query using metadata |
| 76 | + Note right of D: (Data query)<br><br>SELECT <COLUMNS><br>FROM read_parquet(P.files) d<br>JOIN meta m<br>USING (timdex_record_id, run_id, run_record_offset) |
| 77 | +
|
| 78 | + D-->>TD: batches of rows |
| 79 | + TD-->>U: iterator of dicts (one dict per row) |
| 80 | +``` |
| 81 | + |
| 82 | + |
| 83 | +## Quick start examples |
| 84 | + |
| 85 | +```python |
| 86 | +from timdex_dataset_api import TIMDEXDataset |
| 87 | + |
| 88 | +td = TIMDEXDataset("s3://my-bucket/timdex-dataset") # example instance |
| 89 | + |
| 90 | +# 1) Get a single record as a dict |
| 91 | +first = next(td.read_dicts_iter()) |
| 92 | + |
| 93 | +# 2) Read batches with simple filters |
| 94 | +for batch in td.read_batches_iter(source="alma", run_date="2025-06-01", run_id="abc123"): |
| 95 | + ... # process pyarrow.RecordBatch |
| 96 | + |
| 97 | +# 3) DataFrame of one run |
| 98 | +df = td.read_dataframe(source="dspace", run_date="2025-06-01", run_id="def456") |
| 99 | + |
| 100 | +# 4) Only transformed records (used by indexer) |
| 101 | +for rec in td.read_transformed_records_iter(source="aspace", run_type="daily"): |
| 102 | + ... # rec is a dict of the transformed_record |
| 103 | +``` |
| 104 | + |
| 105 | +## `where=` examples |
| 106 | + |
| 107 | +Advanced filtering that complements `DatasetFilters`. |
| 108 | + |
| 109 | +```python |
| 110 | +# date range with BETWEEN |
| 111 | +where = "run_date BETWEEN '2024-12-01' AND '2024-12-31'" |
| 112 | +df = td.read_dataframe(source="alma", where=where) |
| 113 | + |
| 114 | +# greater-than on a timestamp (if present in columns) |
| 115 | +where = "run_timestamp > '2024-12-01T10:00:00Z'" |
| 116 | +df = td.read_dataframe(source="aspace", run_type="daily", where=where) |
| 117 | + |
| 118 | +# combine set membership and action |
| 119 | +where = "run_id IN ('run-1', 'run-3', 'run-5') AND action = 'index'" |
| 120 | +df = td.read_dataframe(source="alma", where=where) |
| 121 | + |
| 122 | +# combine filters (AND) with where= |
| 123 | +where = "run_type = 'daily' AND action = 'index'" |
| 124 | +df = td.read_dataframe(source="libguides", where=where) |
| 125 | +``` |
| 126 | + |
| 127 | +Validation tips: |
| 128 | +- Use only a predicate (no SELECT/FROM, no trailing semicolon). |
| 129 | +- Column names must exist in the target table/view (e.g., records or current_records). |
| 130 | +- `DatasetFilters` + `where=` are ANDed; if the combination yields zero rows, you’ll get an empty result. |
| 131 | + |
| 132 | +## Choosing a table |
| 133 | + |
| 134 | +By default, read methods query the `records` view (all versions). To get only the latest version per `timdex_record_id`, target the `current_records` view: |
| 135 | + |
| 136 | +```python |
| 137 | +# ALL records in the 'libguides' source |
| 138 | +all_libguides_df = td.read_dataframe(table="records", source="libguides") |
| 139 | + |
| 140 | +# latest unique records across the dataset |
| 141 | +current_df = td.read_dataframe(table="current_records") |
| 142 | + |
| 143 | +# current records for a source and specific run |
| 144 | +current_df = td.read_dataframe(table="current_records", source="alma", run_id="run-5") |
| 145 | +``` |
| 146 | + |
| 147 | +## DuckDB context |
| 148 | + |
| 149 | +- `TIMDEXDataset` exposes a DuckDB connection used for data queries against Parquet. |
| 150 | +- `TIMDEXDatasetMetadata` exposes a DuckDB connection used for metadata queries and provides views: |
| 151 | + - `metadata.records`: all record versions with run metadata |
| 152 | + - `metadata.current_records`: latest record per `timdex_record_id` |
| 153 | + - `metadata.append_deltas`: incremental write tracking |
| 154 | + |
| 155 | +You can execute raw DuckDB SQL for inspection and debugging: |
| 156 | + |
| 157 | +```python |
| 158 | +# access metadata connection |
| 159 | +conn = td.metadata.conn # DuckDB connection |
| 160 | + |
| 161 | +# peek at view schemas |
| 162 | +print(conn.sql("DESCRIBE metadata.records").to_df()) |
| 163 | +print(conn.sql("DESCRIBE metadata.current_records").to_df()) |
| 164 | + |
| 165 | +# ad-hoc query (read-only) |
| 166 | +debug_df = conn.sql(""" |
| 167 | + SELECT source, action, COUNT(*) as n |
| 168 | + FROM metadata.records |
| 169 | + WHERE run_date = '2024-12-01' |
| 170 | + GROUP BY 1, 2 |
| 171 | + ORDER BY n DESC |
| 172 | +""").to_df() |
| 173 | +``` |
| 174 | + |
| 175 | +## Performance notes |
| 176 | + |
| 177 | +- Batch iterators (`read_batches_iter()` / `read_dataframes_iter()`) stream results to control memory. |
| 178 | +- `read_dataframe()` loads ALL matching rows into memory; fine for small/filtered sets but can easily overwhelm memory for large result sets |
| 179 | +- Tuning via env vars (advanced): `TDA_READ_BATCH_SIZE`, `TDA_DUCKDB_THREADS`, `TDA_DUCKDB_MEMORY_LIMIT`. |
| 180 | + |
| 181 | +## Troubleshooting |
| 182 | + |
| 183 | +- Empty results? Check that filters and `where=` don’t over-constrain your query. |
| 184 | +- Syntax errors? Ensure `where=` is a valid predicate and references existing columns. |
| 185 | +- Large scans? Make sure to use `_iter()` read methods. |
0 commit comments