@@ -49,7 +49,7 @@ use crate::{
4949use arrow:: array:: { Array , ArrayRef , BooleanArray , Int32Array , UInt32Array , UInt64Array } ;
5050use arrow:: compute:: filter_record_batch;
5151use arrow:: datatypes:: { Schema , SchemaRef } ;
52- use arrow:: record_batch:: RecordBatch ;
52+ use arrow:: record_batch:: { RecordBatch , RecordBatchOptions } ;
5353use arrow_schema:: DataType ;
5454use datafusion_common:: {
5555 JoinSide , JoinType , NullEquality , Result , internal_datafusion_err, internal_err,
@@ -751,7 +751,19 @@ impl HashJoinStream {
751751 return internal_err ! ( "unsupported data type for roaring bitmap" ) ;
752752 }
753753 } ;
754- let batch = filter_record_batch ( & state. batch , & mask) ?;
754+ let filtered_batch = filter_record_batch ( & state. batch , & mask) ?;
755+ // Apply column projection to match output schema
756+ let columns: Vec < ArrayRef > = self
757+ . column_indices
758+ . iter ( )
759+ . map ( |col_idx| {
760+ // For RightSemi/RightAnti, all columns are from the right (probe) side
761+ Arc :: clone ( filtered_batch. column ( col_idx. index ) )
762+ } )
763+ . collect ( ) ;
764+ // Use try_new_with_options to handle empty columns case (e.g., count(*))
765+ let options = RecordBatchOptions :: new ( ) . with_row_count ( Some ( filtered_batch. num_rows ( ) ) ) ;
766+ let batch = RecordBatch :: try_new_with_options ( Arc :: clone ( & self . schema ) , columns, & options) ?;
755767 self . output_buffer . push_batch ( batch) ?;
756768 self . state = HashJoinStreamState :: FetchProbeBatch ;
757769 return Ok ( StatefulStreamResult :: Continue ) ;
0 commit comments