Skip to content

Fix LOGICAL_ERROR in RecursiveCTEChunkGenerator - #91827

Merged
pamarcos merged 4 commits into
ClickHouse:masterfrom
pamarcos:fix-logical-error-in-RecursiveCTEChunkGenerator
Dec 10, 2025
Merged

pamarcos merged 4 commits into
ClickHouse:masterfrom
pamarcos:fix-logical-error-in-RecursiveCTEChunkGenerator

Conversation

@pamarcos

@pamarcos pamarcos commented Dec 9, 2025 •

Copy link
Copy Markdown
Member

Root cause:

Given the SQL statement

WITH RECURSIVE q AS (
  SELECT 1 FROM t0 UNION ALL
    (WITH RECURSIVE x AS
      (SELECT 1 FROM t0 UNION ALL
        (SELECT 1 FROM q WHERE FALSE UNION ALL
         SELECT 1 FROM x WHERE FALSE))
       SELECT 1 FROM x))
  SELECT 1 FROM q;

In RecursiveCTEChunkGenerator, the function
collectTableNodesWithStorage() was searching for table nodes by storage ID. However, during execution of recursive CTEs, the updateStorage() method is called on table nodes (line 155), which changes their storage ID to point to new temporary tables. For nested recursive CTEs, when the inner CTE x was executed multiple times during the outer CTE q's recursion, the table nodes in x's tree had their storage IDs updated, but the RecursiveCTETable stored in the Un→

The fix:

Changed collectTableNodesWithStorage() to
collectTableNodesWithTemporaryTableName(), which searches for table nodes by their temporary table name instead of storage ID. The temporary table name (CTE name) is stable and doesn't change during execution, making it a reliable identifier for finding self-referencing table nodes in recursive CTEs.

Closes #85773

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):

Fix LOGICAL_ERROR in RecursiveCTEChunkGenerator

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Root cause:

In RecursiveCTEChunkGenerator, the function
collectTableNodesWithStorage() was searching for table nodes by storage ID.
However, during execution of recursive CTEs, the updateStorage() method is
called on table nodes (line 155), which changes their storage ID to point to new
temporary tables. For nested recursive CTEs, when the inner CTE x was executed
multiple times during the outer CTE q's recursion, the table nodes in x's tree
had their storage IDs updated, but the RecursiveCTETable stored in the Un→

The fix:

Changed collectTableNodesWithStorage() to
collectTableNodesWithTemporaryTableName(), which searches for table nodes by
their temporary table name instead of storage ID. The temporary table name (CTE
name) is stable and doesn't change during execution, making it a reliable
identifier for finding self-referencing table nodes in recursive CTEs.
@clickhouse-gh

clickhouse-gh Bot commented Dec 9, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [03b0818]

Summary: ❌

job_name test_name status info comment
Integration tests (amd_asan, db disk, old analyzer, 1/6) failure
test_storage_nats/test_nats_jet_stream.py::test_nats_overloaded_insert FAIL cidb, issue
Integration tests (amd_binary, 4/5) failure
test_merge_tree_s3/test.py::test_merge_canceled_by_s3_errors[node-broken_s3_always_multi_part] FAIL cidb, issue
Integration tests (amd_tsan, 1/6) failure
test_storage_nats/test_nats_jet_stream.py::test_nats_overloaded_insert FAIL cidb, issue
Integration tests (amd_tsan, 3/6) failure
test_restore_db_replica/test.py::test_query_after_restore_db_replica[alter table-with exists table-with restart] FAIL cidb, issue
test_restore_db_replica/test.py::test_restore_db_replica_with_diffrent_table_metadata[restore node1-node2] FAIL cidb, issue
test_restore_db_replica/test.py::test_restore_db_replica_with_diffrent_table_metadata[restore node2-node1] FAIL cidb, issue
test_restore_db_replica/test.py::test_failed_restore_db_replica_on_normal_replica FAIL cidb
test_restore_db_replica/test.py::test_restore_db_replica_on_cluster FAIL cidb
Integration tests (amd_tsan, 4/6) failure
test_keeper_snapshots_multinode/test.py::test_restart_multinode FAIL cidb
BuzzHouse (amd_debug) failure
Logical error: 'Inconsistent AST formatting: the query: FAIL cidb, issue
BuzzHouse (arm_asan) failure
Logical error: Distributed task iterator is not initialized FAIL cidb, issue

@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 Dec 9, 2025
@pamarcos
pamarcos marked this pull request as ready for review December 9, 2025 17:43
@pamarcos
pamarcos marked this pull request as draft December 9, 2025 19:03
@pamarcos
pamarcos marked this pull request as ready for review December 9, 2025 19:07
@yariks5s yariks5s self-assigned this Dec 9, 2025
@pamarcos
pamarcos added this pull request to the merge queue Dec 10, 2025
@pamarcos
pamarcos removed this pull request from the merge queue due to a manual request Dec 10, 2025
@pamarcos
pamarcos added this pull request to the merge queue Dec 10, 2025
@pamarcos

pamarcos commented Dec 10, 2025 •

Copy link
Copy Markdown
Member Author

@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Dec 10, 2025
clickhouse-gh Bot added a commit that referenced this pull request Dec 10, 2025
Backport #91827 to 25.11: Fix LOGICAL_ERROR in RecursiveCTEChunkGenerator
clickhouse-gh Bot added a commit that referenced this pull request Dec 10, 2025
Backport #91827 to 25.9: Fix LOGICAL_ERROR in RecursiveCTEChunkGenerator
pamarcos added a commit that referenced this pull request Dec 11, 2025
Backport #91827 to 25.10: Fix LOGICAL_ERROR in RecursiveCTEChunkGenerator
pamarcos added a commit that referenced this pull request Dec 11, 2025
Backport #91827 to 25.8: Fix LOGICAL_ERROR in RecursiveCTEChunkGenerator
pamarcos added a commit that referenced this pull request Dec 11, 2025
Backport #91827 to 25.3: Fix LOGICAL_ERROR in RecursiveCTEChunkGenerator
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-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! 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.

Fuzzer : assert in RecursiveCTEChunkGenerator::RecursiveCTEChunkGenerator

4 participants