Skip to content

ast/formatter: SQL Server table hints WITH (NOLOCK) are never rendered; SQL() also drops PIVOT #604

Closed

Description

@ajitpratap0

Summary

The parser stores SQL Server table hints in TableReference.TableHints, but no renderer emits them. ast.SQL() also drops TableReference.Pivot, which pkg/formatter renders correctly. Round-tripping SQL Server queries therefore silently removes locking hints and PIVOT clauses.

Reproduction

With gosqlx.ParseWithDialect(sql, "sqlserver") on main 61ca6c8:

SELECT * FROM t WITH (NOLOCK)
  SQL():           SELECT * FROM t
  FormatAST:       SELECT * FROM t
  gosqlx format:   SELECT * FROM t

SELECT * FROM s PIVOT (SUM(amt) FOR q IN ([Q1], [Q2])) AS p
  SQL():           SELECT * FROM s p
  FormatAST:       SELECT * FROM s PIVOT (SUM(amt) FOR q IN ([Q1], [Q2])) AS p   (correct)
  gosqlx format:   SELECT * FROM s AS p

Expected: WITH (NOLOCK) and the PIVOT clause are kept by all renderers.

Notes

  • TableHints is declared at pkg/sql/ast/ast_clauses.go:144. It is not referenced in pkg/sql/ast/sql.go (writeTableRef, ~line 1963), pkg/formatter/render.go (tableRefSQL, line 1281) or the CLI formatTableReference.
  • writeTableRef in pkg/sql/ast/sql.go has no Pivot or Unpivot handling. Port the logic from pkg/formatter/render.go:1294-1330.
  • The CLI case is also listed in the CLI formatter umbrella issue.

Found during review of #585.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions