KnpLabs / KnpLabs/KnpPaginatorBundle
SELECT DISTINCT returns 0 id and can not be disabled
- Dominant language
- PHP
- Stars
- 1.8k
- Forks
- 332
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 3
Description
We are using a rather complicated query with a lot of grouping. I will break it down to most interesting parts:
``` php
public function getInspectionResultQuery(Specification $specification, ResultModifier $modifier = null)
{
$qb = $this->_em->createQueryBuilder();
$qb->select([
$this->alias . '.id',
$this->alias . '.date',
sprintf("GroupConcat(DISTINCT %s.comment) AS comment", $this->alias)
])
->from($this->_entityName, $this->alias)
->join(sprintf("%s.parttype", $this->alias), 'part')
->join(sprintf("%s.location", $this->alias), 'testLocation')
->addGroupBy($this->alias . '.date')
->addGroupBy($this->alias . '.shippingnumber')
->addGroupBy('part.id')
->addGroupBy('testLocation.id')
;
$this->applySpecification($qb, $specification);
$query = $qb->getQuery();
if ($modifier !== null) {
$modifier->modify($query);
}
return $qb->getQuery();
}
```
We are using this DQL at two places in our application. Both use the same filters (specifications). The first one works as expected and returns the important parts:
``` sql
SELECT b0_.id AS id_0, b0_.date AS date_1, ...
WHERE b0_.compartment_id = ? AND (c6_.id = ? AND (b0_.date >= ? AND b0_.date <= ?) AND l3_.id = ? AND p2_.id = ?)
GROUP BY b0_.date, b0_.shippingnumber, p2_.id, l3_.id
ORDER BY b0_.date DESC, p2_.integrator_number ASC
```
We are using the default paginator option with `use-distinct` set to `true`. The `COUNT` of the paginator works fine and returns the correct result using `DISTINCT`:
``` sql
SELECT count(DISTINCT b0_.id)
```
Unfortunately we don't get any results from this correctly counted rows. The responsible query will look like this:
``` sql
SELECT DISTINCT b0_.id AS id_0...
```
And this will result in the following original query with the extra `IN` condition for selecting the found IDs:
``` sql
SELECT b0_.id AS id_0, b0_.date AS date_1, ...
WHERE ...
AND b0_.id IN (?)
GROUP BY b0_.date, b0_.shippingnumber, p2_.id, l3_.id ORDER BY b0_.date DESC, p2_.integrator_number ASC
```
Parameters: [1, 6408, '2015-07-30', '2016-07-06', 4, 13295, **['0']**]
The issue is caused by the `DISTINCT` on the `SELECT`. Without it the query will work fine.
We tried to disable it but this will only remove `DISTINCT` on the `COUNT` query.
**We need the `DISTINCT` removed on the `SELECT` IDs query**.
Is (/ should) this (be) possible?
P.S.: We are using a mixed result but the (Doctrine) Pagniator seems to have no problem with it until we enable `wrap-queries`. That would return **Not all identifier properties can be found in the ResultSetMapping: id**. Maybe this is important to know too.
Possibly related:
- https://github.com/KnpLabs/KnpPaginatorBundle/issues/298
- https://github.com/KnpLabs/knp-components/pull/126
- https://github.com/KnpLabs/KnpPaginatorBundle/issues/123
Contributor guide
Research direction
Start by tracing the paginator's DISTINCT ID-selection query and how the use-distinct option affects it, then compare the related issues and pull request linked in the report. Reproduce the grouped mixed-result query with pagination and wrap-queries enabled; done means the ID query can avoid DISTINCT without breaking the count or returned results.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php, symfony
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100