Repository navigation
Fix order by issue in StorageMerge - #85025
Conversation
|
Workflow [PR], commit [a3cc9dc] Summary: ❌
|
|
Could you give some description about the cause and the fix? |
|
Hi @tuanpach, thanks for the reminder. I have supplemented the cause of the issue and the corresponding fix above. |
| QueryPlan plan; | ||
|
|
||
| if (processed_stage <= storage_stage) | ||
| if (processed_stage <= storage_stage && (!std::dynamic_pointer_cast<StorageMerge>(storage) || processed_stage == QueryProcessingStage::FetchColumns)) |
There was a problem hiding this comment.
| if (processed_stage <= storage_stage && (!std::dynamic_pointer_cast<StorageMerge>(storage) || processed_stage == QueryProcessingStage::FetchColumns)) | |
| /// Refer to https://github.com/ClickHouse/ClickHouse/pull/85025 | |
| bool must_rerun_interpreter_select_query_plan | |
| = processed_stage > QueryProcessingStage::FetchColumns && dynamic_cast<StorageMerge *>(storage.get()); | |
| if (processed_stage <= storage_stage && !must_rerun_interpreter_select_query_plan) |
I think it would fit more to your explanation
Therefore, for such cases—where the nested table engine is a Merge Table and the processed_stage is greater than FetchColumns—it is necessary to re-run the InterpreterSelectQueryAnalyzer to generate the query plan. And the query plan is the following, added a step of Sorting (Sorting for ORDER BY)
There was a problem hiding this comment.
@tuanpach Thanks for the feedback! Updated as suggested.
| select sum(e) from (select * from merged order by t limit 10) SETTINGS max_threads = 10; | ||
| select sum(e) from (select * from merged order by t limit 10) SETTINGS max_threads = 50; | ||
|
|
||
| CREATE TABLE t1 (key Int) ENGINE=MergeTree() ORDER BY key; |
There was a problem hiding this comment.
Should we separate this test into a different file?
It would be easier to troubleshoot.
It is very minor, feel free to ignore this suggestion.
There was a problem hiding this comment.
Done. Thanks for the feedback!
fix #84666
In the example, when a Merge Table is nested within another Merge Table, and the nested Merge Table is further nested with a Distributed Table, the current query plan is similar to the following, where an ORDER BY is missing in the nested Merge Table leading to incorrect sorting.
Therefore, for such cases—where the nested table engine is a Merge Table and the processed_stage is greater than FetchColumns—it is necessary to re-run the InterpreterSelectQueryAnalyzer to generate the query plan. And the query plan is the following, added a step of Sorting (Sorting for ORDER BY)
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix potential inaccurate sorting issues in the Merge table engine.
Documentation entry for user-facing changes