When a query with a group by clause is extended by PagerDefault, the count query fails.
This is due to:

public function countQuery() {
  ...
  $group_by = array_keys($count->getGroupBy());

containing linear values as the array returned by getGroupBy() is not associative.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

solotandem’s picture

Status: Active » Needs review
FileSize
745 bytes

The attached 1-line patch seems to fix the problem.

Damien Tournoud’s picture

Issue tags: +Needs tests

ew. That looks right, but that means we need more tests :(

solotandem’s picture

FileSize
1.95 KB

Simplified 1-line patch with revised test.

Status: Needs review » Needs work

The last submitted patch, 1005674-count-query.patch, failed testing.

solotandem’s picture

Status: Needs work » Needs review
FileSize
1.75 KB

Who knew this was a duplicate of #1003860: Count query fails to remove fields and expressions? This 1-line patch is: 1) cleaner than what was committed on that issue (it eliminates a redundant function call), 2) the test more fully tests the count query code by adding an order by clause, and 3) the test has a comment explaining the reason to have a column alias in the test. Posting rerolled patch here instead of reopening the other issue.

Crell’s picture

Status: Needs review » Needs work

As I noted in the issue linked in #5, the existence of drupal_map_assoc() inside SelectQuery is a critical bug as far as I'm concerned. If we need that functionality then inline it. It's not a complicated algorithm. Although I'm not entirely clear at the moment why we need to do that in the first place...

solotandem’s picture

FileSize
1.88 KB

This revised 2-line patch removes the Drupal core function call and makes the groupBy() array an associative one so the count query code will work as "intended." With this change, both the group and order arrays are associative.

solotandem’s picture

Status: Needs work » Needs review
Crell’s picture

That looks much better to me. The additions to the unit test have me confused, though. Isn't the point of that test to make sure that the countQuery() method works? Will it still be a valid test if we're adding the COUNT() statement manually?

solotandem’s picture

The example:
- tests the removal of expressions from the count query (when a column alias is present) (so the function could be SUM, COUNT, MIN, MAX, etc.)
- is a real life example for a form or view that displays counts of grouped items and uses a pager (possibly it should include its own count query in this case)

Crell’s picture

Status: Needs review » Reviewed & tested by the community

Ah, gotcha. In that case...

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Great, thanks for the clean up and the additional test!

Committed to HEAD.

Status: Fixed » Closed (fixed)
Issue tags: -Needs tests

Automatically closed -- issue fixed for 2 weeks with no activity.