Skip to content

Improve prewhere optimization for conditions like func(primary_column) = 'xx' and column in (xxx) - #85529

Merged
hanfei1991 merged 13 commits into
ClickHouse:masterfrom
bigo-sg:opt_prewhere
Aug 28, 2025
Merged

hanfei1991 merged 13 commits into
ClickHouse:masterfrom
bigo-sg:opt_prewhere

Conversation

@taiyang-li

@taiyang-li taiyang-li commented Aug 13, 2025 •

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • Performance Improvement

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

Improve prewhere optimization for conditions like func(primary_column) = 'xx' and column in (xxx)

For example:

  • When prewhere condition is like lower(country) = 'cn', and country is one of the primary index columns, we should not move the condition to the end of prewhere even if move_primary_key_columns_to_end_of_prewhere is enabled. Because lower(country) = 'cn' can't make use of primary index. We should decide its position in prewhere conditions according to compressed column sizes and estimated rows(if allow_statistics_optimize is enabled).
  • When prewhere condition is like value in ('x', 'y', 'z'), its estimated selectivity is always 1.0 because current process about in functions is not right in ConditionSelectivityEstimator::extractAtomFromTree. This PR fixed it.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

@clickhouse-gh

clickhouse-gh Bot commented Aug 13, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [96fc362]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_tsan, parallel, 2/2) failure
03094_grouparraysorted_memory FAIL
02406_try_read_datetime64_bug FAIL
Exception in test runner FAIL
Sanitizer assert (in stderr.log) FAIL
Killed by signal (in clickhouse-server.log or clickhouse-server.err.log) FAIL
Stateless tests (amd_debug, distributed plan, s3 storage, parallel) failure
02443_detach_attach_partition FAIL
Stateless tests (arm_binary, parallel) failure
01825_new_type_json_ghdata_insert_select FAIL
Integration tests (arm_binary, distributed plan, 1/4) failure
test_global_overcommit_tracker/test.py::test_global_overcommit FAIL
Stress test (amd_tsan) failure
Server died FAIL
Hung check failed, possible deadlock found (see hung_check.log) FAIL
Killed by signal (in clickhouse-server.log) FAIL
Fatal message in clickhouse-server.log (see fatal_messages.txt) FAIL
Killed by signal (output files) FAIL
Found signal in gdb.log FAIL

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Aug 13, 2025
@taiyang-li taiyang-li changed the title Improve prewhere optimization for conditions like func(primary_column) = 'xx' and func(column) in (xxx) Improve prewhere optimization for conditions like func(primary_column) = 'xx' and column in (xxx) Aug 13, 2025
@taiyang-li

taiyang-li commented Aug 18, 2025 •

Copy link
Copy Markdown
Contributor Author

@alexey-milovidov hope for your reviews, thanks very much!

@hanfei1991 hanfei1991 self-assigned this Aug 25, 2025
Comment thread src/Storages/MergeTree/MergeTreeWhereOptimizer.cpp Outdated
Comment thread src/Storages/MergeTree/MergeTreeWhereOptimizer.cpp Outdated
Comment thread src/Storages/Statistics/ConditionSelectivityEstimator.cpp
@hanfei1991

Copy link
Copy Markdown
Member

looks like __set_String_4653864928722945989_14468210011007111410 is not a stable name, that's why test failed, can we replace it with __set_String by sed command in test?

@taiyang-li

taiyang-li commented Aug 27, 2025 •

Copy link
Copy Markdown
Contributor Author

@hanfei1991 thanks for your reviews! Let's wait for next CI.

@hanfei1991
hanfei1991 added this pull request to the merge queue Aug 28, 2025
Merged via the queue into ClickHouse:master with commit 63d87bf Aug 28, 2025
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants