feat(scan): deferred materialization for heavy columns#175
Merged
Xuanwo merged 2 commits intolance-format:mainfrom Mar 18, 2026
Merged
feat(scan): deferred materialization for heavy columns#175Xuanwo merged 2 commits intolance-format:mainfrom
Xuanwo merged 2 commits intolance-format:mainfrom
Conversation
…shdown fails When DuckDB's filter cannot be pushed down to Lance SDK (unsupported functions, CAST+LIKE, join predicates, etc.), Lance reads all columns eagerly since it has no filter to optimize around. This wastes I/O on heavy columns (BLOBs, large vectors) for rows that DuckDB will filter out post-scan. This adds C++-side deferred materialization for this specific gap: 1. During scan init, detect heavy columns via `bytes_on_disk` stats (>1KB/row threshold) with type-based fallback (BLOB, LIST of numeric/BLOB, ARRAY>=256, MAP, recursive STRUCT). 2. Remove heavy columns from the Lance scan projection, add `_rowid`. 3. After DuckDB applies its post-scan filter, call `lance_create_dataset_take_stream_unfiltered` with surviving row IDs to fetch only the heavy columns for surviving rows. 4. Merge deferred columns into the output. The optimization only activates when: - Filter pushdown to Lance failed (`!filter_pushed_down`) - DuckDB has pending table filters - Heavy columns exist in the projection but not in the filter Controlled by: `SET lance_deferred_materialization = true|false` (default: true). Benchmark (20K rows, 5 fragments, 100KB blob/row, ~2GB): - Filter 3% selectivity: 555ms → 53ms (10.5x faster) - Filter 10% selectivity: 558ms → 162ms (3.4x faster) Note: When filter IS pushed to Lance, Lance SDK's internal MaterializationStyle::Heuristic already handles late materialization (LanceRead → TakeExec), so this optimization correctly does not activate in that case. Also adds: - Rust FFI: `lance_dataset_list_named_field_stats` for stats-based heavy column detection (resolves field IDs to column names) - Test: `scan_deferred_materialization.test` with plan verification, correctness, setting toggle, and multi-fragment coverage Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
MaterializationStyle::Heuristicalready handles late materialization — this optimization correctly does not activatelance_dataset_list_named_field_statsfor stats-based heavy column detectionSET lance_deferred_materialization = true|false(default: true)How it works
bytes_on_diskstats (>1KB/row threshold) with type-based fallback (BLOB, LIST, ARRAY≥256, MAP)_rowidfrom Lance (without heavy cols)take_rows(surviving_rowids, heavy_cols)fetches heavy data only for surviving rowsBenchmark
20K rows, 5 fragments, 100KB blob/row, ~2GB on disk, non-pushable filter (
id::VARCHAR LIKE '%42%'):Test plan
test/sql/scan_deferred_materialization.test(37 assertions)SET lance_deferred_materialization = falsedisables itCloses #170
🤖 Generated with Claude Code