Skip to content

sql: validate recursive CTE references in subqueries and outer joins - #174792

Open
shashi1973 wants to merge 1 commit into
cockroachdb:masterfrom
shashi1973:feature/pgcompat-recursive-cte-136623
Open

sql: validate recursive CTE references in subqueries and outer joins#174792
shashi1973 wants to merge 1 commit into
cockroachdb:masterfrom
shashi1973:feature/pgcompat-recursive-cte-136623

Conversation

@shashi1973

Copy link
Copy Markdown

Fixes: #136623

This change adds PostgreSQL-compatible validation for recursive CTE
references in contexts where PostgreSQL does not allow them.

Specifically, it rejects recursive references that occur:

  • inside a subquery
  • on the null-extended side of a LEFT, RIGHT, or FULL OUTER JOIN

The validation tracks the subquery and outer-join context relative to
the start of the recursive term. This avoids rejecting a recursive CTE
merely because the CTE itself appears inside an enclosing subquery.

Tests cover:

  • recursive references inside subqueries
  • recursive references on the nullable side of LEFT JOIN
  • recursive references on the nullable side of RIGHT JOIN
  • recursive references on the nullable sides of FULL JOIN
  • a positive case verifying that a recursive CTE inside an enclosing
    subquery remains valid when the recursive reference itself is not
    inside another subquery

Testing:

./dev test pkg/sql/opt/optbuilder --filter=TestBuilder

./dev generate

./dev lint --short was also run. The lint run was blocked by the
existing CODEOWNERS check reporting stale paths **.pb.go and
**.pb.gw.go; no lint failure was reported for the files changed by
this PR.

Release note (bug fix): Recursive CTE references in subqueries and on
the null-extended side of outer joins are now rejected to match
PostgreSQL behavior.

Fixes: cockroachdb#136623

Release note (bug fix): Recursive CTE references in subqueries and on
the null-extended side of outer joins are now rejected to match
PostgreSQL behavior.
@shashi1973
shashi1973 requested a review from a team as a code owner September 6, 2026 20:40
@shashi1973
shashi1973 requested review from ZhouXing19 and removed request for a team September 6, 2026 20:40
@blathers-crl

blathers-crl Bot commented Sep 6, 2026

Copy link
Copy Markdown

Thank you for contributing to CockroachDB. Please ensure you have followed the guidelines for creating a PR.

My owl senses detect your PR is good for review. Please keep an eye out for any test failures in CI.

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@blathers-crl blathers-crl Bot added the O-community Originated from the community label Sep 6, 2026
@cockroachlabs-cla-agent

cockroachlabs-cla-agent Bot commented Sep 6, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

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

Labels

O-community Originated from the community

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sql: missing validation for recursive CTEs

1 participant