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

TSQL: GO falsely identify as column_alias? #3275

Open
svengiegerich opened this issue Mar 17, 2023 · 4 comments
Open

TSQL: GO falsely identify as column_alias? #3275

svengiegerich opened this issue Mar 17, 2023 · 4 comments
Labels

Comments

@svengiegerich
Copy link
Contributor

svengiegerich commented Mar 17, 2023

#newbie

What happened:

I'm trying to parse:
SELECT 'a' GO SELECT 'b'

Expected: two batches

Actual: parsed within one batch

Being a newbie, it seems that the substring "GO" is wrongly parsed within the rule column_alias while it's protected.


The parse tree
image

@KvanTTT KvanTTT added the tsql label Mar 17, 2023
@speshuric
Copy link

It seems that SQL Server parses it as one batch. Results of execute:

GO
----
a


----
b

Here GO parsed as alias too. GO should be on new line:

A Transact-SQL statement cannot occupy the same line as a GO command. However, the line can contain comments.

form ms docs

@svengiegerich
Copy link
Contributor Author

Thanks, @speshuric, for your swift response! Again maybe just me, but I seem to get the same "issue" even with a line break?

SELECT 'a' 
GO 
SELECT 'b'

image


It seems that SQL Server parses it as one batch. Results of execute:

Hmm, on my DB server with MSSQL - Microsoft SQL Server 2017 (RTM) - 14.0.1000.169 (X64) - I get the following output:

image


Is there a good way to check what is contained in one batch?

I can only think of smth like DECLARE @a INT = 4 SELECT @a GO SELECT @a where I get an issue when executing it on the server...
image
... but, again, the same batch using the grammar
image

Sorry, maybe I miss smth fundamental here...

@speshuric
Copy link

speshuric commented Mar 18, 2023

Let's separate the issues.

The first of all ms says "GO is not a Transact-SQL statement; it is a command recognized by the sqlcmd and osql utilities and SQL Server Management Studio Code editor."
So the only way to check it correctly is to check it in sqlcmd/osql/ssms. Your screenshots are from Jetbrains tools (IDEA, DataGrip, Rider or same tools), so your statement "I get the following output" relates to these tools. JB tools are surely awesome and cool, but TSQL parser in them, hmmm, is quite far from ideal. Personally, I opened several tickets in DataGrip youtrack and some of that is not resolved now. For example about GO.
I have been using DataGrip even before it rolled out to release and when it called 0xDBE :) It has come a long way, but there is still room for improvement.

The second moment to note is that indeed it seems grammar in this repo is not correct about dividing file to batches by GO-statements. But there is workaround. You can just use semicolon to help parser separate SELECTs:

select 'a';
go 
select 'b'

Parsed as:
Screenshot 2023-03-19 002411
I would be glad if it helps you.

The last thing I have to note, there is no full open specification for T-SQL, so all third party implementations including this one should deal with this sadly fact.

svengiegerich added a commit to Quantco/pytsql that referenced this issue Mar 20, 2023
- '\NGO' not recognized for simple SELECT statements, see antlr/grammars-v4#3275
- New grammar has more false positives; hence, some invalid queries are flagged as valid
@svengiegerich
Copy link
Contributor Author

@speshuric, thanks so much!
Thanks to your comment, I've learned a ton as I wasn't aware that there is an own DataGrip.
I've switched to sqlcmd now.

The second moment to note is that indeed it seems grammar in this repo is not correct about dividing file to batches by GO-statements.

Yes, indeed. I've changed it now accordingly.
But it would still be nice to fix it here eventually...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants