Skip to content

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

Closed
alexey-milovidov wants to merge 2 commits into
masterfrom
fix-parallel-replicas-read-in-order-mode-mismatch
Closed

alexey-milovidov wants to merge 2 commits into
masterfrom
fix-parallel-replicas-read-in-order-mode-mismatch

Conversation

@alexey-milovidov

Copy link
Copy Markdown
Member

PR #94103 introduced logic to skip "fixed columns" (columns that become constant after WHERE filtering) when determining the read direction for read-in-order optimization. For example, with WHERE tenant = 'A' ORDER BY tenant, time DESC, the tenant column is fixed, so the read direction should come from time DESC.

However, with parallel_replicas_local_plan, each replica builds its own query plan independently. Different replicas may have different fixed_columns sets due to variations in filter pushdown or other optimizations. This causes replicas to compute different read directions:

  • Replica 1: identifies column X as fixed, skips it, gets direction from column Y
  • Replica 2: doesn't identify X as fixed, gets direction from column X

When these directions differ (e.g., ASC vs DESC), replicas announce different coordination modes (WithOrder vs ReverseOrder), causing the coordinator to throw: "Replica N decided to read in WithOrder mode, not in ReverseOrder. This is a bug"

The fix: when parallel replicas are enabled, ignore fixed columns when determining read direction. This ensures all replicas use the same deterministic direction based on the first ORDER BY column, matching the behavior before PR #94103.

This is a conservative fix that disables the optimization only for parallel replicas, preserving the improved behavior for single-node queries.

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

See #94103 (comment)

alexey-milovidov and others added 2 commits January 24, 2026 20:36
…er optimization

PR #94103 introduced logic to skip "fixed columns" (columns that become constant
after WHERE filtering) when determining the read direction for read-in-order
optimization. For example, with `WHERE tenant = 'A' ORDER BY tenant, time DESC`,
the `tenant` column is fixed, so the read direction should come from `time DESC`.

However, with `parallel_replicas_local_plan`, each replica builds its own query
plan independently. Different replicas may have different `fixed_columns` sets
due to variations in filter pushdown or other optimizations. This causes
replicas to compute different read directions:

- Replica 1: identifies column X as fixed, skips it, gets direction from column Y
- Replica 2: doesn't identify X as fixed, gets direction from column X

When these directions differ (e.g., ASC vs DESC), replicas announce different
coordination modes (WithOrder vs ReverseOrder), causing the coordinator to throw:
"Replica N decided to read in WithOrder mode, not in ReverseOrder. This is a bug"

The fix: when parallel replicas are enabled, ignore fixed columns when determining
read direction. This ensures all replicas use the same deterministic direction
based on the first ORDER BY column, matching the behavior before PR #94103.

This is a conservative fix that disables the optimization only for parallel
replicas, preserving the improved behavior for single-node queries.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Test verifies that queries with:
- parallel_replicas_local_plan enabled
- A fixed column (constant due to WHERE)
- ORDER BY that includes the fixed column

do not cause coordination mode mismatch between replicas.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jan 24, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [758cae5]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_binary, old analyzer, s3 storage, DatabaseReplicated, parallel) failure
02435_rollback_cancelled_queries FAIL cidb
Stateless tests (amd_binary, ParallelReplicas, s3 storage, parallel) failure
03789_read_in_order_constant_column_direction FAIL cidb
02346_read_in_order_fixed_prefix FAIL cidb
BuzzHouse (amd_debug) failure
Logical error: 'Inconsistent AST formatting: the query: (STID: 1941-1bfa) FAIL cidb, issue
Integration tests (amd_binary, 4/5) error
test_multiple_disks/test.py::test_concurrent_alter_modify[mt] FAIL cidb
test_multiple_disks/test.py::test_concurrent_alter_modify[replicated] FAIL cidb
test_storage_s3_queue/test_6.py::test_ordered_mode_with_hive[AzureQueue-0-16-2] FAIL cidb
test_multiple_disks/test.py::test_simple_replication_and_moves FAIL cidb
test_multiple_disks/test.py::test_kill_while_insert FAIL cidb, issue
test_storage_s3_queue/test_6.py::test_ordered_mode_with_hive[AzureQueue-4-1-1] FAIL cidb
test_storage_s3_queue/test_6.py::test_ordered_mode_with_hive[AzureQueue-4-1-2] FAIL cidb
test_storage_s3_queue/test_6.py::test_ordered_mode_with_hive[AzureQueue-4-16-1] FAIL cidb
test_storage_s3_queue/test_6.py::test_ordered_mode_with_hive[AzureQueue-4-16-2] FAIL cidb
test_storage_s3_queue/test_6.py::test_ordered_mode_with_regex_partitioning[S3Queue-0-1-1] FAIL cidb
20 more test cases not shown

@alexey-milovidov

Copy link
Copy Markdown
Member Author

If we don't succeed with this fix, we will revert #94103.

@matanper

matanper commented Jan 25, 2026 •

Copy link
Copy Markdown
Contributor

Hi, if I may suggest another approach, maybe the fixed columns should be based on AST (at least in parallel replicas mode)

void enrichFixedColumns(
    const ActionsDAG & dag, 
    FixedColumns & fixed_columns,
    const ReadFromMergeTree * reading = nullptr,
    bool use_ast_based_detection = false)
{
    // For parallel replicas, use AST-based detection instead of DAG for deterministic ordering across all replicas
    if (use_ast_based_detection && reading)
    {
        fixed_columns.clear();  // Clear DAG-based results
        
        const auto & query_info = reading->getQueryInfo();
        const auto * select_query = query_info.query ? query_info.query->as<ASTSelectQuery>() : nullptr;
        
        if (select_query)
        {
            const auto & sorting_key_columns = reading->getStorageMetadata()->getSortingKeyColumns();
            auto fixed_names = getFixedSortingColumns(*select_query, sorting_key_columns, reading->getContext());
            
            // Convert column names to DAG INPUT nodes
            for (const auto & node : dag.getNodes())
            {
                if (node.type == ActionsDAG::ActionType::INPUT && fixed_names.contains(node.result_name))
                    fixed_columns.insert(&node);
            }
        }
    }

@alexey-milovidov

Copy link
Copy Markdown
Member Author

@matanper, let's try!
Although it could be just a temporary fix for bleeding CI before properly using query plans for parallel replicas.

Could you please send a PR with that fix? (then I will close this one)

@matanper

Copy link
Copy Markdown
Contributor

opened #95077

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants