Fix stack overflow in merger with deeply nested SQL structures#14
Merged
Fix stack overflow in merger with deeply nested SQL structures#14
Conversation
…t SQL The maybe_stubbornly_merge step could collapse multiple segments back into a single segment with the same line count as the input. When this happened, maybe_merge_lines would recurse with identical input, creating true infinite recursion (not just deep recursion). This was triggered by complex dbt models with many CTEs, Jinja refs/sources, QUALIFY, and window functions. Added a guard: when segment processing produces a single segment with the same line count as the input, fall through to merge_single_segment instead of recursing with identical input. https://claude.ai/code/session_01QhNW6sYNjRhsG2Qjyut9Rm
Rename dbt_pharmacy_panels.sql to dbt_deeply_nested.sql and replace all domain-specific names (pharmacy, clinical programs, members, appointments) with generic equivalents (categories, entities, events, spans) while preserving the exact SQL structure that triggers the deep recursion bug. https://claude.ai/code/session_01QhNW6sYNjRhsG2Qjyut9Rm
446a090 to
4ca47f1
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixed a stack overflow issue in the line merger that occurred when processing deeply nested SQL structures with many CTEs, window functions, and Jinja references. The problem was caused by infinite recursion when segment processing collapsed multiple segments back into a single segment with identical input.
Changes
src/merger.rs: Added a guard condition to detect and prevent infinite recursion in
maybe_merge_lines(). When segment processing results in a single segment with the same number of lines as the input, the code now falls through to single-segment processing instead of recursing with identical input.tests/integration_test.rs: Added
test_format_dbt_deeply_nested()test case to verify the formatter can handle complex dbt models without stack overflow and that formatting is idempotent.tests/fixtures/dbt_deeply_nested.sql: Added a comprehensive test fixture containing a real-world dbt model with:
ref()andsource()callsImplementation Details
The fix detects when the merger's segment processing has reached a fixed point (single segment with unchanged line count) and prevents further recursion by directly processing that segment instead. This maintains the existing behavior for normal cases while preventing pathological recursion on deeply nested structures.
https://claude.ai/code/session_01QhNW6sYNjRhsG2Qjyut9Rm