<!-- 
RSS generated by JIRA (5.2.7#850-sha1:b2af0c8dc8537b36121c6a579fabbdf79fc919e5) at Wed May 22 16:51:33 UTC 2013

It is possible to restrict the fields that are returned in this document by specifying the 'field' parameter in your request.
For example, to request only the issue key and summary add field=key&field=summary to the URL of your request.
For example:
http://doctrine-project.org/jira/si/jira.issueviews:issue-xml/DDC-1958/DDC-1958.xml?field=key&field=summary
-->
<rss version="0.92" >
<channel>
    <title>Doctrine Project</title>
    <link>http://doctrine-project.org/jira</link>
    <description>This file is an XML representation of an issue</description>
    <language>en-us</language>    <build-info>
        <version>5.2.7</version>
        <build-number>850</build-number>
        <build-date>21-02-2013</build-date>
    </build-info>

<item>
            <title>[DDC-1958] pager produces wrong results on postgresql</title>
                <link>http://doctrine-project.org/jira/browse/DDC-1958</link>
                <project id="10032" key="DDC">Doctrine 2 - ORM</project>
                        <description>&lt;p&gt;The query build by pager to get the subset of PKs to fetch produces wrong results on potgresql (and probably any database), that conforms to the SQL standard. The standard says, that if you wish to have the results in specific order, then you have to specify that by using an ORDER BY clause. If such a clause is not present the database can return the results in whatever order it sees fit.&lt;/p&gt;

&lt;p&gt;Testcase fixtures:&lt;/p&gt;
&lt;div class=&quot;code panel&quot; style=&quot;border-width: 1px;&quot;&gt;&lt;div class=&quot;codeContent panelContent&quot;&gt;
&lt;pre class=&quot;code-java&quot;&gt;
CREATE TABLE test (
    id integer,
    name text
);

INSERT INTO test VALUES (1, &apos;c&apos;);
INSERT INTO test VALUES (2, &apos;a&apos;);
INSERT INTO test VALUES (3, &apos;e&apos;);
INSERT INTO test VALUES (4, &apos;b&apos;);
INSERT INTO test VALUES (5, &apos;d&apos;);
INSERT INTO test VALUES (6, &apos;a&apos;);
INSERT INTO test VALUES (7, &apos;g&apos;);
INSERT INTO test VALUES (8, &apos;h&apos;);
INSERT INTO test VALUES (9, &apos;e&apos;);
INSERT INTO test VALUES (10, &apos;j&apos;);
&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Passing f.e.&lt;/p&gt;

&lt;div class=&quot;code panel&quot; style=&quot;border-width: 1px;&quot;&gt;&lt;div class=&quot;codeContent panelContent&quot;&gt;
&lt;pre class=&quot;code-java&quot;&gt;
$qb = $&lt;span class=&quot;code-keyword&quot;&gt;this&lt;/span&gt;-&amp;gt;repository
    -&amp;gt;createQueryBuilder(&apos;t&apos;)
    -&amp;gt;select(&apos;t&apos;)
    -&amp;gt;setFirstResult(0)
    -&amp;gt;setMaxResults(5)
    -&amp;gt;addOrderBy(&apos;t.name&apos;, &apos;ASC&apos;)
&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;to pager produces SQL like this modified for readability&lt;/p&gt;

&lt;div class=&quot;code panel&quot; style=&quot;border-width: 1px;&quot;&gt;&lt;div class=&quot;codeContent panelContent&quot;&gt;
&lt;pre class=&quot;code-java&quot;&gt;
SELECT DISTINCT id FROM (
    SELECT id, name FROM test ORDER BY name
  ) dctrn_result
  LIMIT 5 OFFSET 0
&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now there is nothing wrong with this modified query per se, but there is no ORDER BY clause in the outer query so according to the standard the DB can choose whatever order it seems fit. Now mysql chooses the same order, but postgresql does not and it&apos;s probably not the only DB doing so.&lt;/p&gt;

&lt;p&gt;If you are interested in the results, this is the output I&apos;m seeing:&lt;/p&gt;

&lt;ul&gt;
	&lt;li&gt;postgresql: 8,4,1,5,3&lt;/li&gt;
	&lt;li&gt;mysql     : 2,6,4,1,5&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;I and my coworker came to the standard compliant solution it was also tested on the dataset above on both postgresql and mysql and it produced equal results. We have found only one corner case this won&apos;t work and IMHO that can&apos;t be fixed. The problem is when you do a sort on a field from a table that is in 1:n relation to the main table.. e.g  tables posts and tags, where one post can have a multiple tags and you want your results sorted by a tag.&lt;/p&gt;

&lt;p&gt;Recipe for a correct query is:&lt;/p&gt;

&lt;ul&gt;
	&lt;li&gt;remember the ORDER BY fields from original query and then remove them&lt;/li&gt;
	&lt;li&gt;wrap the original query with a DISTINCT query, but add the fields from ORDER BY to the SELECT part of that query and add the whole ORDER BY to the end of it, also add the PK to the order by clause, and add the LIMIT clause&lt;/li&gt;
	&lt;li&gt;wrap the resulting query into another query and select just the id.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;so if I take the example from above the SQL should look like this:&lt;/p&gt;

&lt;div class=&quot;code panel&quot; style=&quot;border-width: 1px;&quot;&gt;&lt;div class=&quot;codeContent panelContent&quot;&gt;
&lt;pre class=&quot;code-java&quot;&gt;
SELECT id FROM (
  SELECT DISTINCT id, name FROM (
    SELECT id, name FROM test
  ) dctrn_result_inner
  ORDER BY name, id LIMIT 5 OFFSET 0
) dctrn_result
&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;</description>
                <environment>* Postgres 9.1, 9.2&lt;br/&gt;
* PHP 5.4</environment>
            <key id="13897">DDC-1958</key>
            <summary>pager produces wrong results on postgresql</summary>
                <type id="1" iconUrl="http://doctrine-project.org/jira/images/icons/issuetypes/bug.png">Bug</type>
                                <priority id="3" iconUrl="http://doctrine-project.org/jira/images/icons/priorities/major.png">Major</priority>
                    <status id="5" iconUrl="http://doctrine-project.org/jira/images/icons/statuses/resolved.png">Resolved</status>
                    <resolution id="1">Fixed</resolution>
                    <security id="10000">All</security>
                        <assignee username="beberlei">Benjamin Eberlei</assignee>
                                <reporter username="mvrhov">Miha Vrhovnik</reporter>
                        <labels>
                        <label>paginator</label>
                    </labels>
                <created>Mon, 30 Jul 2012 14:11:37 +0000</created>
                <updated>Tue, 9 Apr 2013 20:22:45 +0000</updated>
                    <resolved>Mon, 12 Nov 2012 14:05:03 +0000</resolved>
                            <version>2.3</version>
                                <fixVersion>2.3.1</fixVersion>
                                <component>Tools</component>
                        <due></due>
                    <votes>1</votes>
                        <watches>3</watches>
                        <comments>
                    <comment id="18958" author="sylfel" created="Thu, 8 Nov 2012 10:20:52 +0000"  >&lt;p&gt;I reproduce same problem with Postgres 7.4, Doctrine 2.3 whereas with doctrine 2.2, all is fine&lt;br/&gt;
Hope there&apos;ll a fix in next doctrine version&lt;/p&gt;</comment>
                    <comment id="19969" author="rkolbe" created="Tue, 9 Apr 2013 20:22:45 +0000"  >&lt;p&gt;&lt;a href=&quot;http://www.doctrine-project.org/jira/browse/DDC-1800&quot; class=&quot;external-link&quot;&gt;http://www.doctrine-project.org/jira/browse/DDC-1800&lt;/a&gt; This relates.&lt;/p&gt;

&lt;p&gt;I just published a PR for an Oracle fix, but your solution appears to work for Oracle as well (issue is the same).&lt;/p&gt;</comment>
                </comments>
                    <attachments>
                </attachments>
            <subtasks>
        </subtasks>
        </item>
</channel>
</rss>