Skip to content

Fix coordination mode mismatch in parallel replicas with read-in-order optimization - #95077

Closed
matanper wants to merge 16 commits into
ClickHouse:masterfrom
matanper:fix-read-in-order-parallel-replicas
Closed

matanper wants to merge 16 commits into
ClickHouse:masterfrom
matanper:fix-read-in-order-parallel-replicas

Conversation

@matanper

@matanper matanper commented Jan 25, 2026 •

Copy link
Copy Markdown
Contributor

When using parallel_replicas_local_plan, different replicas could identify different columns as "fixed" (constant due to WHERE filtering) because each replica builds its own query plan independently. This caused replicas to compute different read directions, leading to coordination mode mismatch errors like "Replica N decided to read in WithOrder mode, not in ReverseOrder".

The fix uses AST-based fixed column detection (via getFixedSortingColumns) instead of DAG-based detection when in parallel replicas local plan mode. AST-based detection analyzes the original WHERE/PREWHERE clauses and produces deterministic results across all replicas.

The bug introduced in #94103

Changelog category (leave one):

  • Critical Bug Fix (crash, data loss, RBAC) or LOGICAL_ERROR

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

Fixed regression causing parallel replicas coordination failures with parallel_replicas_local_plan in read-in-order optimization.

@clickhouse-gh

clickhouse-gh Bot commented Jan 25, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ed8dfed]

Summary: ❌

job_name test_name status info comment
Integration tests (amd_asan, targeted) failure
test_keeper_http_control_readiness/test.py::test_http_readiness_partitioned_cluster[9-10] FAIL cidb
test_keeper_http_control_readiness/test.py::test_http_readiness_partitioned_cluster[4-10] FAIL cidb
Integration tests (amd_tsan, 4/6) failure
test_ttl_replicated/test.py::test_ttl_compatibility[node_left1-node_right1-1] FAIL cidb
Performance Comparison (amd_release, master_head, 1/6) failure
Check Results failure
Performance Comparison (amd_release, master_head, 2/6) failure
Check Results failure
Stateless tests (amd_binary, old analyzer, s3 storage, DatabaseReplicated, parallel) error

@clickhouse-gh clickhouse-gh Bot added pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! labels Jan 25, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member

The new test produces the same logical error.

@matanper

Copy link
Copy Markdown
Contributor Author

@alexey-milovidov seems to pass now

@clickhouse-gh clickhouse-gh Bot added the submodule changed At least one submodule changed in this PR. label Jan 25, 2026
…r optimization

When using parallel replicas with parallel_replicas_local_plan, different replicas
were computing different "fixed columns" due to non-deterministic DAG-based detection.
This caused coordination failures where one replica decided to read in ReverseOrder
while another chose WithOrder.

Solution:
- Add needsDeterministicFixedColumns() method to ReadFromMergeTree that returns true
  when both parallel replicas and parallel_replicas_local_plan are enabled
- Use AST-based fixed column detection (via getFixedSortingColumns) instead of
  DAG-based when deterministic detection is needed
- This ensures all replicas compute the same fixed columns and agree on read order
@matanper
matanper force-pushed the fix-read-in-order-parallel-replicas branch from d3c84c0 to 6478122 Compare January 25, 2026 18:14
…r optimization

When using parallel replicas with parallel_replicas_local_plan, different replicas
were computing different "fixed columns" due to non-deterministic DAG-based detection.
This caused coordination failures where one replica decided to read in ReverseOrder
while another chose WithOrder.

Solution:
- Add needsDeterministicFixedColumns() method to ReadFromMergeTree that returns true
  when both parallel replicas and parallel_replicas_local_plan are enabled
- Use AST-based fixed column detection (via getFixedSortingColumns) instead of
  DAG-based when deterministic detection is needed
- This ensures all replicas compute the same fixed columns and agree on read order
@matanper
matanper force-pushed the fix-read-in-order-parallel-replicas branch from e6106dd to 864b3c2 Compare January 25, 2026 21:02
…r optimization

Use AST-based fixed column detection for parallel replicas with local plan to ensure
deterministic read order decisions across all replicas. DAG-based detection can differ
between replicas due to non-deterministic DAG structures, causing coordination mode
mismatch errors (WithOrder vs ReverseOrder).

The fix uses getFixedSortingColumns() for AST-based detection when
is_parallel_reading_from_replicas && parallel_replicas_local_plan are both enabled,
while keeping DAG-based detection for normal queries to preserve full optimization.
Reverts the unintended submodule updates that were included in commit 864b3c2.
The following submodules have been restored to their previous commits:
- contrib/AMQP-CPP
- contrib/boost
- contrib/c-ares
- contrib/cctz
- contrib/chdig
- contrib/croaring
- contrib/libcpuid
- contrib/librdkafka
- contrib/llvm-project
- contrib/openssl
- Inline needsDeterministicFixedColumns() logic for better clarity
- Revert unnecessary code style changes (use idiomatic if declarations)
- Remove trailing whitespaces
- Remove duplicate include of Core/Settings.h

The functional behavior for parallel replicas coordination remains unchanged.
@matanper
matanper force-pushed the fix-read-in-order-parallel-replicas branch from 752d46a to e5a5ee3 Compare January 25, 2026 22:30
@alexey-milovidov alexey-milovidov removed the submodule changed At least one submodule changed in this PR. label Jan 25, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member

Add a changelog entry please, as in the template: https://github.com/ClickHouse/ClickHouse/blob/master/.github/PULL_REQUEST_TEMPLATE.md

@alexey-milovidov

Copy link
Copy Markdown
Member

The test fails: 03789_read_in_order_constant_column_direction.

@matanper
matanper force-pushed the fix-read-in-order-parallel-replicas branch 3 times, most recently from fbea28e to 6b48144 Compare January 26, 2026 10:28
ORDER BY (tenant, event_time);

-- Insert deterministic test data
INSERT INTO t_read_in_order_coordination SELECT 1, number FROM numbers(10);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The test contains way too less data. In most of the runs local replicas will handle all the data and remote replicas will have almost no chance to respond to the query execution (the query execution on remote replicas will be cancelled).

To make the probability of getting first response from a remote replica higher, I would suggest to set index_granularity = 1, and populate the table with more data.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Have a look if I added enough data

@matanper
matanper force-pushed the fix-read-in-order-parallel-replicas branch 2 times, most recently from 0d89513 to 897ab88 Compare January 26, 2026 13:30
This ensures plan stability with parallel replicas by enabling AST-based
fixed column detection, which works reliably regardless of DAG transformations.
@matanper
matanper force-pushed the fix-read-in-order-parallel-replicas branch from 897ab88 to 52c1b65 Compare January 26, 2026 13:36
The previous fix was checking if parallel replicas are actually active, but the
parallel_replicas_local_plan setting can affect query plan building even when
parallel replicas are not active. This caused DAG-based fixed column detection
to fail in normal queries when the setting was enabled via randomized settings.

Now use AST-based detection when:
- parallel replicas are active AND parallel_replicas_local_plan is enabled, OR
- running on initiator with parallel replicas capability

This fixes test_03789_read_in_order_constant_column_direction which expects
read-in-order optimization to work even with parallel_replicas_local_plan=1
in randomized settings.
Remove the parallel_replicas_local_plan setting check and always use AST-based
fixed column detection for any parallel replicas scenario. This provides:
- Simpler, more maintainable code
- Consistent behavior for both local_plan=0 and local_plan=1
- Fixed optimization for local_plan=0 (previously missed due to DAG issues)

The AST-based detection is more conservative but deterministic, which is what
we need for parallel replicas coordination.
The previous implementation only worked with the old analyzer (ASTSelectQuery).
When the new analyzer is used, query_info.query_tree contains a QueryNode instead.

Changes:
- Add overload of getFixedSortingColumns that takes condition AST directly
- Update isFixedColumnForReadInOrder to handle both analyzers:
  - New analyzer: extract WHERE/PREWHERE from QueryNode via toAST()
  - Old analyzer: extract from ASTSelectQuery
- Combine WHERE + PREWHERE using makeASTForLogicalAnd

This fixes the test failure when running with ParallelReplicas configuration
which uses the new analyzer.
The test requires optimize_read_in_order=1 to work. When the test runner
randomizes settings, it can set optimize_read_in_order=0, causing the test
to fail even though the SET statement should override it.

Adding the no-random-settings tag prevents any setting randomization for
this test.
Without local plan, read-in-order optimization is skipped for parallel
replicas (due to readingFromParallelReplicas check). Setting local_plan=1
ensures the optimization applies and the test works in ParallelReplicas
CI configuration.
@devcrafter devcrafter self-assigned this Jan 26, 2026
Refactor read-in-order optimization to use QueryTree-based detection
for fixed columns when parallel replicas are enabled. This ensures
deterministic results across replicas and correctly handles JOINs by
checking table aliases.

- Add getFixedColumnsFromQueryTree() helper that traverses QueryTree
  conditions and filters columns by table alias
- Update isFixedColumnForReadInOrder() to use QueryTree-based detection
  for parallel replicas, falling back to DAG-based for other cases
- Revert changes to ReadInOrderOptimizer (not needed with new approach)
- Remove no-random-settings tag and parallel_replicas_local_plan from test
@matanper

Copy link
Copy Markdown
Contributor Author

@devcrafter
Hi, I managed to get the optimization in 03789_read_in_order_constant_column_direction.sql work for parallel replicas with and without local plan.
The solution feels a bit complicated though, let me know what you think

@devcrafter

devcrafter commented Jan 27, 2026 •

Copy link
Copy Markdown
Member

I wasn't able to reproduce the error with the test provided here against master version. But I have reproduced it with query from this report #94103 (comment).

It's not related to in order optimization, but to aggregation in order. Trying to fix it in #95306 (at least the repo is already there). So, I'm not exactly sure what are you trying to fix within this change, - have you been able to reproduce the issue with the test?

@matanper

Copy link
Copy Markdown
Contributor Author

@devcrafter I didn't reproduce it personally, but @alexey-milovidov wrote the test in another PR so I trust it

@devcrafter

Copy link
Copy Markdown
Member

After some investigation, the conclusion is that the reason for #94103 (comment) is not #94103. I've created a separate issue, - #95524. So, I'll close this PR. You can reopen it if you think that still something need to be done.

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

Labels

can be tested Allows running workflows for external contributors pr-critical-bugfix 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.

3 participants