Problem/Motivation

With field paths such as entity_id.0.entity:node.created_at it's possible to create queries across entity references which allows to create joins based on related data. However, when generating aggregated queries that use an aggregate condition, groupBy or sortAggregate based on such a field then an invalid query will be created. This happens because QueryBase::getAggregationAlias<code> doesn't escape the <code>: and . characters.

Steps to reproduce

I'm not sure how to reduce this to a small case based on Nodes/Comments, so a query that can easily be executed by others is difficult, but internallly we have something like.

// Find conversations for the current user where they've received a message 
// and sort the conversations by the most recently sent messages.
\Drupal::entityTypeManager()->getStorage('chat_message_receipt')
  ->getAggregateQuery()
  ->condition('recipient', \Drupal::currentUser()->id())
  ->groupBy('message.0.entity:chat_message.conversation')
  ->sortAggregate('message.0.entity:chat_message.sent_at', 'MAX', 'DESC')
  ->execute();

Proposed resolution

Replace the implementation of QueryBase::getAggregationAlias with

    return strtolower(str_replace([':', '.'], '__', $field) . '_' . $function);

The generated alias shows up in the output and may be used for field selection. However, this change should be backwards compatible since current queries cannot contain these characters due to this bug and no replacement happens for any queries that don't currently contain these characters, so their alias will remain stable.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Entity reference fields in aggregate conditions, sort clauses, or group by statements, will no longer cause an invalid SQL query to be created.

Comments

Kingdutch created an issue. See original summary.

kingdutch’s picture

Title: Aggregation queries fail across entity referenes » Aggregation queries fail across entity references
Status: Active » Needs review
StatusFileSize
new575 bytes

Status: Needs review » Needs work

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.

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.

Version: 9.5.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. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.