Skip to content

Support prewhere in delta lake - #126099

Open
scanhex12 wants to merge 4 commits into
masterfrom
delta-lake-prewhere
Open

scanhex12 wants to merge 4 commits into
masterfrom
delta-lake-prewhere

Conversation

@scanhex12

@scanhex12 scanhex12 commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Performance Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Support prewhere in delta lake


Workflow [PR]
Sync PR [sync-upstream/pr/126099]

@clickhouse-gh clickhouse-gh Bot added the comp-datalake Data lake table formats (Iceberg/Delta/Hudi) integration. label Oct 15, 2026
# Conflicts:
#	src/Storages/ObjectStorage/StorageObjectStorage.cpp
@clickhouse-gh

clickhouse-gh Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [39509c5]

Summary: ❌

job_name test_name status info comment
Integration tests (amd_asan_ubsan, db disk, targeted) FAIL
test_storage_delta/test.py::test_snapshot_consistency FAIL cidb
Integration tests (arm_binary, distributed plan, targeted) FAIL
test_storage_delta/test.py::test_snapshot_consistency FAIL cidb
Integration tests (amd_tsan, targeted) FAIL
test_storage_delta/test.py::test_snapshot_consistency FAIL cidb
Stateless tests (amd_debug, sequential) FAIL
05339_iceberg_top_k_query_condition_cache FAIL cidb
Stateless tests (amd_debug, distributed plan, s3 storage, sequential) FAIL
05339_iceberg_top_k_query_condition_cache FAIL cidb
Stateless tests (arm_binary, sequential) FAIL
05339_iceberg_top_k_query_condition_cache FAIL cidb
Stateless tests (arm_asan_ubsan, azure, sequential, 2/2) FAIL
05339_iceberg_top_k_query_condition_cache FAIL cidb
Stateless tests (arm_binary, s3 storage, DBReplicated, sequential) FAIL
05339_iceberg_top_k_query_condition_cache FAIL cidb
Stateless tests (arm_binary, ParallelReplicas, s3 storage, sequential) FAIL
05339_iceberg_top_k_query_condition_cache FAIL cidb
Stateless tests (arm_binary, AsyncInsert, s3 storage, sequential) FAIL
05339_iceberg_top_k_query_condition_cache FAIL cidb
01168_mutations_isolation_3 NOT_FAILED cidb

AI Review

Summary

The PR enables PREWHERE for Delta Lake reads, excludes partition and mapped columns, and preserves requested subcolumns through format-header rewriting. Changes are still required: nested CDF settings bypass the new capability guard, and the claimed performance benefit lacks measurements.

Missing context / blind spots
  • ⚠️ Runtime verification of the nested-CDF case remains pending. The available PR fast-test binary omits Parquet and Delta Kernel; the attempted local checks could not exercise the changed behavior. A kernel-enabled regression run would close this gap.
Findings

❌ Blockers

  • StorageObjectStorage.cpp:474-475: CDF reads must not admit predicates their source never evaluates. supportsPrewhere checks the outer thread query context, but a nested query can set delta_lake_snapshot_start_version = 1 in its own context. That context constructs the table function and reaches the CDF branch of read, while the guard still sees the outer default -1. Consequently, SELECT * FROM (SELECT id, email FROM deltaLakeLocal('<ROOT>/cdf') PREWHERE length(email) = 4 SETTINGS delta_lake_snapshot_start_version = 1) admits PREWHERE, and the unchanged CDF source leaves nonmatching rows unfiltered. Derive the capability from the effective read context or execute the filter in the CDF source. Existing thread.

⚠️ Majors

  • 05368_delta_lake_prewhere.sh:25: The Performance Improvement claim remains unsupported. Result checks and an EXPLAIN marker establish placement of the filter, but no test or PR measurement shows reduced reading, decoding, or elapsed time. Existing thread.
Tests
  • ⚠️ Extend the CDF regression with an inner SETTINGS delta_lake_snapshot_start_version = 1 clause while the outer setting remains -1. Check explicit PREWHERE rejection and ordinary WHERE correctness without automatic movement into the unsupported source.
  • ⚠️ Compare optimize_move_to_prewhere = 0 and 1 on the same Delta fixture with a selective predicate and substantial payload in a file that statistics cannot eliminate. Report read/decoded bytes or repeatable timings.
Final Verdict

Status: ❌ Block. Close the nested-CDF guard gap, add its focused regression, and supply evidence for the performance claim.

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Oct 22, 2026
#endif
#if USE_PARQUET && USE_DELTA_KERNEL_RS
if constexpr (std::is_same_v<DataLakeMetadata, DeltaLakeMetadata>)
return true;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Disable PREWHERE for CDF until its source executes the filter

