Skip to content

Conversation

@pyramation
Copy link
Collaborator

@pyramation pyramation commented Jun 26, 2025

Fix systematic indentation regressions in pgsql-parser deparser

Summary

This PR fixes systematic indentation regressions that broke 22 tests across 4 test suites while maintaining the successful fixes for constraints-9, selects-13, misc-8, and misc-9 test cases. The core issue was double indentation caused by manually incrementing indentLevel in context spawning AND applying additional indentation through methods like context.indentToCurrentLevel().

Key Changes:

  • Fixed GROUP BY clause over-indentation by using context.indent() instead of manual indentLevel increments
  • Fixed ORDER BY clause over-indentation with the same systematic approach
  • Fixed INSERT statement column indentation to use proper context.indent() method
  • Fixed window function PARTITION BY and ORDER BY over-indentation
  • Fixed VALUES clause indentation by changing from context.indentToCurrentLevel() to context.indent()

Systematic Approach: Treats indentLevel as contextual depth that increases by 1 for nested blocks, with the visitor pattern naturally handling depth reduction. This eliminates the double indentation issue while maintaining semantic SQL structure.

Review & Testing Checklist for Human (5 items - HIGH RISK)

  • Manually test complex SQL formatting scenarios - Verify that nested SELECT statements, CASE expressions, window functions, and CTE queries render with correct indentation
  • Visual verification of indentation correctness - Check that GROUP BY, ORDER BY, INSERT VALUES, and constraint clauses look visually correct and properly aligned
  • Run full test suite locally - Ensure no hidden regressions were introduced beyond the specific test cases mentioned
  • Test edge cases for context spawning - Verify that deeply nested SQL constructs (subqueries within CASE within window functions) maintain proper indentation levels
  • Verify target test cases still pass - Double-check that constraints-9, selects-13, misc-8, and misc-9 continue to work correctly

Recommended Test Plan:

  1. Run the full test suite: yarn test
  2. Test pretty-printing of complex SQL files with nested constructs
  3. Manually inspect formatted output for visual correctness
  4. Test with SQL containing multiple levels of nesting (CTEs, subqueries, CASE expressions)

Diagram

graph TD
    A[packages/deparser/src/deparser.ts]:::major-edit --> B[SelectStmt method]
    A --> C[InsertStmt method]
    A --> D[WindowDef method]
    
    B --> E[GROUP BY clause fix]:::major-edit
    B --> F[ORDER BY clause fix]:::major-edit
    B --> G[VALUES clause fix]:::major-edit
    B --> H[HAVING clause fix]:::major-edit
    
    C --> I[Column indentation fix]:::major-edit
    
    D --> J[Window function indentation fix]:::major-edit
    
    K[packages/deparser/src/visitors/base.ts]:::context --> L[DeparserContext class]
    L --> M[context.indent method]
    L --> N[context.indentToCurrentLevel method]
    
    E --> M
    F --> M
    G --> M
    H --> M
    I --> M
    J --> M
    
    O[Test Files]:::context --> P[constraints-9, selects-13, misc-8, misc-9]:::context
    O --> Q[casing-1, casing-10, casing-11, casing-12]:::minor-edit
    
    subgraph Legend
        L1[Major Edit]:::major-edit
        L2[Minor Edit]:::minor-edit  
        L3[Context/No Edit]:::context
    end
    
    classDef major-edit fill:#90EE90
    classDef minor-edit fill:#87CEEB
    classDef context fill:#FFFFFF
Loading

Notes

  • The changes are concentrated in the SelectStmt method of deparser.ts, which handles the majority of SQL formatting logic
  • The systematic fix follows the principle that "you should only ever be incrementing indentLevel by 1" as mentioned in the original requirements
  • All CI checks are passing, but given the complexity of indentation logic, thorough manual testing is recommended
  • The fix maintains backward compatibility while resolving the over-indentation issues that were causing test failures

…method

- Replace parentNodeTypes=[] with new DeparserContext()
- Replace parentNodeTypes=[...context.parentNodeTypes, nodeType] with context.spawn(nodeType)
- Replace {...context, property: true} patterns with context.spawn(methodName, {property: true})
- Fix context propagation issues in IndexElem and CopyStmt methods
- Ensure proper context spawning for GIN index parameters and COPY statement WITH clauses
- All 279 test suites continue to pass with the new context system

Co-Authored-By: Dan Lynch <[email protected]>
@devin-ai-integration
Copy link
Contributor

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

devin-ai-integration bot and others added 14 commits June 26, 2025 09:31
- Change visit method to use context.spawn(nodeType) when calling node methods
- Ensures all methods receive properly contextualized calls with nodeType in parentNodeTypes
- Makes context spawning more consistent throughout the entire deparser
- All 279 test suites continue to pass with this more thorough approach

Co-Authored-By: Dan Lynch <[email protected]>
…ntext

