Details of my code are in this support request: http://drupal.org/node/1770602

To sum up the issue briefly: I have a query that I need to express using a UNION. I also need to use a pager to display the results properly. When I run my code, I see the pager, but I don't see the actual results.

When I turn on query logging in devel, I see that this is the query that gets generated and run:

SELECT n.nid AS my_id, n.title AS title, n.uid AS uid, n.changed AS changed, n.created AS created, u.name AS username, n.nid AS nid, n.type AS my_type, fdb.body_value AS body
FROM node n
 LEFT OUTER JOIN field_data_body fdb ON n.nid = fdb.entity_id
 INNER JOIN users u ON u.uid = n.uid
 WHERE (type = :db_condition_placeholder_0)
 LIMIT 20 OFFSET 0
UNION ALL
SELECT c.cid AS my_id, c.subject AS title, c.uid AS uid, c.changed AS changed, c.created AS created, c.name AS username, n.nid AS nid, n.type AS my_type, fcb.comment_body_value AS body
 FROM comment c
 INNER JOIN node n ON n.nid = c.nid
 LEFT OUTER JOIN field_data_comment_body fcb ON c.cid = fcb.entity_id
 INNER JOIN users u ON u.uid = c.uid

That query is wrong. In fact, when I tested the query against SQL, I see that it doesn't effectively limit the results: everything is returned! StackExchange explains that it's missing a second LIMIT 20 OFFSET 0, which would enable it to work properly:

SELECT n.nid AS my_id, n.title AS title, n.uid AS uid, n.changed AS changed, n.created AS created, u.name AS username, n.nid AS nid, n.type AS my_type, fdb.body_value AS body
FROM node n
 LEFT OUTER JOIN field_data_body fdb ON n.nid = fdb.entity_id
 INNER JOIN users u ON u.uid = n.uid
 WHERE (type = :db_condition_placeholder_0)
 LIMIT 20 OFFSET 0
UNION ALL
SELECT c.cid AS my_id, c.subject AS title, c.uid AS uid, c.changed AS changed, c.created AS created, c.name AS username, n.nid AS nid, n.type AS my_type, fcb.comment_body_value AS body
 FROM comment c
 INNER JOIN node n ON n.nid = c.nid
 LEFT OUTER JOIN field_data_comment_body fcb ON c.cid = fcb.entity_id
 INNER JOIN users u ON u.uid = c.uid
 LIMIT 20 OFFSET 0

(And, yes, testing that on my system, it does indeed return 20 results as required.)

Note that the same StackExchange discussion also says what to do with ORDER BY (which was giving me an error when I tried to add it). The correct final query, including "ORDER BY created DESC", would be:

(SELECT n.nid AS my_id, n.title AS title, n.uid AS uid, n.changed AS changed, n.created AS created, u.name AS username, n.nid AS nid, n.type AS my_type, fdb.body_value AS body
FROM node n
 LEFT OUTER JOIN field_data_body fdb ON n.nid = fdb.entity_id
 INNER JOIN users u ON u.uid = n.uid
 WHERE (type = :db_condition_placeholder_0)
 ORDER BY created DESC LIMIT 10 OFFSET 0)
UNION ALL
(SELECT c.cid AS my_id, c.subject AS title, c.uid AS uid, c.changed AS changed, c.created AS created, c.name AS username, n.nid AS nid, n.type AS my_type, fcb.comment_body_value AS body
 FROM comment c
 INNER JOIN node n ON n.nid = c.nid
 LEFT OUTER JOIN field_data_comment_body fcb ON c.cid = fcb.entity_id
 INNER JOIN users u ON u.uid = c.uid
 ORDER BY created DESC LIMIT 10 OFFSET 0);

In this case, the parentheses around the two UNIONed SELECT statements are necessary, or the query doesn't work. (Note: I divided the "limit 20" into two equal parts, otherwise I get back 40 results total, but that's a minor detail!)

Comments

tim.plunkett’s picture

Version: 7.15 » 8.x-dev
Priority: Major » Normal
Status: Active » Postponed (maintainer needs more info)
Issue tags: +Needs backport to D7

Can you reproduce this in D8?

holtzermann17’s picture

@tim.plunkett (#1):

I tried enabling the module on a fresh D8 instance, and I get the following error:

Fatal error: Class 'PagerDefault' not found in
/Users/jac2349/drupal/core/lib/Drupal/Core/Database/Query/Select.php
on line 299

So I guess the answer is, I can't reproduce it on D8 without some work/porting. And I don't know how the D8 database API works. However, I can point to the code, so others who know more can help explain how it might (or might not) work in D8.

The .module is here: https://github.com/cdavid/drupal_planetary/blob/master/sites/all/modules/planetmath_messages/planetmath_messages.module

sudishth’s picture

Component: mysql database » ajax system
$db = \Drupal::database();
$result = $db->select('test', 'd')
                ->extend('Drupal\Core\Database\Query\PagerSelectExtender')
                ->fields('d')
                ->limit(10)         // this is where you change the number of rows
                ->orderBy('id', 'DESC')
                ->execute();

i werite like this it working for me

dawehner’s picture

Component: ajax system » database system

@sudishth
Your query certainly doesn't contain contain an union.

@holtzermann17
A link to a private repo is kinda useful :P

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

pameeela’s picture

Version: 8.9.x-dev » 7.x-dev
Issue tags: -Needs backport to D7

Moving this over to D7 given #2 saying it can't be reproduced in D8.

Going to leave as postponed though, given that no additional info has been added since 2016. Someone who knows more about D7 can decide whether to keep it open or close :)

pameeela’s picture

Issue tags: +Bug Smash Initiative
mstrelan’s picture

Status: Postponed (maintainer needs more info) » Closed (cannot reproduce)

This has been PMNMI for 11 years. It's time to close it. If anyone has steps to reproduce please re-open or raise a new issue.