@@ -388,8 +388,9 @@ pub(super) struct MaterializingSortMergeJoinStream {
388388 pub early_filtering : bool ,
389389 /// The indices of every buffered column, for materializing all of them
390390 pub buffered_columns : Vec < usize > ,
391- /// The buffered columns that the join filter reads, in the filter's order.
392- /// Empty unless `early_filtering`.
391+ /// The streamed and buffered columns that the join filter reads, each in the
392+ /// filter's order. Empty unless `early_filtering`.
393+ pub streamed_filter_columns : Vec < usize > ,
393394 pub buffered_filter_columns : Vec < usize > ,
394395 /// How many pairs the Inner join's filter has evaluated at freeze so far
395396 /// (see [`Self::filter_first_pays`])
@@ -660,15 +661,17 @@ impl MaterializingSortMergeJoinStream {
660661 let deferred_filtering = needs_deferred_filtering ( filter. as_ref ( ) , join_type) ;
661662 let early_filtering = join_type == JoinType :: Inner && filter. is_some ( ) ;
662663 let buffered_columns = ( 0 ..buffered_schema. fields ( ) . len ( ) ) . collect ( ) ;
663- let buffered_filter_columns = match & filter {
664+ let filter_columns = |side| match & filter {
664665 Some ( f) if early_filtering => f
665666 . column_indices ( )
666667 . iter ( )
667- . filter ( |c| c. side == JoinSide :: Right )
668+ . filter ( |c| c. side == side )
668669 . map ( |c| c. index )
669670 . collect ( ) ,
670671 _ => vec ! [ ] ,
671672 } ;
673+ let streamed_filter_columns = filter_columns ( JoinSide :: Left ) ;
674+ let buffered_filter_columns = filter_columns ( JoinSide :: Right ) ;
672675 let filter_schema = if deferred_filtering && let Some ( f) = & filter {
673676 let mut fields = Vec :: with_capacity ( f. schema ( ) . fields ( ) . len ( ) ) ;
674677 for ( column_index, field) in
@@ -706,6 +709,7 @@ impl MaterializingSortMergeJoinStream {
706709 filter_schema,
707710 early_filtering,
708711 buffered_columns,
712+ streamed_filter_columns,
709713 buffered_filter_columns,
710714 freeze_filter_evaluated : 0 ,
711715 freeze_filter_passed : 0 ,
@@ -1809,14 +1813,12 @@ impl MaterializingSortMergeJoinStream {
18091813 }
18101814
18111815 /// Freezes the matched pairs of an Inner join with a filter. The filter runs
1812- /// on just the columns it reads, and only the pairs that pass have their
1813- /// output columns materialized. A selective filter over a many-to-many join
1814- /// rejects most pairs, so materializing every column of every pair before
1815- /// filtering would spend most of the freeze on rows that are then dropped.
1816- /// When the pairs' indices form ranges on both sides, as in a 1:1 join, the
1817- /// columns materialize as slices and filtering first cannot save anything.
1818- /// Then, or when [`Self::filter_first_pays`] says earlier freezes kept too
1819- /// many of their pairs, the batch is materialized whole and then filtered.
1816+ /// on just the columns it reads, and only the pairs that pass are
1817+ /// materialized, which saves most of the freeze when the filter rejects most
1818+ /// pairs of a many-to-many join. When both sides' indices span ranges, as in
1819+ /// a 1:1 join, the columns materialize as slices and filtering first saves
1820+ /// nothing. Then, or when [`Self::filter_first_pays`] says so, the batch is
1821+ /// materialized whole and then filtered.
18201822 fn freeze_inner_filtered (
18211823 & mut self ,
18221824 matched_chunks : & [ MatchedChunk ] ,
@@ -1849,6 +1851,14 @@ impl MaterializingSortMergeJoinStream {
18491851 ) ;
18501852 }
18511853
1854+ let mut streamed_arrays = materialize_left_columns (
1855+ & self
1856+ . streamed_batch
1857+ . batch
1858+ . project ( & self . streamed_filter_columns ) ?,
1859+ & streamed_indices,
1860+ ) ?
1861+ . into_iter ( ) ;
18521862 let mut buffered_arrays = self
18531863 . materialize_right_columns (
18541864 matched_chunks,
@@ -1860,10 +1870,9 @@ impl MaterializingSortMergeJoinStream {
18601870 . column_indices ( )
18611871 . iter ( )
18621872 . map ( |column_index| match column_index. side {
1863- JoinSide :: Left => take_column (
1864- self . streamed_batch . batch . column ( column_index. index ) ,
1865- & streamed_indices,
1866- ) ,
1873+ JoinSide :: Left => streamed_arrays. next ( ) . ok_or_else ( || {
1874+ internal_datafusion_err ! ( "missing streamed filter column" )
1875+ } ) ,
18671876 JoinSide :: Right => buffered_arrays. next ( ) . ok_or_else ( || {
18681877 internal_datafusion_err ! ( "missing buffered filter column" )
18691878 } ) ,
@@ -2394,15 +2403,6 @@ fn concat_indices<'a>(
23942403 }
23952404}
23962405
2397- /// The rows of `array` at `indices`, sliced when the indices form a range.
2398- fn take_column ( array : & ArrayRef , indices : & UInt64Array ) -> Result < ArrayRef > {
2399- if let Some ( range) = is_contiguous_range ( indices) {
2400- Ok ( array. slice ( range. start , range. len ( ) ) )
2401- } else {
2402- Ok ( compute:: take ( array. as_ref ( ) , indices, None ) ?)
2403- }
2404- }
2405-
24062406/// Evaluates `filter` over `num_rows` pairs whose `columns` follow the filter's
24072407/// intermediate `schema`, and returns which pairs pass. A NULL result does not
24082408/// pass, as in SQL.
0 commit comments