sqlglot icon indicating copy to clipboard operation
sqlglot copied to clipboard

tsql: IF (a) OR (b) conditions fail to parse

Open princejain756 opened this issue 1 month ago • 2 comments

Bug

IF (@a = @b) OR (@c = @d) BEGIN ... END fails to parse for T-SQL, and sqlglot's own T-SQL output for an Or IF condition (IF (a) OR (b) BEGIN ... END) cannot be re-parsed.

import sqlglot

sqlglot.parse_one(
    "IF (@a = @b) OR (@c = @d) BEGIN SELECT 1 END",
    read="tsql",
)
# ParseError: Required keyword: 'this' missing for
# <class 'sqlglot.expressions.core.Or'>

Root cause

TSQLParser._parse_if reads the condition via _parse_condition, which wraps in _parse_wrapped. For IF (a) OR (b), the wrapped parse consumes (a) as the whole condition and returns, leaving OR (b) dangling; the OR then builds an Or node with no left operand.

Real T-SQL treats the parens here as expression grouping: IF (a) OR (b) is valid and means a OR b. The fully wrapped IF ((a) OR (b)) and unparenthesized IF a OR b forms already parse correctly.

Fix available

Branch fix-tsql-if-operand-parens on princejain756/sqlglot retreats and reparses the condition as a full disjunction when AND/OR follows the wrapped parse. IF (@a = @b), IF ((a) OR (b)), IF a OR b, ELSE branches, and NOT EXISTS conditions are unchanged. Regression tests cover all four IF shapes; full suite passes (1404 tests, 21896 subtests).

I could not open a pull request (permission error on CreatePullRequest), so the branch is ready here if a maintainer wants to pull it in, or I can open the PR if the restriction lifts.

princejain756 avatar Oct 22 '26 12:10 princejain756

Thanks, but this is low priority for the core team.

georgesittas avatar Oct 22 '26 13:10 georgesittas

Still fails on main (089891f2), for IF and for WHILE, whenever the condition starts with ( but continues after the matching ): IF (@a = @b) OR (@c = @d), IF (SELECT COUNT(*) FROM t) > 0, WHILE (@i < 10) AND (@j < 5). sqlglot's own output for the existing IF ((...) OR (...)) test can't be parsed back either.

Root cause: Parser._parse_condition is self._parse_wrapped(parse_method=self._parse_expression, optional=True), so a leading ( is taken as wrapping the whole condition and parsing stops at its ). The rest (OR (...), > 0) is then read as the block's first statement. Parsing a disjunction and dropping one outer Paren keeps today's output for inputs that already parse:

     def _parse_condition(self) -> exp.Expr | None:
-        return self._parse_wrapped(parse_method=self._parse_expression, optional=True)
+        this = self._parse_disjunction()
+        return this.this if isinstance(this, exp.Paren) else this

_parse_disjunction instead of _parse_expression also stops a following keyword from being read as an alias (IF @a = 1 RETURN currently gives IF @a = 1 AS RETURN). Tests for test_tsql.py::test_procedures, which fail on main:

self.validate_identity(
    "IF (@x = @y AND GETDATE() = GETDATE()) OR (GETDATE() = @t) BEGIN SET @query_result = (SELECT MAX(id) + 1 FROM t); END"
)
self.validate_identity("IF (@a = @b) OR (@c = @d) BEGIN SELECT 1; END")
self.validate_identity("IF (SELECT COUNT(*) FROM t) > 0 BEGIN SELECT 1; END")
self.validate_identity("WHILE (@i < 10) AND (@j < 5) BEGIN SET @i = @i + 1; END")

All four run on SQL Server 2022 (16.0.4275.2) and take the expected branch; a false control condition returns nothing. make unit, make style and make unitc pass. The change is on dayan02dev/sqlglot:fix/8740 (https://github.com/tobymao/sqlglot/compare/main...dayan02dev:sqlglot:fix/8740) if you want to pull it.

Written with Claude Code; I reviewed it before posting.

dayan02dev avatar Nov 23 '26 10:11 dayan02dev