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