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

Created on 7 September 2018, over 6 years ago
Updated 16 March 2023, about 2 years 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 2 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.

  • 🇮🇳India prem suthar Ahemdabad- Gujrat , Jodhpur - Rajsthan

    Re-roll The #2 Patch For 10.1 branch.

  • 🇪🇸Spain joe_carvajal Seville (Spain)

    #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.

  • First commit to issue fork.
  • 🇮🇳India prem suthar Ahemdabad- Gujrat , Jodhpur - Rajsthan
  • First commit to issue fork.
  • Pipeline finished with Failed
    7 months ago
    Total: 4256s
    #303850
  • 🇮🇳India mrinalini9 New Delhi

    Created MR for patch changes in #18 for 11.x branch, please review it.

    Thanks!

  • The 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.

  • Status changed to Postponed: needs info 3 months ago
  • 🇷🇴Romania vasike Ramnicu Valcea

    I 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

  • 🇨🇭Switzerland boregar

    Hello, patch #18 solved the issue for me, using Drupal 11.1.5

    My way to reproduce the issue:

    1. Add a field referencing a taxonomy (eg "tags") to a media content type
    2. Create some media content filling the tags field for some and leaving it empty for others
    3. Create a view listing media content
    4. Add a filter to the view on the tags field, choose "is not empty (NOT NULL)"

    Whether you check the "Distinct" checkbox in the Other>Query settings parameters tab, the result is:

    • the word DISTINCT is not present in the SQL query
    • the media contents are displayed as many times as the number of tags they are linked to

    After applying the patch #18, the result is correct:

    • the word DISTINCT is present in the SQL query
    • the media contents are displayed only one time times as long as they are linked to at least one tag
  • 🇧🇾Belarus dewalt

    @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.

Production build 0.71.5 2024