Views distinct logic checks too early for non-empty $this->fields

Created on 7 September 2018, about 6 years ago
Updated 16 March 2023, over 1 year ago

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

๐Ÿ› Bug report
Status

Needs work

Version

10.1 โœจ

Component
Viewsย  โ†’

Last updated about 14 hours ago

Created by

๐Ÿ‡จ๐Ÿ‡ญSwitzerland berdir Switzerland

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024