@@ -44,6 +44,8 @@ pub enum VariableType {
4444pub enum ScopeType {
4545 Match ,
4646 Where ,
47+ With ,
48+ PostWithWhere ,
4749 Return ,
4850 OrderBy ,
4951}
@@ -79,32 +81,48 @@ impl SemanticAnalyzer {
7981 }
8082 }
8183
82- // Phase 2: Validate WHERE clause
84+ // Phase 2: Validate WHERE clause (before WITH)
8385 if let Some ( where_clause) = & query. where_clause {
8486 self . current_scope = ScopeType :: Where ;
8587 if let Err ( e) = self . analyze_where_clause ( where_clause) {
8688 errors. push ( format ! ( "WHERE clause error: {}" , e) ) ;
8789 }
8890 }
8991
90- // Phase 3: Validate RETURN clause
92+ // Phase 3: Validate WITH clause if present
93+ if let Some ( with_clause) = & query. with_clause {
94+ self . current_scope = ScopeType :: With ;
95+ if let Err ( e) = self . analyze_with_clause ( with_clause) {
96+ errors. push ( format ! ( "WITH clause error: {}" , e) ) ;
97+ }
98+ }
99+
100+ // Phase 4: Validate post-WITH WHERE clause if present
101+ if let Some ( post_where) = & query. post_with_where_clause {
102+ self . current_scope = ScopeType :: PostWithWhere ;
103+ if let Err ( e) = self . analyze_where_clause ( post_where) {
104+ errors. push ( format ! ( "Post-WITH WHERE clause error: {}" , e) ) ;
105+ }
106+ }
107+
108+ // Phase 5: Validate RETURN clause
91109 self . current_scope = ScopeType :: Return ;
92110 if let Err ( e) = self . analyze_return_clause ( & query. return_clause ) {
93111 errors. push ( format ! ( "RETURN clause error: {}" , e) ) ;
94112 }
95113
96- // Phase 4 : Validate ORDER BY clause
114+ // Phase 6 : Validate ORDER BY clause
97115 if let Some ( order_by) = & query. order_by {
98116 self . current_scope = ScopeType :: OrderBy ;
99117 if let Err ( e) = self . analyze_order_by_clause ( order_by) {
100118 errors. push ( format ! ( "ORDER BY clause error: {}" , e) ) ;
101119 }
102120 }
103121
104- // Phase 5 : Schema validation
122+ // Phase 7 : Schema validation
105123 self . validate_schema ( & mut warnings) ;
106124
107- // Phase 6 : Type checking
125+ // Phase 8 : Type checking
108126 self . validate_types ( & mut errors) ;
109127
110128 Ok ( SemanticResult {
@@ -416,6 +434,21 @@ impl SemanticAnalyzer {
416434 Ok ( ( ) )
417435 }
418436
437+ /// Analyze WITH clause
438+ fn analyze_with_clause ( & mut self , with_clause : & WithClause ) -> Result < ( ) > {
439+ // Validate WITH item expressions (similar to RETURN)
440+ for item in & with_clause. items {
441+ self . analyze_value_expression ( & item. expression ) ?;
442+ }
443+ // Validate ORDER BY within WITH if present
444+ if let Some ( order_by) = & with_clause. order_by {
445+ for item in & order_by. items {
446+ self . analyze_value_expression ( & item. expression ) ?;
447+ }
448+ }
449+ Ok ( ( ) )
450+ }
451+
419452 /// Analyze ORDER BY clause
420453 fn analyze_order_by_clause ( & mut self , order_by : & OrderByClause ) -> Result < ( ) > {
421454 for item in & order_by. items {
@@ -558,6 +591,9 @@ mod tests {
558591 let query = CypherQuery {
559592 match_clauses : vec ! [ ] ,
560593 where_clause : None ,
594+ with_clause : None ,
595+ post_with_match_clauses : vec ! [ ] ,
596+ post_with_where_clause : None ,
561597 return_clause : ReturnClause {
562598 distinct : false ,
563599 items : vec ! [ ReturnItem {
@@ -585,6 +621,9 @@ mod tests {
585621 patterns: vec![ GraphPattern :: Node ( node) ] ,
586622 } ] ,
587623 where_clause : None ,
624+ with_clause : None ,
625+ post_with_match_clauses : vec ! [ ] ,
626+ post_with_where_clause : None ,
588627 return_clause : ReturnClause {
589628 distinct : false ,
590629 items : vec ! [ ReturnItem {
@@ -615,6 +654,9 @@ mod tests {
615654 patterns: vec![ GraphPattern :: Node ( node1) , GraphPattern :: Node ( node2) ] ,
616655 } ] ,
617656 where_clause : None ,
657+ with_clause : None ,
658+ post_with_match_clauses : vec ! [ ] ,
659+ post_with_where_clause : None ,
618660 return_clause : ReturnClause {
619661 distinct : false ,
620662 items : vec ! [ ] ,
@@ -661,6 +703,9 @@ mod tests {
661703 patterns: vec![ GraphPattern :: Path ( path) ] ,
662704 } ] ,
663705 where_clause : None ,
706+ with_clause : None ,
707+ post_with_match_clauses : vec ! [ ] ,
708+ post_with_where_clause : None ,
664709 return_clause : ReturnClause {
665710 distinct : false ,
666711 items : vec ! [ ] ,
@@ -690,6 +735,9 @@ mod tests {
690735 patterns: vec![ GraphPattern :: Node ( node) ] ,
691736 } ] ,
692737 where_clause : Some ( where_clause) ,
738+ with_clause : None ,
739+ post_with_match_clauses : vec ! [ ] ,
740+ post_with_where_clause : None ,
693741 return_clause : ReturnClause {
694742 distinct : false ,
695743 items : vec ! [ ] ,
@@ -729,6 +777,9 @@ mod tests {
729777 patterns: vec![ GraphPattern :: Path ( path) ] ,
730778 } ] ,
731779 where_clause : None ,
780+ with_clause : None ,
781+ post_with_match_clauses : vec ! [ ] ,
782+ post_with_where_clause : None ,
732783 return_clause : ReturnClause {
733784 distinct : false ,
734785 items : vec ! [ ] ,
@@ -755,6 +806,9 @@ mod tests {
755806 patterns: vec![ GraphPattern :: Node ( node) ] ,
756807 } ] ,
757808 where_clause : None ,
809+ with_clause : None ,
810+ post_with_match_clauses : vec ! [ ] ,
811+ post_with_where_clause : None ,
758812 return_clause : ReturnClause {
759813 distinct : false ,
760814 items : vec ! [ ] ,
@@ -792,6 +846,9 @@ mod tests {
792846 patterns: vec![ GraphPattern :: Node ( node) ] ,
793847 } ] ,
794848 where_clause : None ,
849+ with_clause : None ,
850+ post_with_match_clauses : vec ! [ ] ,
851+ post_with_where_clause : None ,
795852 return_clause : ReturnClause {
796853 distinct : false ,
797854 items : vec ! [ ] ,
@@ -834,6 +891,9 @@ mod tests {
834891 patterns: vec![ GraphPattern :: Path ( path) ] ,
835892 } ] ,
836893 where_clause : None ,
894+ with_clause : None ,
895+ post_with_match_clauses : vec ! [ ] ,
896+ post_with_where_clause : None ,
837897 return_clause : ReturnClause {
838898 distinct : false ,
839899 items : vec ! [ ] ,
@@ -898,6 +958,9 @@ mod tests {
898958 patterns: vec![ GraphPattern :: Path ( path) ] ,
899959 } ] ,
900960 where_clause : None ,
961+ with_clause : None ,
962+ post_with_match_clauses : vec ! [ ] ,
963+ post_with_where_clause : None ,
901964 return_clause : ReturnClause {
902965 distinct : false ,
903966 items : vec ! [ ] ,
0 commit comments