X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fprefetch%2Fgrouped.t;h=c0d2224e9d43294fae992a2f41420802cdb14dd0;hb=HEAD;hp=501b25099454ae26d9114ec50dd1b4f8b065cfca;hpb=d8ce00f56c1c32808bfe881e3e9c5c81867bdb35;p=dbsrgits%2FDBIx-Class.git diff --git a/t/prefetch/grouped.t b/t/prefetch/grouped.t index 501b250..c0d2224 100644 --- a/t/prefetch/grouped.t +++ b/t/prefetch/grouped.t @@ -1,15 +1,17 @@ +BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) } + use strict; use warnings; use Test::More; -use Test::Exception; -use lib qw(t/lib); -use DBICTest; -use DBIC::SqlMakerTest; +use DBICTest ':DiffSQL'; +use DBIx::Class::SQLMaker::LimitDialects; + +my $ROWS = DBIx::Class::SQLMaker::LimitDialects->__rows_bindtype; +my $OFFSET = DBIx::Class::SQLMaker::LimitDialects->__offset_bindtype; my $schema = DBICTest->init_schema(); -my $sdebug = $schema->storage->debug; my $cd_rs = $schema->resultset('CD')->search ( { 'tracks.cd' => { '!=', undef } }, @@ -22,10 +24,12 @@ for ($cd_rs->all) { is ($_->tracks->count, 3, '3 tracks for CD' . $_->id ); } +my @cdids = sort $cd_rs->get_column ('cdid')->all; + # Test a belongs_to prefetch of a has_many { my $track_rs = $schema->resultset ('Track')->search ( - { 'me.cd' => { -in => [ $cd_rs->get_column ('cdid')->all ] } }, + { 'me.cd' => { -in => \@cdids } }, { select => [ 'me.cd', @@ -46,21 +50,13 @@ for ($cd_rs->all) { is($track_rs->count, 5, 'Prefetched count with groupby'); is($track_rs->all, 5, 'Prefetched objects with groupby'); - { - my $query_cnt = 0; - $schema->storage->debugcb ( sub { $query_cnt++ } ); - $schema->storage->debug (1); - + $schema->is_executed_querycount( sub { while (my $collapsed_track = $track_rs->next) { my $cdid = $collapsed_track->get_column('cd'); is($collapsed_track->get_column('track_count'), 3, "Correct count of tracks for CD $cdid" ); ok($collapsed_track->cd->title, "Prefetched title for CD $cdid" ); } - - is ($query_cnt, 1, 'Single query on prefetched titles'); - $schema->storage->debugcb (undef); - $schema->storage->debug ($sdebug); - } + }, 1, 'Single query on prefetched titles'); # Test sql by hand, as the sqlite db will simply paper over # improper group/select combinations @@ -76,9 +72,10 @@ for ($cd_rs->all) { WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) ) GROUP BY me.cd ) - count_subq + me )', - [ map { [ 'me.cd' => $_] } ($cd_rs->get_column ('cdid')->all) ], + [ map { [ { sqlt_datatype => 'integer', dbic_colname => 'me.cd' } + => $_ ] } @cdids ], 'count() query generated expected SQL', ); @@ -87,23 +84,24 @@ for ($cd_rs->all) { '( SELECT me.cd, me.track_count, cd.cdid, cd.artist, cd.title, cd.year, cd.genreid, cd.single_track FROM ( - SELECT me.cd, COUNT (me.trackid) AS track_count, + SELECT me.cd, COUNT (me.trackid) AS track_count FROM track me JOIN cd cd ON cd.cdid = me.cd WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) ) GROUP BY me.cd - ) as me + ) me JOIN cd cd ON cd.cdid = me.cd WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) ) )', - [ map { [ 'me.cd' => $_] } ( ($cd_rs->get_column ('cdid')->all) x 2 ) ], + [ map { [ { sqlt_datatype => 'integer', dbic_colname => 'me.cd' } + => $_ ] } (@cdids) x 2 ], 'next() query generated expected SQL', ); # add an extra track to one of the cds, and then make sure we can get it on top # (check if limit works) - my $top_cd = $cd_rs->slice (1,1)->next; + my $top_cd = $cd_rs->search({}, { order_by => 'cdid' })->slice (1,1)->next; $top_cd->create_related ('tracks', { title => 'over the top', }); @@ -148,14 +146,12 @@ for ($cd_rs->all) { FROM ( SELECT me.cdid FROM cd me - LEFT JOIN track tracks ON tracks.cd = me.cdid - LEFT JOIN liner_notes liner_notes ON liner_notes.liner_id = me.cdid WHERE ( me.cdid IS NOT NULL ) GROUP BY me.cdid - LIMIT 2 - ) count_subq + LIMIT ? + ) me )', - [], + [[$ROWS => 2]], 'count() query generated expected SQL', ); @@ -163,48 +159,101 @@ for ($cd_rs->all) { $most_tracks_rs->as_query, '( SELECT me.cdid, me.track_count, me.maxtr, - tracks.trackid, tracks.cd, tracks.position, tracks.title, tracks.last_updated_on, tracks.last_updated_at, tracks.small_dt, + tracks.trackid, tracks.cd, tracks.position, tracks.title, tracks.last_updated_on, tracks.last_updated_at, liner_notes.liner_id, liner_notes.notes FROM ( - SELECT me.cdid, COUNT( tracks.trackid ) AS track_count, MAX( tracks.trackid ) AS maxtr, + SELECT me.cdid, COUNT( tracks.trackid ) AS track_count, MAX( tracks.trackid ) AS maxtr FROM cd me LEFT JOIN track tracks ON tracks.cd = me.cdid WHERE ( me.cdid IS NOT NULL ) GROUP BY me.cdid ORDER BY track_count DESC, maxtr ASC - LIMIT 2 + LIMIT ? ) me LEFT JOIN track tracks ON tracks.cd = me.cdid LEFT JOIN liner_notes liner_notes ON liner_notes.liner_id = me.cdid WHERE ( me.cdid IS NOT NULL ) - ORDER BY track_count DESC, maxtr ASC, tracks.cd + ORDER BY track_count DESC, maxtr ASC )', - [], + [[$ROWS => 2]], 'next() query generated expected SQL', ); is ($most_tracks_rs->count, 2, 'Limit works'); - my $top_cd = $most_tracks_rs->first; + my ($top_cd) = $most_tracks_rs->all; is ($top_cd->id, 2, 'Correct cd fetched on top'); # 2 because of the slice(1,1) earlier - my $query_cnt = 0; - $schema->storage->debugcb ( sub { $query_cnt++ } ); - $schema->storage->debug (1); + $schema->is_executed_querycount( sub { + is ($top_cd->get_column ('track_count'), 4, 'Track count fetched correctly'); + is ($top_cd->tracks->count, 4, 'Count of prefetched tracks rs still correct'); + is ($top_cd->tracks->all, 4, 'Number of prefetched track objects still correct'); + is ( + $top_cd->liner_notes->notes, + 'Buy Whiskey!', + 'Correct liner pre-fetched with top cd', + ); + }, 0, 'No queries executed during prefetched data access'); +} - is ($top_cd->get_column ('track_count'), 4, 'Track count fetched correctly'); - is ($top_cd->tracks->count, 4, 'Count of prefetched tracks rs still correct'); - is ($top_cd->tracks->all, 4, 'Number of prefetched track objects still correct'); - is ( - $top_cd->liner_notes->notes, - 'Buy Whiskey!', - 'Correct liner pre-fetched with top cd', +{ + # test lifted from soulchild + + my $most_tracks_rs = $schema->resultset ('CD')->search ( + { + 'me.cdid' => { '!=' => undef }, # this is just to test WHERE + 'tracks.trackid' => { '!=' => undef }, + }, + { + join => 'tracks', + prefetch => 'liner_notes', + select => ['me.cdid', 'liner_notes.notes', { count => 'tracks.trackid', -as => 'tr_count' }, { max => 'tracks.trackid', -as => 'tr_maxid'} ], + as => [qw/cdid notes track_count max_track_id/], + order_by => [ { -desc => 'tr_count' }, { -asc => 'tr_maxid' } ], + group_by => 'me.cdid', + rows => 2, + } ); - is ($query_cnt, 0, 'No queries executed during prefetched data access'); - $schema->storage->debugcb (undef); - $schema->storage->debug ($sdebug); + is_same_sql_bind( + $most_tracks_rs->as_query, + '(SELECT me.cdid, liner_notes.notes, me.tr_count, me.tr_maxid, + liner_notes.liner_id, liner_notes.notes + FROM ( + SELECT me.cdid, COUNT(tracks.trackid) AS tr_count, MAX(tracks.trackid) AS tr_maxid + FROM cd me + LEFT JOIN track tracks + ON tracks.cd = me.cdid + WHERE me.cdid IS NOT NULL AND tracks.trackid IS NOT NULL + GROUP BY me.cdid + ORDER BY tr_count DESC, tr_maxid ASC + LIMIT ? + ) me + LEFT JOIN track tracks + ON tracks.cd = me.cdid + LEFT JOIN liner_notes liner_notes + ON liner_notes.liner_id = me.cdid + WHERE me.cdid IS NOT NULL AND tracks.trackid IS NOT NULL + ORDER BY tr_count DESC, tr_maxid ASC + )', + [[$ROWS => 2]], + 'Oddball mysql-ish group_by usage yields valid SQL', + ); + + is ($most_tracks_rs->count, 2, 'Limit works'); + my ($top_cd) = $most_tracks_rs->all; + is ($top_cd->id, 2, 'Correct cd fetched on top'); # 2 because of the slice(1,1) earlier + + $schema->is_executed_querycount( sub { + is ($top_cd->get_column ('track_count'), 4, 'Track count fetched correctly'); + is ( + $top_cd->liner_notes->notes, + 'Buy Whiskey!', + 'Correct liner pre-fetched with top cd', + ); + }, 0, 'No queries executed during prefetched data access'); } + # make sure that distinct still works { my $rs = $schema->resultset("CD")->search({}, { @@ -217,15 +266,14 @@ for ($cd_rs->all) { $rs->as_query, '( SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track, - tags.tagid, tags.cd, tags.tag + tags.tagid, tags.cd, tags.tag 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 - ORDER BY cdid ) me LEFT JOIN tags tags ON tags.cd = me.cdid - ORDER BY cdid, tags.cd, tags.tag + ORDER BY cdid )', [], 'Prefetch + distinct resulted in correct group_by', @@ -238,7 +286,7 @@ for ($cd_rs->all) { # RT 47779, test group_by as a scalar ref { my $track_rs = $schema->resultset ('Track')->search ( - { 'me.cd' => { -in => [ $cd_rs->get_column ('cdid')->all ] } }, + { 'me.cd' => { -in => \@cdids } }, { select => [ 'me.cd', @@ -264,9 +312,10 @@ for ($cd_rs->all) { WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) ) GROUP BY SUBSTR(me.cd, 1, 1) ) - count_subq + me )', - [ map { [ 'me.cd' => $_] } ($cd_rs->get_column ('cdid')->all) ], + [ map { [ { sqlt_datatype => 'integer', dbic_colname => 'me.cd' } + => $_ ] } (@cdids) ], 'count() query generated expected SQL', ); } @@ -280,6 +329,25 @@ for ($cd_rs->all) { is($cd_rs->count, 5, 'complex prefetch + non-prefetching has_many join count correct'); is($cd_rs->all, 5, 'complex prefetch + non-prefetching has_many join number of objects correct'); + # make sure join tracks was thrown out + is_same_sql_bind ( + $cd_rs->as_query, + '( + SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track, + artist.artistid, artist.name, artist.rank, artist.charfield + FROM ( + SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track + FROM cd me + JOIN artist artist ON artist.artistid = me.artist + GROUP BY me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track + ) me + JOIN artist artist ON artist.artistid = me.artist + )', + [], + ); + + + # try the same as above, but add a condition so the tracks join can not be thrown away my $cd_rs2 = $cd_rs->search ({ 'tracks.title' => { '!=' => 'ugabuganoexist' } }); is($cd_rs2->count, 5, 'complex prefetch + non-prefetching restricted has_many join count correct'); @@ -306,8 +374,124 @@ for ($cd_rs->all) { GROUP BY me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track, artist.artistid, artist.name, artist.rank, artist.charfield )', - [ map { [ 'tracks.title' => 'ugabuganoexist' ] } (1 .. 2) ], + [ map { [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'tracks.title' } + => 'ugabuganoexist' ] } (1,2) + ], ); } +# make sure distinct applies to the CD part only, not to the prefetched/collapsed order_by part +{ + my $rs = $schema->resultset('CD')->search({}, { + columns => [qw( cdid title )], + '+select' => [{ count => 'tags.tag' }], + '+as' => ['test_count'], + prefetch => ['tags'], + distinct => 1, + order_by => {'-desc' => 'tags.tag'}, + offset => 1, + rows => 3, + }); + + is_same_sql_bind($rs->as_query, + '( + SELECT me.cdid, me.title, me.test_count, + tags.tagid, tags.cd, tags.tag + FROM ( + SELECT me.cdid, me.title, + COUNT( tags.tag ) AS test_count + FROM cd me + LEFT JOIN tags tags + ON tags.cd = me.cdid + GROUP BY me.cdid, me.title + ORDER BY MAX( tags.tag ) DESC + LIMIT ? + OFFSET ? + ) me + LEFT JOIN tags tags + ON tags.cd = me.cdid + ORDER BY tags.tag DESC + )', + [ [$ROWS => 3], [$OFFSET => 1] ], + 'Expected limited prefetch with distinct SQL', + ); + + my $expected_hri = [ + { cdid => 4, test_count => 2, title => "Generic Manufactured Singles", tags => [ + { cd => 4, tag => "Shiny", tagid => 9 }, + { cd => 4, tag => "Cheesy", tagid => 6 }, + ]}, + { + cdid => 5, test_count => 2, title => "Come Be Depressed With Us", tags => [ + { cd => 5, tag => "Cheesy", tagid => 7 }, + { cd => 5, tag => "Blue", tagid => 4 }, + ]}, + { + cdid => 1, test_count => 1, title => "Spoonful of bees", tags => [ + { cd => 1, tag => "Blue", tagid => 1 }, + ]}, + ]; + + is_deeply ( + $rs->all_hri, + $expected_hri, + 'HRI dump of limited prefetch with distinct as expected' + ); + + # pre-multiplied main source also should work + $rs = $schema->resultset('CD')->search_related('artist')->search_related('cds', {}, { + columns => [qw( cdid title )], + '+select' => [{ count => 'tags.tag' }], + '+as' => ['test_count'], + prefetch => ['tags'], + distinct => 1, + order_by => {'-desc' => 'tags.tag'}, + offset => 1, + rows => 3, + }); + + is_same_sql_bind($rs->as_query, + '( + SELECT cds.cdid, cds.title, cds.test_count, + tags.tagid, tags.cd, tags.tag + FROM cd me + JOIN artist artist + ON artist.artistid = me.artist + JOIN ( + SELECT cds.cdid, cds.title, + COUNT( tags.tag ) AS test_count, + cds.artist + FROM cd me + JOIN artist artist + ON artist.artistid = me.artist + JOIN cd cds + ON cds.artist = artist.artistid + LEFT JOIN tags tags + ON tags.cd = cds.cdid + GROUP BY cds.cdid, cds.title, cds.artist + ORDER BY MAX( tags.tag ) DESC + LIMIT ? + OFFSET ? + ) cds + ON cds.artist = artist.artistid + LEFT JOIN tags tags + ON tags.cd = cds.cdid + ORDER BY tags.tag DESC + )', + [ [$ROWS => 3], [$OFFSET => 1] ], + 'Expected limited prefetch with distinct SQL on premultiplied head', + ); + + # Tag counts are multiplied by the cd->artist->cds multiplication + # I would *almost* call this "expected" without wraping an as_subselect_rs + { + local $TODO = 'Not sure if we can stop the count/group of premultiplication abstraction leak'; + is_deeply ( + $rs->all_hri, + $expected_hri, + 'HRI dump of limited prefetch with distinct as expected on premultiplid head' + ); + } +} + done_testing;