File tree Expand file tree Collapse file tree 2 files changed +34
-6
lines changed Expand file tree Collapse file tree 2 files changed +34
-6
lines changed Original file line number Diff line number Diff line change @@ -809,6 +809,31 @@ mod test {
809809 ) ;
810810 }
811811
812+ #[ test]
813+ fn cte_tables_can_be_resolved_in_subqueries ( ) {
814+ let schema = resolver ( schema ! {
815+ tables: {
816+ source_table: {
817+ id,
818+ }
819+
820+ dest_table: {
821+ id,
822+ }
823+ }
824+ } ) ;
825+
826+ let statement = parse (
827+ "
828+ WITH fd AS ( SELECT id FROM source_table )
829+ INSERT INTO dest_table ( id )
830+ SELECT id FROM fd RETURNING id
831+ " ,
832+ ) ;
833+
834+ type_check ( schema, & statement) . unwrap ( ) ;
835+ }
836+
812837 #[ test]
813838 fn aggregates ( ) {
814839 // init_tracing();
Original file line number Diff line number Diff line change @@ -297,17 +297,20 @@ impl<'ast> Visitor<'ast> for ScopeTracker<'ast> {
297297
298298 fn enter < N : Visitable > ( & mut self , node : & ' ast N ) -> ControlFlow < Break < Self :: Error > > {
299299 if node. downcast_ref :: < Statement > ( ) . is_some ( ) {
300- let root = Scope :: new_root ( ) ;
301- self . stack . push ( root. clone ( ) ) ;
302- return ControlFlow :: Continue ( ( ) ) ;
300+ match self . stack . last ( ) {
301+ Some ( scope) => {
302+ self . stack . push ( Scope :: new_child ( scope) ) ;
303+ }
304+ None => {
305+ self . stack . push ( Scope :: new_root ( ) ) ;
306+ }
307+ }
303308 }
304309
305310 if node. downcast_ref :: < Query > ( ) . is_some ( ) {
306311 match self . stack . last ( ) {
307312 Some ( scope) => {
308- let child = Scope :: new_child ( scope) ;
309- self . stack . push ( child. clone ( ) ) ;
310- return ControlFlow :: Continue ( ( ) ) ;
313+ self . stack . push ( Scope :: new_child ( scope) ) ;
311314 }
312315 None => return ControlFlow :: Break ( Break :: Err ( ScopeError :: NoCurrentScope ) ) ,
313316 }
You can’t perform that action at this time.
0 commit comments