1313// limitations under the License.
1414
1515use crate :: errors:: DatabaseError ;
16+ #[ cfg( feature = "spill" ) ]
1617use crate :: execution:: spill:: { SpillReader , SpillVec } ;
1718use crate :: execution:: {
1819 build_read, ExecArena , ExecId , ExecNode , ExecutionContext , ExecutorNode , ReadExecutor ,
@@ -25,7 +26,10 @@ use std::mem;
2526
2627pub ( crate ) enum RecursiveInput {
2728 One ( Option < Tuple > ) ,
29+ #[ cfg( feature = "spill" ) ]
2830 Many ( SpillReader < Tuple > ) ,
31+ #[ cfg( not( feature = "spill" ) ) ]
32+ Many ( std:: vec:: IntoIter < Tuple > ) ,
2933}
3034
3135impl Iterator for RecursiveInput {
@@ -34,7 +38,10 @@ impl Iterator for RecursiveInput {
3438 fn next ( & mut self ) -> Option < Self :: Item > {
3539 match self {
3640 Self :: One ( tuple) => tuple. take ( ) . map ( Ok ) ,
41+ #[ cfg( feature = "spill" ) ]
3742 Self :: Many ( rows) => rows. next ( ) ,
43+ #[ cfg( not( feature = "spill" ) ) ]
44+ Self :: Many ( rows) => rows. next ( ) . map ( Ok ) ,
3845 }
3946 }
4047}
@@ -47,8 +54,17 @@ enum RecursiveRows {
4754 tuple : Tuple ,
4855 output_done : bool ,
4956 } ,
57+ #[ cfg( feature = "spill" ) ]
5058 Writing ( SpillVec < ' static , Tuple > ) ,
59+ #[ cfg( feature = "spill" ) ]
5160 Reading ( SpillReader < Tuple > ) ,
61+ #[ cfg( not( feature = "spill" ) ) ]
62+ Writing ( Vec < Tuple > ) ,
63+ #[ cfg( not( feature = "spill" ) ) ]
64+ Reading {
65+ rows : Vec < Tuple > ,
66+ output_index : usize ,
67+ } ,
5268}
5369
5470impl RecursiveRows {
@@ -65,14 +81,29 @@ impl RecursiveRows {
6581 output_done : false ,
6682 } => {
6783 let first = mem:: take ( first) ;
68- let mut rows = SpillVec :: new ( ) ;
69- let _ = rows. push ( first) ?;
70- let _ = rows. push ( tuple) ?;
71- * self = Self :: Writing ( rows) ;
84+ #[ cfg( feature = "spill" ) ]
85+ {
86+ let mut rows = SpillVec :: new ( ) ;
87+ let _ = rows. push ( first) ?;
88+ let _ = rows. push ( tuple) ?;
89+ * self = Self :: Writing ( rows) ;
90+ }
91+ #[ cfg( not( feature = "spill" ) ) ]
92+ {
93+ * self = Self :: Writing ( vec ! [ first, tuple] ) ;
94+ }
7295 }
96+ #[ cfg( feature = "spill" ) ]
7397 Self :: Writing ( rows) => {
7498 let _ = rows. push ( tuple) ?;
7599 }
100+ #[ cfg( not( feature = "spill" ) ) ]
101+ Self :: Writing ( rows) => rows. push ( tuple) ,
102+ #[ cfg( feature = "spill" ) ]
103+ Self :: One { .. } | Self :: Reading ( _) => {
104+ unreachable ! ( "cannot append to a finished recursive generation" )
105+ }
106+ #[ cfg( not( feature = "spill" ) ) ]
76107 Self :: One { .. } | Self :: Reading { .. } => {
77108 unreachable ! ( "cannot append to a finished recursive generation" )
78109 }
@@ -82,10 +113,16 @@ impl RecursiveRows {
82113
83114 fn finish ( self ) -> Result < Self , DatabaseError > {
84115 match self {
116+ #[ cfg( feature = "spill" ) ]
85117 Self :: Writing ( mut rows) => {
86118 let _ = rows. flush ( ) ?;
87119 Ok ( Self :: Reading ( rows. into_iter ( ) ) )
88120 }
121+ #[ cfg( not( feature = "spill" ) ) ]
122+ Self :: Writing ( rows) => Ok ( Self :: Reading {
123+ rows,
124+ output_index : 0 ,
125+ } ) ,
89126 rows => Ok ( rows) ,
90127 }
91128 }
@@ -100,7 +137,14 @@ impl RecursiveRows {
100137 * output_done = true ;
101138 Ok ( Some ( tuple. clone ( ) ) )
102139 }
140+ #[ cfg( feature = "spill" ) ]
103141 Self :: Reading ( reader) => reader. next ( ) . transpose ( ) ,
142+ #[ cfg( not( feature = "spill" ) ) ]
143+ Self :: Reading { rows, output_index } => {
144+ let tuple = rows. get ( * output_index) . cloned ( ) ;
145+ * output_index += usize:: from ( tuple. is_some ( ) ) ;
146+ Ok ( tuple)
147+ }
104148 Self :: Writing ( _) => unreachable ! ( "recursive generation must be finished first" ) ,
105149 }
106150 }
@@ -109,10 +153,13 @@ impl RecursiveRows {
109153 match self {
110154 Self :: Empty => Ok ( None ) ,
111155 Self :: One { tuple, .. } => Ok ( Some ( RecursiveInput :: One ( Some ( tuple) ) ) ) ,
156+ #[ cfg( feature = "spill" ) ]
112157 Self :: Reading ( mut reader) => {
113158 reader. reset ( ) ?;
114159 Ok ( Some ( RecursiveInput :: Many ( reader) ) )
115160 }
161+ #[ cfg( not( feature = "spill" ) ) ]
162+ Self :: Reading { rows, .. } => Ok ( Some ( RecursiveInput :: Many ( rows. into_iter ( ) ) ) ) ,
116163 Self :: Writing ( _) => unreachable ! ( "recursive generation must be finished first" ) ,
117164 }
118165 }
@@ -288,6 +335,7 @@ mod tests {
288335 use std:: borrow:: Cow ;
289336 use tempfile:: TempDir ;
290337
338+ #[ cfg( feature = "spill" ) ]
291339 #[ test]
292340 fn spilled_generation_is_written_once_and_replayed_for_scan ( ) -> Result < ( ) , DatabaseError > {
293341 let expected = ( 0 ..1100 )
@@ -311,6 +359,30 @@ mod tests {
311359 Ok ( ( ) )
312360 }
313361
362+ #[ cfg( not( feature = "spill" ) ) ]
363+ #[ test]
364+ fn memory_generation_is_replayed_for_scan ( ) -> Result < ( ) , DatabaseError > {
365+ let expected = ( 0 ..3 )
366+ . map ( |value| Tuple :: new ( None , vec ! [ DataValue :: Int32 ( value) ] ) )
367+ . collect :: < Vec < _ > > ( ) ;
368+ let mut rows = RecursiveRows :: default ( ) ;
369+ for tuple in expected. iter ( ) . cloned ( ) {
370+ rows. push ( tuple) ?;
371+ }
372+
373+ let mut working = rows. finish ( ) ?;
374+ assert ! ( matches!( & working, RecursiveRows :: Reading { .. } ) ) ;
375+ let mut output = Vec :: new ( ) ;
376+ while let Some ( tuple) = working. next_output ( ) ? {
377+ output. push ( tuple) ;
378+ }
379+ assert_eq ! ( output, expected) ;
380+
381+ let scan = working. into_input ( ) ?. unwrap ( ) ;
382+ assert_eq ! ( scan. collect:: <Result <Vec <_>, _>>( ) ?, expected) ;
383+ Ok ( ( ) )
384+ }
385+
314386 #[ test]
315387 fn empty_generation_has_no_recursive_input ( ) -> Result < ( ) , DatabaseError > {
316388 let rows = RecursiveRows :: default ( ) ;
0 commit comments