Skip to content

Commit c761101

Browse files
Dousir9andylokandy
andauthored
chore(planner): add tips for wrong usage of semi or anti join (#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>
1 parent 06178b2 commit c761101

File tree

2 files changed

+65
-4
lines changed

2 files changed

+65
-4
lines changed

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

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ impl PhysicalPlanBuilder {
249249

250250
let mut probe_projections = ColumnSet::new();
251251
let mut build_projections = ColumnSet::new();
252-
for column in pre_column_projections {
252+
for column in pre_column_projections.iter() {
253253
if let Some((index, _)) = probe_schema.column_with_name(&column.to_string()) {
254254
probe_projections.insert(index);
255255
}
@@ -320,8 +320,29 @@ impl PhysicalPlanBuilder {
320320
probe_fields.extend(build_fields);
321321
probe_fields
322322
}
323-
JoinType::LeftSemi | JoinType::LeftAnti => probe_fields,
324-
JoinType::RightSemi | JoinType::RightAnti => build_fields,
323+
JoinType::LeftSemi | JoinType::LeftAnti | JoinType::RightSemi | JoinType::RightAnti => {
324+
let (result_fields, dropped_fields) = if join.join_type == JoinType::LeftSemi
325+
|| join.join_type == JoinType::LeftAnti
326+
{
327+
(probe_fields, build_fields)
328+
} else {
329+
(build_fields, probe_fields)
330+
};
331+
for field in dropped_fields.iter() {
332+
if result_fields.iter().all(|x| x.name() != field.name()) &&
333+
let Ok(index) = field.name().parse::<usize>() &&
334+
column_projections.contains(&index)
335+
{
336+
let metadata = self.metadata.read();
337+
return Err(ErrorCode::SemanticError(format!(
338+
"cannot access the {:?}.{:?} in ANTI or SEMI join",
339+
metadata.table(index).name(),
340+
metadata.column(index).name()
341+
)));
342+
}
343+
}
344+
result_fields
345+
}
325346
JoinType::LeftMark => {
326347
let name = if let Some(idx) = join.marker_index {
327348
idx.to_string()

tests/sqllogictests/suites/query/join/join.test

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,44 @@ statement ok
194194
drop table t;
195195

196196
statement ok
197-
drop table t1;
197+
drop table t1;
198+
199+
statement ok
200+
drop table if exists t1;
201+
202+
statement ok
203+
drop table if exists t2;
204+
205+
statement ok
206+
create table t1(a int, b int);
207+
208+
statement ok
209+
insert into t1 values(1, 1),(2, 2),(3, 3);
210+
211+
statement ok
212+
create table t2(a int, b int);
213+
214+
statement ok
215+
insert into t2 values(1, 1),(2, 2);
216+
217+
statement error 1065
218+
SELECT t1.a FROM t1 RIGHT ANTI JOIN t2 ON t1.a = t2.a;
219+
---
220+
221+
statement error 1065
222+
SELECT t1.a FROM t1 RIGHT SEMI JOIN t2 ON t1.a = t2.a;
223+
---
224+
225+
statement error 1065
226+
SELECT t2.a FROM t1 LEFT ANTI JOIN t2 ON t1.a = t2.a;
227+
---
228+
229+
statement error 1065
230+
SELECT t2.a FROM t1 LEFT SEMI JOIN t2 ON t1.a = t2.a;
231+
---
232+
233+
statement ok
234+
drop table t1;
235+
236+
statement ok
237+
drop table t2;

0 commit comments

Comments
 (0)