Problem/Motivation

EntityQuery offers ordering on any field / column. I believe making this a Total order is desirable but it is currently not, there are situations where a ≤ b and b ≤ a and yet a != b. This happens if entity1->field[0]->value is 1, entity1->field[1]->value is 4 , entity2->field[0]->value is 2 and entity2->field[1]->value is 3. We compare maximums and minimums and so we compare 1 < 2 and 4 > 3 .

Proposed resolution

When a field has multiple values, lexicographic sorting helps this situation. Instead of minimum and maximum, we compare elements at delta 0, if they are the same then at delta 1 and so forth. By making a consistent decision on where to put missing values (MySQL puts them first on ASC, last on DESC and DBTNG hardwires this for PostgreSQL too) this sort is good.

When there are multiple translations you must specify a langcode, plain and simple. If you don't the results are undefined. This requires throwing away the current tests. That's fine.

Remaining tasks

Once we we figure the desired ordering out, we still need to make it work on PostgreSQL. The challenge here is this: for count/pager we want on one result row per revision. However, JOINining field tables with delta > 1 or multiple translations will produce more than one row per revision. DISTINCT will not work because PostgreSQL requires us to put the fields ordering on into SELECT so then DISTINCT will do nothing useful. The resulting queries might look http://stackoverflow.com/questions/9796078/selecting-rows-ordered-by-som... very ugly.

Also, I thought revisions were primary key'd by their revision id, since when do we have multiple languages of the same revision? That perhaps makes it necessary(?) to rekey the results of entity query??

User interface changes

API changes

In certain cases the ordering result will change but the previous results were utterly broken.

Data model changes

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Status: Needs review » Needs work

The last submitted patch, sort_1.patch, failed testing.

chx’s picture

Title: EntityQuery shouldn't allow sorting on cardinality > 1 fields » EntityQuery sorting for cardinality > 1 fields
Issue summary: View changes
Status: Needs work » Active
chx’s picture

Status: Active » Needs review
FileSize
5.39 KB

Status: Needs review » Needs work

The last submitted patch, 3: 2537324_3.patch, failed testing.

The last submitted patch, 3: 2537324_3.patch, failed testing.

chx’s picture

Issue summary: View changes
Status: Needs work » Needs review
FileSize
8.54 KB

I tested this on MySQL and it passed. Let's see what the bot says about PostgreSQL and SQLite...

chx’s picture

So the problem is -- although lexicographic sort is good when sorting the shapes in the test -- but the greetings are multiple language and you still have the same problem comparing: entity 15 has both tr and pl, do we want to sort it before or after the ones that only have tr or pl? I do not know.

chx’s picture

Issue summary: View changes
FileSize
3.4 KB

Here's the contents of the test field tables if the comment in the patch is not clear enough.

chx’s picture

Issue summary: View changes
chx’s picture

Issue summary: View changes
chx’s picture

Issue summary: View changes
chx’s picture

Title: EntityQuery sorting for cardinality > 1 fields » EntityQuery sorting for cardinality > 1 fields and translations
chx’s picture

Issue summary: View changes
chx’s picture

Someone needs to do this because I won't.

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.

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

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should 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.

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

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should 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.

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

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should 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.

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

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.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.

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.

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

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.

smustgrave’s picture

Version: 9.4.x-dev » 10.1.x-dev
Status: Needs review » Needs work

Sounds like this still needs discussion and discovery. Hopefully this shakes things loose.

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

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.