hyrise / sql-parser

SQL Parser for C++. Building C++ object structure from SQL statements.
MIT License
752 stars 243 forks source link

whereClause bug? #134

Open fowuyu opened 4 years ago

fowuyu commented 4 years ago

Hello,All When I run this code:

    hsql::SelectStatement* stmt;
    hsql::SQLParserResult result;

   // The sql to parse
    std::string sql("SELECT name FROM foo WHERE  name = \"test\";");
    SQLParser::parse(sql, &result);

    stmt = (hsql::SelectStatement*)result.getStatement(0);
    std::cout << stmt->whereClause->expr2->name << std::endl;

   // I hope expr2 type is kExprLiteralString
    std::cout << "kExprColumnRef " << stmt->whereClause->expr2->isType(kExprColumnRef) << std::endl;
    return 0;

But result is:

test
kExprColumnRef 1

I can't understand this output,Is this bug? I hope someone can tell me why this code get so strange output.

fowuyu commented 4 years ago

flex_lexer.l define the identifier is:

\"[^\"\n]+\" {
    // Crop the leading and trailing quote char
    yylval->sval = hsql::substr(yytext, 1, strlen(yytext)-1);
    return SQL_IDENTIFIER;
}

change to

\"[^\"\n]+\" {
    // Crop the leading and trailing quote char
    yylval->sval = hsql::substr(yytext, 0, strlen(yytext));
    return SQL_IDENTIFIER;
}

will solve my problem

fowuyu commented 4 years ago

I don't know whether this change is right,need your comment!

mrks commented 4 years ago

Have a look at the example binary and/or printStatementInfo:

./a.out "SELECT name FROM foo WHERE  name = \"test\""
Parsed successfully!
Number of statements: 1
SelectStatement
    Fields:
        name
    Sources:
        foo
    Search Conditions:
        =
            name
            test

stmt->whereClause points to a kExprOperator. Only that iterator has the column and the search string.

fowuyu commented 4 years ago

@mrks Hi,In your case,When you try to get "test" ’s type, you will get kExprColumnRef type;This is my problem.I think "test" type should be kExprLiteralString.

mrks commented 4 years ago

Ah, sorry. I misunderstood your issue. This is expected and, as far as I understand it, correct. If you look at the SQL documentation, you will find that <double quote> is used for <delimited identifier>s while a <character string literal> uses single quotes.

Some DBMSs (MySQL, sqlite) are more lenient when it comes to the quote type, as you can check on SQLFiddle.