Skip Menu |

This queue is for tickets about the DBIx-Class CPAN distribution.

Report information
The Basics
Id: 44051
Status: resolved
Priority: 0/
Queue: DBIx-Class

People
Owner: Nobody in particular
Requestors: justin.d.hunter [...] gmail.com
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: 0.08103



Subject: DBIC does not handle ->count() and { group_by => [ ] } correctly (properly?)
$rs->resultset('table')->search({}, { group_by => [ 'col1', 'col2' ] })->count(); makes SELECT COUNT( DISTINCT( col1, col2 ) ) FROM table me: DBI Exception: DBD::Pg::st execute failed: ERROR: could not identify an equality operator for type record [for Statement "SELECT COUNT( DISTINCT( col1, col2 ) ) FROM table me"] PATCH: Index: ResultSet.pm =================================================================== --- ResultSet.pm (revision 5736) +++ ResultSet.pm (working copy) @@ -1128,9 +1128,9 @@ sub _count { # Separated out so pager can get the full count my $self = shift; - my $select = { count => '*' }; + my $attrs = { %{$self->_resolved_attrs} }; + my $from = $attrs->{from}; - my $attrs = { %{$self->_resolved_attrs} }; if (my $group_by = delete $attrs->{group_by}) { delete $attrs->{having}; my @distinct = (ref $group_by ? @$group_by : ($group_by)); @@ -1145,13 +1145,16 @@ } } } - - $select = { count => { distinct => \@distinct } }; + $attrs->{select} = $group_by; + my $from_rs = (ref $self)->new($self->result_source, $attrs); + $from = { 'as_query' => ${$from_rs->as_query}->[0] }; } - $attrs->{select} = $select; + $attrs->{select} = { count => '*' }; $attrs->{as} = [qw/count/]; + $attrs->{from} = $from; + # offset, order by and page are not needed to count. record_filter is cdbi delete $attrs->{$_} for qw/rows offset order_by page pager record_filter/;
Subject: ResulSet-count-fix.patch
Index: ResultSet.pm =================================================================== --- ResultSet.pm (revision 5736) +++ ResultSet.pm (working copy) @@ -1128,9 +1128,9 @@ sub _count { # Separated out so pager can get the full count my $self = shift; - my $select = { count => '*' }; + my $attrs = { %{$self->_resolved_attrs} }; + my $from = $attrs->{from}; - my $attrs = { %{$self->_resolved_attrs} }; if (my $group_by = delete $attrs->{group_by}) { delete $attrs->{having}; my @distinct = (ref $group_by ? @$group_by : ($group_by)); @@ -1145,13 +1145,16 @@ } } } - - $select = { count => { distinct => \@distinct } }; + $attrs->{select} = $group_by; + my $from_rs = (ref $self)->new($self->result_source, $attrs); + $from = { 'as_query' => ${$from_rs->as_query}->[0] }; } - $attrs->{select} = $select; + $attrs->{select} = { count => '*' }; $attrs->{as} = [qw/count/]; + $attrs->{from} = $from; + # offset, order by and page are not needed to count. record_filter is cdbi delete $attrs->{$_} for qw/rows offset order_by page pager record_filter/;
new patch
Subject: ResultSet-distinct-groupby.patch
Index: lib/DBIx/Class/ResultSet.pm =================================================================== --- lib/DBIx/Class/ResultSet.pm (revision 5736) +++ lib/DBIx/Class/ResultSet.pm (working copy) @@ -1128,10 +1128,10 @@ sub _count { # Separated out so pager can get the full count my $self = shift; - my $select = { count => '*' }; + my $attrs = { %{$self->_resolved_attrs} }; + my $from = $attrs->{from}; - my $attrs = { %{$self->_resolved_attrs} }; - if (my $group_by = delete $attrs->{group_by}) { + if (my $group_by = $attrs->{group_by}) { delete $attrs->{having}; my @distinct = (ref $group_by ? @$group_by : ($group_by)); # todo: try CONCAT for multi-column pk @@ -1145,16 +1145,19 @@ } } } - - $select = { count => { distinct => \@distinct } }; + $attrs->{select} = $group_by; + my $from_rs = (ref $self)->new($self->result_source, $attrs); + $from = { 'as_query' => ${$from_rs->as_query}->[0] }; } - $attrs->{select} = $select; + $attrs->{select} = { count => '*' }; $attrs->{as} = [qw/count/]; - # offset, order by and page are not needed to count. record_filter is cdbi - delete $attrs->{$_} for qw/rows offset order_by page pager record_filter/; + $attrs->{from} = $from; + # offset, order by, group by and page are not needed to count. record_filter is cdbi + delete $attrs->{$_} for qw/rows offset order_by group_by page pager record_filter/; + my $tmp_rs = (ref $self)->new($self->result_source, $attrs); my ($count) = $tmp_rs->cursor->next; return $count;
test
Subject: ResultSet-distinct-groupby.t
use strict; use warnings; use Test::More; use lib qw(t/lib); use lib '/sporkrw/xfer/DBIx-Class/0.08/trunk/lib'; use DBICTest; my $schema = DBICTest->init_schema(); eval "use DBD::SQLite"; plan skip_all => 'needs DBD::SQLite for testing' if $@; plan tests => 4; cmp_ok($schema->resultset("Tag")->count({}), '==', 9, 'Count without DISTINCT ok'); cmp_ok($schema->resultset("Tag")->count({}, { group_by => 'tag' }), '==', 3, 'Count with single column group_by ok'); cmp_ok($schema->resultset("Tag")->count({}, { group_by => [ qw/tag cd/ ]}), '==', 9, 'Count with multiple column group_by ok'); cmp_ok($schema->resultset("Tag")->count({}, { distinct => 1 }), '==', 9, "Count with single column distinct ok");