Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix broken SET reuse during projection analysis. #35631

Merged
merged 4 commits into from
Apr 21, 2022

Conversation

amosbird
Copy link
Collaborator

Changelog category (leave one):

  • Bug Fix (user-visible misbehaviour in official stable or prestable release)

Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Fix projection analysis which might lead to wrong query result when IN subquery is used. This fixes #35336

Information about CI checks: https://clickhouse.tech/docs/en/development/continuous-integration/

@novikd
Copy link
Member

novikd commented Mar 29, 2022

@Mergifyio update

@mergify
Copy link
Contributor

mergify bot commented Mar 29, 2022

update

✅ Branch has been successfully updated

src/Interpreters/InterpreterSelectQuery.cpp Outdated Show resolved Hide resolved
@@ -205,6 +208,7 @@ class InterpreterSelectQuery : public IInterpreterUnionOrSelectQuery
StorageSnapshotPtr storage_snapshot;

/// Reuse already built sets for multiple passes of analysis, possibly across interpreters.
std::shared_ptr<SubqueriesForSets> subquery_for_sets;
Copy link
Member

Choose a reason for hiding this comment

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

You don't share ownership over subquery_for_sets so there is no need to use std::shared_ptr.
You can just store SubqueriesForSets as well as prepared_sets or if you want to store it in a separate memory use std::unique_ptr.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The reason to use std::shared_ptr is somehow hacky: to make it copyable.

Copy link
Member

@novikd novikd Mar 29, 2022

Choose a reason for hiding this comment

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

I understand but I don't see where it's used for the creation of the copy.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's because we need SelectQueryInfo to be copyable.

https://github.com/ClickHouse/ClickHouse/blob/master/src/Storages/StorageMerge.cpp#L218

I'll use a different implementation.

@@ -551,8 +550,8 @@ InterpreterSelectQuery::InterpreterSelectQuery(
LOG_TRACE(log, "Running 'analyze' second time");

/// Reuse already built sets for multiple passes of analysis
subquery_for_sets = std::move(query_analyzer->getSubqueriesForSets());
prepared_sets = query_info.sets.empty() ? query_analyzer->getPreparedSets() : query_info.sets;
subquery_for_sets = std::make_shared<SubqueriesForSets>(std::move(query_analyzer->getSubqueriesForSets()));
Copy link
Member

Choose a reason for hiding this comment

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

subquery_for_sets is always initialized, so I'd prefer to use here move-assignment without new memory allocation.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'll add a comment why shared_ptr is used.

query_info.sets = select.getQueryAnalyzer()->getPreparedSets();
query_info.subquery_for_sets = std::make_shared<SubqueriesForSets>(std::move(select.getQueryAnalyzer()->getSubqueriesForSets()));
Copy link
Member

Choose a reason for hiding this comment

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

The same applied here

@amosbird amosbird force-pushed the projection-fix1 branch 2 times, most recently from 141f92d to e9dc26d Compare March 30, 2022 06:13
@robot-ch-test-poll robot-ch-test-poll added the pr-bugfix Pull request with bugfix, not backported by default label Apr 7, 2022
@novikd
Copy link
Member

novikd commented Apr 12, 2022

@Mergifyio update

@mergify
Copy link
Contributor

mergify bot commented Apr 12, 2022

update

✅ Branch has been successfully updated

@novikd novikd added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Apr 21, 2022
@novikd novikd merged commit 77a82cc into ClickHouse:master Apr 21, 2022
alexey-milovidov added a commit that referenced this pull request Jun 20, 2022
Manual backport #35631 to 22.3: Fix broken SET reuse during projection analysis.
@Felixoid Felixoid added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Jul 14, 2022
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-bugfix Pull request with bugfix, not backported by default pr-must-backport Pull request should be backported intentionally. Use this label with great care!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Result of a column using if and a subquery changing when using WHERE 1=1 as a filter
4 participants