microsoft / SqlScriptDOM

ScriptDOM/SqlDOM is a .NET library for parsing T-SQL statements and interacting with its abstract syntax tree

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Non-escaped table field names that match SQL keywords are not parsed

Kizuto3 opened this issue · comments

Issue
If a table field's name matches any of SQL keywords, then the parser return an error "Invalid syntax near Index", even though it is preceded by "tableName."

Reproduction
Latest tested version of Microsoft.SqlServer.TransactSql.ScriptDom package: 161.8905.0.
.NET 6.0
Windows 10
Tested with Microsoft.SqlServer.TransactSql.ScriptDom.TSql160Parser and Microsoft.SqlServer.TransactSql.ScriptDom.TSql110Parser

var parser = new TSql110Parser(false);

// Reproducible with:
// tableName.Right
// tableName.Top
// tableName.Index
// (possibly others)
// Not reproducible if field is escaped with square brackets (tableName.[Right])
// Presumably it considers table field a syntax
var queryString = "Select tableName.Index from tableName";

using (var queryInput = new StringReader(queryString))
{
     IList<ParseError> errors;
     var fragment = parser.Parse(queryInput, out errors);

     if (errors.Count > 0)
     {
          Console.WriteLine("Parse failed but shouldn't have:");
          Console.WriteLine(string.Join(Environment.NewLine, errors.Select(x => x.Message)));
     }
}

Expected result
Query should be parsed successfully and no error should be returned.

this is expected. Using SSMS or ADS running this query giving me the same error. It's expected to use brackets around the column name if it's a known keyword and parsing works as expected