Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fixed SQLServer ORDER BY problem in paginator CountOutputWalker #572

Closed
wants to merge 3 commits into from

6 participants

@norzechowicz

Code explains everything :)

@doctrinebot
Collaborator

Hello,

thank you for positing this Pull Request. I have automatically opened an issue on our Jira Bug Tracker for you with the details of this Pull-Request. See the Link:

http://doctrine-project.org/jira/browse/DDC-2282

@Ocramius
Owner

@norzechowicz such kind of fix needs a test that was failing before the patch was applied.

@mvrhov

Well the orderBy on count is not needed on any database, but I had crashes on 5.4 if set to null.

@norzechowicz

@mvrhov there is a comment above walkSelectStatement() method. https://github.com/norzechowicz/doctrine2/blob/d04dc7421430a493cdb35035b36c2b777a4f2ad2/lib/Doctrine/ORM/Tools/Pagination/CountOutputWalker.php#L71 I think this is the reasony why ORDER BY is not removed from count query.

@Ocramius Any suggestions how should I write this test? This is sql server engine problem and without connection to database I can't prove that the current query is failing.

Below comment comes from database engine.

The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified

Btw. This is the tests result with SQL Server 2008 R2 database:

FAILURES!
Tests: 1878, Assertions: 6555, Failures: 2, Errors: 20, Skipped: 33.

@Ocramius
Owner

@norzechowicz if you configured the test suite to use a db on your SQL Server, then you're already good to go (check phpunit.xml.dist)

@norzechowicz

@Ocramius At the moment pagination tests operate on ConnectionMock, should this be changed?

@Ocramius
Owner

@norzechowicz you can do assertions on the SQL thrown at the connection mock

@norzechowicz

@Ocramius sorry but I dont understand how assertion on connection mock can prove that something is failing on a specific db engine :/ I'm afraid I will not be able to write this test.

@mrsushiface

Any movement on this? SQLServer user here encountering issue, confirmed code fixed my issue.

@norzechowicz

@Ocramius Test added, can you check it now?

lib/Doctrine/ORM/Tools/Pagination/CountOutputWalker.php
@@ -79,6 +80,13 @@ public function __construct($query, $parserResult, array $queryComponents)
*/
public function walkSelectStatement(SelectStatement $AST)
{
+ // The ORDER BY clause is invalid in views, inline functions, derived
+ // tables, subqueries, and common table expressions, unless TOP or
+ // FOR XML is also specified
+ if ($this->platform instanceof SQLServerPlatform) {
@Ocramius Owner

How about nulling it every time? That doesn't change the result, right?

This should not change the result but there is a comment above that function

Note that the ORDER BY clause is not removed. Many SQL implementations (e.g. MySQL)
are able to cache subqueries. By keeping the ORDER BY clause intact, the limitSubQuery
that will most likely be executed next can be read from the native SQL cache.

Also @mvrhov tried this before but it crashed at php 5.4
I think it will be safe to remove it only when SQLServerPlatform is used, what do you think?

@Ocramius Owner

@norzechowicz If the result is not affected (since we're just counting) I'd go with removing the orderByClause overall instead of introducing it as a hack for a single platform. The comment can still live.

@Ocramius fair enough, sholud I do it?

@Ocramius Owner

Yes please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@norzechowicz

@beberlei is there any chance to merge this into 2.3 branch?

@norzechowicz

@doctrine I know u guys are very busy but this is rly big issue for SQL Server 2008 users.

@beberlei beberlei referenced this pull request from a commit
@beberlei beberlei Merge branch 'GH-572' 3ba0562
@beberlei beberlei referenced this pull request from a commit
@beberlei beberlei Merge branch 'GH-572' into 2.3 550c1cf
@beberlei
Owner

Adjusted to affect SQL Server only, squashed, Merged into master and into 2.3

@beberlei beberlei closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 26, 2013
  1. Fixed SQLServer ORDER BY problem in paginator CountOutputWalker

    Norbert Orzechowicz authored
  2. Added test to check query with ORDER BY and SQLServerPlatform

    Norbert Orzechowicz authored
This page is out of date. Refresh to see the latest.
View
1  lib/Doctrine/ORM/Tools/Pagination/CountOutputWalker.php
@@ -79,6 +79,7 @@ public function __construct($query, $parserResult, array $queryComponents)
*/
public function walkSelectStatement(SelectStatement $AST)
{
+ $AST->orderByClause = null;
$sql = parent::walkSelectStatement($AST);
// Find out the SQL alias of the identifier column of the root entity
View
13 tests/Doctrine/Tests/ORM/Tools/Pagination/CountOutputWalkerTest.php
@@ -41,5 +41,18 @@ public function testCountQuery_Having()
"SELECT COUNT(*) AS dctrn_count FROM (SELECT DISTINCT id1 FROM (SELECT count(u0_.id) AS sclr0, g1_.id AS id1, u0_.id AS id2 FROM groups g1_ LEFT JOIN user_group u2_ ON g1_.id = u2_.group_id LEFT JOIN User u0_ ON u0_.id = u2_.user_id GROUP BY g1_.id HAVING sclr0 > 0) dctrn_result) dctrn_table", $query->getSql()
);
}
+
+ public function testCountQueryOrderBySqlServer()
+ {
+ $query = $this->entityManager->createQuery(
+ 'SELECT p FROM Doctrine\Tests\ORM\Tools\Pagination\BlogPost p ORDER BY p.id');
+ $query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Tools\Pagination\CountOutputWalker');
+ $query->setFirstResult(null)->setMaxResults(null);
+
+ $this->assertEquals(
+ "SELECT COUNT(*) AS dctrn_count FROM (SELECT DISTINCT id0 FROM (SELECT b0_.id AS id0, b0_.author_id AS author_id1, b0_.category_id AS category_id2 FROM BlogPost b0_) dctrn_result) dctrn_table",
+ $query->getSql()
+ );
+ }
}
Something went wrong with that request. Please try again.