}
else {
$attrs->{group_by} = [ grep { !ref($_) || (ref($_) ne 'HASH') } @{$attrs->{select}} ];
+
+ # add any order_by parts that are not already present in the group_by
+ # we need to be careful not to add any named functions/aggregates
+ # i.e. select => [ ... { count => 'foo', -as 'foocount' } ... ]
+ my %already_grouped = map { $_ => 1 } (@{$attrs->{group_by}});
+
+ my $storage = $self->result_source->schema->storage;
+ my $rs_column_list = $storage->_resolve_column_info ($attrs->{from});
+ my @chunks = $storage->sql_maker->_order_by_chunks ($attrs->{order_by});
+
+ for my $chunk (map { ref $_ ? @$_ : $_ } (@chunks) ) {
+ $chunk =~ s/\s+ (?: ASC|DESC ) \s* $//ix;
+ if ($rs_column_list->{$chunk} && not $already_grouped{$chunk}++) {
+ push @{$attrs->{group_by}}, $chunk;
+ }
+ }
}
}
FROM (
SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
FROM cd me
- GROUP BY me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
+ GROUP BY me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track, cdid
ORDER BY cdid
) me
LEFT JOIN tags tags ON tags.cd = me.cdid
}
);
is_same_sql_bind($rs->as_query, q{
- SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track, me.test_count, tags.tagid, tags.cd, tags.tag
- FROM (SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track, COUNT( tags.tag ) AS test_count
+ (SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track, me.test_count, tags.tagid, tags.cd, tags.tag
+ FROM (SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track, COUNT( tags.tag ) AS test_count
FROM cd me LEFT JOIN tags tags ON tags.cd = me.cdid
GROUP BY me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track, tags.tag
ORDER BY tags.tag ASC LIMIT 1)
me
- LEFT JOIN tags tags ON tags.cd = me.cdid
- ORDER BY tags.tag ASC, tags.cd, tags.tag
+ LEFT JOIN tags tags ON tags.cd = me.cdid
+ ORDER BY tags.tag ASC, tags.cd, tags.tag
+ )
}, []);
}