@@ -837,39 +837,55 @@ fn format_sweep_audit_ddl_query_body_depth(
837837 let line_starts = line_start_offsets ( formatted) ;
838838 let document = FormatSweepDocument :: new ( formatted, db_type) ;
839839 let mut words_by_line = vec ! [ Vec :: <& str >:: new( ) ; lines. len( ) ] ;
840- let mut first_word_paren_depth_by_line = vec ! [ None ; lines. len( ) ] ;
840+ let mut first_word_delimiter_depth_by_line = vec ! [ None ; lines. len( ) ] ;
841841 let mut first_word_statement_index_by_line = vec ! [ None ; lines. len( ) ] ;
842- let mut trailing_comma_paren_depth_by_line = vec ! [ None ; lines. len( ) ] ;
842+ let mut trailing_comma_delimiter_depth_by_line = vec ! [ None ; lines. len( ) ] ;
843843 let mut trailing_semicolon_paren_depth_by_line = vec ! [ None ; lines. len( ) ] ;
844844 let mut line_idx = 0usize ;
845845 let mut paren_depth = 0usize ;
846+ let mut brace_depth = 0usize ;
847+ let mut bracket_depth = 0usize ;
846848
847849 for token in & document. tokens {
848850 while line_idx + 1 < line_starts. len ( ) && line_starts[ line_idx + 1 ] <= token. start {
849851 line_idx = line_idx. saturating_add ( 1 ) ;
850852 }
851- if matches ! ( & token. token, SqlToken :: Symbol ( symbol) if symbol == ")" ) {
852- paren_depth = paren_depth. saturating_sub ( 1 ) ;
853+ if let SqlToken :: Symbol ( symbol) = & token. token {
854+ match symbol. as_str ( ) {
855+ ")" => paren_depth = paren_depth. saturating_sub ( 1 ) ,
856+ "}" => brace_depth = brace_depth. saturating_sub ( 1 ) ,
857+ "]" => bracket_depth = bracket_depth. saturating_sub ( 1 ) ,
858+ _ => { }
859+ }
853860 }
854861 if let SqlToken :: Word ( word) = & token. token {
855862 if let Some ( words) = words_by_line. get_mut ( line_idx) {
856863 if words. is_empty ( ) {
857- first_word_paren_depth_by_line[ line_idx] = Some ( paren_depth) ;
864+ first_word_delimiter_depth_by_line[ line_idx] =
865+ Some ( ( paren_depth, brace_depth, bracket_depth) ) ;
858866 first_word_statement_index_by_line[ line_idx] = Some ( token. statement_index ) ;
859867 }
860868 words. push ( word) ;
861869 }
862870 }
863871 if !matches ! ( token. token, SqlToken :: Comment ( _) ) {
864- trailing_comma_paren_depth_by_line[ line_idx] =
865- matches ! ( & token. token, SqlToken :: Symbol ( symbol) if symbol == "," )
866- . then_some ( paren_depth) ;
872+ trailing_comma_delimiter_depth_by_line[ line_idx] =
873+ matches ! ( & token. token, SqlToken :: Symbol ( symbol) if symbol == "," ) . then_some ( (
874+ paren_depth,
875+ brace_depth,
876+ bracket_depth,
877+ ) ) ;
867878 trailing_semicolon_paren_depth_by_line[ line_idx] =
868879 matches ! ( & token. token, SqlToken :: Symbol ( symbol) if symbol == ";" )
869880 . then_some ( paren_depth) ;
870881 }
871- if matches ! ( & token. token, SqlToken :: Symbol ( symbol) if symbol == "(" ) {
872- paren_depth = paren_depth. saturating_add ( 1 ) ;
882+ if let SqlToken :: Symbol ( symbol) = & token. token {
883+ match symbol. as_str ( ) {
884+ "(" => paren_depth = paren_depth. saturating_add ( 1 ) ,
885+ "{" => brace_depth = brace_depth. saturating_add ( 1 ) ,
886+ "[" => bracket_depth = bracket_depth. saturating_add ( 1 ) ,
887+ _ => { }
888+ }
873889 }
874890 }
875891
@@ -962,7 +978,9 @@ fn format_sweep_audit_ddl_query_body_depth(
962978 ) ) ;
963979 }
964980
965- let query_paren_depth = first_word_paren_depth_by_line[ body_idx] . unwrap_or ( 0 ) ;
981+ let query_delimiter_depth =
982+ first_word_delimiter_depth_by_line[ body_idx] . unwrap_or ( ( 0 , 0 , 0 ) ) ;
983+ let query_paren_depth = query_delimiter_depth. 0 ;
966984 let statement_span_end_idx = first_word_statement_index_by_line[ body_idx]
967985 . and_then ( |statement_index| document. statements . get ( statement_index) )
968986 . map ( |statement| {
@@ -977,7 +995,7 @@ fn format_sweep_audit_ddl_query_body_depth(
977995 . unwrap_or ( statement_span_end_idx) ;
978996
979997 for clause_idx in body_idx. saturating_add ( 1 ) ..=statement_end_idx {
980- if first_word_paren_depth_by_line [ clause_idx] != Some ( query_paren_depth )
998+ if first_word_delimiter_depth_by_line [ clause_idx] != Some ( query_delimiter_depth )
981999 || !is_direct_query_clause ( & words_by_line[ clause_idx] )
9821000 {
9831001 continue ;
@@ -1001,17 +1019,17 @@ fn format_sweep_audit_ddl_query_body_depth(
10011019 }
10021020 }
10031021
1004- for ( comma_idx, trailing_comma_paren_depth ) in trailing_comma_paren_depth_by_line
1022+ for ( comma_idx, trailing_comma_delimiter_depth ) in trailing_comma_delimiter_depth_by_line
10051023 . iter ( )
10061024 . enumerate ( )
10071025 . take ( statement_end_idx)
10081026 . skip ( body_idx)
10091027 {
1010- if * trailing_comma_paren_depth != Some ( query_paren_depth ) {
1028+ if * trailing_comma_delimiter_depth != Some ( query_delimiter_depth ) {
10111029 continue ;
10121030 }
10131031 let Some ( sibling_idx) = ( comma_idx. saturating_add ( 1 ) ..=statement_end_idx) . find ( |idx| {
1014- first_word_paren_depth_by_line [ * idx] == Some ( query_paren_depth )
1032+ first_word_delimiter_depth_by_line [ * idx] == Some ( query_delimiter_depth )
10151033 && !words_by_line[ * idx] . is_empty ( )
10161034 } ) else {
10171035 continue ;
@@ -2450,6 +2468,22 @@ fn formatting_sweep_ddl_query_body_depth_audit_flags_missing_owner_depth() {
24502468 }
24512469}
24522470
2471+ #[ test]
2472+ fn formatting_sweep_ddl_query_body_depth_ignores_json_duality_delimiters ( ) {
2473+ let formatted = r#"CREATE JSON RELATIONAL DUALITY VIEW dv_demo AS
2474+ SELECT JSON { '_id' :parent_owner.id, 'children' : [
2475+ SELECT JSON { 'childId' :child_owner.id, 'amount' :child_owner.amount }
2476+ FROM child_table child_owner WITH INSERT UPDATE DELETE
2477+ WHERE child_owner.parent_id = parent_owner.id ] }
2478+ FROM parent_table parent_owner WITH UPDATE INSERT DELETE;"# ;
2479+
2480+ let ( _, issues) = format_sweep_audit_ddl_query_body_depth ( formatted, DatabaseType :: Oracle ) ;
2481+ assert ! (
2482+ issues. is_empty( ) ,
2483+ "JSON object/array commas are not DDL query-list siblings: {issues:#?}"
2484+ ) ;
2485+ }
2486+
24532487#[ test]
24542488fn formatting_sweep_mysql_fixed_phrases_ignore_source_line_breaks ( ) {
24552489 let source = r#"DELIMITER $$
0 commit comments