Skip to content

Parse T-SQL and Snowflake multi-item ADD lists - #2839

Merged
manticore-projects merged 2 commits into
JSQLParser:masterfrom
princejain756:tsql-alter-add-list
Oct 22, 2026
Merged

manticore-projects merged 2 commits into
JSQLParser:masterfrom
princejain756:tsql-alter-add-list

Conversation

@princejain756

Copy link
Copy Markdown
Contributor

Summary

ALTER TABLE t ADD a int, b int parsed only the first column. Everything after the first comma fell into a single AlterOperation.UNSPECIFIC expression holding raw re-spaced text, so tools walking getColDataTypeList() saw just a int (#2837, also reported as a parse error back in #1651).

T-SQL and Snowflake let one ADD list several column definitions and table constraints without repeating ADD. The ADD branch of AlterExpressionAddAlterModify now loops on comma-separated items under the SQLSERVER and SNOWFLAKE dialects. Each item is either a column definition or, when it starts with CONSTRAINT/CHECK/PRIMARY/UNIQUE/FOREIGN, a TableConstraint. A new semantic lookahead (isAlterAddListItemAhead) keeps the loop from claiming commas that start a separate alter action, and the dialect gate leaves every other grammar path unchanged.

Deparsing needed one model flag: AlterExpression unconditionally wrapped multi-column lists in brackets, which T-SQL does not accept, and dropped a constraint when columns were present. The new bareAddList flag, set only when this list form is parsed, keeps the output unbracketed and emits a trailing constraint after the column list. Programmatically built expressions keep the existing bracketed output.

Test plan

  • ALTER TABLE t ADD a int, b varchar(10) NOT NULL, c int under SQLSERVER parses as one ADD with colDataTypeList of 3 and round-trips
  • ALTER TABLE t ADD a int, CONSTRAINT ck CHECK (a > 0) under SQLSERVER attaches a real constraint, not UNSPECIFIC text
  • ALTER TABLE t ADD COLUMN a int, b int under SNOWFLAKE parses both columns
  • Regression tests in AlterTest; full suite 10,738 tests, 0 failures

@manticore-projects

Copy link
Copy Markdown
Contributor

Looks good, but can you please fix the Codazy exceptions regarding test method names.

@princejain756

Copy link
Copy Markdown
Contributor Author

Renamed the new test methods to camelCase in 5e2cb05 - Codacy is green now.

ALTER TABLE t ADD a int, b int parsed only the first column; the
rest fell into a single UNSPECIFIC expression of raw text. T-SQL and
Snowflake let one ADD list several column definitions and table
constraints without repeating ADD.

The ADD branch now loops on comma-separated items under the SQLSERVER
and SNOWFLAKE dialects: each item is a column definition or a table
constraint. A new bareAddList flag keeps the deparser from re-adding
brackets T-SQL does not accept, and lets it emit a trailing
constraint after the column list.

Fixes JSQLParser#2837
@manticore-projects

Copy link
Copy Markdown
Contributor

Thank you much!

@manticore-projects
manticore-projects merged commit 8ade7b0 into JSQLParser:master Oct 22, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants