TIMX 646 - avoid virtual filename column reaning in read_parquet - #190
TIMX 646 - avoid virtual filename column reaning in read_parquet#190ghukill wants to merge 3 commits into
Conversation
Why these changes are being introduced: Work on another application revealed a bug in TDA, that might arguably be a bug in DuckDB itself. Wwhen reading parquet you can rename the virtual `"filename"` column to something else. We did so, renaming to `"append_delta_filename"`, because our append delta parquet files had a real `"filename"` column already. When selecting columns, this renaming works as expected. But when applying a `WHERE` clause, specifically against the `"filename"` column, DuckDB does not honor the renaming and the `WHERE` acts on the wrong column. The append deltas are part of the main `metadata.records` view, seamlessly including records in the append deltas with records in the static DuckDB database file. Filtering by `where filename = ...` had unexpected results due to this DuckDB irregularity. How this addresses that need: Until that is fixed upsteam in DuckDB, or in the event it is not fixed, the changes here avoid this renaming altogether. We use `glob()` to identify append delta parquet files for counting and merging, which we had already been using for counting. Side effects of this change: * There is no longer a `"append_delta_filename"` column in `metadata.records`, because we have removed the virtual `"filename"` renaming for the append delta parquet files we project over. Thankfully, this was not used for ETL purposes anywhere, it was only work on a new metadata analysis application that revealed this. Relevant ticket(s): * https://mitlibraries.atlassian.net/browse/TIMX-646
Why these changes are being introduced: It is quite common for there to be zero append deltas for a given data type, this should be gracefully handled. How this addresses that need: * Catch DuckDB errors when attempting to count append deltas Side effects of this change: * None Relevant ticket(s): * https://mitlibraries.atlassian.net/browse/TIMX-646
ehanson8
left a comment
There was a problem hiding this comment.
Interesting bug you discovered and this seems like a solid fix, good manual tests to illustrate the bug and prove the fix didn't break anything
There was a problem hiding this comment.
Pull request overview
This PR adjusts how TIMDEX Dataset API builds and uses append-delta metadata views to avoid a DuckDB edge case where renaming the virtual filename column can cause WHERE filename = ... filters to be applied against the wrong (virtual) column. The change is aimed at making metadata.<data_type>/metadata.<data_type>_append_deltas behave predictably for downstream consumers that filter on filename.
Changes:
- Stop renaming DuckDB’s virtual parquet
filenamecolumn when creatingmetadata.<type>_append_deltasviews; rely on the realfilenamecolumn stored in append-delta parquet files. - Update append-delta merge logic to enumerate delta files via DuckDB
glob()rather than selecting the previously-exposedappend_delta_filenamecolumn. - Update tests to reflect the removal of
append_delta_filenameand add coverage for filtering byfilenameand for missing/merged delta scenarios.
Reviewed changes
Copilot reviewed 3 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
uv.lock |
Bumps local package version reference to 5.2.0. |
timdex_dataset_api/metadata.py |
Removes append_delta_filename surfacing, switches merge filename collection to glob(), and adds safer counting behavior for missing delta views. |
tests/test_metadata.py |
Updates expected schema, adds regression test for filename filtering, and adds count-for behavior tests. |
pyproject.toml |
Bumps project version to 5.2.0. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Purpose and background context
Please see ticket for background: https://mitlibraries.atlassian.net/browse/TIMX-646.
In summary:
where filename = ...clause that had not been done before because it was not needed for day-to-day ETL usage of TDAfilenamewhen reading parquet files is potentially treated differently for selecting vs filtering againstThis PR updates TDA to stop renaming the virtual
filenamecolumn when reading append deltas for data types (records, embeddings, fulltexts) to avoid subtle bugs in downstream applications. Instead, we use a simpleglob()approach, which we were doing already in other contexts.As noted in the Jira ticket, a DuckDB issue has been created. This may or may not result in a change in DuckDB, but either way we need to address it in the short-term.
The effect of these changes in TDA are minimal to none. These changes are mostly for downstream applications that may want to filter on the
metadata.recordstable by the parquet filepaths; the columnfilenameshould be predictably for the data parquet files, not append deltas.How can a reviewer manually see the effects of these changes?
Observe the bug in DuckDB
1- Ensure you are on DuckDB 1.5.2 or higher
2- Open a DuckDB shell
3- Create a couple of parquet files:
COPY ( select 'foo' as name, 42 as number, '/path/to/a/related/thing1' as filename ) TO '/tmp/duckdb-fake-data-1.parquet'; COPY ( select 'bar' as name, 101 as number, '/path/to/a/related/thing2' as filename ) TO '/tmp/duckdb-fake-data-2.parquet';4- Create a view that projects over these, renaming the virtual
filenamecolumn:5- Note that a
selectstatement treats these columns correctly:The
filenamecolumn is the "real" data from the parquet file, and the virtual/dynamicappend_delta_filenamecolumn is the filepath of the parquet files we're reading from usingread_parquet(). At this point, all is working as expected.6- Apply a filter to the
filenamecolumn:Given the results from our
selectstatement above, we should have expected to find the "foo" record. What happened instead was DuckDB applied aFile Filter, ignoring our renamed virtual column, and actually filters on what we intended to be theappend_delta_filenamecolumn not thefilenamecolumn in the view:Confirm that new
glob()approach works in TDA1- Start ipython shell and load a local, temp dataset:
2- Write some records, build metadatda, and write some more. This will result in some append deltas:
3- Count append deltas:
4- Merge append deltas:
5- Count again, should be zero:
This demonstrates that counting + merging of append delta remains the same after these changes.
Includes new or updated dependencies?
NO
Changes expectations for external applications?
YES:
append_delta_filenameis no longer a column in themetadata.<data_type>viewsWhat are the relevant tickets?
Code review