SubhamSinghal commented on code in PR #27578:
URL: https://github.com/apache/datafusion/pull/27578#discussion_r4662438528
##########
datafusion/physical-plan/src/joins/piecewise_merge_join/exec.rs:
##########
@@ -1255,3 +1485,116 @@ pub(super) struct BufferedSideReadyState {
/// Collected build-side data
pub(super) buffered_data: Arc<BufferedSideData>,
}
+
+#[cfg(test)]
+mod tests {
Review Comment:
Addressed in
https://github.com/apache/datafusion/commit/d05143b5cabf3c155cb82dd956715f7f65a94e5d
##########
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.
+#[derive(Debug)]
+struct JoinProjection {
+ /// Indices into the unprojected join schema, as embedded
+ indices: ProjectionRef,
+ /// Buffered columns kept, ascending
+ buffered_columns: Vec<usize>,
+ /// Streamed columns kept, ascending
+ streamed_columns: Vec<usize>,
+ /// The streamed input's schema narrowed to `streamed_columns`
+ streamed_schema: SchemaRef,
+ /// Maps `buffered_columns ++ streamed_columns` onto the output, or `None`
when they
+ /// are already in output order
+ reorder: Option<Vec<usize>>,
+}
+
+impl JoinProjection {
+ fn try_new(
+ indices: ProjectionRef,
+ join_schema: &SchemaRef,
+ streamed_schema: &SchemaRef,
+ ) -> Result<Self> {
+ let num_buffered = join_schema.fields().len() -
streamed_schema.fields().len();
+
+ let mut buffered_columns: Vec<usize> = indices
+ .iter()
+ .copied()
+ .filter(|&i| i < num_buffered)
+ .collect();
+ buffered_columns.sort_unstable();
+ buffered_columns.dedup();
+
+ let mut streamed_columns: Vec<usize> = indices
+ .iter()
+ .filter_map(|&i| i.checked_sub(num_buffered))
+ .collect();
+ streamed_columns.sort_unstable();
+ streamed_columns.dedup();
+
+ let stream_columns: Vec<usize> = buffered_columns
+ .iter()
+ .copied()
+ .chain(streamed_columns.iter().map(|&i| i + num_buffered))
+ .collect();
+ let reorder: Vec<usize> = indices
+ .iter()
+ .map(|i| {
+ stream_columns
+ .iter()
+ .position(|j| j == i)
+ .ok_or_else(|| internal_datafusion_err!("column {i} not
kept"))
+ })
+ .collect::<Result<_>>()?;
+ let is_identity = reorder.len() == stream_columns.len()
+ && reorder.iter().enumerate().all(|(k, &i)| k == i);
+
+ Ok(Self {
+ streamed_schema:
Arc::new(streamed_schema.project(&streamed_columns)?),
+ reorder: (!is_identity).then_some(reorder),
+ indices,
+ buffered_columns,
+ streamed_columns,
+ })
Review Comment:
Addressed in
https://github.com/apache/datafusion/commit/d05143b5cabf3c155cb82dd956715f7f65a94e5d
--
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]