Skip to main content

datafusion_physical_plan/joins/
mod.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18//! DataFusion Join implementations
19
20use arrow::array::BooleanBufferBuilder;
21pub use cross_join::CrossJoinExec;
22use datafusion_physical_expr::PhysicalExprRef;
23pub use hash_join::{
24    HashExpr, HashJoinExec, HashJoinExecBuilder, HashTableLookupExpr, SeededRandomState,
25};
26pub use nested_loop_join::{NestedLoopJoinExec, NestedLoopJoinExecBuilder};
27use parking_lot::Mutex;
28// Note: SortMergeJoin is not used in plans yet
29pub use piecewise_merge_join::PiecewiseMergeJoinExec;
30pub use sort_merge_join::SortMergeJoinExec;
31pub use symmetric_hash_join::SymmetricHashJoinExec;
32pub mod chain;
33mod cross_join;
34mod hash_join;
35mod nested_loop_join;
36mod piecewise_merge_join;
37#[cfg(feature = "proto")]
38mod proto;
39mod sort_merge_join;
40mod stream_join_utils;
41mod symmetric_hash_join;
42pub mod utils;
43
44mod array_map;
45mod join_filter;
46/// Hash map implementations for join operations.
47///
48/// Note: This module is public for internal testing purposes only
49/// and is not guaranteed to be stable across versions.
50pub mod join_hash_map;
51
52use array_map::ArrayMap;
53use utils::JoinHashMapType;
54
55/// The build-side map of a hash join, indexing build rows by join key.
56///
57/// Under [`NullEquality::NullEqualsNothing`], build rows with a NULL in any
58/// join key column can never match a probe row and are omitted from the map.
59/// [`Map::is_empty`] and [`Map::num_of_distinct_key`] therefore reflect the
60/// *matchable* build rows: the map can be empty even when the build side
61/// contains rows.
62///
63/// [`NullEquality::NullEqualsNothing`]: datafusion_common::NullEquality::NullEqualsNothing
64pub enum Map {
65    HashMap(Box<dyn JoinHashMapType>),
66    ArrayMap(ArrayMap),
67}
68
69impl Map {
70    /// Returns the number of elements in the map.
71    pub fn num_of_distinct_key(&self) -> usize {
72        match self {
73            Map::HashMap(map) => map.len(),
74            Map::ArrayMap(array_map) => array_map.num_of_distinct_key(),
75        }
76    }
77
78    /// Returns `true` if the map contains no elements.
79    pub fn is_empty(&self) -> bool {
80        self.num_of_distinct_key() == 0
81    }
82}
83
84pub(crate) type MapOffset = (usize, Option<u64>);
85
86#[cfg(test)]
87pub mod test_utils;
88
89/// The on clause of the join, as vector of (left, right) columns.
90pub type JoinOn = Vec<(PhysicalExprRef, PhysicalExprRef)>;
91/// Reference for JoinOn.
92pub type JoinOnRef<'a> = &'a [(PhysicalExprRef, PhysicalExprRef)];
93
94#[derive(Clone, Copy, Debug, PartialEq, Eq)]
95/// Hash join Partitioning mode
96pub enum PartitionMode {
97    /// Left/right children are partitioned using the left and right keys
98    Partitioned,
99    /// Left side will collected into one partition
100    CollectLeft,
101    /// DataFusion optimizer decides which PartitionMode
102    /// mode(Partitioned/CollectLeft) is optimal based on statistics. It will
103    /// also consider swapping the left and right inputs for the Join
104    Auto,
105}
106
107/// Partitioning mode to use for symmetric hash join
108#[derive(Hash, Clone, Copy, Debug, PartialEq, Eq)]
109pub enum StreamJoinPartitionMode {
110    /// Left/right children are partitioned using the left and right keys
111    Partitioned,
112    /// Both sides will collected into one partition
113    SinglePartition,
114}
115
116/// Shared bitmap for visited left-side indices
117type SharedBitmapBuilder = Mutex<BooleanBufferBuilder>;