Apache DataFusion SQL Query Engine
feat: emitting partial join results in `HashJoinStream` (#8020)
Eduard Karacharovf2e67019c365
diff --git a/datafusion/physical-plan/Cargo.toml b/datafusion/physical-plan/Cargo.tomlindex 357e036b6f..1c638d9c18 100644--- a/datafusion/physical-plan/Cargo.toml+++ b/datafusion/physical-plan/Cargo.toml@@ -61,6 +61,7 @@ uuid = { version = "^1.2", features = ["v4"] }[dev-dependencies]rstest = { workspace = true }+rstest_reuse = "0.6.0"termtree = "0.4.1"tokio = { version = "1.28", features = ["macros",diff --git a/datafusion/physical-plan/src/joins/hash_join.rs b/datafusion/physical-plan/src/joins/hash_join.rsindex 374a0ad507..0c213f4257 100644--- a/datafusion/physical-plan/src/joins/hash_join.rs+++ b/datafusion/physical-plan/src/joins/hash_join.rs@@ -26,7 +26,7 @@ use std::{any::Any, usize, vec};use crate::joins::utils::{adjust_indices_by_join_type, apply_join_filter_to_indices, build_batch_from_indices,calculate_join_output_ordering, get_final_indices_from_bit_map,- need_produce_result_in_final, JoinHashMap, JoinHashMapType,+ need_produce_result_in_final, JoinHashMap, JoinHashMapOffset, JoinHashMapType,};use crate::{coalesce_partitions::CoalescePartitionsExec,@@ -61,7 +61,8 @@ use arrow::util::bit_util;use arrow_array::cast::downcast_array;use arrow_schema::ArrowError;use datafusion_common::{- exec_err, internal_err, plan_err, DataFusionError, JoinSide, JoinType, Result,+ internal_datafusion_err, internal_err, plan_err, DataFusionError, JoinSide, JoinType,+ Result,};use datafusion_execution::memory_pool::{MemoryConsumer, MemoryReservation};use datafusion_execution::TaskContext;@@ -644,6 +645,8 @@ impl ExecutionPlan for HashJoinExec {}};+ let batch_size = context.session_config().batch_size();+let reservation = MemoryConsumer::new(format!("HashJoinStream[{partition}]")).register(context.memory_pool());@@ -665,6 +668,8 @@ impl ExecutionPlan for HashJoinExec {reservation,state: HashJoinStreamState::WaitBuildSide,build_side: BuildSide::Initial(BuildSideInitialState { left_fut }),+ batch_size,+ hashes_buffer: vec![],}))}@@ -908,16 +913,10 @@ enum HashJoinStreamState {Completed,}-/// Container for HashJoinStreamState::ProcessProbeBatch related data-struct ProcessProbeBatchState {- /// Current probe-side batch- batch: RecordBatch,-}-impl HashJoinStreamState {/// Tries to extract ProcessProbeBatchState from HashJoinStreamState enum./// Returns an error if state is not ProcessProbeBatchState.- fn try_as_process_probe_batch(&self) -> Result<&ProcessProbeBatchState> {+ fn try_as_process_probe_batch_mut(&mut self) -> Result<&mut ProcessProbeBatchState> {match self {HashJoinStreamState::ProcessProbeBatch(state) => Ok(state),_ => internal_err!("Expected hash join stream in ProcessProbeBatch state"),@@ -925,6 +924,25 @@ impl HashJoinStreamState {}}+/// Container for HashJoinStreamState::ProcessProbeBatch related data+struct ProcessProbeBatchState {+ /// Current probe-side batch+ batch: RecordBatch,+ /// Starting offset for JoinHashMap lookups+ offset: JoinHashMapOffset,+ /// Max joined probe-side index from current batch+ joined_probe_idx: Option<usize>,+}++impl ProcessProbeBatchState {+ fn advance(&mut self, offset: JoinHashMapOffset, joined_probe_idx: Option<usize>) {+ self.offset = offset;+ if joined_probe_idx.is_some() {+ self.joined_probe_idx = joined_probe_idx;+ }+ }+}+/// [`Stream`] for [`HashJoinExec`] that does the actual join.////// This stream:@@ -960,6 +978,10 @@ struct HashJoinStream {state: HashJoinStreamState,/// Build sidebuild_side: BuildSide,+ /// Maximum output batch size+ batch_size: usize,+ /// Scratch space for computing hashes+ hashes_buffer: Vec<u64>,}impl RecordBatchStream for HashJoinStream {@@ -968,7 +990,10 @@ impl RecordBatchStream for HashJoinStream {}}-/// Returns build/probe indices satisfying the equality condition.+/// Executes lookups by hash against JoinHashMap and resolves potential+/// hash collisions.+/// Returns build/probe indices satisfying the equality condition, along with+/// (optional) starting point for next iteration.////// # Example///@@ -1014,20 +1039,17 @@ impl RecordBatchStream for HashJoinStream {/// Probe indices: 3, 3, 4, 5/// ```#[allow(clippy::too_many_arguments)]-pub fn build_equal_condition_join_indices<T: JoinHashMapType>(- build_hashmap: &T,+fn lookup_join_hashmap(+ build_hashmap: &JoinHashMap,build_input_buffer: &RecordBatch,probe_batch: &RecordBatch,build_on: &[Column],probe_on: &[Column],- random_state: &RandomState,null_equals_null: bool,- hashes_buffer: &mut Vec<u64>,- filter: Option<&JoinFilter>,- build_side: JoinSide,- deleted_offset: Option<usize>,- fifo_hashmap: bool,-) -> Result<(UInt64Array, UInt32Array)> {+ hashes_buffer: &[u64],+ limit: usize,+ offset: JoinHashMapOffset,+) -> Result<(UInt64Array, UInt32Array, Option<JoinHashMapOffset>)> {let keys_values = probe_on.iter().map(|c| c.evaluate(probe_batch)?.into_array(probe_batch.num_rows()))@@ -1039,76 +1061,24 @@ pub fn build_equal_condition_join_indices<T: JoinHashMapType>(.into_array(build_input_buffer.num_rows())}).collect::<Result<Vec<_>>>()?;- hashes_buffer.clear();- hashes_buffer.resize(probe_batch.num_rows(), 0);- let hash_values = create_hashes(&keys_values, random_state, hashes_buffer)?;- // In case build-side input has not been inverted while JoinHashMap creation, the chained list algorithm- // will return build indices for each probe row in a reverse order as such:- // Build Indices: [5, 4, 3]- // Probe Indices: [1, 1, 1]- //- // This affects the output sequence. Hypothetically, it's possible to preserve the lexicographic order on the build side.- // Let's consider probe rows [0,1] as an example:- //- // When the probe iteration sequence is reversed, the following pairings can be derived:- //- // For probe row 1:- // (5, 1)- // (4, 1)- // (3, 1)- //- // For probe row 0:- // (5, 0)- // (4, 0)- // (3, 0)- //- // After reversing both sets of indices, we obtain reversed indices:- //- // (3,0)- // (4,0)- // (5,0)- // (3,1)- // (4,1)- // (5,1)- //- // With this approach, the lexicographic order on both the probe side and the build side is preserved.- let (mut probe_indices, mut build_indices) = if fifo_hashmap {- build_hashmap.get_matched_indices(hash_values.iter().enumerate(), deleted_offset)- } else {- let (mut matched_probe, mut matched_build) = build_hashmap- .get_matched_indices(hash_values.iter().enumerate().rev(), deleted_offset);-- matched_probe.as_slice_mut().reverse();- matched_build.as_slice_mut().reverse();-- (matched_probe, matched_build)- };+ let (mut probe_builder, mut build_builder, next_offset) = build_hashmap+ .get_matched_indices_with_limit_offset(hashes_buffer, None, limit, offset);- let left: UInt64Array = PrimitiveArray::new(build_indices.finish().into(), None);- let right: UInt32Array = PrimitiveArray::new(probe_indices.finish().into(), None);+ let build_indices: UInt64Array =+ PrimitiveArray::new(build_builder.finish().into(), None);+ let probe_indices: UInt32Array =+ PrimitiveArray::new(probe_builder.finish().into(), None);- let (left, right) = if let Some(filter) = filter {- // Filter the indices which satisfy the non-equal join condition, like `left.b1 = 10`- apply_join_filter_to_indices(- build_input_buffer,- probe_batch,- left,- right,- filter,- build_side,- )?- } else {- (left, right)- };-- equal_rows_arr(- &left,- &right,+ let (build_indices, probe_indices) = equal_rows_arr(+ &build_indices,+ &probe_indices,&build_join_values,&keys_values,null_equals_null,- )+ )?;++ Ok((build_indices, probe_indices, next_offset))}// version of eq_dyn supporting equality on null arrays@@ -1253,9 +1223,25 @@ impl HashJoinStream {self.state = HashJoinStreamState::ExhaustedProbeSide;}Some(Ok(batch)) => {+ // Precalculate hash values for fetched batch+ let keys_values = self+ .on_right+ .iter()+ .map(|c| c.evaluate(&batch)?.into_array(batch.num_rows()))+ .collect::<Result<Vec<_>>>()?;++ self.hashes_buffer.clear();+ self.hashes_buffer.resize(batch.num_rows(), 0);+ create_hashes(&keys_values, &self.random_state, &mut self.hashes_buffer)?;++ self.join_metrics.input_batches.add(1);+ self.join_metrics.input_rows.add(batch.num_rows());+self.state =HashJoinStreamState::ProcessProbeBatch(ProcessProbeBatchState {batch,+ offset: (0, None),+ joined_probe_idx: None,});}Some(Err(err)) => return Poll::Ready(Err(err)),@@ -1270,70 +1256,108 @@ impl HashJoinStream {fn process_probe_batch(&mut self,) -> Result<StatefulStreamResult<Option<RecordBatch>>> {- let state = self.state.try_as_process_probe_batch()?;+ let state = self.state.try_as_process_probe_batch_mut()?;let build_side = self.build_side.try_as_ready_mut()?;- self.join_metrics.input_batches.add(1);- self.join_metrics.input_rows.add(state.batch.num_rows());let timer = self.join_metrics.join_time.timer();- let mut hashes_buffer = vec![];- // get the matched two indices for the on condition- let left_right_indices = build_equal_condition_join_indices(+ // get the matched by join keys indices+ let (left_indices, right_indices, next_offset) = lookup_join_hashmap(build_side.left_data.hash_map(),build_side.left_data.batch(),&state.batch,&self.on_left,&self.on_right,- &self.random_state,self.null_equals_null,- &mut hashes_buffer,- self.filter.as_ref(),- JoinSide::Left,- None,- true,- );+ &self.hashes_buffer,+ self.batch_size,+ state.offset,+ )?;- let result = match left_right_indices {- Ok((left_side, right_side)) => {- // set the left bitmap- // and only left, full, left semi, left anti need the left bitmap- if need_produce_result_in_final(self.join_type) {- left_side.iter().flatten().for_each(|x| {- build_side.visited_left_side.set_bit(x as usize, true);- });- }+ // apply join filter if exists+ let (left_indices, right_indices) = if let Some(filter) = &self.filter {+ apply_join_filter_to_indices(+ build_side.left_data.batch(),+ &state.batch,+ left_indices,+ right_indices,+ filter,+ JoinSide::Left,+ )?+ } else {+ (left_indices, right_indices)+ };- // adjust the two side indices base on the join type- let (left_side, right_side) = adjust_indices_by_join_type(- left_side,- right_side,- state.batch.num_rows(),- self.join_type,- );+ // mark joined left-side indices as visited, if required by join type+ if need_produce_result_in_final(self.join_type) {+ left_indices.iter().flatten().for_each(|x| {+ build_side.visited_left_side.set_bit(x as usize, true);+ });+ }- let result = build_batch_from_indices(- &self.schema,- build_side.left_data.batch(),- &state.batch,- &left_side,- &right_side,- &self.column_indices,- JoinSide::Left,- );- self.join_metrics.output_batches.add(1);- self.join_metrics.output_rows.add(state.batch.num_rows());- result- }- Err(err) => {- exec_err!("Fail to build join indices in HashJoinExec, error:{err}")- }+ // The goals of index alignment for different join types are:+ //+ // 1) Right & FullJoin -- to append all missing probe-side indices between+ // previous (excluding) and current joined indices.+ // 2) SemiJoin -- deduplicate probe indices in range between previous+ // (excluding) and current joined indices.+ // 3) AntiJoin -- return only missing indices in range between+ // previous and current joined indices.+ // Inclusion/exclusion of the indices themselves don't matter+ //+ // As a summary -- alignment range can be produced based only on+ // joined (matched with filters applied) probe side indices, excluding starting one+ // (left from previous iteration).++ // if any rows have been joined -- get last joined probe-side (right) row+ // it's important that index counts as "joined" after hash collisions checks+ // and join filters applied.+ let last_joined_right_idx = match right_indices.len() {+ 0 => None,+ n => Some(right_indices.value(n - 1) as usize),};++ // Calculate range and perform alignment.+ // In case probe batch has been processed -- align all remaining rows.+ let index_alignment_range_start = state.joined_probe_idx.map_or(0, |v| v + 1);+ let index_alignment_range_end = if next_offset.is_none() {+ state.batch.num_rows()+ } else {+ last_joined_right_idx.map_or(0, |v| v + 1)+ };++ let (left_indices, right_indices) = adjust_indices_by_join_type(+ left_indices,+ right_indices,+ index_alignment_range_start..index_alignment_range_end,+ self.join_type,+ );++ let result = build_batch_from_indices(+ &self.schema,+ build_side.left_data.batch(),+ &state.batch,+ &left_indices,+ &right_indices,+ &self.column_indices,+ JoinSide::Left,+ )?;++ self.join_metrics.output_batches.add(1);+ self.join_metrics.output_rows.add(result.num_rows());timer.done();- self.state = HashJoinStreamState::FetchProbeBatch;+ if next_offset.is_none() {+ self.state = HashJoinStreamState::FetchProbeBatch;+ } else {+ state.advance(+ next_offset+ .ok_or_else(|| internal_datafusion_err!("unexpected None offset"))?,+ last_joined_right_idx,+ )+ };- Ok(StatefulStreamResult::Ready(Some(result?)))+ Ok(StatefulStreamResult::Ready(Some(result)))}/// Processes unmatched build-side rows for certain join types and produces output batch@@ -1399,15 +1423,15 @@ mod tests {use super::*;use crate::{- common, expressions::Column, hash_utils::create_hashes,- joins::hash_join::build_equal_condition_join_indices, memory::MemoryExec,+ common, expressions::Column, hash_utils::create_hashes, memory::MemoryExec,repartition::RepartitionExec, test::build_table_i32, test::exec::MockExec,};use arrow::array::{ArrayRef, Date32Array, Int32Array, UInt32Builder, UInt64Builder};use arrow::datatypes::{DataType, Field, Schema};use datafusion_common::{- assert_batches_eq, assert_batches_sorted_eq, assert_contains, ScalarValue,+ assert_batches_eq, assert_batches_sorted_eq, assert_contains, exec_err,+ ScalarValue,};use datafusion_execution::config::SessionConfig;use datafusion_execution::runtime_env::{RuntimeConfig, RuntimeEnv};@@ -1415,6 +1439,21 @@ mod tests {use datafusion_physical_expr::expressions::{BinaryExpr, Literal};use hashbrown::raw::RawTable;+ use rstest::*;+ use rstest_reuse::{self, *};++ fn div_ceil(a: usize, b: usize) -> usize {+ (a + b - 1) / b+ }++ #[template]+ #[rstest]+ fn batch_sizes(#[values(8192, 10, 5, 2, 1)] batch_size: usize) {}++ fn prepare_task_ctx(batch_size: usize) -> Arc<TaskContext> {+ let session_config = SessionConfig::default().with_batch_size(batch_size);+ Arc::new(TaskContext::default().with_session_config(session_config))+ }fn build_table(a: (&str, &Vec<i32>),@@ -1533,9 +1572,10 @@ mod tests {Ok((columns, batches))}+ #[apply(batch_sizes)]#[tokio::test]- async fn join_inner_one() -> Result<()> {- let task_ctx = Arc::new(TaskContext::default());+ async fn join_inner_one(batch_size: usize) -> Result<()> {+ let task_ctx = prepare_task_ctx(batch_size);let left = build_table(("a1", &vec![1, 2, 3]),("b1", &vec![4, 5, 5]), // this has a repetition@@ -1580,9 +1620,10 @@ mod tests {Ok(())}+ #[apply(batch_sizes)]#[tokio::test]- async fn partitioned_join_inner_one() -> Result<()> {- let task_ctx = Arc::new(TaskContext::default());+ async fn partitioned_join_inner_one(batch_size: usize) -> Result<()> {+ let task_ctx = prepare_task_ctx(batch_size);let left = build_table(("a1", &vec![1, 2, 3]),("b1", &vec![4, 5, 5]), // this has a repetition@@ -1703,9 +1744,10 @@ mod tests {Ok(())}+ #[apply(batch_sizes)]#[tokio::test]- async fn join_inner_two() -> Result<()> {- let task_ctx = Arc::new(TaskContext::default());+ async fn join_inner_two(batch_size: usize) -> Result<()> {+ let task_ctx = prepare_task_ctx(batch_size);let left = build_table(("a1", &vec![1, 2, 2]),("b2", &vec![1, 2, 2]),@@ -1732,7 +1774,13 @@ mod tests {assert_eq!(columns, vec!["a1", "b2", "c1", "a1", "b2", "c2"]);- assert_eq!(batches.len(), 1);+ // expected joined records = 3+ // in case batch_size is 1 - additional empty batch for remaining 3-2 row+ let mut expected_batch_count = div_ceil(3, batch_size);+ if batch_size == 1 {+ expected_batch_count += 1;+ }+ assert_eq!(batches.len(), expected_batch_count);let expected = ["+----+----+----+----+----+----+",@@ -1751,9 +1799,10 @@ mod tests {}/// Test where the left has 2 parts, the right with 1 part => 1 part+ #[apply(batch_sizes)]#[tokio::test]- async fn join_inner_one_two_parts_left() -> Result<()> {- let task_ctx = Arc::new(TaskContext::default());+ async fn join_inner_one_two_parts_left(batch_size: usize) -> Result<()> {+ let task_ctx = prepare_task_ctx(batch_size);let batch1 = build_table_i32(("a1", &vec![1, 2]),("b2", &vec![1, 2]),@@ -1787,7 +1836,13 @@ mod tests {assert_eq!(columns, vec!["a1", "b2", "c1", "a1", "b2", "c2"]);- assert_eq!(batches.len(), 1);+ // expected joined records = 3+ // in case batch_size is 1 - additional empty batch for remaining 3-2 row+ let mut expected_batch_count = div_ceil(3, batch_size);+ if batch_size == 1 {+ expected_batch_count += 1;+ }+ assert_eq!(batches.len(), expected_batch_count);let expected = ["+----+----+----+----+----+----+",@@ -1856,9 +1911,10 @@ mod tests {}/// Test where the left has 1 part, the right has 2 parts => 2 parts+ #[apply(batch_sizes)]#[tokio::test]- async fn join_inner_one_two_parts_right() -> Result<()> {- let task_ctx = Arc::new(TaskContext::default());+ async fn join_inner_one_two_parts_right(batch_size: usize) -> Result<()> {+ let task_ctx = prepare_task_ctx(batch_size);let left = build_table(("a1", &vec![1, 2, 3]),("b1", &vec![4, 5, 5]), // this has a repetition@@ -1890,7 +1946,14 @@ mod tests {// first partlet stream = join.execute(0, task_ctx.clone())?;let batches = common::collect(stream).await?;- assert_eq!(batches.len(), 1);++ // expected joined records = 1 (first right batch)+ // and additional empty batch for non-joined 20-6-80+ let mut expected_batch_count = div_ceil(1, batch_size);+ if batch_size == 1 {+ expected_batch_count += 1;+ }+ assert_eq!(batches.len(), expected_batch_count);let expected = ["+----+----+----+----+----+----+",@@ -1906,7 +1969,11 @@ mod tests {// second partlet stream = join.execute(1, task_ctx.clone())?;let batches = common::collect(stream).await?;- assert_eq!(batches.len(), 1);++ // expected joined records = 2 (second right batch)+ let expected_batch_count = div_ceil(2, batch_size);+ assert_eq!(batches.len(), expected_batch_count);+let expected = ["+----+----+----+----+----+----+","| a1 | b1 | c1 | a2 | b1 | c2 |",@@ -1934,9 +2001,10 @@ mod tests {)}+ #[apply(batch_sizes)]#[tokio::test]- async fn join_left_multi_batch() {- let task_ctx = Arc::new(TaskContext::default());+ async fn join_left_multi_batch(batch_size: usize) {+ let task_ctx = prepare_task_ctx(batch_size);let left = build_table(("a1", &vec![1, 2, 3]),("b1", &vec![4, 5, 7]), // 7 does not exist on the right@@ -1975,9 +2043,10 @@ mod tests {assert_batches_sorted_eq!(expected, &batches);}+ #[apply(batch_sizes)]#[tokio::test]- async fn join_full_multi_batch() {- let task_ctx = Arc::new(TaskContext::default());+ async fn join_full_multi_batch(batch_size: usize) {+ let task_ctx = prepare_task_ctx(batch_size);let left = build_table(("a1", &vec![1, 2, 3]),("b1", &vec![4, 5, 7]), // 7 does not exist on the right@@ -2019,9 +2088,10 @@ mod tests {assert_batches_sorted_eq!(expected, &batches);}+ #[apply(batch_sizes)]#[tokio::test]- async fn join_left_empty_right() {- let task_ctx = Arc::new(TaskContext::default());+ async fn join_left_empty_right(batch_size: usize) {+ let task_ctx = prepare_task_ctx(batch_size);let left = build_table(("a1", &vec![1, 2, 3]),("b1", &vec![4, 5, 7]),@@ -2055,9 +2125,10 @@ mod tests {assert_batches_sorted_eq!(expected, &batches);}+ #[apply(batch_sizes)]#[tokio::test]- async fn join_full_empty_right() {- let task_ctx = Arc::new(TaskContext::default());+ async fn join_full_empty_right(batch_size: usize) {+ let task_ctx = prepare_task_ctx(batch_size);let left = build_table(("a1", &vec![1, 2, 3]),("b1", &vec![4, 5, 7]),@@ -2091,9 +2162,10 @@ mod tests {assert_batches_sorted_eq!(expected, &batches);}+ #[apply(batch_sizes)]#[tokio::test]- async fn join_left_one() -> Result<()> {- let task_ctx = Arc::new(TaskContext::default());+ async fn join_left_one(batch_size: usize) -> Result<()> {+ let task_ctx = prepare_task_ctx(batch_size);let left = build_table(("a1", &vec![1, 2, 3]),("b1", &vec![4, 5, 7]), // 7 does not exist on the right@@ -2134,9 +2206,10 @@ mod tests {Ok(())}+ #[apply(batch_sizes)]#[tokio::test]- async fn partitioned_join_left_one() -> Result<()> {- let task_ctx = Arc::new(TaskContext::default());+ async fn partitioned_join_left_one(batch_size: usize) -> Result<()> {+ let task_ctx = prepare_task_ctx(batch_size);let left = build_table(("a1", &vec![1, 2, 3]),("b1", &vec![4, 5, 7]), // 7 does not exist on the right@@ -2197,9 +2270,10 @@ mod tests {)}+ #[apply(batch_sizes)]#[tokio::test]- async fn join_left_semi() -> Result<()> {- let task_ctx = Arc::new(TaskContext::default());+ async fn join_left_semi(batch_size: usize) -> Result<()> {+ let task_ctx = prepare_task_ctx(batch_size);let left = build_semi_anti_left_table();let right = build_semi_anti_right_table();// left_table left semi join right_table on left_table.b1 = right_table.b2@@ -2231,9 +2305,10 @@ mod tests {Ok(())}+ #[apply(batch_sizes)]#[tokio::test]- async fn join_left_semi_with_filter() -> Result<()> {- let task_ctx = Arc::new(TaskContext::default());+ async fn join_left_semi_with_filter(batch_size: usize) -> Result<()> {+ let task_ctx = prepare_task_ctx(batch_size);let left = build_semi_anti_left_table();let right = build_semi_anti_right_table();@@ -2317,9 +2392,10 @@ mod tests {Ok(())}+ #[apply(batch_sizes)]#[tokio::test]- async fn join_right_semi() -> Result<()> {- let task_ctx = Arc::new(TaskContext::default());+ async fn join_right_semi(batch_size: usize) -> Result<()> {+ let task_ctx = prepare_task_ctx(batch_size);let left = build_semi_anti_left_table();let right = build_semi_anti_right_table();@@ -2353,9 +2429,10 @@ mod tests {Ok(())}+ #[apply(batch_sizes)]#[tokio::test]- async fn join_right_semi_with_filter() -> Result<()> {- let task_ctx = Arc::new(TaskContext::default());+ async fn join_right_semi_with_filter(batch_size: usize) -> Result<()> {+ let task_ctx = prepare_task_ctx(batch_size);let left = build_semi_anti_left_table();let right = build_semi_anti_right_table();@@ -2442,9 +2519,10 @@ mod tests {Ok(())}+ #[apply(batch_sizes)]#[tokio::test]- async fn join_left_anti() -> Result<()> {- let task_ctx = Arc::new(TaskContext::default());+ async fn join_left_anti(batch_size: usize) -> Result<()> {+ let task_ctx = prepare_task_ctx(batch_size);let left = build_semi_anti_left_table();let right = build_semi_anti_right_table();// left_table left anti join right_table on left_table.b1 = right_table.b2@@ -2475,9 +2553,10 @@ mod tests {Ok(())}+ #[apply(batch_sizes)]#[tokio::test]- async fn join_left_anti_with_filter() -> Result<()> {- let task_ctx = Arc::new(TaskContext::default());+ async fn join_left_anti_with_filter(batch_size: usize) -> Result<()> {+ let task_ctx = prepare_task_ctx(batch_size);let left = build_semi_anti_left_table();let right = build_semi_anti_right_table();// left_table left anti join right_table on left_table.b1 = right_table.b2 and right_table.a2!=8@@ -2568,9 +2647,10 @@ mod tests {Ok(())}+ #[apply(batch_sizes)]#[tokio::test]- async fn join_right_anti() -> Result<()> {- let task_ctx = Arc::new(TaskContext::default());+ async fn join_right_anti(batch_size: usize) -> Result<()> {+ let task_ctx = prepare_task_ctx(batch_size);let left = build_semi_anti_left_table();let right = build_semi_anti_right_table();let on = vec![(@@ -2601,9 +2681,10 @@ mod tests {Ok(())}+ #[apply(batch_sizes)]#[tokio::test]- async fn join_right_anti_with_filter() -> Result<()> {- let task_ctx = Arc::new(TaskContext::default());+ async fn join_right_anti_with_filter(batch_size: usize) -> Result<()> {+ let task_ctx = prepare_task_ctx(batch_size);let left = build_semi_anti_left_table();let right = build_semi_anti_right_table();// left_table right anti join right_table on left_table.b1 = right_table.b2 and left_table.a1!=13@@ -2701,9 +2782,10 @@ mod tests {Ok(())}+ #[apply(batch_sizes)]#[tokio::test]- async fn join_right_one() -> Result<()> {- let task_ctx = Arc::new(TaskContext::default());+ async fn join_right_one(batch_size: usize) -> Result<()> {+ let task_ctx = prepare_task_ctx(batch_size);let left = build_table(("a1", &vec![1, 2, 3]),("b1", &vec![4, 5, 7]),@@ -2739,9 +2821,10 @@ mod tests {Ok(())}+ #[apply(batch_sizes)]#[tokio::test]- async fn partitioned_join_right_one() -> Result<()> {- let task_ctx = Arc::new(TaskContext::default());+ async fn partitioned_join_right_one(batch_size: usize) -> Result<()> {+ let task_ctx = prepare_task_ctx(batch_size);let left = build_table(("a1", &vec![1, 2, 3]),("b1", &vec![4, 5, 7]),@@ -2778,9 +2861,10 @@ mod tests {Ok(())}+ #[apply(batch_sizes)]#[tokio::test]- async fn join_full_one() -> Result<()> {- let task_ctx = Arc::new(TaskContext::default());+ async fn join_full_one(batch_size: usize) -> Result<()> {+ let task_ctx = prepare_task_ctx(batch_size);let left = build_table(("a1", &vec![1, 2, 3]),("b1", &vec![4, 5, 7]), // 7 does not exist on the right@@ -2845,21 +2929,26 @@ mod tests {("c", &vec![30, 40]),);+ // Join key column for both join sides+ let key_column = Column::new("a", 0);+let join_hash_map = JoinHashMap::new(hashmap_left, next);- let (l, r) = build_equal_condition_join_indices(+ let right_keys_values =+ key_column.evaluate(&right)?.into_array(right.num_rows())?;+ let mut hashes_buffer = vec![0; right.num_rows()];+ create_hashes(&[right_keys_values], &random_state, &mut hashes_buffer)?;++ let (l, r, _) = lookup_join_hashmap(&join_hash_map,&left,&right,- &[Column::new("a", 0)],- &[Column::new("a", 0)],- &random_state,- false,- &mut vec![0; right.num_rows()],- None,- JoinSide::Left,- None,+ &[key_column.clone()],+ &[key_column],false,+ &hashes_buffer,+ 8192,+ (0, None),)?;let mut left_ids = UInt64Builder::with_capacity(0);@@ -2941,9 +3030,10 @@ mod tests {JoinFilter::new(filter_expression, column_indices, intermediate_schema)}+ #[apply(batch_sizes)]#[tokio::test]- async fn join_inner_with_filter() -> Result<()> {- let task_ctx = Arc::new(TaskContext::default());+ async fn join_inner_with_filter(batch_size: usize) -> Result<()> {+ let task_ctx = prepare_task_ctx(batch_size);let left = build_table(("a", &vec![0, 1, 2, 2]),("b", &vec![4, 5, 7, 8]),@@ -2981,9 +3071,10 @@ mod tests {Ok(())}+ #[apply(batch_sizes)]#[tokio::test]- async fn join_left_with_filter() -> Result<()> {- let task_ctx = Arc::new(TaskContext::default());+ async fn join_left_with_filter(batch_size: usize) -> Result<()> {+ let task_ctx = prepare_task_ctx(batch_size);let left = build_table(("a", &vec![0, 1, 2, 2]),("b", &vec![4, 5, 7, 8]),@@ -3024,9 +3115,10 @@ mod tests {Ok(())}+ #[apply(batch_sizes)]#[tokio::test]- async fn join_right_with_filter() -> Result<()> {- let task_ctx = Arc::new(TaskContext::default());+ async fn join_right_with_filter(batch_size: usize) -> Result<()> {+ let task_ctx = prepare_task_ctx(batch_size);let left = build_table(("a", &vec![0, 1, 2, 2]),("b", &vec![4, 5, 7, 8]),@@ -3066,9 +3158,10 @@ mod tests {Ok(())}+ #[apply(batch_sizes)]#[tokio::test]- async fn join_full_with_filter() -> Result<()> {- let task_ctx = Arc::new(TaskContext::default());+ async fn join_full_with_filter(batch_size: usize) -> Result<()> {+ let task_ctx = prepare_task_ctx(batch_size);let left = build_table(("a", &vec![0, 1, 2, 2]),("b", &vec![4, 5, 7, 8]),@@ -3211,6 +3304,140 @@ mod tests {}}+ #[tokio::test]+ async fn join_splitted_batch() {+ let left = build_table(+ ("a1", &vec![1, 2, 3, 4]),+ ("b1", &vec![1, 1, 1, 1]),+ ("c1", &vec![0, 0, 0, 0]),+ );+ let right = build_table(+ ("a2", &vec![10, 20, 30, 40, 50]),+ ("b2", &vec![1, 1, 1, 1, 1]),+ ("c2", &vec![0, 0, 0, 0, 0]),+ );+ let on = vec![(+ Column::new_with_schema("b1", &left.schema()).unwrap(),+ Column::new_with_schema("b2", &right.schema()).unwrap(),+ )];++ let join_types = vec![+ JoinType::Inner,+ JoinType::Left,+ JoinType::Right,+ JoinType::Full,+ JoinType::RightSemi,+ JoinType::RightAnti,+ JoinType::LeftSemi,+ JoinType::LeftAnti,+ ];+ let expected_resultset_records = 20;+ let common_result = [+ "+----+----+----+----+----+----+",+ "| a1 | b1 | c1 | a2 | b2 | c2 |",+ "+----+----+----+----+----+----+",+ "| 1 | 1 | 0 | 10 | 1 | 0 |",+ "| 2 | 1 | 0 | 10 | 1 | 0 |",+ "| 3 | 1 | 0 | 10 | 1 | 0 |",+ "| 4 | 1 | 0 | 10 | 1 | 0 |",+ "| 1 | 1 | 0 | 20 | 1 | 0 |",+ "| 2 | 1 | 0 | 20 | 1 | 0 |",+ "| 3 | 1 | 0 | 20 | 1 | 0 |",+ "| 4 | 1 | 0 | 20 | 1 | 0 |",+ "| 1 | 1 | 0 | 30 | 1 | 0 |",+ "| 2 | 1 | 0 | 30 | 1 | 0 |",+ "| 3 | 1 | 0 | 30 | 1 | 0 |",+ "| 4 | 1 | 0 | 30 | 1 | 0 |",+ "| 1 | 1 | 0 | 40 | 1 | 0 |",+ "| 2 | 1 | 0 | 40 | 1 | 0 |",+ "| 3 | 1 | 0 | 40 | 1 | 0 |",+ "| 4 | 1 | 0 | 40 | 1 | 0 |",+ "| 1 | 1 | 0 | 50 | 1 | 0 |",+ "| 2 | 1 | 0 | 50 | 1 | 0 |",+ "| 3 | 1 | 0 | 50 | 1 | 0 |",+ "| 4 | 1 | 0 | 50 | 1 | 0 |",+ "+----+----+----+----+----+----+",+ ];+ let left_batch = [+ "+----+----+----+",+ "| a1 | b1 | c1 |",+ "+----+----+----+",+ "| 1 | 1 | 0 |",+ "| 2 | 1 | 0 |",+ "| 3 | 1 | 0 |",+ "| 4 | 1 | 0 |",+ "+----+----+----+",+ ];+ let right_batch = [+ "+----+----+----+",+ "| a2 | b2 | c2 |",+ "+----+----+----+",+ "| 10 | 1 | 0 |",+ "| 20 | 1 | 0 |",+ "| 30 | 1 | 0 |",+ "| 40 | 1 | 0 |",+ "| 50 | 1 | 0 |",+ "+----+----+----+",+ ];+ let right_empty = [+ "+----+----+----+",+ "| a2 | b2 | c2 |",+ "+----+----+----+",+ "+----+----+----+",+ ];+ let left_empty = [+ "+----+----+----+",+ "| a1 | b1 | c1 |",+ "+----+----+----+",+ "+----+----+----+",+ ];++ // validation of partial join results output for different batch_size setting+ for join_type in join_types {+ for batch_size in (1..21).rev() {+ let task_ctx = prepare_task_ctx(batch_size);++ let join =+ join(left.clone(), right.clone(), on.clone(), &join_type, false)+ .unwrap();++ let stream = join.execute(0, task_ctx).unwrap();+ let batches = common::collect(stream).await.unwrap();++ // For inner/right join expected batch count equals dev_ceil result,+ // as there is no need to append non-joined build side data.+ // For other join types it'll be div_ceil + 1 -- for additional batch+ // containing not visited build side rows (empty in this test case).+ let expected_batch_count = match join_type {+ JoinType::Inner+ | JoinType::Right+ | JoinType::RightSemi+ | JoinType::RightAnti => {+ (expected_resultset_records + batch_size - 1) / batch_size+ }+ _ => (expected_resultset_records + batch_size - 1) / batch_size + 1,+ };+ assert_eq!(+ batches.len(),+ expected_batch_count,+ "expected {} output batches for {} join with batch_size = {}",+ expected_batch_count,+ join_type,+ batch_size+ );++ let expected = match join_type {+ JoinType::RightSemi => right_batch.to_vec(),+ JoinType::RightAnti => right_empty.to_vec(),+ JoinType::LeftSemi => left_batch.to_vec(),+ JoinType::LeftAnti => left_empty.to_vec(),+ _ => common_result.to_vec(),+ };+ assert_batches_eq!(expected, &batches);+ }+ }+ }+#[tokio::test]async fn single_partition_join_overallocation() -> Result<()> {let left = build_table(diff --git a/datafusion/physical-plan/src/joins/nested_loop_join.rs b/datafusion/physical-plan/src/joins/nested_loop_join.rsindex 6951642ff8..f89a2445fd 100644--- a/datafusion/physical-plan/src/joins/nested_loop_join.rs+++ b/datafusion/physical-plan/src/joins/nested_loop_join.rs@@ -28,9 +28,9 @@ use crate::coalesce_batches::concat_batches;use crate::joins::utils::{append_right_indices, apply_join_filter_to_indices, build_batch_from_indices,build_join_schema, check_join_is_valid, estimate_join_statistics, get_anti_indices,- get_anti_u64_indices, get_final_indices_from_bit_map, get_semi_indices,- get_semi_u64_indices, partitioned_join_output_partitioning, BuildProbeJoinMetrics,- ColumnIndex, JoinFilter, OnceAsync, OnceFut,+ get_final_indices_from_bit_map, get_semi_indices,+ partitioned_join_output_partitioning, BuildProbeJoinMetrics, ColumnIndex, JoinFilter,+ OnceAsync, OnceFut,};use crate::metrics::{ExecutionPlanMetricsSet, MetricsSet};use crate::{@@ -649,20 +649,20 @@ fn adjust_indices_by_join_type(// matched// unmatched left row will be produced in this batchlet left_unmatched_indices =- get_anti_u64_indices(count_left_batch, &left_indices);+ get_anti_indices(0..count_left_batch, &left_indices);// combine the matched and unmatched left result togetherappend_left_indices(left_indices, right_indices, left_unmatched_indices)}JoinType::LeftSemi => {// need to remove the duplicated record in the left side- let left_indices = get_semi_u64_indices(count_left_batch, &left_indices);+ let left_indices = get_semi_indices(0..count_left_batch, &left_indices);// the right_indices will not be used later for the `left semi` join(left_indices, right_indices)}JoinType::LeftAnti => {// need to remove the duplicated record in the left side// get the anti index for the left side- let left_indices = get_anti_u64_indices(count_left_batch, &left_indices);+ let left_indices = get_anti_indices(0..count_left_batch, &left_indices);// the right_indices will not be used later for the `left anti` join(left_indices, right_indices)}@@ -671,20 +671,20 @@ fn adjust_indices_by_join_type(// matched// unmatched right row will be produced in this batchlet right_unmatched_indices =- get_anti_indices(count_right_batch, &right_indices);+ get_anti_indices(0..count_right_batch, &right_indices);// combine the matched and unmatched right result togetherappend_right_indices(left_indices, right_indices, right_unmatched_indices)}JoinType::RightSemi => {// need to remove the duplicated record in the right side- let right_indices = get_semi_indices(count_right_batch, &right_indices);+ let right_indices = get_semi_indices(0..count_right_batch, &right_indices);// the left_indices will not be used later for the `right semi` join(left_indices, right_indices)}JoinType::RightAnti => {// need to remove the duplicated record in the right side// get the anti index for the right side- let right_indices = get_anti_indices(count_right_batch, &right_indices);+ let right_indices = get_anti_indices(0..count_right_batch, &right_indices);// the left_indices will not be used later for the `right anti` join(left_indices, right_indices)}diff --git a/datafusion/physical-plan/src/joins/symmetric_hash_join.rs b/datafusion/physical-plan/src/joins/symmetric_hash_join.rsindex 7719c72774..00950f0825 100644--- a/datafusion/physical-plan/src/joins/symmetric_hash_join.rs+++ b/datafusion/physical-plan/src/joins/symmetric_hash_join.rs@@ -32,7 +32,7 @@ use std::task::Poll;use std::{usize, vec};use crate::common::SharedMemoryReservation;-use crate::joins::hash_join::{build_equal_condition_join_indices, update_hash};+use crate::joins::hash_join::{equal_rows_arr, update_hash};use crate::joins::stream_join_utils::{calculate_filter_expr_intervals, combine_two_batches,convert_sort_expr_with_filter_schema, get_pruning_anti_indices,@@ -41,22 +41,26 @@ use crate::joins::stream_join_utils::{StreamJoinMetrics,};use crate::joins::utils::{- build_batch_from_indices, build_join_schema, check_join_is_valid,- partitioned_join_output_partitioning, ColumnIndex, JoinFilter, JoinOn,- StatefulStreamResult,+ apply_join_filter_to_indices, build_batch_from_indices, build_join_schema,+ check_join_is_valid, partitioned_join_output_partitioning, ColumnIndex, JoinFilter,+ JoinHashMapType, JoinOn, StatefulStreamResult,};use crate::{expressions::{Column, PhysicalSortExpr},joins::StreamJoinPartitionMode,metrics::{ExecutionPlanMetricsSet, MetricsSet},DisplayAs, DisplayFormatType, Distribution, EquivalenceProperties, ExecutionPlan,- Partitioning, RecordBatchStream, SendableRecordBatchStream, Statistics,+ Partitioning, PhysicalExpr, RecordBatchStream, SendableRecordBatchStream, Statistics,};-use arrow::array::{ArrowPrimitiveType, NativeAdapter, PrimitiveArray, PrimitiveBuilder};+use arrow::array::{+ ArrowPrimitiveType, NativeAdapter, PrimitiveArray, PrimitiveBuilder, UInt32Array,+ UInt64Array,+};use arrow::compute::concat_batches;use arrow::datatypes::{Schema, SchemaRef};use arrow::record_batch::RecordBatch;+use datafusion_common::hash_utils::create_hashes;use datafusion_common::utils::bisect;use datafusion_common::{internal_err, plan_err, DataFusionError, JoinSide, JoinType, Result,@@ -785,7 +789,7 @@ pub(crate) fn join_with_probe_batch(if build_hash_joiner.input_buffer.num_rows() == 0 || probe_batch.num_rows() == 0 {return Ok(None);}- let (build_indices, probe_indices) = build_equal_condition_join_indices(+ let (build_indices, probe_indices) = lookup_join_hashmap(&build_hash_joiner.hashmap,&build_hash_joiner.input_buffer,probe_batch,@@ -794,11 +798,22 @@ pub(crate) fn join_with_probe_batch(random_state,null_equals_null,&mut build_hash_joiner.hashes_buffer,- filter,- build_hash_joiner.build_side,Some(build_hash_joiner.deleted_offset),- false,)?;++ let (build_indices, probe_indices) = if let Some(filter) = filter {+ apply_join_filter_to_indices(+ &build_hash_joiner.input_buffer,+ probe_batch,+ build_indices,+ probe_indices,+ filter,+ build_hash_joiner.build_side,+ )?+ } else {+ (build_indices, probe_indices)+ };+if need_to_produce_result_in_final(build_hash_joiner.build_side, join_type) {record_visited_indices(&mut build_hash_joiner.visited_rows,@@ -835,6 +850,102 @@ pub(crate) fn join_with_probe_batch(}}+/// This method performs lookups against JoinHashMap by hash values of join-key columns, and handles potential+/// hash collisions.+///+/// # Arguments+///+/// * `build_hashmap` - hashmap collected from build side data.+/// * `build_batch` - Build side record batch.+/// * `probe_batch` - Probe side record batch.+/// * `build_on` - An array of columns on which the join will be performed. The columns are from the build side of the join.+/// * `probe_on` - An array of columns on which the join will be performed. The columns are from the probe side of the join.+/// * `random_state` - The random state for the join.+/// * `null_equals_null` - A boolean indicating whether NULL values should be treated as equal when joining.+/// * `hashes_buffer` - Buffer used for probe side keys hash calculation.+/// * `deleted_offset` - deleted offset for build side data.+///+/// # Returns+///+/// A [Result] containing a tuple with two equal length arrays, representing indices of rows from build and probe side,+/// matched by join key columns.+#[allow(clippy::too_many_arguments)]+fn lookup_join_hashmap(+ build_hashmap: &PruningJoinHashMap,+ build_batch: &RecordBatch,+ probe_batch: &RecordBatch,+ build_on: &[Column],+ probe_on: &[Column],+ random_state: &RandomState,+ null_equals_null: bool,+ hashes_buffer: &mut Vec<u64>,+ deleted_offset: Option<usize>,+) -> Result<(UInt64Array, UInt32Array)> {+ let keys_values = probe_on+ .iter()+ .map(|c| c.evaluate(probe_batch)?.into_array(probe_batch.num_rows()))+ .collect::<Result<Vec<_>>>()?;+ let build_join_values = build_on+ .iter()+ .map(|c| c.evaluate(build_batch)?.into_array(build_batch.num_rows()))+ .collect::<Result<Vec<_>>>()?;++ hashes_buffer.clear();+ hashes_buffer.resize(probe_batch.num_rows(), 0);+ let hash_values = create_hashes(&keys_values, random_state, hashes_buffer)?;++ // As SymmetricHashJoin uses LIFO JoinHashMap, the chained list algorithm+ // will return build indices for each probe row in a reverse order as such:+ // Build Indices: [5, 4, 3]+ // Probe Indices: [1, 1, 1]+ //+ // This affects the output sequence. Hypothetically, it's possible to preserve the lexicographic order on the build side.+ // Let's consider probe rows [0,1] as an example:+ //+ // When the probe iteration sequence is reversed, the following pairings can be derived:+ //+ // For probe row 1:+ // (5, 1)+ // (4, 1)+ // (3, 1)+ //+ // For probe row 0:+ // (5, 0)+ // (4, 0)+ // (3, 0)+ //+ // After reversing both sets of indices, we obtain reversed indices:+ //+ // (3,0)+ // (4,0)+ // (5,0)+ // (3,1)+ // (4,1)+ // (5,1)+ //+ // With this approach, the lexicographic order on both the probe side and the build side is preserved.+ let (mut matched_probe, mut matched_build) = build_hashmap+ .get_matched_indices(hash_values.iter().enumerate().rev(), deleted_offset);++ matched_probe.as_slice_mut().reverse();+ matched_build.as_slice_mut().reverse();++ let build_indices: UInt64Array =+ PrimitiveArray::new(matched_build.finish().into(), None);+ let probe_indices: UInt32Array =+ PrimitiveArray::new(matched_probe.finish().into(), None);++ let (build_indices, probe_indices) = equal_rows_arr(+ &build_indices,+ &probe_indices,+ &build_join_values,+ &keys_values,+ null_equals_null,+ )?;++ Ok((build_indices, probe_indices))+}+pub struct OneSideHashJoiner {/// Build sidebuild_side: JoinSide,diff --git a/datafusion/physical-plan/src/joins/utils.rs b/datafusion/physical-plan/src/joins/utils.rsindex 1e3cf5abb4..6ab08d3db0 100644--- a/datafusion/physical-plan/src/joins/utils.rs+++ b/datafusion/physical-plan/src/joins/utils.rs@@ -20,7 +20,7 @@use std::collections::HashSet;use std::fmt::{self, Debug};use std::future::Future;-use std::ops::IndexMut;+use std::ops::{IndexMut, Range};use std::sync::Arc;use std::task::{Context, Poll};use std::usize;@@ -35,6 +35,8 @@ use arrow::array::{use arrow::compute;use arrow::datatypes::{Field, Schema, SchemaBuilder};use arrow::record_batch::{RecordBatch, RecordBatchOptions};+use arrow_array::{ArrowPrimitiveType, NativeAdapter, PrimitiveArray};+use arrow_buffer::ArrowNativeType;use datafusion_common::cast::as_boolean_array;use datafusion_common::stats::Precision;use datafusion_common::{@@ -136,6 +138,53 @@ impl JoinHashMap {}}+// Type of offsets for obtaining indices from JoinHashMap.+pub(crate) type JoinHashMapOffset = (usize, Option<u64>);++// Macro for traversing chained values with limit.+// Early returns in case of reacing output tuples limit.+macro_rules! chain_traverse {+ (+ $input_indices:ident, $match_indices:ident, $hash_values:ident, $next_chain:ident,+ $input_idx:ident, $chain_idx:ident, $deleted_offset:ident, $remaining_output:ident+ ) => {+ let mut i = $chain_idx - 1;+ loop {+ let match_row_idx = if let Some(offset) = $deleted_offset {+ // This arguments means that we prune the next index way before here.+ if i < offset as u64 {+ // End of the list due to pruning+ break;+ }+ i - offset as u64+ } else {+ i+ };+ $match_indices.append(match_row_idx);+ $input_indices.append($input_idx as u32);+ $remaining_output -= 1;+ // Follow the chain to get the next index value+ let next = $next_chain[match_row_idx as usize];++ if $remaining_output == 0 {+ // In case current input index is the last, and no more chain values left+ // returning None as whole input has been scanned+ let next_offset = if $input_idx == $hash_values.len() - 1 && next == 0 {+ None+ } else {+ Some(($input_idx, Some(next)))+ };+ return ($input_indices, $match_indices, next_offset);+ }+ if next == 0 {+ // end of list+ break;+ }+ i = next - 1;+ }+ };+}+// Trait defining methods that must be implemented by a hash map type to be used for joins.pub trait JoinHashMapType {/// The type of list used to store the next list@@ -224,6 +273,78 @@ pub trait JoinHashMapType {(input_indices, match_indices)}++ /// Matches hashes with taking limit and offset into account.+ /// Returns pairs of matched indices along with the starting point for next+ /// matching iteration (`None` if limit has not been reached).+ ///+ /// This method only compares hashes, so additional further check for actual values+ /// equality may be required.+ fn get_matched_indices_with_limit_offset(+ &self,+ hash_values: &[u64],+ deleted_offset: Option<usize>,+ limit: usize,+ offset: JoinHashMapOffset,+ ) -> (+ UInt32BufferBuilder,+ UInt64BufferBuilder,+ Option<JoinHashMapOffset>,+ ) {+ let mut input_indices = UInt32BufferBuilder::new(0);+ let mut match_indices = UInt64BufferBuilder::new(0);++ let mut remaining_output = limit;++ let hash_map: &RawTable<(u64, u64)> = self.get_map();+ let next_chain = self.get_list();++ // Calculate initial `hash_values` index before iterating+ let to_skip = match offset {+ // None `initial_next_idx` indicates that `initial_idx` processing has'n been started+ (initial_idx, None) => initial_idx,+ // Zero `initial_next_idx` indicates that `initial_idx` has been processed during+ // previous iteration, and it should be skipped+ (initial_idx, Some(0)) => initial_idx + 1,+ // Otherwise, process remaining `initial_idx` matches by traversing `next_chain`,+ // to start with the next index+ (initial_idx, Some(initial_next_idx)) => {+ chain_traverse!(+ input_indices,+ match_indices,+ hash_values,+ next_chain,+ initial_idx,+ initial_next_idx,+ deleted_offset,+ remaining_output+ );++ initial_idx + 1+ }+ };++ let mut row_idx = to_skip;+ for hash_value in &hash_values[to_skip..] {+ if let Some((_, index)) =+ hash_map.get(*hash_value, |(hash, _)| *hash_value == *hash)+ {+ chain_traverse!(+ input_indices,+ match_indices,+ hash_values,+ next_chain,+ row_idx,+ index,+ deleted_offset,+ remaining_output+ );+ }+ row_idx += 1;+ }++ (input_indices, match_indices, None)+ }}/// Implementation of `JoinHashMapType` for `JoinHashMap`.@@ -1079,7 +1200,7 @@ pub(crate) fn build_batch_from_indices(pub(crate) fn adjust_indices_by_join_type(left_indices: UInt64Array,right_indices: UInt32Array,- count_right_batch: usize,+ adjust_range: Range<usize>,join_type: JoinType,) -> (UInt64Array, UInt32Array) {match join_type {@@ -1095,21 +1216,20 @@ pub(crate) fn adjust_indices_by_join_type(JoinType::Right | JoinType::Full => {// matched// unmatched right row will be produced in this batch- let right_unmatched_indices =- get_anti_indices(count_right_batch, &right_indices);+ let right_unmatched_indices = get_anti_indices(adjust_range, &right_indices);// combine the matched and unmatched right result togetherappend_right_indices(left_indices, right_indices, right_unmatched_indices)}JoinType::RightSemi => {// need to remove the duplicated record in the right side- let right_indices = get_semi_indices(count_right_batch, &right_indices);+ let right_indices = get_semi_indices(adjust_range, &right_indices);// the left_indices will not be used later for the `right semi` join(left_indices, right_indices)}JoinType::RightAnti => {// need to remove the duplicated record in the right side// get the anti index for the right side- let right_indices = get_anti_indices(count_right_batch, &right_indices);+ let right_indices = get_anti_indices(adjust_range, &right_indices);// the left_indices will not be used later for the `right anti` join(left_indices, right_indices)}@@ -1151,72 +1271,62 @@ pub(crate) fn append_right_indices(}}-/// Get unmatched and deduplicated indices-pub(crate) fn get_anti_indices(- row_count: usize,- input_indices: &UInt32Array,-) -> UInt32Array {- let mut bitmap = BooleanBufferBuilder::new(row_count);- bitmap.append_n(row_count, false);- input_indices.iter().flatten().for_each(|v| {- bitmap.set_bit(v as usize, true);- });-- // get the anti index- (0..row_count)- .filter_map(|idx| (!bitmap.get_bit(idx)).then_some(idx as u32))- .collect::<UInt32Array>()-}+/// Returns `range` indices which are not present in `input_indices`+pub(crate) fn get_anti_indices<T: ArrowPrimitiveType>(+ range: Range<usize>,+ input_indices: &PrimitiveArray<T>,+) -> PrimitiveArray<T>+where+ NativeAdapter<T>: From<<T as ArrowPrimitiveType>::Native>,+{+ let mut bitmap = BooleanBufferBuilder::new(range.len());+ bitmap.append_n(range.len(), false);+ input_indices+ .iter()+ .flatten()+ .map(|v| v.as_usize())+ .filter(|v| range.contains(v))+ .for_each(|v| {+ bitmap.set_bit(v - range.start, true);+ });-/// Get unmatched and deduplicated indices-pub(crate) fn get_anti_u64_indices(- row_count: usize,- input_indices: &UInt64Array,-) -> UInt64Array {- let mut bitmap = BooleanBufferBuilder::new(row_count);- bitmap.append_n(row_count, false);- input_indices.iter().flatten().for_each(|v| {- bitmap.set_bit(v as usize, true);- });+ let offset = range.start;// get the anti index- (0..row_count)- .filter_map(|idx| (!bitmap.get_bit(idx)).then_some(idx as u64))- .collect::<UInt64Array>()+ (range)+ .filter_map(|idx| {+ (!bitmap.get_bit(idx - offset)).then_some(T::Native::from_usize(idx))+ })+ .collect::<PrimitiveArray<T>>()}-/// Get matched and deduplicated indices-pub(crate) fn get_semi_indices(- row_count: usize,- input_indices: &UInt32Array,-) -> UInt32Array {- let mut bitmap = BooleanBufferBuilder::new(row_count);- bitmap.append_n(row_count, false);- input_indices.iter().flatten().for_each(|v| {- bitmap.set_bit(v as usize, true);- });-- // get the semi index- (0..row_count)- .filter_map(|idx| (bitmap.get_bit(idx)).then_some(idx as u32))- .collect::<UInt32Array>()-}+/// Returns intersection of `range` and `input_indices` omitting duplicates+pub(crate) fn get_semi_indices<T: ArrowPrimitiveType>(+ range: Range<usize>,+ input_indices: &PrimitiveArray<T>,+) -> PrimitiveArray<T>+where+ NativeAdapter<T>: From<<T as ArrowPrimitiveType>::Native>,+{+ let mut bitmap = BooleanBufferBuilder::new(range.len());+ bitmap.append_n(range.len(), false);+ input_indices+ .iter()+ .flatten()+ .map(|v| v.as_usize())+ .filter(|v| range.contains(v))+ .for_each(|v| {+ bitmap.set_bit(v - range.start, true);+ });-/// Get matched and deduplicated indices-pub(crate) fn get_semi_u64_indices(- row_count: usize,- input_indices: &UInt64Array,-) -> UInt64Array {- let mut bitmap = BooleanBufferBuilder::new(row_count);- bitmap.append_n(row_count, false);- input_indices.iter().flatten().for_each(|v| {- bitmap.set_bit(v as usize, true);- });+ let offset = range.start;// get the semi index- (0..row_count)- .filter_map(|idx| (bitmap.get_bit(idx)).then_some(idx as u64))- .collect::<UInt64Array>()+ (range)+ .filter_map(|idx| {+ (bitmap.get_bit(idx - offset)).then_some(T::Native::from_usize(idx))+ })+ .collect::<PrimitiveArray<T>>()}/// Metrics for build & probe joinsdiff --git a/datafusion/physical-plan/src/lib.rs b/datafusion/physical-plan/src/lib.rsindex 1dd1392b9d..01d4f89418 100644--- a/datafusion/physical-plan/src/lib.rs+++ b/datafusion/physical-plan/src/lib.rs@@ -610,4 +610,7 @@ pub fn get_plan_string(plan: &Arc<dyn ExecutionPlan>) -> Vec<String> {}#[cfg(test)]+#[allow(clippy::single_component_path_imports)]+use rstest_reuse;+pub mod test;diff --git a/datafusion/physical-plan/src/test/exec.rs b/datafusion/physical-plan/src/test/exec.rsindex 1f6ee1f117..5a8ef2db77 100644--- a/datafusion/physical-plan/src/test/exec.rs+++ b/datafusion/physical-plan/src/test/exec.rs@@ -61,7 +61,7 @@ impl BatchIndex {/// Iterator over batches#[derive(Debug, Default)]-pub(crate) struct TestStream {+pub struct TestStream {/// Vector of record batchesdata: Vec<RecordBatch>,/// Index into the data that has been returned so far@@ -684,7 +684,7 @@ pub struct PanicExec {}impl PanicExec {- /// Create new [`PanickingExec`] with a give schema and number of+ /// Create new [`PanicExec`] with a give schema and number of/// partitions, which will each panic immediately.pub fn new(schema: SchemaRef, n_partitions: usize) -> Self {Self {@@ -708,7 +708,7 @@ impl DisplayAs for PanicExec {) -> std::fmt::Result {match t {DisplayFormatType::Default | DisplayFormatType::Verbose => {- write!(f, "PanickingExec",)+ write!(f, "PanicExec",)}}}