Skip to content

Add test: Recursive CTE whose seed term is itself a UNION (resolveUnion at QueryAnalyzer.cpp:6374) is untested - #110193

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
clickgapai:qa-bot/coverage-pr109863
Jul 18, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
clickgapai:qa-bot/coverage-pr109863

Conversation

@clickgapai

Copy link
Copy Markdown
Contributor

Found via ClickGap automated review. Please close or comment if this is incorrect or needs adjustment.

This is a test-only PR — no source code changes. Please review: test quality, whether the claimed coverage gaps are real, and whether test output makes sense.

Adds test coverage for 1 untested code path(s), found during automated review of PR #109863.
That PR The PR adds two correlation guards to QueryAnalyzer::resolveUnion for recursive CTEs (WITH RECURSIVE): (1) after resolving the seed/non-recursive term it throws UNSUPPORTED_METHOD if the seed isCorrelated; (2) after resolving each recursive term it throws UNSUPPORTED_METHOD if that term `i

1. Recursive CTE whose seed term is itself a UNION (resolveUnion at QueryAnalyzer.cpp:6374) is untested
src/Analyzer/Resolve/QueryAnalyzer.cpp:6374
Risk: In resolveUnion, when non_recursive_query->getNodeType() is UNION rather than QUERY, resolution takes the else branch at line 6374 and later computes the temporary-table columns via non_recursive_query->as<UnionNode &>().computeProjectionColumns() (6391). Risk: if this seed-union branch or its projection/supertype computation is broken, recursive CTEs with a UNION base case would resolve to wrong column types or wrong row results (silent wrong results) instead of the correct fixed-point.
What's unique vs PR tests: The PR's 04401 test only uses scalar/single-SELECT seed terms and exercises the correlation guards; it never uses a multi-operand UNION as the recursive-CTE seed, so the resolveUnion seed branch at 6374 and the union-seed computeProjectionColumns type inference remain uncovered.
Try it on ClickHouse Fiddle

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

Not applicable — test-only change.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

@clickgapai

Copy link
Copy Markdown
Contributor Author

Pre-PR Validation Gate (per ClickGap pipeline):

a) Deterministic repro? ✅
Test exercises the previously-uncovered branch and passes (correct behaviour).

b) Root cause explained? ✅
src/Analyzer/Resolve/QueryAnalyzer.cpp:6374 resolveUnion(non_recursive_query, non_recursive_subquery_scope); — the seed-is-UNION branch — has zero test coverage (LLVM-confirmed uncovered); all existing recursive-CTE tests use a QUERY-node seed.

c) Fix matches root cause? ✅
N/A for coverage gap — this PR adds a regression test, not a fix. Asserts current correct behaviour at the cited line.

d) Test intent preserved? / New tests added? ✅
New regression test added: 04516_recursive_cte_union_seed.sql.

e) Both directions demonstrated? ✅
Test passes (MATCH) on master proving current correctness; would fail if the cited branch regressed. Single-direction is the correct shape for a coverage gap test.

f) Fix is general, not a narrow patch? ✅
Test asserts the invariant at the cited line — covers the branch, not a specific input shape.

@groeneai

Copy link
Copy Markdown
Collaborator

Reviewed. LGTM.

Verified against master (build 85c4835):

  • Coverage gap is real. The seed-is-UNION path in resolveUnion (the else resolveUnion(non_recursive_query, ...) branch and the computeProjectionColumns() seed columns, QueryAnalyzer.cpp:6301/6305) fires only when the recursive-CTE seed term is a UNION node. A scan of every existing *recursive_cte* test confirms none uses a parenthesized (SELECT ... UNION ... SELECT) seed; all use a single QUERY-node seed. 04401 only exercises the correlation guards, not this branch.
  • Both cases pass and the output matches the reference (19 7 / 306 4), traced by hand. The second case has a heterogeneous seed union (toUInt8(1) / toUInt64(300)), so it also exercises supertype inference over the union seed columns, which is the specific risk the gap describes.
  • Parallel-safe: fully inline subqueries, no tables/files, only session-scoped SET enable_analyzer = 1. No no-parallel needed.

I do not have write access to apply labels on this repo (fork contributor). Leaving the can be tested label for a maintainer.

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Jul 13, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [56a3206]

Summary: ✅


AI Review

Summary

This PR adds a stateless regression test for recursive CTEs whose seed term is itself a UNION, including a heterogeneous seed case that now asserts the inferred output type directly with toTypeName. I checked the current QueryAnalyzer::resolveUnion path, reviewed the prior discussion, and compared adjacent recursive-CTE tests; the claimed gap still looks real, and the earlier concern about recursive widening masking a bad seed-union type inference is fixed in the current head.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Jul 13, 2026
@clickgapai

Copy link
Copy Markdown
Contributor Author

Reworked the heterogeneous seed-union case so the inferred supertype is observed directly instead of through a masked sum.

  • 04516_recursive_cte_union_seed.sql:16-29: replaced SELECT sum(n), count() over SELECT n + 1 FROM t WHERE n < 3 with SELECT toTypeName(n), n over a type-preserving recursive arm (SELECT n FROM t WHERE n < 0, no rows). The seed (toUInt8(1) UNION DISTINCT toUInt64(300)) now reports its resolved element type per row. If the seed union were mis-inferred (e.g. UInt8/UInt16/UInt32), toTypeName would print the wrong type — no longer hidden by the widening arm, which was your point about 306 4 staying stable.
  • 04516_recursive_cte_union_seed.reference:2-3: 306 4 → UInt64 1 / UInt64 300.

The seed-is-UNION branch at QueryAnalyzer.cpp:6374 still executes — it resolves the non-recursive seed union at analysis time, independent of how many rows the recursive arm yields — so branch coverage is unchanged; the type contract is now directly asserted.

Verification: ran the SQL against the local server (clickhouse client --port 19000, exit 0) and diffed stdout against the updated reference — clean (diff exit 0). The full clickhouse-test harness hung on port discovery in this sandbox, so verification was the direct client+diff path rather than the wrapper.


ClickGapAI · Confidence: HIGH

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jul 18, 2026
@alexey-milovidov alexey-milovidov self-assigned this Jul 18, 2026
Merged via the queue into ClickHouse:master with commit b841210 Jul 18, 2026
175 checks passed
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 18, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…pr109863

Add test: Recursive CTE whose seed term is itself a UNION (`resolveUnion` at QueryAnalyzer.cpp:6374) is untested
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-not-for-changelog This PR should not be mentioned in the changelog 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.

4 participants