Skip to content

ast/formatter: SQL() and pkg/formatter drop IGNORE NULLS, named arguments, ON DUPLICATE KEY UPDATE and INSERT OUTPUT; parser discards SELECT ... SETTINGS #600

Closed

Description

@ajitpratap0

Summary

The parser stores several constructs that neither ast.SQL() nor pkg/formatter renders, so the output silently changes meaning. ClickHouse SELECT ... SETTINGS is worse: the parser accepts it but has no AST field to store it in.

Reproduction

On main 61ca6c8, tree.SQL(), formatter.FormatAST(tree, ast.CompactStyle()) and ReadableStyle() all give the same result:

Input (dialect) Output Stored in AST?
SELECT FIRST_VALUE(x) IGNORE NULLS OVER (ORDER BY y) FROM t ... FIRST_VALUE(x) OVER (ORDER BY y) ... yes, FunctionCall.NullTreatment = "IGNORE NULLS"
SELECT f(x => 1) FROM t SELECT f(x) FROM t yes, arg is *ast.NamedArgument
INSERT INTO t (a) VALUES (1) ON DUPLICATE KEY UPDATE a = 2 (mysql) INSERT INTO t (a) VALUES (1) yes, InsertStatement.OnDuplicateKey != nil
INSERT INTO t (a) OUTPUT INSERTED.id VALUES (1) (sqlserver) INSERT INTO t (a) VALUES (1) yes, len(InsertStatement.Output) == 1
SELECT a FROM t SETTINGS max_threads = 4 (clickhouse) SELECT a FROM t no, SelectStatement has no Settings field

Expected: each clause is kept. For contrast, PostgreSQL ON CONFLICT ... RETURNING round-trips correctly through pkg/formatter.

Notes

  • pkg/sql/ast/sql.go has no rendering for NamedArgument or FunctionCall.NullTreatment (neither appears in sql.go or pkg/formatter/render.go). The InsertStatement renderers ignore OnDuplicateKey and Output (fields at pkg/sql/ast/ast_statements.go:252,257).
  • For SETTINGS, the parser should store the assignments on SelectStatement, as it already does for the ClickHouse DDL SETTINGS in pkg/sql/parser/ddl.go:406, and the renderers should emit them.
  • Add round-trip tests (parse, then SQL()/FormatAST, then re-parse, then compare) for each case.

Found during review of #591, #583, #584.

Metadata

Metadata

Assignees

No one assigned

    Labels

    astAST node changesbugSomething isn't workingformatter

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions