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

sql: CTE name resolution can't pass through VALUES or UNION clauses #22418

Closed
jordanlewis opened this issue Feb 6, 2018 · 13 comments
Closed
Assignees
Labels
A-sql-name-resolution SQL name resolution rules and CTEs. C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. docs-done docs-known-limitation
Milestone

Comments

@jordanlewis
Copy link
Member

WITH outermost(x) AS (
  SELECT 1
  UNION (WITH innermost as (SELECT 2)
         SELECT * FROM innermost
         UNION SELECT 3)
)
SELECT * FROM outermost ORDER BY 1;

expected:

 x 
---
 1
 2
 3
(3 rows)

actual:

relation "innermost" does not exist
@jordanlewis jordanlewis self-assigned this Feb 6, 2018
@jordanlewis jordanlewis added the A-sql-name-resolution SQL name resolution rules and CTEs. label Feb 6, 2018
@petermattis petermattis added this to the 2.0 milestone Feb 21, 2018
@jordanlewis jordanlewis modified the milestones: 2.0, 2.1 Feb 27, 2018
@jseldess
Copy link
Contributor

jseldess commented Apr 1, 2018

Documented as a known limitation in cockroachdb/docs#2823.

@jseldess
Copy link
Contributor

jseldess commented Apr 2, 2018

@jordanlewis, I'm trying to document this for 2.0, but I can't quite understand it. Does CTE name resolution can't pass through VALUES or UNION clauses mean CTEs can't be used VALUES or UNION clauses? Or is it more specific? Can you perhaps draft up a sentence or two for me?

@jseldess
Copy link
Contributor

jseldess commented Apr 2, 2018

@knz
Copy link
Contributor

knz commented Apr 2, 2018

No this is a different issue.

Jesse I can volunteer to document this known limitation, together with the other one you found yesterday.

@jseldess
Copy link
Contributor

jseldess commented Apr 2, 2018

Thanks, @knz! To save extra git work, please just add your writeup as a comment on this PR: cockroachdb/docs#2823

@knz
Copy link
Contributor

knz commented Apr 2, 2018

done

@jordanlewis jordanlewis added C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) and removed docs-done labels Apr 25, 2018
@knz knz moved this from Triage to Backlog in (DEPRECATED) SQL Front-end, Lang & Semantics May 3, 2018
@andreimatei
Copy link
Contributor

@knz, @jseldess the workaround documented in the "known limitation" doesn't work for me. It says:
https://www.cockroachlabs.com/docs/dev/common-table-expressions.html#using-ctes-with-set-operations

It is not yet possible to use a common table expression defined outside of a set expression in the right operand of a set operator, for example:

WITH a AS (SELECT 1)
SELECT * FROM users UNION SELECT * FROM a; -- "a" used on the right, not yet supported.

For UNION, you can work around this limitation by swapping the operands. For the other set operators, you can inline the definition of the CTE inside the right operand.

Swapping the operands results in the same error:

WITH a AS (SELECT 1)
  SELECT * FROM a UNION SELECT 2;

pq: relation "a" does not exist

@knz knz added C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. and removed C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) labels Aug 30, 2018
@knz knz modified the milestones: 2.1, 2.2 Aug 30, 2018
@sploiselle
Copy link
Contributor

@jordanlewis Can I get a quick blurb describing this known limitation w/r/t the impact to user experience? Ideally, we need it by Friday 10/26 for the 2.1 Known Limitations page. Posting it on this issue and/or pinging me would be great.

@knz
Copy link
Contributor

knz commented Oct 24, 2018

This has been fixed in the new opt code -- it's only a limitation in the heuristic planner.

"""
title: Using common table expressions in VALUES and UNION clauses

When the cost-based optimizer is disabled, or when it does not support a query, a common table expression defined outside of a VALUES or UNION clause will not be available inside it. For example WITH a AS (...) SELECT ... FROM (VALUES(SELECT * FROM a)). This limitation will be lifted when the cost-based optimizer covers all queries. Until then applications can work around this limitation by including the entire CTE query in the place where it is used.
"""

@jordanlewis
Copy link
Member Author

Closing due to fixed on CBO status.

@jseldess
Copy link
Contributor

@jordanlewis
Copy link
Member Author

Yes!

@jseldess
Copy link
Contributor

Thanks, @jordanlewis.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sql-name-resolution SQL name resolution rules and CTEs. C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. docs-done docs-known-limitation
Projects
No open projects
Development

No branches or pull requests

6 participants