Skip to content

Conversation

@xterao
Copy link
Collaborator

@xterao xterao commented Aug 27, 2025

Summary
This PR fixes an issue where subqueries were incorrectly indented under certain conditions, leading to inconsistent SQL formatting.

Details
Indentation inside subqueries is now corrected in the following cases:

  • When a subquery is used inside an IN group.
  • When a subquery is used with EXISTS.
  • When a subquery is used in a condition immediately following WHERE.

Example (after fix):

DELETE FROM user_session s
 WHERE s.count = ( SELECT COUNT(*)
                     FROM user u
                    WHERE u.id = /* id */1
                      AND u.session_id = u.id
                      AND u.time_stamp < /* current */'2099-12-31 00:00:00' )
    OR EXISTS ( SELECT u.id
                  FROM user u
                 WHERE u.id = /* id */1
                   AND u.session_id = u.id
                   AND u.time_stamp < /* current */'2099-12-31 00:00:00' )
   AND s.user_number IN ( SELECT u.number
                            FROM user u
                           WHERE u.status = /* status */'active' )

Impact
This ensures that subqueries within conditions are consistently indented, improving readability and maintaining a uniform SQL style.

@xterao xterao requested a review from Copilot August 27, 2025 08:15
@xterao xterao self-assigned this Aug 27, 2025
@github-actions github-actions bot added the fix Bug fixes label Aug 27, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes SQL formatting issues where subqueries were not properly indented in specific contexts (IN groups, EXISTS clauses, and WHERE conditions). The fix ensures consistent indentation by refactoring indentation calculation logic and adding proper handling for subqueries in different SQL constructs.

Key changes:

  • Refactored indentation calculation by extracting common logic into a shared method
  • Added special handling for subqueries in WHERE clauses and conditional expressions
  • Added test coverage for DELETE statements with subqueries

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlBlock.kt Added shared calculatePrevBlocksLength method for consistent indentation calculation
src/main/kotlin/org/domaframework/doma/intellij/formatter/block/word/SqlFunctionGroupBlock.kt Removed duplicate indentation calculation method
src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/option/SqlInGroupBlock.kt Refactored to use shared indentation calculation method
src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/condition/SqlConditionalExpressionGroupBlock.kt Updated to use shared indentation calculation for better subquery formatting
src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/condition/SqlConditionKeywordGroupBlock.kt Added special handling for WHERE group blocks
src/test/kotlin/org/domaframework/doma/intellij/formatter/SqlFormatterTest.kt Added test case for DELETE with subquery formatting
src/test/testData/sql/formatter/DeleteWithSubQuery.sql Test input with poorly formatted subqueries
src/test/testData/sql/formatter/DeleteWithSubQuery_format.sql Expected output with properly formatted subqueries

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@xterao xterao merged commit 725ec6c into main Aug 27, 2025
5 checks passed
@xterao xterao deleted the fix/sql-format-delete-condition-subquery branch August 27, 2025 09:21
@xterao xterao linked an issue Aug 28, 2025 that may be closed by this pull request
@xterao xterao added this to the 2.2.0 Release milestone Aug 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix Bug fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect Indentation for Subqueries in Conditions

2 participants