Skip to content

feat(parser): PostgreSQL INSERT target-table alias INSERT INTO t AS u - #969

Open
kshvakov wants to merge 2 commits into
ajitpratap0:mainfrom
kshvakov:feat/insert-target-alias
Open

kshvakov wants to merge 2 commits into
ajitpratap0:mainfrom
kshvakov:feat/insert-target-alias

Conversation

@kshvakov

Copy link
Copy Markdown

INSERT INTO core."user" AS u (org_id, ...) VALUES (...) RETURNING u.id
is standard PostgreSQL that GoSQLX currently rejects with
E2002: expected VALUES, SELECT, or WITH, got AS.

Verified against libpg_query (pg_query.Parse): the AS form parses
(INSERT INTO t AS u (a) VALUES (1) → ok), the bare-alias form does not
(INSERT INTO t u → invalid SQL). Accepting only the AS form therefore
keeps the grammar unambiguous across all dialects — mirroring how the SQL
Server OUTPUT clause is already accepted dialect-agnostically.

Changes:

  • ast: InsertDialectFields gains TableAlias / TableAliasQuote next
    to TableNameQuote (stored in Extras, keeping the pinned
    InsertStatement struct size unchanged)
  • parser: after the target table name, consume AS + identifier with
    the same reserved-word guard the UPDATE/DELETE target alias uses
  • ast SQL(): writeInsertHead renders AS alias with quoting

Tests (pkg/sql/parser/insert_grammar_gaps_test.go):

  • parse shape, quoted schema target (core."user"), no-column-list and
    INSERT ... SELECT forms through the grammar-gap harness
    (parse + SQL() + re-parse fixpoint)
  • error pins: non-identifier alias, missing alias, reserved word as
    alias, bare alias without AS

Real-world case that surfaced this: production Go code formatting SQL
literals with INSERT INTO core."user" AS u (...) ... RETURNING u.id, u.org_id, .... Changelog fragment: changelog.d/insert-target-alias.added.md.

go test ./... passes; the only failure, scripts/perf/gate
TestProfileCachegrindTable, fails identically on clean main
(environment-dependent perf gate, unrelated).

@vercel

vercel Bot commented Nov 14, 2026

Copy link
Copy Markdown

@kshvakov is attempting to deploy a commit to the coolajitpratapsingh0-9932's projects Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions github-actions Bot added parser ast AST node changes labels Nov 14, 2026
@vercel

vercel Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
gosqlx-website Ignored Ignored Preview Nov 14, 2026 10:12pm UTC

@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 72.72727% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pkg/sql/ast/sql_dml.go 0.00% 2 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

INSERT INTO core."user" AS u (...) is standard PostgreSQL that GoSQLX
rejected with E2002 (expected VALUES, SELECT, or WITH, got AS). Real
libpg_query parses the AS form and rejects the bare-alias form, so AS
after the target table is unambiguous and is accepted regardless of
dialect, mirroring how OUTPUT is accepted dialect-agnostically.

- ast: InsertDialectFields gains TableAlias / TableAliasQuote next to
  TableNameQuote (Extras keeps the pinned InsertStatement size)
- parser: after the target table name, consume AS + identifier with the
  same reserved-word guard the UPDATE/DELETE target alias uses
- ast SQL(): writeInsertHead renders ' AS alias' with quoting
- tests: parse shape, quoted target, no-column-list and INSERT...SELECT
  forms through the grammar-gap harness (parse + SQL + re-parse
  fixpoint); error pins for non-identifier, missing and reserved-word
  aliases and for the bare alias without AS
Codecov flagged the three sql_dml.go patch lines as uncovered: Go
coverage profiles are per-package, so the parser tests that exercise
writeInsertHead do not count toward the ast package's profile. Adds two
cases to TestInsertStatementSQL_Forms — a plain alias and a quoted
alias — both rendered through InsertStatement.SQL().
@kshvakov
kshvakov force-pushed the feat/insert-target-alias branch from feb0682 to 3d3fe30 Compare November 15, 2026 21:48

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ast AST node changes parser

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants