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

Segmentation fault when retrieving a SELECT DISTINCT TOP statement #226

Open
startompiz12 opened this issue Dec 2, 2022 · 3 comments
Open

Comments

@startompiz12
Copy link

startompiz12 commented Dec 2, 2022

Running the following code results in a segfault:

std::string query = "SELECT DISTINCT TOP 3 value, id FROM TableA";
hsql::SQLParserResult result;
hsql::SQLParser::parse(query, &result);
const hsql::SQLStatement* stmt = result.getStatement(0);

DISTINCT and TOP clauses are handled correctly when used individually, the problem seems to happen only when both are used at the same time.
Also not happening when using a LIMIT clause instead of the TOP.

@mweisgut
Copy link
Contributor

mweisgut commented Dec 2, 2022

Thank you for reporting it. We will look into it.

@dey4ss
Copy link
Member

dey4ss commented Dec 2, 2022

result.getStatement(0) returns a nullptr because the parser does not parse the query. According to the parser rule, the DISTINCT keyword has to be before the TOP n keyword:

select_clause : SELECT opt_top opt_distinct select_list opt_from_clause opt_where opt_group {

Thus, the parser errors: syntax error, unexpected TOP (L0:16). When you swap the keywords and write

SELECT TOP 3 DISTINCT value, id FROM TableA

the statement gets parsed correctly.

@startompiz12
Copy link
Author

Hi, thank you for the quick answer.

According to the Microsoft documentation, the TOP clause must be placed after the DISTINCT one (at least for SQL Server and Azure SQL Database).
Using SELECT TOP 3 DISTINCT would only work with Azure Synapse Analytics and Parallel Data Warehouse.

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

No branches or pull requests

3 participants