When using aggregation, additional fields should not be included in the grouping function

Created on 24 May 2018, over 6 years ago
Updated 6 December 2023, about 1 year ago

Problem/Motivation

When working on aggregated Views for Commerce Reports, I noticed some weird behaviors with our Price field. Output would be fine when the normal "group", but once I changed to SUM or AVG it would render as a numeric/integer field and lose all formatting. I did some digging and found out the Display plugin overrides the handler based on information in \Drupal\views\Plugin\views\query\Sql::getAggregationInfo.

The problem is that \Drupal\views\Plugin\views\field\FieldPluginBase::addAdditionalFields applies that grouping to each additional field. In the case of an entity field that means it tries to SUM/AVG the bundle, entity ID, or any other field columns. In our case, it kills the currency code.

Upon reviewing the code I see:


    $group_params = [];
    if ($this->options['group_type'] != 'group') {
      $group_params = [
        'function' => $this->options['group_type'],
      ];
    }
    if (!empty($fields) && is_array($fields)) {
....
          $params += $group_params;
          $this->aliases[$identifier] = $this->query->addField($table_alias, $info['field'], NULL, $params);

As you can see in this screenshot, the langcode, bundle, and currency_code become "0" since it performs SUM() on a string.

Proposed resolution

Views should not apply the group/aggregate function on all additional fields.

I don't know if this is considered BC or not. None of the additional fields are rendered or ever managed (or ever had valid data.)

Remaining tasks

Create patch
Discuss if BC
Hopefully fix?

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Needs work

Version

11.0 🔥

Component
Views 

Last updated about 2 hours ago

Created by

🇺🇸United States mglaman WI, USA

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

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