This also enables PREWHERE for Delta table functions with delta_lake_snapshot_start_version set. Those reads take the separate branch in StorageObjectStorage::read and use ReadFromDeltaLakeTableChangesStep: its updatePrewhereInfo only stores the DAG, and neither initializePipeline nor TableChanges::next evaluates it. Once the optimizer moves a whole WHERE condition, it removes the corresponding FilterStep, so the predicate is lost. The kernel's EnginePredicate only performs statistics/partition pruning; an expression such as length(email) = 7 is not translated at all.

For example, a CDF query selecting id, email with PREWHERE length(email) = 7 returns nonmatching rows from a file containing different email lengths. Ordinary WHERE queries can also regress when automatically moved. Please disable both explicit and automatic PREWHERE in CDF mode, or apply the filter to the Arrow chunks, and add a CDF regression with matching and nonmatching rows in the same file.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

39509c58 handles the top-level case, but src/Storages/ObjectStorage/StorageObjectStorage.cpp:474-475 still checks the outer thread query context. A nested query's SETTINGS are applied to a separate context (QueryTreeBuilder.cpp:457-466), which is also used to execute its table function (QueryAnalyzer.cpp:5791-5800). The read therefore enters CDF mode while this guard still sees delta_lake_snapshot_start_version = -1 and admits PREWHERE.

Using the new test's CDF fixture, with the outer setting left at its default:

SELECT *
FROM
(
    SELECT id, email
    FROM deltaLakeLocal('<ROOT>/cdf')
    PREWHERE length(email) = 4
    SETTINGS delta_lake_snapshot_start_version = 1
);

This must return only IDs 0–9. The current CDF source still never evaluates the admitted predicate, so the source trace leaves IDs 10–19 unfiltered too. Please derive the capability from the effective read context (for example, retain the CDF mode on the table-function storage), and cover nested queries with both explicit and automatically moved PREWHERE. Runtime confirmation needs a kernel-enabled build; the available fast-test binary omits these formats.

SELECT 'prewhere output', id, length(email) AS l FROM t_unpart PREWHERE length(email) = 7 ORDER BY id LIMIT 3;
SELECT 'prewhere count', count() FROM t_unpart PREWHERE email LIKE 'u1%';
SELECT 'prewhere virtual', _file != '', id FROM t_unpart PREWHERE email = 'u7@x';
SELECT 'moved to prewhere', trim(explain) FROM (EXPLAIN actions = 1 SELECT id FROM t_unpart WHERE email = 'u1500@x') WHERE explain LIKE '%Prewhere filter column%';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Add evidence for the claimed performance improvement

The new tests check result equality and an EXPLAIN marker, but do not show that enabling PREWHERE actually saves read or decoding work for Delta tables. There are no before/after measurements in the PR description either. Since the selected changelog category is Performance Improvement, please provide a focused comparison with optimize_move_to_prewhere = 0 and 1 on the same Delta fixture, using a selective predicate and a substantial payload that cannot be eliminated by file statistics. Measured read/decoded bytes or a repeatable timing benchmark would establish the claimed benefit.

`updateFormatPrewhereInfo` rebuilt `requested_columns` and `source_header` from the
physical format header, so a subcolumn read through its storage column (`arr.size0`
read as `arr`, `n.null` read as `n`) disappeared, and queries failed with
`NOT_FOUND_COLUMN_IN_BLOCK`. This also affected plain `Parquet` files on master. Keep
the requested columns as they were requested instead.

`ReadFromDeltaLakeTableChangesStep` applies neither `PREWHERE` nor the row-level
filter, so report no `PREWHERE` support when `delta_lake_snapshot_start_version` is set.

Addresses review comments in #126099

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 39509c581 with master 71b8c26a6 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of abbcee3cf; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

25 object files changed (+93.07 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Storages/ObjectStorage/registerStorageObjectStorage.cpp.o 1.50 MiB 1.52 MiB +19.04 KiB (+1.24%)
src/TableFunctions/CMakeFiles/clickhouse_table_functions.dir/TableFunctionObjectStorage.cpp.o 2.58 MiB 2.60 MiB +18.51 KiB (+0.70%)
src/CMakeFiles/dbms.dir/Databases/DataLake/DatabaseDataLake.cpp.o 1.65 MiB 1.66 MiB +17.41 KiB (+1.03%)

719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

123 translation units recompiled, 1281 s compile time in total, 122 of them have a recent master baseline.

Median compile-time ratio to the baselines is ×1.08 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
The matched translation units cost +73.6 s (+6%) in total before that adjustment.

Job report

@clickhouse-gh clickhouse-gh Bot added comp-delta-lake Delta Lake tables: reading Delta metadata through delta-kernel-rs (table snapshots, schema and ty... and removed comp-datalake Data lake table formats (Iceberg/Delta/Hudi) integration. labels Nov 19, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-delta-lake Delta Lake tables: reading Delta metadata through delta-kernel-rs (table snapshots, schema and ty... pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant