Skip to content

Commit

Permalink
add more tests from ck
Browse files Browse the repository at this point in the history
  • Loading branch information
xudong963 committed Aug 11, 2022
1 parent 154f648 commit 9e38d64
Show file tree
Hide file tree
Showing 2 changed files with 329 additions and 0 deletions.
1 change: 1 addition & 0 deletions query/src/sql/planner/binder/bind_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ impl BindContext {
pub fn replace(&self) -> Self {
let mut bind_context = BindContext::new();
bind_context.parent = self.parent.clone();
bind_context.ctes_map = self.ctes_map.clone();
bind_context
}

Expand Down
328 changes: 328 additions & 0 deletions tests/logictest/suites/base/15_query/cte.test
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,331 @@ with t2(tt) as (select a from t1) SELECT t1.a, t1.b FROM t1 WHERE EXISTS(SELECT
107 105
111 112
115 118

statement ok
DROP TABLE IF EXISTS test1;

statement ok
CREATE TABLE test1(i int, j int);

statement ok
INSERT INTO test1 VALUES (1, 2), (3, 4);

statement query I
WITH test1 AS (SELECT * FROM numbers(5)) SELECT * FROM test1;

----
0
1
2
3
4

statement query II
WITH test1 AS (SELECT i + 1, j + 1 FROM test1) SELECT * FROM test1;

----
2 3
4 5

statement query II
WITH test1 AS (SELECT i + 1, j + 1 FROM test1) SELECT * FROM (SELECT * FROM test1);

----
2 3
4 5

statement query II
SELECT * FROM (WITH t1 AS (SELECT to_int32(number) i FROM numbers(5)) SELECT * FROM t1) l INNER JOIN test1 r on l.i = r.i;

----
1 1 2
3 3 4

statement ok
DROP TABLE IF EXISTS test1;

statement query I
WITH test1 AS (SELECT number-1 as n FROM numbers(42))
SELECT max(n+1)+1 z FROM test1;

----
42

statement query I
WITH test1 AS (SELECT number-1 as n FROM numbers(4442) order by n limit 100) SELECT max(n) FROM test1 where n=422;

----
0

statement query I
WITH test1 AS (SELECT number-1 as n FROM numbers(4442) order by n limit 100) SELECT max(n) FROM test1 where n=42;

----
42

statement ok
drop table if exists with_test;

statement ok
create table with_test(n int64 null);

statement ok
insert into with_test select number - 1 from numbers(10000);

statement query I
WITH test1 AS (SELECT n FROM with_test order by n limit 100)
SELECT max(n) FROM test1 where n=422;

----
NULL

statement query I
WITH test1 AS (SELECT n FROM with_test order by n limit 100)
SELECT max(n) FROM test1 where n=42;

----
42

statement query I
WITH test1 AS (SELECT n FROM with_test where n = 42 order by n limit 100)
SELECT max(n) FROM test1 where n=42;

----
42

statement query I
WITH test1 AS (SELECT n FROM with_test where n = 42 or 1=1 order by n limit 100)
SELECT max(n) FROM test1 where n=42;

----
42

statement query I
WITH test1 AS (SELECT n, null b FROM with_test where 1=1 and n = 42 order by n)
SELECT max(n) FROM test1 where n=45;

----
NULL

statement query I
WITH test1 AS (SELECT n, null b, n+1 m FROM with_test where 1=0 or n = 42 order by n limit 4)
SELECT max(n) m FROM test1 where test1.m=43 having max(n)=42;

----
42

statement query I
with
test1 as (select n, null b, n+1 m from with_test where n = 42 order by n limit 4),
test2 as (select n + 1 as x, n - 1 as y from test1),
test3 as (select x * y as z from test2)
select z + 1 as q from test3;

----
1764

statement ok
drop table with_test;

statement query I
WITH
x AS (SELECT number AS a FROM numbers(10)),
y AS (SELECT number AS a FROM numbers(5))
SELECT * FROM x WHERE a in (SELECT a FROM y)
ORDER BY a;

----
0
1
2
3
4

statement query I
WITH
x AS (SELECT number AS a FROM numbers(10)),
y AS (SELECT number AS a FROM numbers(5))
SELECT * FROM x left JOIN y USING (a)
ORDER BY a;

----
0
1
2
3
4
NULL
NULL
NULL
NULL
NULL

statement query I
WITH
x AS (SELECT number AS a FROM numbers(10)),
y AS (SELECT number AS a FROM numbers(5))
SELECT * FROM x JOIN y USING (a)
ORDER BY x.a;

----
0
1
2
3
4

statement query I
WITH
x AS (SELECT number AS a FROM numbers(10)),
y AS (SELECT number AS a FROM numbers(5)),
z AS (SELECT 1::UInt64 b)
SELECT * FROM x JOIN y USING (a) WHERE x.a in (SELECT * FROM z);

----
0
1
2
3
4

statement query I
WITH
x AS (SELECT number AS a FROM numbers(10)),
y AS (SELECT number AS a FROM numbers(5)),
z AS (SELECT * FROM x WHERE a % 2),
w AS (SELECT * FROM y WHERE a > 0)
SELECT * FROM x JOIN y USING (a) WHERE x.a in (SELECT * FROM z)
ORDER BY x.a;

----
1
3

statement query I
WITH
x AS (SELECT number AS a FROM numbers(10)),
y AS (SELECT number AS a FROM numbers(5)),
z AS (SELECT * FROM x WHERE a % 2),
w AS (SELECT * FROM y WHERE a > 0)
SELECT x.a FROM x JOIN y USING (a) WHERE x.a in (SELECT * FROM z)
HAVING x.a <= (SELECT max(a) FROM w)
ORDER BY x.a;

----
1
3

statement ok
CREATE TABLE cte1(a Int64);

statement ok
CREATE TABLE cte2(a Int64);

statement ok
INSERT INTO cte1 SELECT * FROM numbers(10000);

statement ok
INSERT INTO cte2 SELECT * FROM numbers(5000);

statement query I
WITH
x AS (SELECT * FROM cte1),
y AS (SELECT * FROM cte2),
z AS (SELECT * FROM x WHERE a % 2 = 1),
w AS (SELECT * FROM y WHERE a > 333)
SELECT max(x.a)
FROM x JOIN y USING (a)
WHERE x.a in (SELECT * FROM z) AND x.a <= (SELECT max(a) FROM w);

----
4999

statement query I
WITH
x AS (SELECT * FROM cte1),
y AS (SELECT * FROM cte2),
z AS (SELECT * FROM x WHERE a % 3 = 1),
w AS (SELECT * FROM y WHERE a > 333 AND a < 1000)
SELECT count(x.a)
FROM x left JOIN y USING (a)
WHERE x.a in (SELECT * FROM z) AND x.a <= (SELECT max(a) FROM w);

----
333

statement query I
WITH
x AS (SELECT * FROM cte1),
y AS (SELECT * FROM cte2),
z AS (SELECT * FROM x WHERE a % 3 = 1),
w AS (SELECT * FROM y WHERE a > 333 AND a < 1000)
SELECT count(x.a)
FROM x left JOIN y USING (a)
WHERE x.a in (SELECT * FROM z);

----
3333

statement query I
WITH
x AS (SELECT a-4000 a FROM cte1 WHERE cte1.a >700),
y AS (SELECT * FROM cte2),
z AS (SELECT * FROM x WHERE a % 3 = 1),
w AS (SELECT * FROM y WHERE a > 333 AND a < 1000)
SELECT count(*)
FROM x left JOIN y USING (a)
WHERE x.a in (SELECT * FROM z);

----
2000

statement query III
WITH
x AS (SELECT a-4000 a FROM cte1 WHERE cte1.a >700),
y AS (SELECT * FROM cte2),
z AS (SELECT * FROM x WHERE a % 3 = 1),
w AS (SELECT * FROM y WHERE a > 333 AND a < 1000)
SELECT max(a), min(a), count(*)
FROM x
WHERE a in (SELECT * FROM z) AND a <100;

----
97 1 33

statement query III
WITH
x AS (SELECT a-4000 a FROM cte1 WHERE cte1.a >700),
y AS (SELECT * FROM cte2),
z AS (SELECT * FROM x WHERE a % 3 = 1),
w AS (SELECT * FROM y WHERE a > 333 AND a < 1000)
SELECT max(a), min(a), count(*) FROM x
WHERE a <100;

----
99 -3299 3399

statement query III
WITH
x AS (SELECT a-4000 a FROM cte1 t WHERE t.a >700),
y AS (SELECT x.a a FROM x left JOIN cte1 USING (a)),
z AS (SELECT * FROM x WHERE a % 3 = 1),
w AS (SELECT * FROM y WHERE a > 333 AND a < 1000)
SELECT max(a), min(a), count(*)
FROM y
WHERE a <100;

----
99 -3299 3399

statement ok
DROP TABLE cte1;

statement ok
DROP TABLE cte2;

statement query I
with it as ( select * from numbers(1) ) select i.number from it as i;

----
0

0 comments on commit 9e38d64

Please sign in to comment.