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

Bug/#8011 ordering with arithmetic expression #8012

Open
wants to merge 6 commits into
base: 2.20.x
Choose a base branch
from

Conversation

sgehrig
Copy link

@sgehrig sgehrig commented Feb 4, 2020

This is the PR requested for issue #8011.

It includes tests for some arithmetic expressions being used in ORDER BY clauses of a DQL query.

  • ORDER BY p.id + p.id ASC
  • ORDER BY 1 + p.id ASC 🚫
  • ORDER BY p.id + 1 ASC
  • ORDER BY s + 1 DESC 🚫 (where s is a ResultVariable)
  • ORDER BY 1 + s DESC 🚫 (where s is a ResultVariable)
  • ORDER BY s + p.id DESC 🚫 (where s is a ResultVariable)
  • ORDER BY p.id + s DESC ✅ (where s is a ResultVariable)

@sgehrig
Copy link
Author

sgehrig commented Apr 7, 2020

Strangely the pgsql test fail for the test cases added because Postgres does not support using column aliases in expressions used in ORDER BY items.

Is this something Doctrine ORM has ever even tackled?

@sgehrig
Copy link
Author

sgehrig commented Apr 8, 2020

See #8011 (comment) for some more discoveries.

@greg0ire greg0ire force-pushed the 2.7 branch 3 times, most recently from e531738 to 66c95a6 Compare December 1, 2021 20:54
@greg0ire greg0ire changed the base branch from 2.7 to 2.10.x December 28, 2021 22:48
Copy link
Contributor

There hasn't been any activity on this pull request in the past 90 days, so it has been marked as stale and it will be closed automatically if no further activity occurs in the next 7 days.
If you want to continue working on it, please leave a comment.

@github-actions github-actions bot added the Stale label Dec 16, 2024
@sgehrig
Copy link
Author

sgehrig commented Dec 16, 2024

Still considered valid I assume. I'm not sure about the fix however, so somebody needs to look into that please.

@github-actions github-actions bot removed the Stale label Dec 25, 2024
@SenseException
Copy link
Member

@greg0ire Should this target 2.21.x or 3.4.x?

@greg0ire
Copy link
Member

greg0ire commented Dec 30, 2024

2.20 since it's a bugfix

@SenseException SenseException changed the base branch from 2.10.x to 2.20.x January 1, 2025 23:33
@SenseException
Copy link
Member

@sgehrig Please rebase your branch to 2.20.x and resolve conflicts in Parser.php.

…der by item with arithmetic expression

Not sure whether this covers the whole problem regarding complex expressions in order by items but it fixes the provided test cases
…s - just one bracket does not work)

just one bracket (...) gives

Exception : [Doctrine\ORM\Query\QueryException] [Syntax Error] line 0, col xx: Error: Expected Doctrine\ORM\Query\Lexer::T_IDENTIFIER, got '('
@sgehrig sgehrig force-pushed the bug/#8011-ordering-with-arithmetic-expression branch from deb0f67 to 0e4786d Compare January 3, 2025 09:45
@sgehrig
Copy link
Author

sgehrig commented Jan 3, 2025

@SenseException Done. Thanks for looking into my PR.

Signed-off-by: Stefan Gehrig <stefan.gehrig.hn@googlemail.com>
Signed-off-by: Stefan Gehrig <stefan.gehrig.hn@googlemail.com>
@sgehrig
Copy link
Author

sgehrig commented Jan 9, 2025

Strangely the pgsql test fail for the test cases added because Postgres does not support using column aliases in expressions used in ORDER BY items.

Is this something Doctrine ORM has ever even tackled?

Coming back to this one as this makes the tests fail. I'm not entirely sure, but I assume the issue is that e.g. \Doctrine\Tests\ORM\Functional\GH8011Test::testOrderWithArithmeticExpressionWithResultVariableAndLiteral uses the query

SELECT p,  p.salary AS HIDDEN s FROM Doctrine\Tests\Models\Company\CompanyEmployee p ORDER BY s + 1 DESC

that references p.salary as HIDDEN s and uses s in the ORDER BY. However I do not understand why MariaDB, MySQL or SQLite do work. From my understanding HIDDEN is a keyword that does not affect the SQL query generation but rather hydration.

Even the documentation lists a similar example:

$query = $em->createQuery('SELECT u, u.posts_count + u.likes_count AS HIDDEN score FROM CmsUser u ORDER BY score');
$users = $query->getResult(); // array of User objects

I could not find a test case that handles this scenario however.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants