Skip to content

Conversation

@xterao
Copy link
Collaborator

@xterao xterao commented Jul 15, 2025

This update introduces formal formatting rules for handling function names in SQL statements.


Implemented Features

  • Defined elements to represent function names with a new element type: FUNCTION_NAME.
  • Escape handling:
    When a function name is enclosed in escape characters (e.g., double quotes or backticks), ensure no extra spaces are inserted between the escape symbols and the function name.
  • Case sensitivity:
    Unlike keywords, function names are not automatically uppercased during formatting.

Token Registration by Database

  • Registered some commonly used function names as tokens per database:

    • PostgreSQL
    • MySQL
    • Oracle
  • Implemented token management classes per database dialect.

  • The lexer determines whether a token is a function name via a helper class that references these token sets.


Reference Documentation

@xterao xterao self-assigned this Jul 15, 2025
@xterao xterao requested a review from Copilot July 15, 2025 06:18
@xterao xterao linked an issue Jul 15, 2025 that may be closed by this pull request
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 implements formal SQL formatting rules to recognize and correctly format function names, especially when escaped, without altering their case.

  • Introduce a new FUNCTION_NAME token type in lexer, grammar, and syntax highlighter.
  • Add custom spacing rules to prevent extra spaces between function names and parentheses or escape characters.
  • Register common SQL functions per dialect and aggregate them in a helper for token classification.

Reviewed Changes

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

Show a summary per file
File Description
src/test/testData/sql/formatter/SelectEscapeFunctionName.sql Raw SQL fixture with extra spaces around escaped identifiers.
src/test/testData/sql/formatter/SelectEscapeFunctionName_format.sql Expected formatted SQL ensuring no extra spaces around escapes.
src/test/kotlin/org/domaframework/doma/intellij/formatter/SqlFormatterTest.kt Added testSelectEscapeFunctionNameFormatter to validate spacing.
src/main/kotlin/org/domaframework/doma/intellij/formatter/visitor/SqlFormatVisitor.kt Updated to include SqlTypes.FUNCTION_NAME in formatting targets.
src/main/kotlin/org/domaframework/doma/intellij/formatter/builder/SqlFormattingModelBuilder.kt Defined custom spacing rules for FUNCTION_NAME tokens.
src/main/java/org/domaframework/doma/intellij/tokens/SqlFunctionToken.java Introduced default set of SQL functions for tokenization.
src/main/java/org/domaframework/doma/intellij/SqlTokenHelper.java Aggregates function tokens across dialects for classification.
src/main/java/org/domaframework/doma/intellij/Sql.flex Lexer updated to classify words as FUNCTION_NAME.
src/main/java/org/domaframework/doma/intellij/Sql.bnf Grammar extended to recognize FUNCTION_NAME non-terminals.
Comments suppressed due to low confidence (3)

src/test/kotlin/org/domaframework/doma/intellij/formatter/SqlFormatterTest.kt:57

  • Consider adding tests for non-escaped function names (e.g., SUM(col)) to verify spacing and case preservation rules.
    fun testSelectEscapeFunctionNameFormatter() {

src/main/java/org/domaframework/doma/intellij/Sql.flex:33

  • [nitpick] Method isWindowFunction checks all function tokens, not just window functions; consider renaming to isFunctionToken or isFunctionName for clarity.
        private static boolean isWindowFunction(CharSequence word) {

src/main/kotlin/org/domaframework/doma/intellij/formatter/util/SqlBlockUtil.kt:282

  • Returning a SqlKeywordBlock for a subsequent function group may misclassify parameters; consider returning a SqlFunctionParamBlock or appropriate function parameter block instead.
                        if (lastGroupBlock is SqlFunctionGroupBlock) {

@xterao xterao merged commit f3f3b7f into feature/sql-format-official-version Jul 15, 2025
5 checks passed
@xterao xterao deleted the feature/sql-format-support-functions branch July 15, 2025 07:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support for Formatting Reserved Words Used as Functions

2 participants