Problem/Motivation
I have a table display view of nodes that is showing duplicates. I enabled the distinct option but views does not actually add a DISTINCT to the query it builds, apparently because in the following condition in , $this->fields is empty:
\Drupal\views\Plugin\views\query\Sql::query
// Check query distinct value.
if (empty($this->noDistinct) && $this->distinct && !empty($this->fields)) {
$base_field_alias = $this->addField($this->view->storage->get('base_table'), $this->view->storage->get('base_field'));
$this->addGroupBy($base_field_alias);
$distinct = TRUE;
}
Later in the method, it adds the entity base ids based on the entity information. I think that block should be moved up or that !empty($this->fields) removed but I don't know if that would have any side effects.
I also don't understand why it sets a $distinct = TRUE; variable, that is as far as I see only used a few lines down to call $query->distinct(). Possibly some left-over of no longer having to build the query itself?
There are more $this->fields checks a bit below that I guess are also not doing much in such cases.
Proposed resolution
Move the entity information up as the first thing in that method?
Remaining tasks
User interface changes
API changes
Data model changes
| Comment | File | Size | Author |
|---|---|---|---|
| #30 | 2998016-30.patch | 1.79 KB | drupatz |
| #26 | 2998016-nr-bot.txt | 791 bytes | needs-review-queue-bot |
| #18 | 2998016-18.patch | 1.79 KB | prem suthar |
| #2 | views-distinct-2998016-2.patch | 1.74 KB | berdir |
Issue fork drupal-2998016
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
berdirActually moved the distinct stuff down and combined it into a single condition as the other parts all somehow depend on each other.
Lets see if this fails anything.
Comment #3
dawehnerI'm not able to fully judge whether this change could break things at the moment.
One thing which seems to be a potential risk is:
Would't this change its behaviour potentially?
You are spot on there!
Comment #4
berdirNot quite sure about the impact too, yes.
It could also break/change things because I imagine that the distinct isn't added in quite a few views right now, so people probably tried other things too like explicit aggregation.
Now adding the distinct could make the query slower and possibly also change the results, but not sure what we can do about that? Only thing I can think of is a site builder change record that explains that the distinct is now added correctly in more cases and that they should possibly review their views that use that setting?
Comment #7
monya commentedMet the same issue with not working a distinct option. This patch works well for my cases and from my point of view does exactly what should do. The next distinct variable check performs on toString method right before query invocation and should not touch any other cases.
Regarding notification about performance impact - it's already there on query settings form.
Comment #10
dewalt commentedThe issue is reproduced for Rest export displays, I have rest export for nodes (without relationships) with multi-condition on multiple field. The export duplicates nodes and "Distinct" option doesn't work because of the bug.
I'm supersized that it is not reviewed for 2 years. Drupal community, do you have plans to apply it?
About the impacts - user should understand it checking "Distinct" option, but if it checked he might expect that the option is applied on queries.
Comment #12
mstrelan commentedI've just come to the same conclusion while debugging #3221418: MySQL error when a view has a default tablesort and uses a distinct query. After applying this patch I can reproduce the error reported in that issue by creating a view with distinct and a default tablesort. I believe the solution would be to ensure the sort field is added to the query fields. I'll see if I can write a test to demonstrate the issue. It might need to be a separate issue that would be a blocker for this IMHO.
Comment #15
mstrelan commentedSetting to Needs Work as it needs a failing test. Tagging for Bug Smash Initiative.
Comment #16
webdrips commented#2 Worked great for me. I had a field added as a filter and set to "Is not empty" to trigger the "No Results Behavior", and it was causing duplicate rows.
I'll have to see if there is fallout on views with aggregation set, so I will report back if I find anything so other people know what to look out for.
Side note: Drupal 8+ seems to be more finicky about what is considered a view with no results. To me, if all fields are empty, that should trigger the no result, but I digress.
Comment #18
prem suthar commentedRe-roll The #2 Patch For 10.1 branch.
Comment #20
joe_carvajal#18 worked perfectly for me using Drupal 10.3.1. Before the patch, some views didn't apply the DISTINCT in the query despite of being enabled in the views config. After applying it, all of them worked.
Comment #22
prem suthar commentedComment #25
mrinalini9 commentedCreated MR for patch changes in #18 for 11.x branch, please review it.
Thanks!
Comment #26
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #27
vasikeI can't replicate this anymore .. I remember it happened
And the patched solved the issue (in the past)
If Anyone could share a replicable View config for this ... so maybe it could be used for tests
p.s.: the idea is: "not to carry all unneeded patches" in the projects ...
Comment #28
boregar commentedHello, the code of patch #18 solved the issue for me, using Drupal 11.1.5
My way to reproduce the issue:
Whether you check or not the "Distinct" checkbox in the Other>Query settings parameters tab, the result is:
After applying the code of patch #18, the result is correct:
Comment #29
dewalt commented@vasike please don't forget to provide Drupal version it isn't reproduced more and more details that are related to described behaviors above. I see @boregar confirmed that issue still has place.
Comment #30
drupatz commentedThe patch in #18 isn't applicable in Drupal 11.2.2, so here an updated one