-
Notifications
You must be signed in to change notification settings - Fork 590
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add duckdb cte tests & fix some minor bugs (#12527)
- Loading branch information
Showing
16 changed files
with
371 additions
and
8 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
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
18 changes: 18 additions & 0 deletions
18
e2e_test/batch/duckdb/cte/insert_cte_bug_3417.test.slt.part
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,18 @@ | ||
# name: test/sql/cte/insert_cte_bug_3417.test | ||
# description: Test for a crash reported in issue #3417 | ||
# group: [cte] | ||
|
||
statement ok | ||
CREATE TABLE table1 (id INTEGER, a INTEGER); | ||
|
||
statement ok | ||
CREATE TABLE table2 (table1_id INTEGER); | ||
|
||
statement error | ||
INSERT INTO table2 WITH cte AS (INSERT INTO table1 SELECT 1, 2 RETURNING id) SELECT id FROM cte; | ||
|
||
statement ok | ||
DROP TABLE table1; | ||
|
||
statement ok | ||
DROP TABLE table2; |
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,8 @@ | ||
# name: test/sql/cte/test_bug_922.test | ||
# description: Test for a crash reported in issue #922 | ||
# group: [cte] | ||
|
||
query I | ||
WITH my_list(value) AS (VALUES (1), (2), (3)) | ||
SELECT * FROM my_list LIMIT 0 OFFSET 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,145 @@ | ||
# name: test/sql/cte/test_cte.test | ||
# description: Test Common Table Expressions (CTE) | ||
# group: [cte] | ||
|
||
statement ok | ||
SET RW_IMPLICIT_FLUSH TO TRUE; | ||
|
||
statement ok | ||
create table a(i integer); | ||
|
||
statement ok | ||
insert into a values (42); | ||
|
||
query I | ||
with cte1 as (Select i as j from a) select * from cte1; | ||
---- | ||
42 | ||
|
||
query I | ||
with cte1 as (Select i as j from a) select x from cte1 t1(x); | ||
---- | ||
42 | ||
|
||
query I | ||
with cte1(xxx) as (Select i as j from a) select xxx from cte1; | ||
---- | ||
42 | ||
|
||
query I | ||
with cte1(xxx) as (Select i as j from a) select x from cte1 t1(x); | ||
---- | ||
42 | ||
|
||
query II | ||
with cte1 as (Select i as j from a), cte2 as (select ref.j as k from cte1 as ref), cte3 as (select ref2.j+1 as i from cte1 as ref2) select * from cte2 , cte3; | ||
---- | ||
42 43 | ||
|
||
query I rowsort | ||
with cte1 as (select i as j from a), cte2 as (select ref.j as k from cte1 as ref), cte3 as (select ref2.j+1 as i from cte1 as ref2) select * from cte2 union all select * FROM cte3; | ||
---- | ||
42 | ||
43 | ||
|
||
|
||
# FIXME: this should be an error | ||
# duplicate CTE alias | ||
query I | ||
with cte1 as (select 42), cte1 as (select 43) select * FROM cte1; | ||
---- | ||
43 | ||
|
||
# reference to CTE before its actually defined | ||
# duckdb is ok | ||
# postgres: query failed: db error: ERROR: relation "cte1" does not exist | ||
# DETAIL: There is a WITH item named "cte1", but it cannot be referenced from this part of the query. | ||
# HINT: Use WITH RECURSIVE, or re-order the WITH items to remove forward references. | ||
query error table or source not found: cte1 | ||
with cte3 as (select ref2.j as i from cte1 as ref2), cte1 as (Select i as j from a), cte2 as (select ref.j+1 as k from cte1 as ref) select * from cte2 union all select * FROM cte3; | ||
|
||
|
||
# multiple uses of same CTE | ||
query II | ||
with cte1 as (Select i as j from a) select * from cte1 cte11, cte1 cte12; | ||
---- | ||
42 42 | ||
|
||
# refer to CTE in subquery | ||
query I | ||
with cte1 as (Select i as j from a) select * from cte1 where j = (select max(j) from cte1 as cte2); | ||
---- | ||
42 | ||
|
||
# multi-column name alias | ||
query II | ||
with cte1(x, y) as (select 42 a, 84 b) select zzz, y from cte1 t1(zzz); | ||
---- | ||
42 84 | ||
|
||
# use a CTE in a view definition | ||
statement ok | ||
create view va AS (with cte as (Select i as j from a) select * from cte); | ||
|
||
query I | ||
select * from va | ||
---- | ||
42 | ||
|
||
# nested CTE views that re-use CTE aliases | ||
query I | ||
with cte AS (SELECT * FROM va) SELECT * FROM cte; | ||
---- | ||
42 | ||
|
||
# multiple ctes in a view definition | ||
statement ok | ||
create view vb AS (with cte1 as (Select i as j from a), cte2 as (select ref.j+1 as k from cte1 as ref) select * from cte2); | ||
|
||
query I | ||
select * from vb | ||
---- | ||
43 | ||
|
||
# cte in set operation node | ||
query I | ||
SELECT 1 UNION ALL (WITH cte AS (SELECT 42) SELECT * FROM cte); | ||
---- | ||
1 | ||
42 | ||
|
||
# # cte in recursive cte | ||
# query I | ||
# WITH RECURSIVE cte(d) AS ( | ||
# SELECT 1 | ||
# UNION ALL | ||
# (WITH c(d) AS (SELECT * FROM cte) | ||
# SELECT d + 1 | ||
# FROM c | ||
# WHERE FALSE | ||
# ) | ||
# ) | ||
# SELECT max(d) FROM cte; | ||
# ---- | ||
# 1 | ||
|
||
# test CTE with nested aliases in where clause | ||
# Note: postgres doesn't support this: column "alias1" does not exist | ||
query error failed to bind expression: alias1 | ||
with cte (a) as ( | ||
select 1 | ||
) | ||
select | ||
a as alias1, | ||
alias1 as alias2 | ||
from cte | ||
where alias2 > 0; | ||
|
||
statement ok | ||
drop view vb; | ||
|
||
statement ok | ||
drop view va; | ||
|
||
statement ok | ||
drop table a; |
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,57 @@ | ||
# name: test/sql/cte/test_cte_in_cte.test | ||
# description: Test Nested Common Table Expressions (CTE) | ||
# group: [cte] | ||
|
||
statement ok | ||
SET RW_IMPLICIT_FLUSH TO TRUE; | ||
|
||
statement ok | ||
create table a(i integer); | ||
|
||
statement ok | ||
insert into a values (42); | ||
|
||
query I | ||
with cte1 as (Select i as j from a) select * from cte1; | ||
---- | ||
42 | ||
|
||
query I | ||
with cte1 as (with b as (Select i as j from a) Select j from b) select x from cte1 t1(x); | ||
---- | ||
42 | ||
|
||
query I | ||
with cte1(xxx) as (with ncte(yyy) as (Select i as j from a) Select yyy from ncte) select xxx from cte1; | ||
---- | ||
42 | ||
|
||
query II | ||
with cte1 as (with b as (Select i as j from a) select j from b), cte2 as (with c as (select ref.j+1 as k from cte1 as ref) select k from c) select * from cte1 , cte2; | ||
---- | ||
42 43 | ||
|
||
# refer to CTE in subquery tableref | ||
query I | ||
with cte1 as (Select i as j from a) select * from (with cte2 as (select max(j) as j from cte1) select * from cte2) f | ||
---- | ||
42 | ||
|
||
# refer to CTE in subquery expression | ||
query I | ||
with cte1 as (Select i as j from a) select * from cte1 where j = (with cte2 as (select max(j) as j from cte1) select j from cte2); | ||
---- | ||
42 | ||
|
||
# refer to same-named CTE in a subquery expression | ||
query I | ||
with cte as (Select i as j from a) select * from cte where j = (with cte as (select max(j) as j from cte) select j from cte); | ||
---- | ||
42 | ||
|
||
# self-refer to non-existent cte | ||
statement error | ||
with cte as (select * from cte) select * from cte | ||
|
||
statement ok | ||
drop table a; |
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,26 @@ | ||
# name: test/sql/cte/test_cte_overflow.test | ||
# description: Ensure no stack overflow for CTE names that match existing tables | ||
# group: [cte] | ||
|
||
statement ok | ||
SET RW_IMPLICIT_FLUSH TO TRUE; | ||
|
||
statement ok | ||
create table a (id integer) | ||
|
||
statement ok | ||
insert into a values (1729) | ||
|
||
statement ok | ||
create view va as (with v as (select * from a) select * from v) | ||
|
||
query I | ||
with a as (select * from va) select * from a | ||
---- | ||
1729 | ||
|
||
statement ok | ||
drop view va; | ||
|
||
statement ok | ||
drop table a; |
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,54 @@ | ||
# name: test/sql/cte/test_issue_5673.test | ||
# description: Issue #5673 and #4987: CTE and Table name are name shadowing | ||
# group: [cte] | ||
|
||
statement ok | ||
SET RW_IMPLICIT_FLUSH TO TRUE; | ||
|
||
statement ok | ||
create table orders(ordered_at int); | ||
|
||
statement ok | ||
create table stg_orders(ordered_at int); | ||
|
||
statement ok | ||
insert into orders values (1); | ||
|
||
statement ok | ||
insert into stg_orders values (1); | ||
|
||
# Note: postgres succeeds. | ||
# duckdb returns Binder Error: Circular reference to CTE "orders", There are two possible solutions. | ||
query ok | ||
with | ||
orders as ( | ||
select * from stg_orders | ||
where ordered_at >= (select max(ordered_at) from orders) | ||
), | ||
some_more_logic as ( | ||
select * | ||
from orders | ||
) | ||
select * from some_more_logic; | ||
---- | ||
1 | ||
|
||
query I | ||
with | ||
orders as ( | ||
select * from public.stg_orders | ||
where ordered_at >= (select max(ordered_at) from public.orders) | ||
), | ||
some_more_logic as ( | ||
select * | ||
from orders | ||
) | ||
select * from some_more_logic; | ||
---- | ||
1 | ||
|
||
statement ok | ||
drop table orders; | ||
|
||
statement ok | ||
drop table stg_orders; |
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.