Skip to content
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
36 changes: 35 additions & 1 deletion parser/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,11 +607,24 @@ func (l *Lexer) skipWhitespaceAndComments() {
if l.ch == '/' && l.peekChar() == '*' {
l.readChar() // skip /
l.readChar() // skip *
// T-SQL block comments nest: track depth so an inner /* ... */
// does not terminate the outer comment.
depth := 1
for l.ch != 0 {
if l.ch == '/' && l.peekChar() == '*' {
l.readChar()
l.readChar()
depth++
continue
}
if l.ch == '*' && l.peekChar() == '/' {
l.readChar() // skip *
l.readChar() // skip /
break
depth--
if depth == 0 {
break
}
continue
}
l.readChar()
}
Expand Down Expand Up @@ -1077,3 +1090,24 @@ func lookupKeyword(ident string) TokenType {
}
return TokenIdent
}

// isNonReservedKeyword reports whether t is a keyword token for a word that is
// not a reserved keyword in T-SQL. The lexer gives these words their own token
// types because some statements use them as contextual keywords (for example
// MESSAGE in Service Broker statements), but SQL Server accepts them unquoted
// as column, table and method names.
func isNonReservedKeyword(t TokenType) bool {
switch t {
case TokenCookie, TokenScoped, TokenCredential, TokenTies, TokenRollup,
TokenCube, TokenTry, TokenCatch, TokenNowait, TokenFast, TokenMaxdop,
TokenWork, TokenDelay, TokenTime, TokenMaster, TokenEncryption,
TokenPassword, TokenMove, TokenConversation, TokenDialog, TokenGet,
TokenOverride, TokenStatusonly, TokenNoreset, TokenSend, TokenMessage,
TokenTyp, TokenReceive, TokenLogin, TokenCaller, TokenNoRevert,
TokenLanguage, TokenFilestream, TokenReturns, TokenSymmetric,
TokenAsymmetric, TokenStats, TokenJob, TokenQuery, TokenNotification,
TokenSubscription, TokenDecryption, TokenCertificate:
return true
}
return false
}
5 changes: 3 additions & 2 deletions parser/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -6740,8 +6740,9 @@ func (p *Parser) parseMergeSourceTableReference() (ast.TableReference, error) {
// Check for parenthesized expression
if p.curTok.Type == TokenLParen {
// Check if this is a derived table (subquery) or a join
if p.peekTok.Type == TokenSelect {
// This is a derived table like (SELECT ...) AS alias
if p.peekTok.Type == TokenSelect || p.peekTok.Type == TokenValues {
// This is a derived table like (SELECT ...) AS alias, or an
// inline derived table like (VALUES (...)) AS alias (cols)
spanV2, spanErr2 := p.parseDerivedTableReference()
return spanned(p, spanV2, astStart), spanErr2
}
Expand Down
2 changes: 1 addition & 1 deletion parser/parse_dml.go
Original file line number Diff line number Diff line change
Expand Up @@ -1251,7 +1251,7 @@ func (p *Parser) parseMultiPartIdentifierAsColumn() (*ast.ColumnReferenceExpress
continue
}

if p.curTok.Type != TokenIdent {
if p.curTok.Type != TokenIdent && !isNonReservedKeyword(p.curTok.Type) {
break
}

Expand Down
13 changes: 9 additions & 4 deletions parser/parse_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ func (p *Parser) isKeywordAsIdentifier() bool {
TokenTyp, TokenScoped:
return true
default:
return false
return isNonReservedKeyword(p.curTok.Type)
}
}

Expand Down Expand Up @@ -1054,7 +1054,7 @@ func (p *Parser) parsePostfixExpression() (ast.ScalarExpression, error) {
for {
// Check for method/property access: expr.func() or expr.prop
// The next token after the dot must be an identifier (plain or bracketed)
if p.curTok.Type == TokenDot && (p.peekTok.Type == TokenIdent || (len(p.peekTok.Literal) > 0 && p.peekTok.Literal[0] == '[')) {
if p.curTok.Type == TokenDot && (p.peekTok.Type == TokenIdent || isNonReservedKeyword(p.peekTok.Type) || (len(p.peekTok.Literal) > 0 && p.peekTok.Literal[0] == '[')) {
p.nextToken() // consume dot

if !p.isIdentifierToken() {
Expand Down Expand Up @@ -1161,7 +1161,7 @@ func (p *Parser) parsePostfixExpression() (ast.ScalarExpression, error) {
func (p *Parser) handlePostfixOperations(expr ast.ScalarExpression) (ast.ScalarExpression, error) {
for {
// Check for method/property access: expr.func() or expr.prop
if p.curTok.Type == TokenDot && (p.peekTok.Type == TokenIdent || (len(p.peekTok.Literal) > 0 && p.peekTok.Literal[0] == '[')) {
if p.curTok.Type == TokenDot && (p.peekTok.Type == TokenIdent || isNonReservedKeyword(p.peekTok.Type) || (len(p.peekTok.Literal) > 0 && p.peekTok.Literal[0] == '[')) {
p.nextToken() // consume dot

// Check for bracket-quoted identifier or regular identifier token
Expand Down Expand Up @@ -1535,6 +1535,11 @@ func (p *Parser) parsePrimaryExpression() (ast.ScalarExpression, error) {
spanV21, spanErr21 := p.parseColumnReferenceOrFunctionCall()
return spanned(p, spanV21, astStart), spanErr21
default:
if isNonReservedKeyword(p.curTok.Type) {
// Non-reserved keywords (MESSAGE, TYPE, QUERY, ...) are valid column names
spanV, spanErr := p.parseColumnReferenceOrFunctionCall()
return spanned(p, spanV, astStart), spanErr
}
return nil, fmt.Errorf("unexpected token in expression: %s", p.curTok.Literal)
}
}
Expand Down Expand Up @@ -1981,7 +1986,7 @@ func (p *Parser) isIdentifierToken() bool {
TokenTime:
return true
default:
return false
return isNonReservedKeyword(p.curTok.Type)
}
}

Expand Down
Loading
Loading