Bug description

We've met a buggy behaviour of a view having two reference-field filters in an OR subgroup.

That's how filters looked like:

And this is the resulting query:

There are two issues:
1. AND operator is used instead of OR one.
This is from hardcoded group identifier- value of 0 - for building adding where expression addWhereExpression which in some case will fail ... if the group is other than 0.
2. Even if OR operator would be used, INNER join breaks the OR logic.
The fix for this was included in the latest MR, originally it was being handled separately in #1766338: Incorrect filter group OR behavior, LEFT JOIN changed to INNER JOIN
This is from forcing INNER join - for performance - even the operator is OR for the current group.

Steps to reproduce

  1. Create 2 taxonomies
    and add some terms for both of vocabularies
  2. Create 2 taxonomy term reference fields
    And create different content
  3. Create a view with 2 filters for those 2 fields
  4. Play with AND and OR operators for those filters

Proposed resolution

on ManyToOneHelper

  • - Fix addWhereExpression usage by using the current option group
  • - Use INNER join only there is no OR operator for the current group

STR

1. Create new Drupal 8 installation with a Standard profile.
2. Export configuration.
3. Copy/replace files from attached configuration-parts.tgz.
4. Import/synchronize the configuration.
5. At admin/structure/views/settings enable the "Show the SQL query" option.
6. Visit admin/structure/views/view/many_to_one_test and see the resulting query.

Remaining tasks

- decide if we want to somehow determine when the INNER join can be used

CommentFileSizeAuthor
#126 2559961-126.patch9 KBangrytoast
#125 interdiff-121-125.txt7.76 KBangrytoast
#125 2559961-125.patch9 KBangrytoast
#122 interdiff-105-121.txt19.93 KBangrytoast
#121 interdiff-105-121.patch19.93 KBangrytoast
#121 2559961-121-simplified.patch3.82 KBangrytoast
#119 interdiff-105-118.txt1.59 KBangrytoast
#118 2559961-118.patch22.12 KBangrytoast
#116 interdiff-105-116.txt811 bytesangrytoast
#116 2559961-116.patch21.96 KBangrytoast
#105 2559961-105.patch21.87 KBangrytoast
#101 2559961-101.patch21.88 KBJordiK
#96 drupal_9.JPG58.13 KBKrzysztof Domański
#90 2559961-90.patch21.82 KBsam711
#87 manyToOneHelper.png16.79 KBfroboy
#84 2559961-84.patch22.21 KBKrzysztof Domański
#84 interdiff-80-84.txt981 bytesKrzysztof Domański
#84 2559961-test-only.patch20.85 KBKrzysztof Domański
#80 interdiff-79-80.txt2.21 KBKrzysztof Domański
#80 2559961-80.patch22.21 KBKrzysztof Domański
#80 2559961-test-only.patch20.85 KBKrzysztof Domański
#79 2559961-79.patch22.21 KBKrzysztof Domański
#79 interdiff-75-79.txt584 bytesKrzysztof Domański
#75 2559961-75.patch22.28 KBmlncn
#65 2559961-64.patch3.96 KBLendude
#63 manytoonehelper_ignores-2559961-62-8.7.x.patch3.48 KBdrup16
#63 interdiff-44-62-8.7.x.txt2.13 KBdrup16
#53 e6a5fe3b10f77b9ae8a6f67b95a6e7e3.png36.16 KBPascal-
#50 views_filters.png70.66 KBjoshua.boltz
#44 manytoonehelper_ignores-2559961-44-8.5.x.patch1.36 KBdrup16
#42 manytoonehelper_ignores-2559961-42-8.5.x.patch1.39 KBdrup16
#29 manytoonehelper_ignores-2559961-29-8.3.x.patch3.57 KBadinac
#26 manytoonehelper_ignores-2559961-26-8.3.x.patch3.54 KBjibran
#26 manytoonehelper_ignores-2559961-26-8.4.x.patch3.54 KBjibran
#19 2559961-19.patch4.54 KBLeksat
#19 2559961-19.png242.74 KBLeksat
#19 2559961-17.png228.99 KBLeksat
#19 2559961-clean.png237.61 KBLeksat
#17 2559961-17.patch3.53 KBLendude
#17 2559961-17-TEST_ONLY.patch2.18 KBLendude
#15 2559961-15.patch1.35 KBtar_inet
#2 2559961-1.patch1.96 KBLeksat
configuration-parts.tgz2.42 KBLeksat
resulting-query.png143.29 KBLeksat
filter-settings.png50.25 KBLeksat

Issue fork drupal-2559961

Command icon 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:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Leksat created an issue. See original summary.

Leksat’s picture

Issue summary: View changes
Status: Active » Needs review
FileSize
1.96 KB

Here is the patch that fixes both bugs, but also introduces a possible performance regression because INNER join is replaced with LEFT join.

dawehner’s picture

Issue tags: +Needs tests

- decide if tests are required

There is no decision to be made :)

Leksat’s picture

@geertvd, no, this is a different issue.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

dawehner’s picture

Status: Needs review » Needs work
Lendude’s picture

Closed #2865149: We should always use defined group and #2840335: Views Filter And/Or operator issue as duplicates of this. This could really use a more descriptive issue title.

Lendude’s picture

Berdir’s picture

Thanks @Lendude. What about using my issue title as referenced in #11 as title of this? :)

Lendude’s picture

Title: Bugs in ManyToOneHelper » ManyToOneHelper ignores group configuration for some cases and forces INNER joins

@Berdir++, was thinking the same thing but didn't want to plagiarise your work blindly :)

andypost’s picture

There's a tests were added in closed (duplicate of this) #2865149: We should always use defined group

tar_inet’s picture

FileSize
1.35 KB

Rolling patch from #2865149: We should always use defined group

Please, consider writing a test for ManyToOne as @dawehner said in #9 in #2865149: We should always use defined group.

@andyceo also provided a patch for testing but what I can find is a test module and I don´t know how to use it for testing.

I spent a few hours trying to figure out how to test it but I´m not conformable with my ideas becacuse they don´t flollow views.module procedure or they are too complex. Any help?

tar_inet’s picture

Status: Needs work » Needs review
Lendude’s picture

Test for this. Since we have multiple ifs in the patch we might need extra scenario's added to this, but it's a start.

The test only patch is the interdiff to #15

The last submitted patch, 17: 2559961-17-TEST_ONLY.patch, failed testing.

Leksat’s picture

FileSize
237.61 KB
228.99 KB
242.74 KB
4.54 KB

I manually tested #17 on Drupal 8.3.x-dev 4c6a12e. It solves AND/OR issue, but INNER JOIN still breaks the view result.

Clean install: https://www.drupal.org/files/issues/2559961-clean.png

Patch #17: https://www.drupal.org/files/issues/2559961-17.png

So I added no-INNER-force to the patch.

This patch: https://www.drupal.org/files/issues/2559961-19.png

Here is the test project: https://github.com/Leksat/drupal-2559961

Leksat’s picture

Issue summary: View changes
Status: Needs review » Needs work
Issue tags: +Needs tests

I guess we need
- extend current test to cover the INNER JOIN issue
- decide if it's possible to detect allowed cases for INNER JOIN usage

Leksat’s picture

Status: Needs work » Needs review

I checked code a bit more and now I think that my patch #19 is wrong.

I created a view with filters that look similar to #19 setup, but using node reference fields. There is no issue in this case because ManyToOneHelper is not used at all. Filtering is handled with NumericFilter.

But for taxonomy terms the handler is altered to \Drupal\taxonomy\Plugin\views\filter\TaxonomyIndexTid: https://github.com/drupal/drupal/blob/58f43803e27fb269251f79344199f652d7...
I guess this was done for better UX, so users can use autocomplete and dropdowns for selecting terms instead of filling their IDs manually (which is the case for node reference fields).

