-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
38670: opt: add WITH operator r=justinj a=justinj This commit introduces a With operator to opt, which allows us to reference CTEs multiple times. This enables some missing functionality, where we can reference CTEs multiple times, or not at all, even if they contain mutations. We lose some optimizations because CTEs now present an optimization fence. This might be fixable with a rule something like: ``` (With $value:* $input:(WithRef) & (References $input $value) ) => $value ``` subject to certain side-effect restrictions, along with some rule props to get the Withs down further in the tree. Additionally, as they are now, WithExprs present another optimization fence, since we don't have any rules for pushing them further down the tree. Ideally, we would get all WithExprs that *can* be inlined down to their point of use, and all those that cannot up to the very root of the tree. This is future work. Fixes #24307. Fixes #21084. Release note (sql change): Common Table Expressions (CTEs) may now be referenced from multiple locations in a query. Co-authored-by: Justin Jaffray <[email protected]>
- Loading branch information
Showing
37 changed files
with
1,929 additions
and
583 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# LogicTest: local fakedist | ||
|
||
statement ok | ||
CREATE TABLE x(a) AS SELECT generate_series(1, 3) | ||
|
||
statement ok | ||
CREATE TABLE y(a) AS SELECT generate_series(2, 4) | ||
|
||
# Regression test for #24307 until CockroachDB learns how to execute | ||
# side effects no matter what. | ||
query error unimplemented: common table expression "t" with side effects was not used in query | ||
WITH t AS ( | ||
INSERT INTO x(a) VALUES(0) RETURNING a | ||
) | ||
SELECT 1 | ||
|
||
query error unimplemented: common table expression "t" with side effects was not used in query | ||
WITH t AS ( | ||
SELECT * FROM ( | ||
WITH b AS (INSERT INTO x(a) VALUES(0) RETURNING a) | ||
TABLE b | ||
) | ||
) | ||
SELECT 1 | ||
|
||
query error unimplemented: common table expression "t" with side effects was not used in query | ||
WITH t AS ( | ||
DELETE FROM x RETURNING a | ||
) | ||
SELECT 1 | ||
|
||
query error unimplemented: common table expression "t" with side effects was not used in query | ||
WITH t AS ( | ||
UPSERT INTO x(a) VALUES(0) RETURNING a | ||
) | ||
SELECT 1 | ||
|
||
query error unimplemented: common table expression "t" with side effects was not used in query | ||
WITH t AS ( | ||
UPDATE x SET a = 0 RETURNING a | ||
) | ||
SELECT 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# LogicTest: local-opt fakedist-opt | ||
|
||
statement ok | ||
CREATE TABLE x(a) AS SELECT generate_series(1, 3) | ||
|
||
statement ok | ||
CREATE TABLE y(b) AS SELECT generate_series(2, 4) | ||
|
||
# Referencing a CTE multiple times. | ||
query II | ||
WITH t AS (SELECT b FROM y) SELECT * FROM t JOIN t AS q ON true | ||
---- | ||
2 2 | ||
2 3 | ||
2 4 | ||
3 2 | ||
3 3 | ||
3 4 | ||
4 2 | ||
4 3 | ||
4 4 | ||
|
||
query II | ||
WITH | ||
one AS (SELECT a AS u FROM x), | ||
two AS (SELECT b AS v FROM (SELECT b FROM y UNION ALL SELECT u FROM one)) | ||
SELECT | ||
* | ||
FROM | ||
one JOIN two ON u = v | ||
---- | ||
1 1 | ||
2 2 | ||
3 3 | ||
2 2 | ||
3 3 | ||
|
||
# Mutation CTEs that aren't referenced elsewhere in the query. | ||
statement ok | ||
CREATE TABLE z (c INT PRIMARY KEY); | ||
|
||
query I | ||
WITH foo AS (INSERT INTO z VALUES (10) RETURNING 1) SELECT 2 | ||
---- | ||
2 | ||
|
||
query I | ||
SELECT * FROM z | ||
---- | ||
10 | ||
|
||
query I | ||
WITH foo AS (UPDATE z SET c = 20 RETURNING 1) SELECT 3 | ||
---- | ||
3 | ||
|
||
query I | ||
SELECT * FROM z | ||
---- | ||
20 | ||
|
||
query I | ||
WITH foo AS (DELETE FROM z RETURNING 1) SELECT 4 | ||
---- | ||
4 | ||
|
||
query I | ||
SELECT count(*) FROM z | ||
---- | ||
0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.