Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support CORRESPONDING specification in set operations #17891

Merged
merged 12 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 58 additions & 12 deletions e2e_test/batch/basic/union.slt.part
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,33 @@ statement ok
SET RW_IMPLICIT_FLUSH TO true;

statement ok
create table t1 (v1 int, v2 bigint);
create table t1 (v1 int, v2 bigint, v4 int);

statement ok
create table t2 (v1 int, v3 int);
create table t2 (v1 int, v3 int, v4 int);

statement ok
insert into t1 values(1, 2);
insert into t1 values(1, 2, 3);

statement ok
insert into t2 values(1, 2);
insert into t2 values(1, 2, 3);

query II
query III
select * from t1 union select * from t2
----
1 2
1 2 3

query II
query III
select * from t1 union all select * from t2
----
1 2
1 2
1 2 3
1 2 3

query II
query III
select * from t1 union all select * from t2 order by v1
----
1 2
1 2
1 2 3
1 2 3

statement error
select * from t1 union all select * from t2 order by v1 + 1
Expand Down Expand Up @@ -69,9 +69,55 @@ NULL
statement error
select null union all select null select union 1

query II
select * from t1 union all corresponding select * from t2 order by v1
----
1 3
1 3

query II
select * from t1 union corresponding select v4, v3 as v1 from t2 order by v1
----
1 3
2 3

query II
select * from t1 union all corresponding by (v4, v1) select * from t2
----
3 1
3 1

query II
select * from t1 union corresponding by (v4) select * from t2
----
3

statement error
select * from t1 union corresponding by (vxx) select * from t2
----
db error: ERROR: Failed to run the query

Caused by:
Invalid input syntax: Column name `vxx` in CORRESPONDING BY is not found in a side of the UNION operation. It shall be included in both sides.


statement ok
create table txx (vxx int);

statement error
select * from t1 union corresponding select * from txx
----
db error: ERROR: Failed to run the query

Caused by:
Invalid input syntax: When CORRESPONDING is specified, at least one column of the left side shall have a column name that is the column name of some column of the right side in a UNION operation. Left side query column list: ("v1", "v2", "v4"). Right side query column list: ("vxx").
Copy link
Contributor Author

@yuhao-su yuhao-su Aug 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xxchan should remove the column list of this too?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can decide it :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll keep this considering it can be fairly difficult to identify the faulty query without any information.



statement ok
drop table t1;

statement ok
drop table t2;

statement ok
drop table txx;
110 changes: 91 additions & 19 deletions e2e_test/streaming/union.slt
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@ statement ok
SET RW_IMPLICIT_FLUSH TO true;

statement ok
create table t1 (v1 int, v2 int);
create table t1 (v1 int, v2 int, v4 int);

statement ok
create table t2 (v1 int, v3 int);
create table t2 (v1 int, v3 int, v4 int);

statement ok
create materialized view v as select * from t1 union all select * from t2;

statement ok
create materialized view v2 as select * from t1 union select * from t2;

statement ok
create materialized view v3 as select * from t1 union all corresponding select * from t2;

statement ok
create materialized view v4 as select * from t1 union corresponding by (v4, v1) select * from t2;

query II
select * from v;
----
Expand All @@ -22,64 +28,130 @@ select * from v2;
----

statement ok
insert into t1 values(1, 2);
insert into t1 values(1, 2, 3);

query II
query III
select * from v;
----
1 2
1 2 3

query II
query III
select * from v2;
----
1 2
1 2 3

query II
select * from v3;
----
1 3

query II
select * from v4;
----
3 1

statement ok
insert into t2 values(1, 2);
insert into t2 values(1, 2, 3);


query II
query III
select * from v;
----
1 2
1 2
1 2 3
1 2 3

query II
query III
select * from v2;
----
1 2
1 2 3

query II
select * from v3;
----
1 3
1 3

query II
select * from v4;
----
3 1

statement ok
delete from t1 where v1 = 1;

query II
query III
select * from v;
----
1 2
1 2 3

query II
query III
select * from v2;
----
1 2
1 2 3

query II
select * from v3;
----
1 3

query II
select * from v4;
----
3 1

statement ok
delete from t2 where v1 = 1;

query II
query III
select * from v;
----

query II
query III
select * from v2;
----

query II
select * from v3;
----

query II
select * from v4;
----


statement ok
drop materialized view v;

statement ok
drop materialized view v2;

statement ok
drop materialized view v3;

statement ok
drop materialized view v4;

statement error
create materialized view v5 as select * from t1 union corresponding by (vxx, v1) select * from t2
----
db error: ERROR: Failed to run the query

Caused by:
Invalid input syntax: Column name `vxx` in CORRESPONDING BY is not found in a side of the UNION operation. It shall be included in both sides.


statement ok
create table txx (vxx int);

statement error
create materialized view v5 as select * from t1 union corresponding select * from txx
----
db error: ERROR: Failed to run the query

Caused by:
Invalid input syntax: When CORRESPONDING is specified, at least one column of the left side shall have a column name that is the column name of some column of the right side in a UNION operation. Left side query column list: ("v1", "v2", "v4"). Right side query column list: ("vxx").


statement ok
drop table t1;

Expand Down
8 changes: 8 additions & 0 deletions src/common/src/catalog/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,14 @@ impl Schema {
true
}
}

pub fn formatted_col_names(&self) -> String {
self.fields
.iter()
.map(|f| format!("\"{}\"", &f.name))
.collect::<Vec<_>>()
.join(", ")
}
}

impl Field {
Expand Down
28 changes: 28 additions & 0 deletions src/frontend/planner_test/tests/testdata/input/union.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,31 @@
select * from t1 union all select * from t2 union all select * from t3 union all select * from t4 union all select * from t5;
expected_outputs:
- stream_dist_plan

- name: test corresponding union
sql: |
create table t1 (a int, b numeric, c bigint);
create table t2 (a int, b numeric, y bigint);
create table t3 (x int, b numeric, c bigint);
select * from t1 union corresponding select * from t2 union all corresponding by (b) select * from t3;
expected_outputs:
- batch_plan
- stream_plan
- stream_dist_plan

- name: test corresponding union error - corresponding list
sql: |
create table t1 (a int, b numeric, c bigint);
create table t2 (a int, b numeric, y bigint);
create table t3 (x int, b numeric, c bigint);
select * from t1 union corresponding select * from t2 union all corresponding by (c) select * from t3;
expected_outputs:
- binder_error

- name: test corresponding union error - duplicate names
sql: |
create table t1 (a int, b numeric, c bigint);
create table t2 (a int, b numeric, y bigint);
select a, b as a from t1 union corresponding select * from t2;
expected_outputs:
- binder_error
Loading
Loading