Skip to content

Commit

Permalink
chore(planner): add tips for wrong usage of semi or anti join (datafu…
Browse files Browse the repository at this point in the history
…selabs#13948)

* add tips for wrong usage of semi anti join

* fix

* refine code

* fix

* fix

* fix

* Update src/query/sql/src/executor/physical_plans/physical_hash_join.rs

Co-authored-by: Andy Lok <andylokandy@hotmail.com>

* Update src/query/sql/src/executor/physical_plans/physical_hash_join.rs

Co-authored-by: Andy Lok <andylokandy@hotmail.com>

* Update src/query/sql/src/executor/physical_plans/physical_hash_join.rs

Co-authored-by: Andy Lok <andylokandy@hotmail.com>

* map_or

* refine tips

* remove useless code

* make lint

* add sqllogictest

* fix test

* fix

* make lint

* bendsql error message

* fix sq;logictest

* add more tests

---------

Co-authored-by: Andy Lok <andylokandy@hotmail.com>
  • Loading branch information
Dousir9 and andylokandy committed Dec 8, 2023
1 parent 06178b2 commit c761101
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
27 changes: 24 additions & 3 deletions src/query/sql/src/executor/physical_plans/physical_hash_join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ impl PhysicalPlanBuilder {

let mut probe_projections = ColumnSet::new();
let mut build_projections = ColumnSet::new();
for column in pre_column_projections {
for column in pre_column_projections.iter() {
if let Some((index, _)) = probe_schema.column_with_name(&column.to_string()) {
probe_projections.insert(index);
}
Expand Down Expand Up @@ -320,8 +320,29 @@ impl PhysicalPlanBuilder {
probe_fields.extend(build_fields);
probe_fields
}
JoinType::LeftSemi | JoinType::LeftAnti => probe_fields,
JoinType::RightSemi | JoinType::RightAnti => build_fields,
JoinType::LeftSemi | JoinType::LeftAnti | JoinType::RightSemi | JoinType::RightAnti => {
let (result_fields, dropped_fields) = if join.join_type == JoinType::LeftSemi
|| join.join_type == JoinType::LeftAnti
{
(probe_fields, build_fields)
} else {
(build_fields, probe_fields)
};
for field in dropped_fields.iter() {
if result_fields.iter().all(|x| x.name() != field.name()) &&
let Ok(index) = field.name().parse::<usize>() &&
column_projections.contains(&index)
{
let metadata = self.metadata.read();
return Err(ErrorCode::SemanticError(format!(
"cannot access the {:?}.{:?} in ANTI or SEMI join",
metadata.table(index).name(),
metadata.column(index).name()
)));
}
}
result_fields
}
JoinType::LeftMark => {
let name = if let Some(idx) = join.marker_index {
idx.to_string()
Expand Down
42 changes: 41 additions & 1 deletion tests/sqllogictests/suites/query/join/join.test
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,44 @@ statement ok
drop table t;

statement ok
drop table t1;
drop table t1;

statement ok
drop table if exists t1;

statement ok
drop table if exists t2;

statement ok
create table t1(a int, b int);

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

statement ok
create table t2(a int, b int);

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

statement error 1065
SELECT t1.a FROM t1 RIGHT ANTI JOIN t2 ON t1.a = t2.a;
---

statement error 1065
SELECT t1.a FROM t1 RIGHT SEMI JOIN t2 ON t1.a = t2.a;
---

statement error 1065
SELECT t2.a FROM t1 LEFT ANTI JOIN t2 ON t1.a = t2.a;
---

statement error 1065
SELECT t2.a FROM t1 LEFT SEMI JOIN t2 ON t1.a = t2.a;
---

statement ok
drop table t1;

statement ok
drop table t2;

0 comments on commit c761101

Please sign in to comment.