comphead commented on code in PR #27578:
URL: https://github.com/apache/datafusion/pull/27578#discussion_r4657238428
##########
datafusion/physical-plan/src/joins/piecewise_merge_join/exec.rs:
##########
@@ -957,29 +1082,129 @@ impl DisplayAs for PiecewiseMergeJoinExec {
fmt_sql(self.on.1.as_ref())
);
+ let projection_str = self.projection().map(|indices| {
+ let columns = indices
+ .iter()
+ .map(|&i| format!("{}@{i}", self.join_schema.field(i).name()))
+ .collect::<Vec<_>>();
+ format!("projection=[{}]", columns.join(", "))
+ });
+
match t {
DisplayFormatType::Default | DisplayFormatType::Verbose => {
write!(
f,
"PiecewiseMergeJoin: operator={:?}, join_type={:?}, on={}",
self.operator, self.join_type, on_str
- )
+ )?;
+ if let Some(projection_str) = projection_str {
+ write!(f, ", {projection_str}")?;
+ }
}
DisplayFormatType::TreeRender => {
writeln!(f, "operator={:?}", self.operator)?;
if self.join_type != JoinType::Inner {
writeln!(f, "join_type={:?}", self.join_type)?;
}
- writeln!(f, "on={on_str}")
+ writeln!(f, "on={on_str}")?;
+ if let Some(projection_str) = projection_str {
+ writeln!(f, "{projection_str}")?;
+ }
}
}
+ Ok(())
+ }
+}
+
+impl EmbeddedProjection for PiecewiseMergeJoinExec {
+ fn with_projection(&self, projection: Option<Vec<usize>>) -> Result<Self> {
+ self.with_projection(projection)
+ }
+}
+
+/// An embedded projection split per side, so each side is narrowed as it
arrives instead
+/// of every column being materialized into the output and dropped afterwards.
+///
+/// The classic stream builds each output batch's columns as `buffered_columns
++
+/// streamed_columns`; `reorder` then puts them into `indices` order,
zero-copy.
Review Comment:
Checking this split against the EPIC
(https://github.com/apache/datafusion/issues/17427), two of its items build on
it:
- "Support leftover predicates" would evaluate a residual filter on
candidate pairs, after both narrowing points. The filter's columns then have to
be in the kept set, and `reorder` drops the filter-only ones afterwards.
- "Support swap inputs" (`swap_inputs` is still `todo!()`) will have to
remap the projection with `swap_join_projection`, as `HashJoinExec` does, and
then rebuild this split.
Saying here that the kept set is every column the stream reads after the
key, not only the output columns, would keep the next PR from narrowing away
filter columns.
##########
datafusion/physical-plan/src/joins/piecewise_merge_join/exec.rs:
##########
@@ -1007,6 +1232,11 @@ async fn build_buffered_data(
let buffered_values = on_buffered
.evaluate(&single_batch)?
.into_array(single_batch.num_rows())?;
+ // The key is stored apart from the batch, so the columns it was computed
from can go.
+ let single_batch = match &projection {
+ Some(columns) => single_batch.project(columns)?,
+ None => single_batch,
+ };
Review Comment:
The narrowing does not reach the memory pool. The fold above grows the
reservation and `build_mem_used` by every full-width input batch, and
`try_grow(size_estimation)` below adds the narrowed batch on top, with no
resize afterwards. So for the whole join the pool and `EXPLAIN ANALYZE` still
charge the full-width input, which `batches` releases when this function
returns. The double count predates this PR, but this is where the PR's saving
should become visible. Dropping `batches` after the concat and using
`try_resize(size_estimation)` with `build_mem_used.set(..)` would make both
match what the join holds. That is also the EPIC's "Improve metrics accounting"
item.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]