Skip to content

Allow WITH subqueries to take effect immediately - #16575

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
amosbird:cte2
Nov 4, 2020
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
amosbird:cte2

Conversation

@amosbird

@amosbird amosbird commented Oct 31, 2020 •

Copy link
Copy Markdown
Collaborator

I hereby agree to the terms of the CLA available at: https://yandex.ru/legal/cla/?lang=en

Changelog category (leave one):

  • Improvement

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/

@robot-clickhouse robot-clickhouse added the pr-improvement Pull request with some product improvements label Oct 31, 2020
@den-crane

den-crane commented Nov 2, 2020 •

Copy link
Copy Markdown
Contributor

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);

@den-crane

Copy link
Copy Markdown
Contributor

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 │
└────────┴────────┴─────────┘

@alexey-milovidov

Copy link
Copy Markdown
Member

Testflows check — failed: 369, passed: 1838, other: 22

@vzakaznikov Could you please look what's wrong with Testflows?

@alexey-milovidov

Copy link
Copy Markdown
Member

@den-crane Coult you please commit all the tests with a separate PR?

@alexey-milovidov
alexey-milovidov merged commit 7b468c0 into ClickHouse:master Nov 4, 2020
@alexey-milovidov alexey-milovidov self-assigned this Nov 4, 2020
@alexey-milovidov

Copy link
Copy Markdown
Member

@amosbird I've edited the changelog entry, is it correct?

@amosbird

amosbird commented Nov 4, 2020

Copy link
Copy Markdown
Collaborator Author

@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.

@alexey-milovidov

Copy link
Copy Markdown
Member

Ok.

alexey-milovidov added a commit that referenced this pull request Nov 4, 2020
@garneja

garneja commented Dec 31, 2020

Copy link
Copy Markdown

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):
Code: 47. DB::Exception: Received from 127.0.0.1:9000. DB::Exception: Missing columns: 'q1' while processing query: 'SELECT a FROM (SELECT number AS a FROM numbers(10) HAVING a > q1) AS x WHERE a > q1', required columns: 'a' 'q1'
'a' 'q1'.

@den-crane

Copy link
Copy Markdown
Contributor

@garneja you need to set enable_global_with_statement=1

--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 │
└───┘

@garneja

garneja commented Jan 1, 2021

Copy link
Copy Markdown

that works! thank you so much.

@garneja

garneja commented Jan 2, 2021 •

Copy link
Copy Markdown

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: .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants