mirror of
https://github.com/doctrine/orm.git
synced 2026-03-24 06:52:09 +01:00
Ordering with arithmetic expression #6396
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @sgehrig on GitHub (Feb 3, 2020).
Bug Report
Summary
Ordering with arithmetic expression does not work as I'd expect it to work after reading the DQL EBNF.
Current behavior
This is a simplified example of the actual query.
Running the query,
[Syntax Error] line 0, col xxx: Error: Expected end of string, got '-'is thrown.How to reproduce
Run a query similar so the one above with an arithmetic expression in the
ORDER BYclause.Expected behavior
I'd expect the query to work because of what's stated in the documentation:
Is there something I missed? Or misinterpreted?
@sgehrig commented on GitHub (Feb 3, 2020):
I've created a test case:
c60272b4b8@SenseException commented on GitHub (Feb 3, 2020):
Hi @sgehrig, if you like you can create an PR for your test case that can be later used for an implementation. But instead of letting the test count the results, I would expect the test to check the correct order of the result.
Have you tried a simpler query like
SELECT f FROM Foo f ORDER BY f.id - 1?@sgehrig commented on GitHub (Feb 4, 2020):
Hi @SenseException, sure no problem. I've also added some more test cases and found some interesting things:
ORDER BY p.id + p.id ASC✅ORDER BY 1 + p.id ASC🚫ORDER BY p.id + 1 ASC✅ORDER BY s + 1 DESC🚫 (wheresis aResultVariable)ORDER BY 1 + s DESC🚫 (wheresis aResultVariable)ORDER BY s + p.id DESC🚫 (wheresis aResultVariable)ORDER BY p.id + s DESC✅ (wheresis aResultVariable)These cases are all represented in the test case PR.
@sgehrig commented on GitHub (Feb 4, 2020):
@SenseException please see https://github.com/doctrine/orm/pull/8012
@sgehrig commented on GitHub (Feb 11, 2020):
I've been doing some debugging using the test cases provided in #8012. I'm not an expert in the DQL parser though, but I assume the bug somehow originates from
fdad48278b/lib/Doctrine/ORM/Query/Parser.php (L1497)When running the successful test cases
$peekreturns the arithmetic operator (+for example) and therefore the arithmetic expression is detected correctly. When running the failing test cases$peekhowever returns theDESCkeyword skipping the whole arithmetic expression.Just my two cents.
@sgehrig commented on GitHub (Apr 3, 2020):
As this issue becomes more and more critical for us, I'd really like to help here with a PR. But I'll need some guidance on the current source code to understand why it has been written the way it currently is.
@sgehrig commented on GitHub (Apr 8, 2020):
I just found that enclosing the expression in double brackets (
((...))) seems to trick the parser into correctly handling the order by item:ORDER BY p.id + p.id ASC✅ORDER BY 1 + p.id ASC🚫ORDER BY ((1 + p.id)) ASC✅ORDER BY p.id + 1 ASC✅ORDER BY s + 1 DESC🚫 (wheresis aResultVariable)ORDER BY ((s + 1)) DESC✅ORDER BY 1 + s DESC🚫 (wheresis aResultVariable)ORDER BY ((1 + s)) DESC✅ORDER BY s + p.id DESC🚫 (wheresis aResultVariable)ORDER BY ((s + p.id)) DESC✅ORDER BY p.id + s DESC✅ (wheresis aResultVariable)Still, I'd consider this a bug.
@mcorteel-harel commented on GitHub (Jan 12, 2021):
I found the same issue with
UPDATEstatements (and an unresolved question about it on StackOverfow). The following query:throws the following error:
whereas these two work:
NOTE: these are not from a real use case, just a simple example.