Skip to content

Commit

Permalink
fix:right join not matched case
Browse files Browse the repository at this point in the history
  • Loading branch information
crwen committed Mar 16, 2024
1 parent 2f2df06 commit 8f21af7
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 15 deletions.
33 changes: 18 additions & 15 deletions src/execution/volcano/dql/join/nested_loop_join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ use std::sync::Arc;

use crate::catalog::{ColumnCatalog, ColumnRef};
use crate::errors::DatabaseError;
use crate::execution::volcano::dql::projection::Projection;
use crate::execution::volcano::{build_read, BoxedExecutor, ReadExecutor};
use crate::expression::ScalarExpression;
use crate::planner::operator::join::{JoinCondition, JoinOperator, JoinType};
use crate::planner::LogicalPlan;
use crate::storage::Transaction;
use crate::types::tuple::{Schema, SchemaRef, Tuple};
use crate::types::value::{DataValue, ValueRef, NULL_VALUE};
use crate::types::value::{DataValue, NULL_VALUE};
use futures_async_stream::try_stream;
use itertools::Itertools;

Expand Down Expand Up @@ -54,19 +55,11 @@ impl EqualCondition {
if self.on_left_keys.is_empty() {
return Ok(true);
}
let eval_keys = |on_keys: &[ScalarExpression],
tuple: &Tuple,
schema: &SchemaRef|
-> Result<Vec<ValueRef>, DatabaseError> {
let mut values = Vec::with_capacity(on_keys.len());
for expr in on_keys {
values.push(expr.eval(tuple, schema)?);
}
Ok(values)
};
let left_values =
Projection::projection(left_tuple, &self.on_left_keys, &self.left_schema)?;
let right_values =
Projection::projection(right_tuple, &self.on_right_keys, &self.right_schema)?;

let left_values = eval_keys(&self.on_left_keys, left_tuple, &self.left_schema)?;
let right_values = eval_keys(&self.on_right_keys, right_tuple, &self.right_schema)?;
Ok(left_values == right_values)
}
}
Expand Down Expand Up @@ -154,6 +147,8 @@ impl NestedLoopJoin {
unreachable!("{} cannot be handled in nested loop join", self.ty)
}

let right_schema_len = eq_cond.right_schema.len();

#[for_await]
for tuple in build_read(left_input, transaction) {
let left_tuple: Tuple = tuple?;
Expand Down Expand Up @@ -185,7 +180,7 @@ impl NestedLoopJoin {
tuple
}
DataValue::Boolean(Some(_) | None) => None,
_ => unreachable!(),
_ => return Err(DatabaseError::InvalidType),
}
}
_ => None,
Expand All @@ -206,7 +201,15 @@ impl NestedLoopJoin {
let tuple = match ty {
JoinType::LeftAnti if !has_matched => Some(left_tuple.clone()),
JoinType::LeftOuter | JoinType::LeftSemi | JoinType::RightOuter if !has_matched => {
Self::emit_tuple(&left_tuple, &left_tuple, ty, false)
let right_tuple = Tuple {
id: None,
values: vec![NULL_VALUE.clone(); right_schema_len],
};
if matches!(ty, JoinType::RightOuter) {
Self::emit_tuple(&right_tuple, &left_tuple, ty, false)
} else {
Self::emit_tuple(&left_tuple, &right_tuple, ty, false)
}
}
_ => None,
};
Expand Down
58 changes: 58 additions & 0 deletions tests/slt/join.slt
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,61 @@ select a.*, c.* from a inner join a as c using (id)
0 1 1 0 1 1
1 2 2 1 2 2
2 3 3 2 3 3

query IIIIIII rowsort
select a.*, b.* from a join b on v1 > v3
----
1 2 2 0 1 1 1
1 2 2 3 1 1 5
2 3 3 0 1 1 1
2 3 3 1 2 2 2
2 3 3 3 1 1 5

query IIIIIII rowsort
select a.*, b.* from a join b on v1 = 1
----
0 1 1 0 1 1 1
0 1 1 1 2 2 2
0 1 1 2 3 3 4
0 1 1 3 1 1 5

query IIIIIII rowsort
select a.*, b.* from a left join b on v1 < v3 or v2 < v4
----
0 1 1 1 2 2 2
0 1 1 2 3 3 4
1 2 2 2 3 3 4
2 3 3 null null null null

query IIIIIII rowsort
select a.*, b.* from a right join b on v1 <> v3 and v2 < v4
----
0 1 1 1 2 2 2
0 1 1 2 3 3 4
1 2 2 2 3 3 4
null null null 0 1 1 1
null null null 3 1 1 5

query IIIIIII rowsort
select a.*, b.* from a cross join b
----
0 1 1 0 1 1 1
0 1 1 1 2 2 2
0 1 1 2 3 3 4
0 1 1 3 1 1 5
1 2 2 0 1 1 1
1 2 2 1 2 2 2
1 2 2 2 3 3 4
1 2 2 3 1 1 5
2 3 3 0 1 1 1
2 3 3 1 2 2 2
2 3 3 2 3 3 4
2 3 3 3 1 1 5

query III rowsort
select a.* from a where v1 >= (select 1)
----
0 1 1
1 2 2
2 3 3

0 comments on commit 8f21af7

Please sign in to comment.