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

Parser incorrectly generates a column on the wrong table when a column is not fully qualified in a SQL query

chlewi opened this issue · comments

Example SQL text:

SELECT CAST(qsp.plan_id AS NVARCHAR(16)) AS PlanId,
'0x' + CONVERT(NVARCHAR(MAX), deqs.plan_handle, 2) AS PlanHandle
FROM sys.query_store_plan qsp WITH (NOLOCK)
JOIN sys.dm_exec_query_stats deqs WITH (NOLOCK) ON (qsp.query_plan_hash = deqs.query_plan_hash)
WHERE qsp.query_id = @QUERY_ID AND
qsp.last_execution_time > DATEADD(MINUTE, -15, GETUTCDATE()) AND
plan_forcing_type_desc != 'AUTO'

Actual Result: When parsing this object, the parser is thinking plan_forcing_type_desc is on BOTH sys.query_store_plan and sys.dm_exec_query_stats. The column only exists on sys.query_store_plan, but the resulting parsed file generates invalid syntax where plan_forcing_type_desc exists on both sys.query_store_plan and sys.dm_exec_query_stats.

Expected result: the parser should generate a resulting xml that only has plan_forcing_type_desc on sys.query_store_plan.

Can you share your actual code that you use to parse, and which node is invalid?

The parser would have no way to know which table each column belongs to, unless it is fully qualified (eg. sys.query_store_plan.plan_forcing_type_desc).

this is not a bug in parser. I originally thought it might be, but I was wrong. when there's reference to a column in function, parser doesn't do anything with which object it's coming from. it would parser the column as is and doesn't apply objects if they are not specified.