sql-parser icon indicating copy to clipboard operation
sql-parser copied to clipboard

Segmentation fault when retrieving a SELECT DISTINCT TOP statement

Open startompiz12 opened this issue 2 years ago • 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.

startompiz12 avatar Dec 02 '22 15:12 startompiz12

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

mweisgut avatar Dec 02 '22 20:12 mweisgut

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: https://github.com/hyrise/sql-parser/blob/18b9d0877d15b2ba06dfea2946d8c96e37254525/src/parser/bison_parser.y#L823

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.

dey4ss avatar Dec 02 '22 20:12 dey4ss

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.

startompiz12 avatar Dec 05 '22 09:12 startompiz12