Skip to content
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

perf: Toggle coalesce in join if non-coalesced key isn't projected #16677

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions crates/polars-lazy/src/tests/projection_queries.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use polars_ops::frame::JoinCoalesce;

use super::*;

#[test]
Expand Down Expand Up @@ -130,3 +132,41 @@ fn concat_str_regex_expansion() -> PolarsResult<()> {

Ok(())
}

#[test]
fn test_coalesce_toggle_projection_pushdown() -> PolarsResult<()> {
// Test that the optimizer toggle coalesce to true if the non-coalesced column isn't used.
let q1 = df!["a" => [1],
"b" => [2]
]?
.lazy();

let q2 = df!["a" => [1],
"c" => [2]
]?
.lazy();

let plan = q1
.join(
q2,
[col("a")],
[col("a")],
JoinArgs {
how: JoinType::Left,
coalesce: JoinCoalesce::KeepColumns,
..Default::default()
},
)
.select([col("a"), col("b")])
.to_alp_optimized()?;

let node = plan.lp_top;
let lp_arena = plan.lp_arena;

assert!((&lp_arena).iter(node).all(|(_, plan)| match plan {
IR::Join { options, .. } => options.args.should_coalesce(),
_ => true,
}));

Ok(())
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![allow(clippy::too_many_arguments)]
use std::collections::BTreeSet;
use std::borrow::Cow;

use super::*;

Expand Down Expand Up @@ -97,7 +97,7 @@ pub(super) fn process_asof_join(

// The join on keys can lead that columns are already added, we don't want to create
// duplicates so store the names.
let mut local_projected_names = BTreeSet::new();
let mut local_projected_names = PlHashSet::new();

// We need the join columns so we push the projection downwards
for e in &left_on {
Expand Down Expand Up @@ -194,9 +194,9 @@ pub(super) fn process_join(
input_right: Node,
left_on: Vec<ExprIR>,
right_on: Vec<ExprIR>,
options: Arc<JoinOptions>,
mut options: Arc<JoinOptions>,
acc_projections: Vec<ColumnNode>,
_projected_names: PlHashSet<Arc<str>>,
projected_names: PlHashSet<Arc<str>>,
projections_seen: usize,
lp_arena: &mut Arena<IR>,
expr_arena: &mut Arena<AExpr>,
Expand All @@ -212,7 +212,7 @@ pub(super) fn process_join(
right_on,
options,
acc_projections,
_projected_names,
projected_names,
projections_seen,
lp_arena,
expr_arena,
Expand Down Expand Up @@ -242,7 +242,7 @@ pub(super) fn process_join(

// The join on keys can lead that columns are already added, we don't want to create
// duplicates so store the names.
let mut local_projected_names = BTreeSet::new();
let mut local_projected_names = PlHashSet::new();

// We need the join columns so we push the projection downwards
for e in &left_on {
Expand All @@ -260,8 +260,34 @@ pub(super) fn process_join(
)
.unwrap();
}
// In full outer joins both columns remain. So `add_local=true` also for the right table
let add_local = !options.args.coalesce.coalesce(&options.args.how);

// For left and innner joins we can set `coalesce` to `true` if the rhs key columns are not projected.
// This saves a materialization.
if !options.args.should_coalesce()
&& matches!(options.args.how, JoinType::Left | JoinType::Inner)
{
let non_coalesced_key_is_used = right_on.iter().any(|e| {
let key_name = e.output_name();

// If the name is in the lhs table, a suffix is added.
let key_name_after_join = if schema_left.contains(key_name) {
Cow::Owned(_join_suffix_name(key_name, options.args.suffix()))
} else {
Cow::Borrowed(key_name)
};

projected_names.contains(key_name_after_join.as_ref())
});

// If they key is not used, coalesce the columns as that is often cheaper.
if !non_coalesced_key_is_used {
let options = Arc::make_mut(&mut options);
options.args.coalesce = JoinCoalesce::CoalesceColumns;
}
}

// In both columns remain. So `add_local=true` also for the right table
let add_local = !options.args.should_coalesce();
for e in &right_on {
// In case of full outer joins we also add the columns.
// But before we do that we must check if the column wasn't already added by the lhs.
Expand Down