-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat: Make join selection configurable through enable_*
and join_method_priority
options
#17467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
2010YOUY01
wants to merge
15
commits into
apache:main
Choose a base branch
from
2010YOUY01:config-join
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
9b7ac55
Make join selection configurable through `enable_*` and `prefer_*` op…
2010YOUY01 bde1a65
clean up
2010YOUY01 521e51b
Update datafusion/core/src/physical_planner/join_planner.rs
2010YOUY01 abe8e2a
fix clippy
2010YOUY01 941be55
fix test
2010YOUY01 6043716
fix fmt
2010YOUY01 3bf325b
use `join_method_priority` instead
2010YOUY01 498077f
fix typo
2010YOUY01 73a9cd5
clean up
2010YOUY01 71e0f90
fix test
2010YOUY01 3a1471f
fix test
2010YOUY01 00f6dbb
review
2010YOUY01 b659c61
bot review
2010YOUY01 2323ef7
revert suggestion from AI -- it's not true
2010YOUY01 415dac5
fix
2010YOUY01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use std::sync::Arc; | ||
|
||
use crate::error::Result; | ||
use crate::execution::context::SessionState; | ||
use crate::physical_plan::joins::utils as join_utils; | ||
use crate::physical_plan::joins::{ | ||
CrossJoinExec, HashJoinExec, NestedLoopJoinExec, PartitionMode, SortMergeJoinExec, | ||
}; | ||
use crate::physical_plan::ExecutionPlan; | ||
use arrow::compute::SortOptions; | ||
use datafusion_common::{config_err, plan_err}; | ||
use datafusion_expr::JoinType; | ||
|
||
/// Build the appropriate join `ExecutionPlan` for the given join type, filter, and | ||
/// configurations. | ||
/// | ||
/// For example, given an equi-join, the planner may execute it as a Nested Loop | ||
/// Join, Hash Join, or another strategy. Configuration settings determine which | ||
/// ExecutionPlan is used. | ||
/// | ||
/// # Strategy | ||
/// - Step 1: Find all possible physical join types for the given join logical plan | ||
/// - No join on keys and no filter => CrossJoin | ||
/// - With equality? => HJ and SMJ (if with multiple partition) | ||
/// TODO: The constraint on SMJ is added previously for optimization. Should | ||
/// we remove it for configurability? | ||
/// TODO: Allow NLJ for equal join for better configurability. | ||
/// - Without equality? => NLJ | ||
/// - Step 2: Filter the possible join types from step 1 according to the configuration | ||
/// by checking if they're enabled by options like `datafusion.optimizer.enable_hash_join` | ||
/// - Step 3: Choose one according to the built-in heuristics and also the preference | ||
/// in the configuration `datafusion.optimizer.join_method_priority` | ||
pub(super) fn plan_initial_join_exec( | ||
session_state: &SessionState, | ||
physical_left: Arc<dyn ExecutionPlan>, | ||
physical_right: Arc<dyn ExecutionPlan>, | ||
join_on: join_utils::JoinOn, | ||
join_filter: Option<join_utils::JoinFilter>, | ||
join_type: &JoinType, | ||
null_equality: &datafusion_common::NullEquality, | ||
) -> Result<Arc<dyn ExecutionPlan>> { | ||
// Short-circuit: handle pure cross join (existing behavior) | ||
if join_on.is_empty() && join_filter.is_none() && matches!(join_type, JoinType::Inner) | ||
{ | ||
return Ok(Arc::new(CrossJoinExec::new(physical_left, physical_right))); | ||
} | ||
|
||
// Step 1: Find possible join types for the given Logical Plan | ||
// ---------------------------------------------------------------------- | ||
|
||
// Build the list of possible algorithms for this join | ||
#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
enum Algo { | ||
Nlj, | ||
Hj, | ||
Smj, | ||
} | ||
|
||
let cfg = &session_state.config_options().optimizer; | ||
let can_smj = session_state.config().target_partitions() > 1 | ||
&& session_state.config().repartition_joins(); | ||
|
||
let mut possible: Vec<Algo> = Vec::new(); | ||
if join_on.is_empty() { | ||
possible.push(Algo::Nlj); | ||
} else { | ||
possible.push(Algo::Hj); | ||
if can_smj { | ||
possible.push(Algo::Smj); | ||
} | ||
} | ||
|
||
// Step 2: Filter the possible list according to enable flags from config | ||
// ---------------------------------------------------------------------- | ||
|
||
// Filter by enable flags | ||
let enabled_and_possible: Vec<Algo> = possible | ||
.iter() | ||
.copied() | ||
.filter(|a| match a { | ||
Algo::Nlj => cfg.enable_nested_loop_join, | ||
Algo::Hj => cfg.enable_hash_join, | ||
Algo::Smj => cfg.enable_sort_merge_join, | ||
}) | ||
.collect(); | ||
|
||
if enabled_and_possible.is_empty() { | ||
return plan_err!( | ||
"No enabled join algorithm is applicable for this join. Possible join types are {:?}. Try to enable them through configurations like `datafusion.optimizer.enable_hash_join`", &possible | ||
); | ||
} | ||
|
||
// Step 3: Choose and plan the physical join type according to | ||
// `join_method_priority` and built-in heuristics | ||
// ---------------------------------------------------------------------- | ||
|
||
// Parse join method priority string into an ordered list of algorithms | ||
let parse_priority = |s: &str| -> Result<Vec<Algo>> { | ||
let mut out = Vec::new(); | ||
let mut unknown = Vec::new(); | ||
for token in s.split(',').map(|t| t.trim()).filter(|t| !t.is_empty()) { | ||
match token { | ||
"hj" | "hash_join" => out.push(Algo::Hj), | ||
"smj" | "sort_merge_join" => out.push(Algo::Smj), | ||
"nlj" | "nested_loop_join" => out.push(Algo::Nlj), | ||
2010YOUY01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
other => unknown.push(other.to_string()), | ||
} | ||
} | ||
if !unknown.is_empty() { | ||
let valid = "hj/hash_join, smj/sort_merge_join, nlj/nested_loop_join"; | ||
return config_err!( | ||
"Invalid join method(s) in datafusion.optimizer.join_method_priority: {}. Valid values: {}", | ||
unknown.join(", "), | ||
valid | ||
); | ||
} | ||
Ok(out) | ||
}; | ||
|
||
// Backward compatibility: | ||
// If `join_method_priority` is empty, honor legacy `prefer_hash_join` by | ||
// setting the priority to a single entry accordingly. Otherwise, parse the | ||
2010YOUY01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// provided priority string. | ||
let priority = if cfg.join_method_priority.trim().is_empty() { | ||
#[allow(deprecated)] | ||
if cfg.prefer_hash_join { | ||
vec![Algo::Hj] | ||
} else { | ||
vec![Algo::Smj] | ||
} | ||
} else { | ||
parse_priority(&cfg.join_method_priority)? | ||
}; | ||
|
||
// Default heuristic order if priority is empty or does not match any candidate | ||
let default_order = [Algo::Hj, Algo::Smj, Algo::Nlj]; | ||
|
||
// Helper: pick the first algorithm in `order` that is in `candidates` | ||
let pick_in_order = |candidates: &[Algo], order: &[Algo]| -> Option<Algo> { | ||
for algo in order { | ||
if candidates.contains(algo) { | ||
return Some(*algo); | ||
} | ||
} | ||
None | ||
}; | ||
|
||
// Intersect enabled+possible with priority order first; otherwise fallback to default order | ||
let chosen = pick_in_order(&enabled_and_possible, &priority) | ||
.or_else(|| pick_in_order(&enabled_and_possible, &default_order)) | ||
.expect("enabled_and_possible is non-empty"); | ||
|
||
match chosen { | ||
Algo::Nlj => Ok(Arc::new(NestedLoopJoinExec::try_new( | ||
physical_left, | ||
physical_right, | ||
join_filter, | ||
join_type, | ||
None, | ||
)?)), | ||
Algo::Hj => { | ||
// Determine partition mode based solely on partitioning configuration | ||
let partition_mode = if session_state.config().target_partitions() > 1 | ||
&& session_state.config().repartition_joins() | ||
{ | ||
PartitionMode::Auto | ||
} else { | ||
PartitionMode::CollectLeft | ||
}; | ||
|
||
Ok(Arc::new(HashJoinExec::try_new( | ||
physical_left, | ||
physical_right, | ||
join_on, | ||
join_filter, | ||
join_type, | ||
None, | ||
partition_mode, | ||
*null_equality, | ||
)?)) | ||
} | ||
Algo::Smj => { | ||
let join_on_len = join_on.len(); | ||
Ok(Arc::new(SortMergeJoinExec::try_new( | ||
physical_left, | ||
physical_right, | ||
join_on, | ||
join_filter, | ||
*join_type, | ||
vec![SortOptions::default(); join_on_len], | ||
*null_equality, | ||
)?)) | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.