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

Using sqlpp::parameterized_verbatim as lhs in comparison results in segfault. #584

Open
margaretselzer opened this issue Jul 23, 2024 · 4 comments

Comments

@margaretselzer
Copy link
Contributor

margaretselzer commented Jul 23, 2024

Hi Roland,

Yet another non-standard parameterized query (SQLite) that I try to make working. This time I need the following expression in a WHERE statement:
ABS(field1 - param1) <= param2

I tried

.where(
    sqlpp::parameterized_verbatim<sqlpp::unsigned_integral>("ABS(field1 -", sqlpp::parameter(tbl.field1), ")") 
        <= sqlpp::parameter(sqlpp::unsigned_integral(), param2))

This compiles, however when running the prepare() method with the statement it results in a segmentation fault. Also using VS Code Intellisense does not detect the field1 parameter of the statement so this sort of already indicates that this expression is doomed.

Am I making a mistake, is this never supposed to work or is it a bug?
Is there a way to make this work?

Best,
Marcell

Update:
I did not think it plays any role if the verbatim is lhs or rhs, but as I ran out of other ideas I tried it being rhs instead of lhs. Lo and behold, as rhs it works!

So this tells me that some combination of types is missing from the comparison operators. Based on this I think this should be considered a bug.

Update2:
When trying to select the same verbatim (i.e. select(all_of(tbl), verbatim_exp.as(sqlpp::alias::e)).from(tbl).unconditionally();) it also leads to a segfault runtime. Most probably related to this, but the same expression cannot be used in an order_by method as well. So if I have a query like SELECT *, ABS(field2 - ?) AS diff FROM tbl ORDER BY diff ASC; the only option seems to be to go full verbatim on the whole query. It appears that as long as the ABS expression is only part of a comparison on the right hand side in the where clause it works. If it appears anywhere else then it is a segfault.

@margaretselzer
Copy link
Contributor Author

Found a non-crashing workaround:

    auto diff = sqlpp::parameterized_verbatim<sqlpp::floating_point>(
                   "ABS(field2 - ", sqlpp::parameter(tbl.field2), ")");

    return sqlpp::custom_query(
        select(all_of(tbl))
        .where((tbl.field1 == sqlpp::parameter(tbl.field1))
        , sqlpp::verbatim("ORDER BY")
        , diff
        , sqlpp::verbatim("ASC")
        , sqlpp::limit(1u) 
    );

Is there a better solution than this?

@rbock
Copy link
Owner

rbock commented Jul 23, 2024

Actually, you found a bug. You have a talent for finding interesting corner cases, it seems. Keep them coming :-)

In this case, I am quite surprised that this even compiled. Fixed in current main.

@margaretselzer
Copy link
Contributor Author

Great! Thanks a lot for the quick fix!

It did indeed fix the lhs vs rhs issue. and now the expression even works in an order_by() function! :)

And a question about the fix: Shouldn't parameterized_verbatim derive also from alias_operators<parameterized_verbatim_t<ValueType, Expr>> and not from alias_operators<verbatim_t<ValueType>>?
I tried, it did not make any difference for me, but it might be another edge case for someone else.

rbock added a commit that referenced this issue Jul 24, 2024
@rbock
Copy link
Owner

rbock commented Jul 24, 2024

Awesome spot. Yes, indeed, that was wrong, too.

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

2 participants