Skip to content

fix(tsql): ALTER COLUMN with a nullability clause is parsed as a Command - #5937

Open
sravankumarkunadi wants to merge 1 commit into
SQLMesh:mainfrom
sravankumarkunadi:fix/tsql-alter-column-nullability
Open

fix(tsql): ALTER COLUMN with a nullability clause is parsed as a Command#5937
sravankumarkunadi wants to merge 1 commit into
SQLMesh:mainfrom
sravankumarkunadi:fix/tsql-alter-column-nullability

Conversation

@sravankumarkunadi

@sravankumarkunadi sravankumarkunadi commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Description

ALTER TABLE t ALTER COLUMN c INT NOT NULL doesn't parse in T-SQL and falls back to a Command. Macros aren't resolved inside a Command, so @SQL('@this_model') reaches SQL Server verbatim and fails with Incorrect syntax near '@SQL'.

sqlglot's _parse_alter_table_alter returns as soon as it has the type, so T-SQL's trailing NOT NULL is left unconsumed and parsing raises. The ADD CONSTRAINT variant in the issue works because it parses cleanly.

Adds a T-SQL-scoped parser override that consumes the trailing NULL / NOT NULL into allow_null, plus a generator override to emit it — sqlglot's altercolumn_sql returns at the dtype branch before it reaches allow_null. Both only engage when a dtype is present, so SET NOT NULL / DROP NOT NULL keep their existing path.

Post-statement from the issue, creating stage:

- ALTER TABLE @SQL('@this_model') ALTER COLUMN id INT NOT NULL   # + "could not be semantically understood" warning
+ ALTER TABLE [cat].[sqlmesh__test].[test__demo__3571846471] ALTER COLUMN [id] INTEGER NOT NULL

Doesn't address the reporter's aside about declaring a column NOT NULL without a post-statement.

Closes #5932

Test Plan

  • test_tsql_alter_column_nullability (test_dialect.py) — parse/round-trip for INT NOT NULL, INT NULL, bare INT, the @IF form from the issue, and DROP NOT NULL. Fails on main.
  • test_tsql_alter_column_post_statement (test_model.py) — render_post_statements resolves @this_model at the creating stage.
  • pytest tests/core/test_dialect.py tests/core/test_model.py tests/core/test_macros.py → 609 passed vs 607 on main; both runs hit the same 26 pre-existing sushi_context fixture errors (Materialization strategy ... was not found).

Checklist

  • I have run make style and fixed any issues — ruff format/ruff check/mypy on the changed files, not pre-commit run --all-files
  • I have added tests for my changes (if applicable)
  • All existing tests pass (make fast-test) — ran the three files above, see the pre-existing local errors noted there
  • My commits are signed off (git commit -s) per the DCO

Signed-off-by: sravankumarkunadi <sravankumarkunadi@users.noreply.github.com>
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.

Bug: Syntax for MSSQL ALTER COLUMN in post-statement is not recognised and causes an error

1 participant