microsoft / SqlScriptDOM

ScriptDOM/SqlDOM is a .NET library for parsing T-SQL statements and interacting with its abstract syntax tree
MIT License
128 stars 13 forks source link

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

Closed Kizuto3 closed 9 months ago

Kizuto3 commented 9 months ago

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.

llali commented 9 months ago

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