Skip to content

Conversation

@devin-ai-integration
Copy link
Contributor

Implement Pretty Formatting Improvements for 6 Complex Misc SQL Test Cases

This PR implements comprehensive pretty formatting improvements for 6 complex misc SQL test cases, focusing on CTEs, window functions, lateral joins, and complex WHERE clauses.

🎯 Overview

Building on the existing pretty formatting infrastructure, this PR enhances the deparser's ability to format complex SQL constructs with improved readability while maintaining strict AST equivalence.

✨ Key Improvements

1. Enhanced CaseExpr Formatting

  • Fixed critical bug: Resolved "CASE1" syntax error by ensuring proper spacing between CASE and its argument
  • Multi-line formatting: Proper WHEN/THEN/ELSE alignment with consistent indentation
  • Preserved semantics: All kitchen-sink tests now pass with AST validation

2. Improved WindowDef Formatting

  • OVER clause formatting: Better readability for PARTITION BY and ORDER BY clauses
  • Multi-line window functions: Proper indentation for complex window expressions
  • FILTER clause support: Enhanced formatting for window functions with FILTER expressions

3. Enhanced JoinExpr Formatting

  • Complex JOIN conditions: Improved formatting for multi-line ON clauses with nested boolean expressions
  • Proper indentation: Better alignment for complex boolean logic in JOIN conditions
  • Parentheses handling: Maintained proper grouping while improving readability

4. String Literal Preservation

  • Multi-line string detection: Added containsMultilineStringLiteral() helper method
  • Content preservation: Ensures indentation doesn't modify string literal content during pretty printing
  • AST equivalence: Maintains semantic integrity across all formatting scenarios

🧪 Test Coverage

New Test Suite: misc-pretty.test.ts

Created comprehensive test coverage for 6 complex SQL scenarios:

  1. misc-1: Complex CTE with joins and aggregation
  2. misc-2: Window functions with FILTER and GROUPING SETS
  3. misc-3: LATERAL joins with JSON accessor functions
  4. misc-4: EXISTS with nested subqueries and CASE statements
  5. misc-5: Nested CTEs with type casting and subqueries
  6. misc-6: Complex multi-table joins with deeply nested boolean conditions

Snapshot Testing

  • Generated comprehensive snapshots for all 6 test cases
  • Visual validation of pretty formatting improvements
  • AST equivalence validation using expectParseDeparse() helper

🔧 Technical Details

Core Changes

  • deparser.ts: Enhanced CaseExpr, WindowDef, and JoinExpr methods with pretty formatting logic
  • String literal detection: Added helper method to preserve multi-line string content
  • Formatting consistency: Maintained existing pretty formatting patterns and conventions

Bug Fixes

  • Critical CaseExpr fix: Resolved syntax error that was breaking 5 kitchen-sink tests
  • Spacing issues: Fixed improper join operations that caused malformed SQL output
  • AST preservation: Ensured all pretty formatting maintains semantic equivalence

📊 Test Results

  • All kitchen-sink tests: ✅ Passing (previously 5 were failing due to CaseExpr bug)
  • New misc tests: ✅ 7/7 passing with AST validation
  • Overall test suite: ✅ 310/311 tests passing
  • Snapshot coverage: ✅ 6 new snapshots generated and validated

🎨 Formatting Examples

Before (misc-4):

SELECT p.id, p.title, CASE WHEN EXISTS (SELECT 1 FROM reviews r WHERE r.product_id = p.id AND r.rating >= 4) THEN 'Popular' ELSE 'Unrated' END AS status FROM products p WHERE p.archived = false

After (misc-4):

SELECT
  p.id,
  p.title,
  CASE 
    WHEN EXISTS (SELECT
      1
    FROM reviews AS r
    WHERE
      r.product_id = p.id
      AND r.rating >= 4) THEN 'Popular' 
    ELSE 'Unrated' 
  END AS status
FROM products AS p
WHERE
  p.archived = false

🚀 Impact

  • Improved readability: Complex SQL statements are now much more readable with proper formatting
  • Maintained compatibility: All existing functionality preserved with AST equivalence validation
  • Enhanced developer experience: Better formatted SQL output for debugging and code review
  • Comprehensive coverage: Handles edge cases like multi-line strings, complex joins, and nested expressions

🔗 Related Work


Link to Devin run: https://app.devin.ai/sessions/e2a49c9a83094092a217b1c66ad778de
Requested by: Dan Lynch ([email protected])

Testing: All changes have been thoroughly tested with AST validation to ensure semantic preservation across different formatting options.

devin-ai-integration bot and others added 2 commits June 23, 2025 10:13
…cases

- Enhanced CaseExpr formatting with proper WHEN/THEN/ELSE alignment
- Improved WindowDef formatting for OVER clauses with PARTITION BY/ORDER BY
- Enhanced JoinExpr formatting for complex JOIN ON conditions with proper indentation
- Added containsMultilineStringLiteral helper to preserve string literal content
- Created comprehensive test suite for misc SQL scenarios including CTEs, window functions, LATERAL joins, and complex subqueries
- Fixed critical CaseExpr bug that caused 'CASE1' syntax error by ensuring proper spacing
- Generated snapshots for 6 misc test cases to visualize pretty formatting improvements
- All kitchen-sink tests now pass with AST equivalence validation

Co-Authored-By: Dan Lynch <[email protected]>
- Added tests with pretty=false for all 6 misc SQL cases to ensure no formatting is applied when pretty is disabled
- Generated 12 new snapshots (6 pretty=true, 6 pretty=false) to validate correct scoping behavior
- Verified that pretty=false produces single-line SQL output while pretty=true produces formatted SQL
- All pretty formatting logic in deparser.ts is properly guarded with this.formatter.isPretty() checks
- Ensures formatting is only applied when pretty=true, addressing scoping concerns

Co-Authored-By: Dan Lynch <[email protected]>
@pyramation pyramation closed this Jun 23, 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