Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CTE with RECURSIVE and UNION ALL #1912

Closed
oliverpool opened this issue Oct 26, 2022 · 4 comments
Closed

CTE with RECURSIVE and UNION ALL #1912

oliverpool opened this issue Oct 26, 2022 · 4 comments

Comments

@oliverpool
Copy link
Contributor

oliverpool commented Oct 26, 2022

Version

1.15.0

What happened?

I try to generate go code with a recursive CTE, which uses UNION ALL and it fails.

My query is quite complex, but here is a simple reproduction:

Relevant log output

query.sql:7:8: column "n" does not exist

Database schema

None

SQL queries

-- name: CTERecursive :many
WITH RECURSIVE t(n) AS (
    VALUES (1)
  UNION ALL
    SELECT n+1 FROM t WHERE n < 100
)
SELECT n FROM t;

Configuration

{
  "version": "1",
  "packages": [
    {
      "path": "go",
      "name": "querytest",
      "schema": "query.sql",
      "queries": "query.sql"
    }
  ]
}

Playground URL

https://play.sqlc.dev/p/5c0c86493af9912a8d54ab083a92edcd4105ea8baf30681e1abe761a0cce1246

What operating system are you using?

Linux

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

@oliverpool oliverpool added bug Something isn't working triage New issues that hasn't been reviewed labels Oct 26, 2022
@oliverpool
Copy link
Contributor Author

Note that if I replace SELECT n with SELECT sum(n) everything works 🤔

-- name: CTERecursive :many
WITH RECURSIVE t(n) AS (
    VALUES (1)
  UNION ALL
    SELECT n+1 FROM t WHERE n < 100
)
SELECT sum(n) FROM t;

https://play.sqlc.dev/p/5c0380fceda01ba464e7e65219c9c5bb7b35e8047793a6b01d97aced8e2e21bd

@oliverpool
Copy link
Contributor Author

I have a branch of sqlc with this exact test case. I can open a PR (however I have no idea where to look to fix this).

@kyleconroy kyleconroy added 📚 postgresql 💻 linux 🔧 golang and removed triage New issues that hasn't been reviewed labels Nov 9, 2022
@oliverpool
Copy link
Contributor Author

@kyleconroy is there anything I can do to help debug/fix this issue ? (I know go, but don't know where to start debugging such an issue)

@kyleconroy
Copy link
Collaborator

This is fixed in v1.23.0 by enabling the database-backed query analyzer. You can play around with the working example on the playground

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

No branches or pull requests

2 participants