IdentifierBracketing.IncludeBrackets brackets aggregate function name in PIVOT (SUM becomes [SUM]), producing invalid T-SQL

Aperta
#239 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
67/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
csharp, sql

Direzione di ricerca

Read SqlScriptGeneratorVisitor.PivotedTableReference.cs and trace how AggregateFunctionIdentifier is emitted; then inspect SqlScriptGeneratorVisitor.Identifier.cs and the casing options mentioned in the issue. Reproduce with the supplied PIVOT query and IdentifierBracketing = IncludeBrackets. Done when the aggregate name remains unbracketed and uses built-in function casing while ordinary identifiers still follow their configured behavior.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Summary

With IdentifierBracketing.IncludeBrackets, the script generator wraps the aggregate function name inside a PIVOT clause in square brackets. The output is invalid T-SQL and fails to parse.

This shows up in SSMS 22.7+ (built-in SQL formatter with identifier_bracketing = IncludeBrackets), because the formatter is built on ScriptDOM.

Repro

Input:

SELECT [p].[2024], [p].[2025]
FROM [#chk] PIVOT (SUM([volume]) FOR [year] IN ([2024], [2025])) AS [p];

Format with IdentifierBracketing = IncludeBrackets (SSMS: Tools > Options > SQL Formatter, or identifier_bracketing = IncludeBrackets in .editorconfig).

Actual output:

... PIVOT ([SUM] ([volume]) FOR [year] IN ([2024], [2025])) AS [p];

Executing the result fails with:

Msg 102, Level 15, State 1
Incorrect syntax near '('.

Expected output: SUM stays unbracketed:

... PIVOT (SUM([volume]) FOR [year] IN ([2024], [2025])) AS [p];
Likely cause

PivotedTableReference.AggregateFunctionIdentifier is a MultiPartIdentifier, and SqlScriptGeneratorVisitor.PivotedTableReference.cs emits it with GenerateFragmentIfNotNull(node.AggregateFunctionIdentifier). That sends it through the normal identifier path in SqlScriptGeneratorVisitor.Identifier.cs, so IncludeBrackets applies to it like a column or table name.

The aggregate name should probably be exempt from bracketing. built_in_function_casing should apply to it instead of identifier_casing, so that with identifier_casing = Lowercase we don't get [sum].

Workaround

Use IdentifierBracketing.Preserve, or rewrite the PIVOT as conditional aggregation (SUM(CASE WHEN [year] = 2024 THEN [volume] END) AS [2024]).

Environment
  • SSMS 22.7+ built-in SQL formatter (ScriptDOM-based)
Lingua principale
GAP
Stelle
284
Fork
47
Merge medio
1g 23h
PR unite (30g)
3

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di microsoft/SqlScriptDOM

Tutte le issue di microsoft/SqlScriptDOM

Issue simili

Altre issue su Backend & API Design

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.