Skip to content

Conversation

@xterao
Copy link
Collaborator

@xterao xterao commented Aug 29, 2025

Description

This PR addresses issue #428 by fixing multiple SQL formatting issues related to ORDER BY and GROUP BY clauses, particularly when used with Doma condition directives and within function parameters.

Changes Made

1. Improved ORDER BY formatting with condition directives

  • Fixed indentation for ORDER BY clauses wrapped in /*%if*/ condition directives
  • Properly handles ORDER BY within window functions
  • Ensures correct alignment when ORDER BY appears after condition directives

2. Fixed GROUP BY formatting issues

  • Resolved spacing issues between keyword groups and parameter groups
  • Fixed indentation calculation for consecutive operators
  • Improved handling of WITHIN GROUP clauses for aggregate functions

3. Enhanced block indentation logic

  • Refactored parent group assignment logic for better consistency
  • Fixed indentation calculation for nested conditional blocks
  • Improved handling of function parameters containing SQL keywords

4. Added comprehensive test coverage

  • Added test case for ORDER BY with condition directives
  • Added test case for function keywords in condition directives
  • Added test case for function names used as column names

Example of Fixed Formatting

Before:

SELECT e.id
       , ROW_NUMBER() /*%if order */OVER(ORDER BY e.manager_id DESC)/*%end*/ AS row_num
       , RANK() OVER(PARTITION BY department_id
/*%if order */ORDER BY e.manager_id DESC/*%end */
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS rank_num

After:

SELECT e.id
       , ROW_NUMBER()
         /*%if order */
         OVER(ORDER BY e.manager_id DESC)
         /*%end*/
         AS row_num
       , RANK() OVER(PARTITION BY department_id
                     /*%if order */
                     ORDER BY e.manager_id DESC
                     /*%end */
                     ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS rank_num

Testing

All existing tests pass, and new test cases have been added to ensure the formatting improvements work correctly with:

  • Window functions with ORDER BY clauses
  • Conditional directives wrapping ORDER BY/GROUP BY
  • Complex nested SQL expressions
  • Aggregate functions with WITHIN GROUP clauses

Related Issues

Fixes #428

Checklist

  • Code follows the project's style guidelines
  • Tests have been added to cover the changes
  • All existing tests pass
  • The formatter correctly handles edge cases
  • Documentation has been updated if necessary

@xterao xterao added this to the 2.2.0 Release milestone Aug 29, 2025
@xterao xterao requested a review from Copilot August 29, 2025 02:23
@xterao xterao self-assigned this Aug 29, 2025
@github-actions github-actions bot added the fix Bug fixes label Aug 29, 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 with ORDER BY and GROUP BY clauses, particularly when used with Doma condition directives and within function parameters. The changes improve block indentation handling and ensure proper alignment of SQL keywords within conditional blocks.

  • Improved ORDER BY/GROUP BY formatting with condition directives by fixing indentation and spacing
  • Enhanced block indentation logic for nested conditional blocks and function parameters
  • Added comprehensive test coverage for the formatting improvements

Reviewed Changes

Copilot reviewed 26 out of 26 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
SqlKeywordUtil.kt Added new keywords and improved keyword relationship mappings
SqlBlockGenerator.kt Enhanced function name detection and table block handling
SqlFormatPreProcessor.kt Fixed keyword formatting after block comments
NotQueryGroupHandler.kt Added parameter keyword detection for better block creation
SqlBlockRelationBuilder.kt Refactored parent group assignment logic and improved indentation calculation
SqlFunctionGroupBlock.kt Fixed function block indentation calculation
SqlSubQueryGroupBlock.kt Improved child block filtering for indentation
SqlSubGroupBlock.kt Enhanced new line detection for conditional blocks
SqlSecondKeywordBlock.kt Improved space handling for function parameters
SqlKeywordGroupBlock.kt Enhanced space handling with condition directives
SqlElConditionLoopCommentBlock.kt Fixed line break detection logic
SqlCommaBlock.kt Improved indentation and spacing for function parameters
SqlRightPatternBlock.kt Added comments for parenthesis handling
SqlFileBlock.kt Enhanced spacing logic for function parameters and operation symbols
SqlBlock.kt Improved text length calculation and added operation symbol detection
TypeUtil.kt Added SqlColumnRawGroupBlock to expected types
Test files Added comprehensive test cases for ORDER BY/GROUP BY formatting
Token files Added missing SQL keywords and function tokens

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

@xterao xterao force-pushed the fix/sql-format-order-by-group branch from a3d8719 to c37d509 Compare August 29, 2025 03:08
@xterao xterao merged commit 0c19ca6 into main Aug 29, 2025
5 checks passed
@xterao xterao deleted the fix/sql-format-order-by-group branch August 29, 2025 04:05
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 Line Break in ORDER BY

2 participants