TaxonomyIndexTid extends \Drupal\views\Plugin\views\filter\ManyToOne. It seems like ManyToOne (and its ManyToOneHelper) was designed to improve performance on multiply JOINs of the same table. This works well if you use "Has taxonomy terms" filter (test in #17 patch) which always joins taxonomy_index table, but leads to unexpected behavior if handler is used with reference fields.

One way to solve this would be to
- remove taxonomy_field_views_data_alter()
- create an update hook to update existing views

But would be cool to get feedback from someone who has better knowledge of ManyToOne and ManyToOneHelper.

Probably it's better to fix this in a separate issue, because #17 fixes AND/OR bug perfectly and has test coverage for it.

Lendude’s picture

@Leksat yes we should put the inner/left join in its own issue. Looked around a bit and could only find #1766338: Incorrect filter group OR behavior, LEFT JOIN changed to INNER JOIN, so that should really be set to D8 and then marked for backport to D7, but not sure how happy people will be when we hijack a 5 year old critical, but that would be the preferred workflow.

luenemann’s picture

@Lendude +1 - see #1766338-77: Incorrect filter group OR behavior, LEFT JOIN changed to INNER JOIN

IHMO it's very difficult to determine when it's safe to use an inner join here.

For example when you add a field to an entity type with existing content. No records are added to the field value table. So all existing content is excluded by an inner join to the field value table.

The inner join optimization was added in 2008 (#248749: Consider Use of Inner Joins for Taxonomy Filters , http://cgit.drupalcode.org/views/commit/?id=1f4cbd4) and I can't find any evidence of a real need.

What is the performance penalty of using always a left join?
Wouldn't it be better to let the user opt-in for optimization?

Leksat’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs tests

1. Group logic is fixed with #17.
This is covered with tests. Also code-reviewed and manually tested by me.
So marking this issue RTBC.

2. INNER JOIN bug should be solved in a separate issue (not yet created). It can be fixed in several ways:
- always use LEFT JOIN
- stop using TaxonomyIndexTid for taxonomy reference fields
I did not create an issue because I found #2429699: Add Views EntityReference filter to be available for all entity reference fields. I commented there about the INNER JOIN bug.

dagmar’s picture

Status: Reviewed & tested by the community » Needs review

You cannot RTBC your own patch. Sorry.

jibran’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
3.54 KB
3.54 KB

@dagmar he RTBCed #17 not his own patch. Re-uploading #17 for 8.3.x and 8.4.x. Patch looks great fix makes sense.

jibran’s picture

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

As far as I can see the comment

Test for this. Since we have multiple ifs in the patch we might need extra scenario's added to this, but it's a start.

Hasn't been addressed - there's one test case and 4 ifs.

adinac’s picture

The path core/modules/taxonomy/src/Tests/Views/TaxonomyIndexTidUiTest.php from the patch in #26 doesn't seem right...there is no such file in 8.3.x nor in 8.4.x. There is a core/modules/taxonomy/tests/src/Functional/Views/TaxonomyIndexTidUiTest.php. I'm attaching another version of the patch.

andypost’s picture

yes, tests are moved so still NW for #28

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

ckaotik’s picture

For the INNER JOIN part, isn't this only a problem when using OR conjunctions?
Edit: Just realized the discussion on that has moved on to a separate issue, moved the comment there: #1766338-83: Incorrect filter group OR behavior, LEFT JOIN changed to INNER JOIN.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

alison’s picture

#29 applied cleanly for me on 8.4.5! Bonus: it fixed the "OR"/inner-join problem :D

Side-effect: When I used either/both of my "OR" filters, my results were flooded with duplicates. BUT, both/either of these config changes fixed the dupe problem (in other words, I could use either of these changes, I did not need both)

  1. Query settings -> Distinct
  2. Filter criteria -> each filter in the "OR" filter group -> Reduce duplicates
alison’s picture

Priority: Normal » Major

Er, btw, this seems super critical to me, I'd like us to at least change it to "major" -- I mean for heaven's sake, the "filter groups" > "OR" operator simply does not work in a fairly simple scenario, and the Views interface just lets you try to use it as if it were a real thing. Extremely frustrating. I checked the Priority documentation, I see no reason to keep this "normal," it seems quite "major" to me.

(I mean I can't fathom not including this fix in 5.x!!! Blerg.)

Thank you for the patching, I'm thrilled and relieved that #29 worked for me.

KarimBou’s picture

Hello i've got the same issue with different group and like to just change to "OR" between to 'subgroups' it's not working, it'll not change. I'm using a solr index view, so the patch #29 wont help me.

oriol_e9g’s picture

@KarimBou I have tested the patch and works perfect if all the groups have the same separator.

You can do:
GROUP1 AND GROUP2 AND GROUP3
GROUP1 OR GROUP2 OR GROUP3

but fails when you try to do:
GROUP1 AND GROUP2 OR GROUP3.

only works if all group separator are the same.

KarimBou’s picture

@oriol_e9g But i need to do GROUP1 and [GROUP2 or GROUP3] OR [GROUP4 and GROUP5 or GROUP6]

hudri’s picture

Come accross this issue too, my setup were 2 filter groups A B, with B having OR criterias:

(criteria_A1 and criteria_A2 and criteria_A3) and (criteria_B1 or criteria_B2)

Patch #29 fixed it for me on v8.5.4, but also had to tick "Reduce duplicates"

Romixua’s picture

I have the same issue, and I made hotfix in view config:
Change needed filters group to 0

      filters:
        # Othe filters
        field_match_team_host_target_id:
          id: field_match_team_host_target_id
          table: node__field_match_team_host
          field: field_match_team_host_target_id
          relationship: none
          group_type: group
          admin_label: ''
          operator: or
          value:
            4: 4
          group: 1 # we nead to change this value to 0 "group: 0"
          exposed: false       
          is_grouped: false
          reduce_duplicates: false
          type: select
          limit: true
          vid: team
          hierarchy: false
          error_message: true
          plugin_id: taxonomy_index_tid
        field_match_team_guest_target_id:
          id: field_match_team_guest_target_id
          table: node__field_match_team_guest
          field: field_match_team_guest_target_id
          relationship: none
          group_type: group
          admin_label: ''
          operator: or
          value:
            4: 4
          group: 1 # we nead to change this value to 0 "group: 0"
          exposed: false
          is_grouped: false
          reduce_duplicates: false
          type: select
          limit: true
          vid: team
          hierarchy: false
          error_message: true
          plugin_id: taxonomy_index_tid

Also need to change filter group key to 0

      filter_groups:
        operator: AND
        groups:
          1: OR # we nead to change this key to 0 "0: OR"
          2: AND

I know that the solution is not good but it allows to fix the filter without a patch.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

drup16’s picture

We were not able to apply patch from #29 as we are running Drupal Core 8.5.5. We decided to manually apply the changes from #29 and adding the patch here.

We did have to set the "Remove Duplicates" option to true.

Status: Needs review » Needs work

The last submitted patch, 42: manytoonehelper_ignores-2559961-42-8.5.x.patch, failed testing. View results

drup16’s picture

This patch for #44 is to fix issue with wrong directory for patch #42.

dchatry’s picture

#44 works for me! AND has been changed to OR and without the need for "Remove duplicates", thanks for the patch, hope it gets commited soon!

alison’s picture

Patch applies cleanly for me on 8.5.6.

But...

The site I tested on before no longer uses the filtering setup with the grouped-OR situation, and I'm having trouble reproducing the problem on another site -- for now, I'll leave it at, the patch applies cleanly for me on 8.5.6, and I'll try to give it another try with a fresh brain on Monday :)

Thank you so much for all of y'all's work on this issue!

oriol_e9g’s picture

This patch not solves all group problems but fixes some of them. If somebody have time we still need 3 extra tests to push this. See #28

anita_novicell’s picture

The patch didn't solve the problem for me.
I have a view with filters grouped like this:

( condition1 AND condition2 ) OR ( condition3 AND condition4)

The code in the patch checks if the condition is of the type "AND". In this case it is, but the parent group is of the type "OR", so we should still use a left join, not an inner join.

drup16’s picture

@anita_novicell,

In regards to use of left join vs inner join, can you check if this helps #1766338-83: Incorrect filter group OR behavior, LEFT JOIN changed to INNER JOIN?

joshua.boltz’s picture

FileSize
70.66 KB

This patch did not solve the issue I am experiencing. I have filters setup like the screenshot posted.
If i go in and Add/Or rearrange to make the first OR an AND and apply, it still remains an OR.
But if i change the second OR to an AND, it changes both of them to an AND.

druplr’s picture

As far as I understand this issue is mentioned here too: https://www.drupal.org/project/drupal/issues/2906185

jazzper's workaround (Comment #7) fixed the issue for us on Drupal 8.5.

Please note that "reduce duplicates" option should be enabled on the filters and not in the advanced section of the view.

drup16’s picture

@joshua.boltz which patch are you using?

patch #29 was the patch I tried to apply but failed because i was on a different Drupal Core version so i created patch #44, which only applies the $options['group'] change.

Pascal-’s picture

I was using this filter, the 1st part of it was ignored in my view resulsts.
I also tried moving the 1st part to the second part and then the 2nd part of the filter was ignored.

Patch in #44 fixed this for me and now both parts work!

example filter

nashkrammer’s picture

Status: Needs review » Reviewed & tested by the community

Reviewed and test on core 8.5.8. The patch at #44 is working fine.

oriol_e9g’s picture

Status: Reviewed & tested by the community » Needs work

We still need tests. There are 4 ifs and only test one.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

specky_rum’s picture

fwiw I have a view with grouped filters in the following structure:

group 1
  condition 1
  AND condition 2
AND
group 2
  condition 3
  OR condition 4

I applied the patch in #44 but I also needed the one from here: https://www.drupal.org/project/drupal/issues/1766338#comment-12287268. Together these 2 patches resolved the issue and the view is returning the correct results.

This on D8.6.7

nwoodland’s picture

Patch from #44 on Drupal 8.6.13 and PHP 7.2.15 works great. Thanks!

codesmith’s picture

Patch from #44 worked for me - thanks!

noah’s picture

The patch from #44 also fixed this problem for me—what's required to get it to RTBC in time for 8.8?

drup16’s picture

I believe we need to add the tests, which I am working on now. If others want to volunteer to write the tests as well, please do.

drup16’s picture

Status: Needs work » Needs review

Adding interdiff between patch from #44 and latest patch for v8.7.x.

Credit for testing should go to @Lendude (#17)

drup16’s picture

For some reason the status and files got uploaded into two separate comments, my apologies there.

Lendude’s picture

Reroll to make this work again, this is essentially the same patch as #17.

So what we need is still coverage for all 4 options, we now only cover this change in the second 'else':
$this->handler->query->addWhereExpression($options['group'], "$field $operator($placeholder)", [$placeholder => $value]);

So we need coverage for cases:

  • with handlers with more then 1 value (date? int min max?)
  • coverage for IS NULL operator with single value
  • coverage for IS NULL operator with multi value handlers.
Lendude’s picture

now with patch...

muldos’s picture

I've just run into this issue and the patch of #65 has fixed it.
I am running 8.7.2.

codesmith’s picture

Patch in #65 fixed one of my views but did not fix one of my other views with a multi-value field. As noted in #48, #49 and #57 this only fixes the WHERE part of the query and seems to work fine if you're using single value fields. However, this fix does not address the INNER JOIN vs LEFT JOIN issue. So if you're using multi-value fields like taxonomy fields with multiple values, you also need the patch to fix the JOIN from here https://www.drupal.org/project/drupal/issues/1766338#comment-12287268

sam711’s picture

The WHERE part of #65 patch is working for me in D8.7.2 and D8.7.3.

Thanks!

jannakha’s picture

Proposed patch didn't work for me, neither did combination with https://www.drupal.org/project/drupal/issues/1766338#comment-12287268
what I need is to have in where clause:
tax_value in (101, 103) or tax_value in (102, 103)
but it adds three inner joins for each of the values, eg always returning 0 results

pretty basic SQL functionality, surprisingly it takes so long to fix :-(

daffie’s picture

The patch look good.

  1. +++ b/core/modules/taxonomy/tests/src/Functional/Views/TaxonomyIndexTidUiTest.php
    @@ -66,7 +66,7 @@ protected function setUp($import_test_views = TRUE) {
    +          'parent' => isset($terms[$i][0]) ? $this->terms[$i][0]->id() : 0,
    

    Should this line not be:

      'parent' => isset($this->terms[$i][0]) ? $this->terms[$i][0]->id() : 0,
    

    Missing I something or is there no variable named $terms?

  2. So we need coverage for cases:

    with handlers with more then 1 value (date? int min max?)
    coverage for IS NULL operator with single value
    coverage for IS NULL operator with multi value handlers.

    @lendude: Do you want to add these tests in this issue or in a followup issue?

  3. Also comments #67 and #69 are still open.
maxilein’s picture

sagesolutions’s picture

The patch is not working for me. I have taxonomy terms as conditions, which is an issue. It checks that the group type is an `AND` which is true, but the main grouping is using an `OR` which means it still needs to be set to a `left` join.

group 1
  condition 1
  AND condition 2
OR
group 2
  condition 3
  AND condition 4

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

mlncn’s picture

Title: ManyToOneHelper ignores group configuration for some cases and forces INNER joins » ManyToOneHelper ignores group configuration for some cases
Issue summary: View changes

sagesolutions the related but separable INNER JOIN issue is being handled in #1766338: Incorrect filter group OR behavior, LEFT JOIN changed to INNER JOIN ... oh, i don't need to tell you that ... you've been posting the most recent patches there! Thank you!!!

But that goes for everyone, i'm removing the inner join part from the title, 'cause that's being fixed over there and not here.

I think a full set of tests was provided by andyceo in #2865149: We should always use defined group: https://www.drupal.org/files/issues/views_test_filters_tests_only.patch

Can we just add them to the patch and its one test here, and at long last get this fixed?

mlncn’s picture

Status: Needs work » Needs review
FileSize
22.28 KB

What i meant to say above, comments #67 and #69 are covered in #1766338: Incorrect filter group OR behavior, LEFT JOIN changed to INNER JOIN

So the only thing holding this up is more tests.

The attached patch adds andyceo's tests from #2865149: We should always use defined group.

It also makes the change daffie suggests in #70; note that this was incorrect before any patch was added, then the previous patches only fixed the part that couldn't affect anything.

Status: Needs review » Needs work

The last submitted patch, 75: 2559961-75.patch, failed testing. View results

Heisen-blue’s picture

The patch works for me, in addition with this one Incorrect filter group OR behavior, LEFT JOIN changed to INNER JOIN
Drupal 8.7.5

drup16’s picture

Status: Needs work » Reviewed & tested by the community

As mentioned by @Heisen-blue

Patch #75 worked for us as well by combining with Incorrect filter group OR behavior, LEFT JOIN changed to INNER JOIN

Krzysztof Domański’s picture

Krzysztof Domański’s picture

Version: 8.9.x-dev » 8.8.x-dev

Major bug.

The last submitted patch, 80: 2559961-test-only.patch, failed testing. View results

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 80: 2559961-80.patch, failed testing. View results

Krzysztof Domański’s picture

Ignore previous patch. I restore what I deleted...

Krzysztof Domański’s picture

Krzysztof Domański’s picture

-- edited --

froboy’s picture

FileSize
16.79 KB

I've tested and the patch in #84 makes this configuration of groups work as expected.

drup16’s picture

+1 to RTBC

Patch from #84 also worked for us.

sam711’s picture

The patch needs reroll.

sam711’s picture

I removed the changes on TaxonomyIndexTidUiTest:71 which seemed to be already there.

@@ -71,7 +71,7 @@ protected function setUp($import_test_views = TRUE) {
         $this->terms[$i][$j] = $term = Term::create([
           'vid' => 'tags',
           'name' => "Term $i.$j",
-          'parent' => isset($terms[$i][0]) ? $terms[$i][0]->id() : 0,
+          'parent' => isset($this->terms[$i][0]) ? $this->terms[$i][0]->id() : 0,
         ]);
         $term->save();
       }

Hope it passes now.

sam711’s picture

Status: Needs work » Needs review
Krzysztof Domański’s picture

Status: Needs review » Needs work

1. Still needs additional testing.

Since we have multiple ifs in the patch we might need extra scenario's added to this, but it's a start.

Hasn't been addressed - there's one test case and 4 ifs.

2. Instead of adding a new module views_test_filter, we should add view config to core/modules/views/tests/modules/views_test_config/test_views or use existing one.

--- /dev/null
+++ b/core/modules/views/tests/modules/views_test_filter/views_test_filter.info.yml
Krzysztof Domański’s picture

Can we remove this duplicate here?

if ($operator == 'IS NULL') {
  $this->handler->query->addWhereExpression($options['group'], "$field $operator");
}

Instead of:

$placeholder = $this->placeholder();
if (count($this->handler->value) > 1) {
  $placeholder .= '[]';

  if ($operator == 'IS NULL') {
    $this->handler->query->addWhereExpression($options['group'], "$field $operator");
  }
  else {
    $this->handler->query->addWhereExpression($options['group'], "$field $operator($placeholder)", [$placeholder => $value]);
  }
}
else {
  if ($operator == 'IS NULL') {
    $this->handler->query->addWhereExpression($options['group'], "$field $operator");
  }
  else {
    $this->handler->query->addWhereExpression($options['group'], "$field $operator $placeholder", [$placeholder => $value]);
  }
}

It can be shorter:

if ($operator == 'IS NULL') {
  $this->handler->query->addWhereExpression($options['group'], "$field $operator");
}
else {
  $placeholder = $this->placeholder();
  if (count($this->handler->value) > 1) {
    $placeholder .= '[]';
    $this->handler->query->addWhereExpression($options['group'], "$field $operator($placeholder)", [$placeholder => $value]);
  }
  else {
    $this->handler->query->addWhereExpression($options['group'], "$field $operator $placeholder", [$placeholder => $value]);
  }
}
AaronBauman’s picture

I'd like to help fix up these tests, but I don't understand what the purpose of the new module views_test_filter is.

I see that it creates list_integer fields and uses them in a view to reproduce this bug, but it never gets enabled for any tests.
Is there some views magic that enables and tests all the modules in this directory?
Is part of the patch from #90 missing?

Krzysztof Domański’s picture

1. Only selected modules are enabled in TaxonomyIndexTidUiTest.php.
public static $modules = ['node', 'taxonomy', 'views', 'views_ui', 'taxonomy_test_views'];

The views_test_filter module is not used so you can delete it if you don't need it.

2. Two views are used in TaxonomyIndexTidUiTest. There are others but not included here taxonomy/tests/modules/taxonomy_test_views/test_views.

public static $testViews = ['test_filter_taxonomy_index_tid', 'test_taxonomy_term_name'];

3. The testFilterGrouping() uses the test_filter_taxonomy_index_tid view.

$view = View::load('test_filter_taxonomy_index_tid');

So far we have one test case out of four #93.

Krzysztof Domański’s picture

FileSize
58.13 KB

4. The "Is none of" operator (additional test case) has the value "not". In ManyToOneHelper class this is "IS NULL" then. A bit complicated...

if ($operator == 'not') {
  $value = NULL;
  $operator = 'IS NULL';
  $add_condition = FALSE;
}

(...)

if ($operator == 'IS NULL') {
  $this->handler->query->addWhereExpression($options['group'], "$field $operator");
}

Views filter operators.

Additional test will be similar to testFilterGrouping, but should have operator "not":

+    $display['display_options']['filters']['tid']['value'][0] = $this->terms[1][0]->id();
+    $display['display_options']['filters']['tid']['group'] = 2;
+    $display['display_options']['filters']['tid']['operator'] = 'not';
AaronBauman’s picture

Thanks for that explanation.
I'll work on adding some test coverage.
This seems to be similar / same issue as #1810148: Grouped exposed taxonomy term filters do not work because the group key is added to the query and not the taxonomy ID - I'll confirm whether this patch fixes both.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

COBadger’s picture

I tried #90 and found that the group operator in the resulting query was indeed appearing as "OR", but something about the joins must not have been correct, because with values in two of the filter fields some of our view's results were missing.

As a workaround, what worked for me was to select "Reduce duplicates" in the filter fields in our "OR" condition group.

Dubs’s picture

I've found by changing the view to be based off the related entities, and then having reverse relationships to get the other fields I can work around this problem. Be aware though, that if the ORd filter is empty (i.e. an exposed filter) you will get NO results. I added some alter code to a custom module add all items to the WHERE clause to work around this.

If anyone needs any elaboration on this let me know and I can expand on this.

Bottom line, you can work around this issue for now without any core patches.

JordiK’s picture

Re-rolled #90 to apply to 8.9.x.

ocastle’s picture

Patch #101 resolved the issue for me.

wranvaud’s picture

As said in the description, there are two issues. This patch #101 + patch at related issue solved it for me.

glynster’s picture

@Dubs we are experiencing that exact issue

that if the ORd filter is empty (i.e. an exposed filter) you will get NO results. I added some alter code to a custom module add all items to the WHERE clause to work around this.

If the OR group is empty no results. For example say we have this:

GROUP A
Exposed 1 AND
Exposed 2 AND
Exposed 3 AND

OR

GROUP B
Exposed 1 AND
Exposed 2 AND
Exposed 3 AND

OR

GROUP C
Exposed 1 AND
Exposed 2 AND
Exposed 3 AND

You will get no filtering results until one exposed filter from each group is present. Then it works perfectly fine.

@Dubs would love to know what you added to your custom module to resolve the issue.

angrytoast’s picture

Here is a very minor change from the patch in #101 that makes it more compatible with 9.x.

It removes the core: 8.x from the new test module info.yml. Having the core: key causes exceptions when running the test.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
alphawebgroup’s picture

Version: 9.3.x-dev » 9.5.x-dev
dewalt’s picture

Version: 9.5.x-dev » 9.3.x-dev
Status: Needs work » Needs review
Issue tags: +views filters

Patch #105 works for me. Looks like group "0" was really hardcoded by mistake for ManyToOneHelper.

Could the patch be applied for 9.3.x version and ported to 9.5.x in the separate issue? I think it could speed up the process. Tests up to 9.4.x are good, but waiting 9.5.x tests fix in the scope of this issue can take a time, when as I see by comments many people waiting for the fix.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

E_H’s picture

I agree with @dewalt plan to speed up the process.

Is there any prospect of this issue reviewed? How does this work?

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

very_random_man’s picture

Version: 9.5.x-dev » 9.4.x-dev
Status: Needs review » Reviewed & tested by the community

Works for me in 9.4.

xjm’s picture

Version: 9.4.x-dev » 9.5.x-dev

9.4.x is no longer open for development and receives security fixes only, so this will be committed to 9.5.x at the lowest. At this point in the development cycle, we need a patch that has working versions for 9.5.x, 10.0.x, and 10.1.x. I'm queuing tests against those branches.

xjm’s picture

Status: Reviewed & tested by the community » Needs work

This is failing the following test:

1) Drupal\Tests\taxonomy\Functional\Views\TaxonomyIndexTidUiTest::testFilterGrouping
Failed asserting that actual size 0 matches expected size 2.

/var/www/html/vendor/phpunit/phpunit/src/Framework/Constraint/Constraint.php:122
/var/www/html/vendor/phpunit/phpunit/src/Framework/Constraint/Constraint.php:55
/var/www/html/core/modules/taxonomy/tests/src/Functional/Views/TaxonomyIndexTidUiTest.php:400
/var/www/html/vendor/phpunit/phpunit/src/Framework/TestResult.php:728

https://www.drupal.org/pift-ci-job/2573416

angrytoast’s picture

Updating the patch per xjm's comments from #115.

It looks like the reason for the fails is due to the change of default test themes from Classy to Stark (https://www.drupal.org/project/drupal/issues/3248295) which no longer inserts the view-content class the test originally relied on.

This changes the selector to CSS based on the views-element-container class which is added directly in Drupal\views\Element\View::preRenderViewElement rather than in a theme template.

Status: Needs review » Needs work

The last submitted patch, 116: 2559961-116.patch, failed testing. View results

angrytoast’s picture

Of course patch 116 "worked on my local machine" but fails on Drupal CI...

Trying out a different approach in the test to avoid counting elements on the page and instead checking for presence of the expected node titles in the test view output. This is arguably a bit more accurate and doesn't rely on any HTML selectors.

angrytoast’s picture

That's looking better, adding the interdiff for 105 and 118

angrytoast’s picture

Status: Needs work » Needs review
angrytoast’s picture

Reading through earlier comments again, I agree with comment #95 in that the views_test_filter test module introduced in earlier patches isn't necessary--the newly added test Drupal\Tests\taxonomy\Functional\Views\TaxonomyIndexTidUiTest::testFilterGrouping uses an already existing view config test_filter_taxonomy_index_tid.

Updating the patch to remove the portion that adds the core/modules/views/tests/modules/views_test_filter module.

angrytoast’s picture

FileSize
19.93 KB

Attach the correct interdiff between patch #105 and #121 as txt instead of a patch file.

lubwn’s picture

Thank you @angrytoast for patch from #121, works flawlessly on Drupal 9.4.8

Lendude’s picture

Status: Needs review » Needs work
Issue tags: -views filters +Needs tests

This still needs extra test coverage so we can hit all the changes

angrytoast’s picture

Revisiting this one. After looking through history and specifically at comments #64 and #96, adding patch to hit the additional test cases.

I'm not certain about modifying and saving the view repeatedly in the test. Thoughts?

angrytoast’s picture

Small update to the patch, missing a trailing comma which caused CI build fail.

angrytoast’s picture

Status: Needs work » Needs review

OK. Builds are green, that's a good start. Marking as needs review again to get feedback on updated tests.

vasike’s picture

Version: 9.5.x-dev » 11.x-dev

let's try with the latest

vasike’s picture

Created MR with latest patch #126 from the current dev branch 11.x

But for this scenario won't work properly ... taken from the related issue https://www.drupal.org/project/drupal/issues/1766338
1. Create 2 taxonomies
and add some terms
2. Create 2 taxonomy term reference fields
And create different content
3. Create a view with 2 filters for those 2 fields
4. Play with AND and OR operators for those filters

So i "completed" the MR with the solution from there
+ Added the tests for this scenario.

And it passes

p.s. i have no idea why those 2 issues were separated.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs issue summary update

@vasike are you mixing the two tickets?

Think the issue summary should be updated about proposed solution just for this ticket.

vasike’s picture

Issue summary: View changes
Status: Needs work » Needs review

issue summary updated ... mention both fixes for this ticket and its MR.

Also ... the MR adds tests ... so maybe we could move both tags ....

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: -Needs tests
Related issues: +#3366823: Additional tests for ManyToOneHelper

Sorry if it's not super clear to me but:

Can the issue summary be updated with the steps to reproduce #130 helped but not entirely sure the bug to be on the look out for.

Also the proposed solution is missing

Brought this up in slack and appears it's one of those issues that didn't have enough test coverage. But not in scope of this ticket so opened #3366823: Additional tests for ManyToOneHelper

vasike’s picture

Issue summary: View changes
Status: Needs work » Needs review

updated the summary with more info about issue, step to reproduce and solution proposal.

I hope it's enough and it's not that technical ...

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs issue summary update +Needs Review Queue Initiative

Thanks. Based on the proposed solution I think this is ready for committer review.

longwave’s picture

Status: Reviewed & tested by the community » Fixed

Thanks for everyone's patience here, with a useful issue summary and a great patch with an easy-to-understand fix and a comprehensive test case.

Committed and pushed aec9cf8ca1 to 11.x and 12f091439f to 10.1.x.

  • longwave committed 12f09143 on 10.1.x
    Issue #2559961 by angrytoast, Krzysztof Domański, drup16, Lendude,...

  • longwave committed aec9cf8c on 11.x
    Issue #2559961 by angrytoast, Krzysztof Domański, drup16, Lendude,...

Status: Fixed » Closed (fixed)

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