Repository navigation
feat: keep column groups through compaction and add rewrite_columns - #9291
dshepelev15 wants to merge 2 commits into
Conversation
Compaction re-encodes every column of the fragments it rewrites, so migrating a table with a few multi-KB embedding columns to a newer file version rewrites the whole table for the sake of its narrow columns, and compaction folds any per-column file layout back into one wide file, which makes later single-column updates rewrite every column again. `CompactionOptions.column_groups` (config key `lance.compaction.column_groups`) writes each listed group of top-level columns to its own data file per compacted fragment and the remaining columns to one shared file. One scan feeds one writer per group with the same planned row counts, so the files of a fragment stay row-aligned; binary copy is disabled and `max_bytes_per_file` ignored while groups are set. `Dataset::rewrite_columns` / `FileFragment::rewrite_columns` rewrite only the named columns of every fragment into a new data file in the requested V2 version and tombstone them in the files they came from. The other columns' files are neither read nor written; rows, fragment ids, row addresses and indices are unchanged. Fragments already in the requested layout are skipped, and the fragment-level call returns metadata for a single distributed `Update` commit in `RewriteColumns` mode.
Fold the two internal errors on misaligned column group output into one, hand the forwarder its projections by value instead of cloning them, and look up an unknown rewrite column with find instead of a loop.
|
I did similar thing before: #8614 This change been postponed because I think it's better to refactor our compaction logic so that make lance to produce stats instead of a black box. |
There was a problem hiding this comment.
✅ Gate recommendation: approve.
The earlier physical-layout PR and the statistics-first concern do not change the recommendation. Producing layout statistics would improve policy selection, but it does not replace the executor needed to rewrite chosen groups. This PR supplies that row-aligned execution through existing compaction jobs and table configuration, while rewrite_columns avoids rewriting wide columns; its row, index, overlay, and stable-row-ID contracts remain preserved.
Please mark this PR with the breaking-change label.
| def rewrite_columns( | ||
| self, | ||
| columns: List[str], | ||
| *, | ||
| data_storage_version: Optional[str] = None, | ||
| ): |
There was a problem hiding this comment.
suggestion(blocking): instead of adding a new API, I think we should find a way to fold this into the existing compaction operation. That way we aren't adding yet another maintenance operation a user has to call. Rewrite_columns can just be a particular runtime configuration you pass, if you only want to do those jobs. #8614 (comment)
#9604) Builds on #9705, which added `data_change` to the format. ## What's wrong today? Each `add_columns` backfill gives every fragment another data file, which slows scans and random reads. `compact_files` never merges them: it picks fragments with many deletions or overlays, or few rows (#4286, #3995). ## What does this change? `compact_files` can also plan a task that repacks one fragment's columns into fewer data files. Rows don't move, so fragment ids, deletions, overlays and index coverage stay. A fragment no rewrite takes is repacked when it holds its columns in more than `max_data_files_per_fragment` files (off by default; also table config). This is the only trigger; #11035 tracks dead slots. A repack merges only files it can empty, at least two at a time. Files with a blob column, part of a column or spilled row lineage stay, and V1 fragments are skipped. `scope` limits a run to rewrites or repacks, and repacks are budgeted after rewrites. `ForceBinaryCopy` plans no repacks, and a repack-only run with no limit or under `ForceBinaryCopy` is refused. ## How does a repack commit? `commit_compaction` commits rewrites as one `Rewrite`, then repacks as one `DataReplacement { data_change: false }`: index coverage of the moved fields stays, overlays keep shadowing, and row versions don't move. A concurrent change to a moved column, a rewrite or delete of the fragment, or an `add_columns` makes it retry. The planner's fragment reuse index checks apply only to runs that rewrite. `DataReplacement` now takes several groups per fragment, and what it does to a fragment's files is one pure function, `Fragment::replace_files`, shared by `build_manifest` and the planner. ## Breaking changes Rust code that builds these with a struct literal stops compiling: - `Operation::DataReplacement` gains a required `data_change: bool`; `true` keeps the old behavior. - `TaskData` gains `kind`, `RewriteResult` gains `repacked_files`, and `CompactionOptions` gains `max_data_files_per_fragment` and `scope`. Use `TaskData::rewrite_fragments` or `repack_columns`, `repacked_files: None` and `..Default::default()`. Older serialized tasks, results and transactions still read, and an older worker rewrites a repack task's fragment instead. Java and Python only add API. ## Known limits - A run that rewrites and repacks commits two versions until composite transactions (#8644) land; if the repack fails, the rewrite stays. - A concurrent index build keeps coverage of repacked fragments only if its first commit attempt sees every version since its read, none a `Merge`. Segments committed with `commit_existing_index_segments` always lose it. - Grouping columns into files is left to #10509. ## How is it tested? | Test | What it pins | |---|---| | `repack_collapses_backfilled_files`, `repack_keeps_the_biggest_file` | backfilled files collapse in one commit, values and fragment ids unchanged; the layout over a limit of 2; a second run commits nothing | | `repack_keeps_deletions`, `repack_columns_preserves_data_overlay`, `repack_keeps_scalar_index`, `repack_columns_preserves_vector_index` | deleted rows keep their base values; overlays still shadow; BTree and IVF_PQ keep uuid and coverage and return the same rows | | `index_built_across_a_repack` | an index built before a repack keeps its coverage; a visible in-place update withdraws its fragment; a cleaned-up update or a `Merge` withdraws every repacked fragment | | `repack_against_concurrent_*`, `test_conflicts_data_replacement` | which concurrent changes a repack commits over and which make it retry | | `compaction_rewrites_and_repacks_in_one_run`, `commit_rejects_a_fragment_in_both_kinds_of_task`, `repack_commits_after_its_read_version_is_expired` | a mixed run commits `Rewrite` then `DataReplacement`; a fragment in both is refused; the repack still commits after its read version is expired | | `repack_never_adds_a_file`, `repack_merges_only_files_it_empties`, the lineage, blob, V1 and struct-header cases | the merge rules | | `compaction_plans_no_repack_by_default`, `repack_only_run_that_plans_nothing_is_refused`, `repack_is_not_planned_under_force_binary_copy`, `repack_counts_only_the_files_it_reads_against_the_byte_budget` | defaults, refusals and the byte budget | | `test_undrained_fri_allows_a_repack_only_plan`, `test_defer_index_remap_allows_a_repack_only_plan` | a repack-only plan isn't refused for pending remaps, with or without stable row ids, under `RepackColumns` or `All` | | `task_data_without_kind_rewrites_fragments`, `rewrite_result_without_repacked_files_is_a_rewrite`, `repack_tasks_run_distributed`, `testDeserializeTaskAndResultFromOlderVersion` (Java) | older tasks and results read as rewrites; repack tasks survive serde and commit together | | `test_data_replacement_data_change_roundtrips`, `test_data_replacement_applies_several_groups_to_one_fragment`, `test_data_replacement_data_change_round_trips` (Python) | `data_change` round-trips and only `true` withdraws coverage; several groups apply to one fragment | | `test_compact_files_repacks_columns`, `test_distributed_repack`, `test_repack_options_*`, `test_commit_leaves_dataset_at_the_rewrite_when_the_repack_fails` (Python), `testRepackColumns` (Java) | options, table config, task kind and layout stats through the bindings; the Python handle follows a landed rewrite | The repack task writes files the way @dshepelev15's #9291 did, and `Dataset::column_layout_stats` started in @Xuanwo's #8614. Thanks to both. Co-authored-by: dshepelev15 <[email protected]> Co-authored-by: Xuanwo <[email protected]>
What
CompactionOptions.column_groups(Pythoncompact_files(column_groups=[["emb"], ["cap", "tags"]]), dataset config keylance.compaction.column_groups="emb;cap,tags"): each listed group of top-level columns is written to its own data file per compacted fragment; every column not listed goes to one shared file. A single scan feeds one writer per group with the same planned row counts, so the files of a fragment stay row-aligned. Binary copy is disabled while groups are set (force_binary_copyreports NotSupported) andmax_bytes_per_fileis ignored so all groups split at the same rows. Unknown columns fail at planning, before tasks are distributed. The Java JNI builds its options throughfrom_dataset_config, so it needs no change and picks the groups up from the table config.Dataset::rewrite_columns(columns, data_storage_version)/FileFragment::rewrite_columns(PythonLanceDataset.rewrite_columns,LanceFragment.rewrite_columns): rewrite only the named top-level columns of every fragment into one new data file in the requested V2 version and tombstone them in the files they came from; a file left holding only tombstones is dropped. The other columns' files are neither read nor written, and rows, fragment ids, row addresses and indices are unchanged. Commits oneUpdateinRewriteColumnsmode with emptyfields_modified, since the values did not change. Fragments whose columns already sit alone in a file of the requested version are skipped, so an interrupted rewrite can be rerun; the fragment-level call returns metadata for a single distributed commit.Why
Compaction rewrites whole fragments, so moving a table to a newer data file version through compaction re-encodes every column, including the multi-KB embedding columns that dominate its size. It also folds any per-column file layout (from
merge_insertinrewrite_columnsmode oradd_columns) back into one wide file, which makes the next single-column update rewrite every column again. Together the two options let a table keep its wide columns in their own files across compactions and migrate its narrow columns without touching them.Notes
rewrite_columnsrejects blob columns for the same reason the merge-insertRewriteColumnspath does: the stored descriptor form differs from the logical form the writer expects. Nested paths are not accepted incolumn_groups.versions::validate_write_schemafactors out the blob checkswrite_fragmentsalready ran, andcreate_seed_writersbecomespub(super)so the grouped writer can seed indices too.dataset::optimize(column groups across compaction modes and target versions, config parsing, validation) anddataset::rewrite_columns(migration and layout, index coverage kept, bad input, compaction preserving the layout); Pythontest_optimize.pyandtest_schema_evolution.py, including the per-fragment commit path.