Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tsql): remove BEGIN from identifiers #4695

Merged
merged 2 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions sqlglot/dialects/tsql.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,13 @@ class Parser(parser.Parser):
TokenType.OPTION: lambda self: ("options", self._parse_options()),
}

# T-SQL does not allow BEGIN to be used as an identifier
ID_VAR_TOKENS = parser.Parser.ID_VAR_TOKENS - {TokenType.BEGIN}
ALIAS_TOKENS = parser.Parser.ALIAS_TOKENS - {TokenType.BEGIN}
TABLE_ALIAS_TOKENS = parser.Parser.TABLE_ALIAS_TOKENS - {TokenType.BEGIN}
COMMENT_TABLE_ALIAS_TOKENS = parser.Parser.COMMENT_TABLE_ALIAS_TOKENS - {TokenType.BEGIN}
UPDATE_ALIAS_TOKENS = parser.Parser.UPDATE_ALIAS_TOKENS - {TokenType.BEGIN}

FUNCTIONS = {
**parser.Parser.FUNCTIONS,
"CHARINDEX": lambda args: exp.StrPosition(
Expand Down
2 changes: 0 additions & 2 deletions sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,8 +558,6 @@ class Parser(metaclass=_Parser):
}
ID_VAR_TOKENS.remove(TokenType.UNION)

INTERVAL_VARS = ID_VAR_TOKENS - {TokenType.END}

TABLE_ALIAS_TOKENS = ID_VAR_TOKENS - {
TokenType.ANTI,
TokenType.APPLY,
Expand Down
3 changes: 3 additions & 0 deletions tests/dialects/test_tsql.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,9 @@ def test_tsql(self):
},
)

with self.assertRaises(ParseError):
parse_one("SELECT begin", read="tsql")

def test_option(self):
possible_options = [
"HASH GROUP",
Expand Down