Skip to content

Fix condition not being moved to PREWHERE in case there is a row policy (version 2)#87303

Merged
novikd merged 22 commits intomasterfrom
row_policy_prewhere_with_optimize_2
Oct 2, 2025
Merged

Fix condition not being moved to PREWHERE in case there is a row policy (version 2)#87303
novikd merged 22 commits intomasterfrom
row_policy_prewhere_with_optimize_2

Conversation

@KochetovNicolai
Copy link
Member

@KochetovNicolai KochetovNicolai commented Sep 18, 2025

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fixed move-to-prewhere optimization, which did not work in the presence of row policy.
Continuation of #85118.
Closes #69777.
Closes #83748.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

@clickhouse-gh
Copy link
Contributor

clickhouse-gh bot commented Sep 18, 2025

Workflow [PR], commit [9871383]

Summary:

@clickhouse-gh clickhouse-gh bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 18, 2025
@novikd novikd self-assigned this Sep 19, 2025
@KochetovNicolai KochetovNicolai marked this pull request as ready for review September 21, 2025 11:03
@mkmkme
Copy link
Contributor

mkmkme commented Sep 22, 2025

Hey @KochetovNicolai!

Great to see this issue being fixed. Will this PR also address #85834 ?

@mkmkme
Copy link
Contributor

mkmkme commented Sep 22, 2025

Will this PR also address #85834 ?

Answering myself: Yes, it seems this PR fixes that as well.

* Only row policy can end up not in WHERE section
* Row-policy filter is never modified: No need to update it's value in
  the SourceStepWithFilter::updatePrewhereInfo
It was just a copy from SelectQueryInfo and it was error-prone because
it's easy to forget to update it's value.
Comment on lines +622 to +626
if (prewhere_info)
{
for (const auto * input : filter_info.actions.getInputs())
prewhere_info->prewhere_actions.tryRestoreColumn(input->result_name);
}
Copy link
Member

Choose a reason for hiding this comment

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

I still don't get why is it needed

Copy link
Member Author

Choose a reason for hiding this comment

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

This is because additional filters may need a column which can be removed by prewhere.

Simply saying, we don't track dependency for additional filters DAG. And I don't want to, it will make the code more complicated.

There is a test which I had to fix, so added this hack.

@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 7, 2025
rienath added a commit that referenced this pull request Oct 8, 2025
Cherry pick #87303 to 25.9: Fix condition not being moved to PREWHERE in case there is a row policy (version 2)
robot-clickhouse added a commit that referenced this pull request Oct 8, 2025
clickhouse-gh bot added a commit that referenced this pull request Oct 9, 2025
Backport #87303 to 25.9: Fix condition not being moved to PREWHERE in case there is a row policy (version 2)
@rzilkha
Copy link

rzilkha commented Oct 12, 2025

Thank you for this fix , can someone say if this is going to be backported also to older versions , such as 24.8 , where it seems to happen as well

@rienath
Copy link
Member

rienath commented Oct 12, 2025 via email

mkmkme added a commit to Altinity/ClickHouse that referenced this pull request Oct 15, 2025
This commit is a manual backport of two PRs:
  1. ClickHouse#87303
  2. ClickHouse#88017

Additionally, the test from ClickHouse#88036
was added.
zvonand added a commit to Altinity/ClickHouse that referenced this pull request Oct 16, 2025
24.8.14 Backport of ClickHouse#87303 - Fix condition not being moved to PREWHERE in case there is a row policy (version 2)
@rienath
Copy link
Member

rienath commented Oct 17, 2025

@rzilkha we decided against it. This is a performance improvement that touches a lot of files. Such large backport can introduce problems in a stable release

@robot-clickhouse robot-clickhouse added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 17, 2025
mkmkme added a commit to Altinity/ClickHouse that referenced this pull request Nov 7, 2025
This commit is a manual backport of two PRs:
    1. ClickHouse#87303
    2. ClickHouse#88017

Additionally, the test from ClickHouse#88036
was added.
Also `02679_explain_merge_tree_prewhere_row_policy` was fixed as per
0aed477
zvonand added a commit to Altinity/ClickHouse that referenced this pull request Nov 10, 2025
25.3.8 Backport of ClickHouse#87303 - Fix condition not being moved to PREWHERE in case there is a row policy (version 2)
zvonand pushed a commit to Altinity/ClickHouse that referenced this pull request Dec 24, 2025
…re_with_optimize_2

Fix condition not being moved to PREWHERE in case there is a row policy (version 2)
zvonand pushed a commit to Altinity/ClickHouse that referenced this pull request Dec 26, 2025
…re_with_optimize_2

Fix condition not being moved to PREWHERE in case there is a row policy (version 2)
zvonand pushed a commit to Altinity/ClickHouse that referenced this pull request Jan 15, 2026
Backport ClickHouse#87303 to 25.9: Fix condition not being moved to PREWHERE in case there is a row policy (version 2)
zvonand pushed a commit to Altinity/ClickHouse that referenced this pull request Jan 15, 2026
zvonand pushed a commit to Altinity/ClickHouse that referenced this pull request Jan 27, 2026
Backport ClickHouse#87303 to 25.9: Fix condition not being moved to PREWHERE in case there is a row policy (version 2)
zvonand pushed a commit to Altinity/ClickHouse that referenced this pull request Jan 27, 2026
zvonand added a commit to Altinity/ClickHouse that referenced this pull request Feb 2, 2026
25.8.15 Backport of ClickHouse#87303 - Fix condition not being moved to PREWHERE in case there is a row policy (version 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-backports-created-cloud deprecated label, NOOP pr-bugfix Pull request with bugfix, not backported by default pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR 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.

Analyzer: row policy may disable optimize_move_to_prewhere Conditions isn't moved to PREWHERE if a row policy presents

9 participants