Repository navigation
Allow WITH subqueries to take effect immediately - #16575
Conversation
|
It's really super cool !!! Everything is working 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);
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;
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;
with
x as (select number as a from numbers(10)),
y as (select number as a from numbers(5)),
z as (select toUInt64(1) b)
select * from x join y using a where a in (select * from z);
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 a in (select * from z);
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 max(a) from x join y using a where a in (select * from z)
having a > (select min(a) from w);
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 a from x join y using a where a in (select * from z)
having a <= (select max(a) from w); |
|
I compared PG result with CH. Everything is perfect. PG
create table t(a int);
create table s(a int);
insert into t SELECT generate_series(0, 9999);
insert into s SELECT generate_series(0, 4999);
CH
create table t(a Int64) engine=Memory;
create table s(a Int64) engine=Memory;
insert into t SELECT * from numbers(10000)
insert into s SELECT * from numbers(5000)
with
x as (select * from t),
y as (select * from s),
z as (select * from x where a % 2 = 1),
w as (select * from y where a > 333)
select max(a) from x join y using (a) where a in (select * from z)
and a <= (select max(a) from w);
max
------
4999
┌─max(a)─┐
│ 4999 │
└────────┘
with
x as (select * from t),
y as (select * from s),
z as (select * from x where a % 3 = 1),
w as (select * from y where a > 333 and a < 1000)
select count(a) from x left join y using (a) where a in (select * from z)
and a <= (select max(a) from w);
count
-------
333
┌─count(a)─┐
│ 333 │
└──────────┘
with
x as (select * from t),
y as (select * from s),
z as (select * from x where a % 3 = 1),
w as (select * from y where a > 333 and a < 1000)
select count(a) from x left join y using (a) where a in (select * from z)
count
-------
3333
┌─count(a)─┐
│ 3333 │
└──────────┘
with
x as (select a-4000 a from t where t.a >700),
y as (select * from s),
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 a in (select * from z)
count
-------
2000
┌─count()─┐
│ 2000 │
└─────────┘
with
x as (select a-4000 a from t where t.a >700),
y as (select * from s),
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
max | min | count
-----+-----+-------
97 | 1 | 33
┌─max(a)─┬─min(a)─┬─count()─┐
│ 97 │ 1 │ 33 │
└────────┴────────┴─────────┘
with
x as (select a-4000 a from t where t.a >700),
y as (select * from s),
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
max | min | count
-----+-------+-------
99 | -3299 | 3399
┌─max(a)─┬─min(a)─┬─count()─┐
│ 99 │ -3299 │ 3399 │
└────────┴────────┴─────────┘
with
x as (select a-4000 a from t where t.a >700),
y as (select * from s),
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
max | min | count
-----+-----+-------
99 | 0 | 100
┌─max(a)─┬─min(a)─┬─count()─┐
│ 99 │ 0 │ 100 │
└────────┴────────┴─────────┘
with
x as (select a-4000 a from t where t.a >700),
y as (select x.a a from x left join t 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
max | min | count
-----+-------+-------
99 | -3299 | 3399
┌─max(a)─┬─min(a)─┬─count()─┐
│ 99 │ -3299 │ 3399 │
└────────┴────────┴─────────┘
|
@vzakaznikov Could you please look what's wrong with Testflows? |
|
@den-crane Coult you please commit all the tests with a separate PR? |
|
@amosbird I've edited the changelog entry, is it correct? |
Might need to denote that we can only refer to previous ones, similar to how c/c++ find declarations. |
|
Ok. |
more tests for CTE #16575
|
is this supposed to work? with 5 as q1, x as (select number as a from numbers(10)), y as (select number+100 as b, number as a from numbers(10)) select a from (select * from x where a > q1) x1 left join y using a i'm getting this: Received exception from server (version 20.13.1): |
|
@garneja you need to --enable_global_with_statement arg Propagate WITH statements to UNION queries and all subqueries set enable_global_with_statement=1;
with 5 as q1, x as (select number as a from numbers(10)), y as (select number+100 as b, number as a from numbers(10)) select a from (select * from x where a > q1) x1 left join y using a;
┌─a─┐
│ 6 │
│ 7 │
│ 8 │
│ 9 │
└───┘ |
|
that works! thank you so much. |
|
is with substitution not supposed to be transitive? this one doesn't work for me: with 5 as q1, x as (select number+100 as b, number as a from numbers(10) where number > q1) select * from x Code: 47. DB::Exception: Received from 127.0.0.1:9000. DB::Exception: Missing columns: 'q1' while processing query: 'SELECT number + 100 AS b, number AS a FROM numbers(10) WHERE number > q1', required columns: 'number' 'q1', maybe you meant: . |
I hereby agree to the terms of the CLA available at: https://yandex.ru/legal/cla/?lang=en
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Subqueries in WITH section (CTE) can reference previous subqueries in WITH section by their name.
Detailed description / Documentation draft:
@den-crane Could you please check if this is the right behavior?
Information about CI checks: https://clickhouse.tech/docs/en/development/continuous-integration/