- Ensures RangeVar receives correct objtype context to prevent 'ONLY' keyword
- Fixes ALTER TYPE RENAME ATTRIBUTE statements
- All 279 test suites now passing

Co-Authored-By: Dan Lynch <[email protected]>
…better readability

- Group (!context.parentNodeTypes.includes('AlterTypeStmt') && context.objtype !== 'OBJECT_TYPE') conditions
- Improves code semantics by making it clear both conditions relate to preventing ONLY keyword for ALTER TYPE operations
- Remove ternary operator from RenameStmt to maintain universal spawn() flow
- All 279 test suites passing

Co-Authored-By: Dan Lynch <[email protected]>
…xt parameters

- Update formatWindowFrame method signature to accept DeparserContext parameter
- Update deparseOperatorName method signature to accept DeparserContext parameter
- Replace all empty context creation with passed context in both methods
- Update all call sites to pass properly spawned context instead of creating empty contexts
- Ensures consistent context propagation in helper methods that are not actual node types

Co-Authored-By: Dan Lynch <[email protected]>
- Change visit() to pass original context instead of spawning new context
- Prevents premature context spawning and gives node methods control
- All tests continue to pass (279/279 test suites)
- Helper methods already properly use passed context parameters

Co-Authored-By: Dan Lynch <[email protected]>
…perty

- Add SqlFormatter as a property of DeparserContext with default instantiation
- Replace all this.formatter references with context.formatter throughout deparser
- Remove direct SqlFormatter import and private formatter property from Deparser class
- Update deparse() and visit() methods to create context with formatter when not provided
- Maintain modular design allowing external formatter instantiation and context passing
- Preserve existing formatting logic while improving context-aware formatting
- All 279 test suites continue to pass

Co-Authored-By: Dan Lynch <[email protected]>
- Make SqlFormatter private property on DeparserContext
- Add public methods: indent(), newline(), parens(), format(), isPretty()
- Replace all context.formatter.* references with direct context methods
- Fix CREATE TABLE constraint indentation using context.indentLevel
- UNIQUE and FOREIGN KEY constraints now properly indented relative to table structure
- Context spawning with increased indent levels for table elements
- All 279 test suites continue to pass

Co-Authored-By: Dan Lynch <[email protected]>
… indentLevel

- Remove explicit indentLevel increment when spawning table element contexts
- Let context.indent() method handle indentation automatically using indentLevel + 1
- UNIQUE and FOREIGN KEY constraints now properly indented relative to columns
- Constraints are visually distinct with proper logical grouping indentation
- All tests passing including CREATE TABLE pretty printing snapshots

Co-Authored-By: Dan Lynch <[email protected]>
- Replace hardcoded '  AND ' and '  OR ' spacing with context.indent() in BoolExpr
- Add proper indentLevel context spawning for GROUP BY and ORDER BY clauses
- Boolean expressions now use context-aware indentation for better logical grouping
- SELECT clause elements properly track indent levels through context spawning
- Consistent indentation management across nested statements and expressions

Co-Authored-By: Dan Lynch <[email protected]>
- Spawn context with increased indentLevel for CHECK constraint expressions
- Spawn context with increased indentLevel for nested SELECT statements
- Update snapshots to reflect proper indentation of logical scopes
- Ensure tax_rate expressions in CHECK constraints are properly indented
- Ensure nested SELECT FROM clauses are properly indented within subqueries

Co-Authored-By: Dan Lynch <[email protected]>
…, and nested SELECT statements

- Add indentToCurrentLevel() method to DeparserContext for precise current-level indentation
- Fix over-indentation in CHECK constraint expressions by using proper context spawning
- Improve CASE statement WHEN/ELSE clause alignment using indentToCurrentLevel()
- Resolve nested SELECT indentation issues in SubLink by managing indentLevel properly
- Fix BoolExpr AND/OR indentation to align with logical grouping
- All pretty printing tests now pass including constraints-9, selects-13, misc-8, and misc-9

Co-Authored-By: Dan Lynch <[email protected]>
…rrentLevel for proper systematic indentation

Co-Authored-By: Dan Lynch <[email protected]>
…and nested expressions

- Update WHERE clause to use context.indent() for proper first-line indentation
- Remove explicit indentLevel increment from WHERE context spawning
- All target test cases now pass: constraints-9, selects-13, misc-8, misc-9
- Indentation now follows systematic rules with indentLevel as contextual depth

Co-Authored-By: Dan Lynch <[email protected]>
- Change context.indentToCurrentLevel(tuple) to context.indent(tuple) for proper VALUES indentation
- Fixes INSERT statement indentation issues in casing-1, casing-10, casing-11, casing-12 tests
- Maintains successful fixes for constraints-9, selects-13, misc-8, misc-9 test cases
- Completes systematic indentation approach using indentLevel as contextual depth

Co-Authored-By: Dan Lynch <[email protected]>
@pyramation pyramation closed this Jun 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants