-
Notifications
You must be signed in to change notification settings - Fork 0
Implement SQL Formatting Rules for Function Names #316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement SQL Formatting Rules for Function Names #316
Conversation
There was a problem hiding this 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_NAMEtoken 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
isWindowFunctionchecks all function tokens, not just window functions; consider renaming toisFunctionTokenorisFunctionNamefor clarity.
private static boolean isWindowFunction(CharSequence word) {
src/main/kotlin/org/domaframework/doma/intellij/formatter/util/SqlBlockUtil.kt:282
- Returning a
SqlKeywordBlockfor a subsequent function group may misclassify parameters; consider returning aSqlFunctionParamBlockor appropriate function parameter block instead.
if (lastGroupBlock is SqlFunctionGroupBlock) {
This update introduces formal formatting rules for handling function names in SQL statements.
Implemented Features
FUNCTION_NAME.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.
Unlike keywords, function names are not automatically uppercased during formatting.
Token Registration by Database
Registered some commonly used function names as tokens per database:
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
https://www.postgresql.jp/docs/9.4/functions.html
https://dev.mysql.com/doc/refman/8.0/ja/functions.html
https://docs.oracle.com/en/database/oracle/oracle-database/23/sqlrf/Functions.html