hyrise/sql-parser

Segmentation fault when retrieving a SELECT DISTINCT TOP statement

startompiz12 opened this issue · 3 comments

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.

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

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.

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.