8683b160ec0fc5dcca4e0aaf44ada0258f3962ca
[dbsrgits/DBIx-Class.git] / t / prefetch / rows_bug.t
1 # Test to ensure we get a consistent result set wether or not we use the
2 # prefetch option in combination rows (LIMIT).
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use lib qw(t/lib);
8 use DBICTest;
9
10 plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 2);
11
12 my $schema = DBICTest->init_schema();
13 my $no_prefetch = $schema->resultset('Artist')->search(
14   undef,
15   { rows => 3 }
16 );
17
18 my $use_prefetch = $schema->resultset('Artist')->search(
19   undef,
20   {
21     prefetch => 'cds',
22     rows     => 3
23   }
24 );
25
26 my $no_prefetch_count  = 0;
27 my $use_prefetch_count = 0;
28
29 is($no_prefetch->count, $use_prefetch->count, '$no_prefetch->count == $use_prefetch->count');
30
31 TODO: {
32   local $TODO = "This is a difficult bug to fix, workaround is not to use prefetch with rows";
33   $no_prefetch_count++  while $no_prefetch->next;
34   $use_prefetch_count++ while $use_prefetch->next;
35   is(
36     $no_prefetch_count,
37     $use_prefetch_count,
38     "manual row count confirms consistency"
39     . " (\$no_prefetch_count == $no_prefetch_count, "
40     . " \$use_prefetch_count == $use_prefetch_count)"
41   );
42 }
43 __END__
44 my $rs1 = $schema->resultset('Artist')->search(
45     undef, {
46 #        join => 'cds',
47     },
48 );
49 warn ${$rs1->as_query}->[0], $/;
50 { my @x = $rs1->all; warn "$#x\n"; }
51
52 my $rs2 = $schema->resultset('Artist')->search(
53     undef, {
54         from => [{
55             me => $rs1->as_query,
56         }],
57         prefetch => 'cds',
58     },
59 );
60
61 warn ${$rs2->as_query}->[0], $/;
62 { my @x = $rs2->all; warn "$#x\n"; }
63
64 __END__
65 The fix is to, when using prefetch, take the query and put it into a subquery
66 joined to the tables we're prefetching from. This might result in the same
67 table being joined once in the main subquery and once in the main query. This
68 may actually resolve other, unknown edgecase bugs. It is also the right way
69 to do prefetching. Optimizations can come later.
70
71 This means that:
72   $foo_rs->search(
73     { ... },
74     {
75       prefetch => 'bar',
76       ...
77     },
78   );
79
80 becomes:
81   my $temp = $foo_rs->search(
82     { ... },
83     {
84       join => 'bar',
85       ...
86     },
87   );
88   $foo_rs->storage->schema->resultset('foo')->search(
89     undef,
90     {
91       from => [
92         { me => $temp->as_query },
93       ],
94       prefetch => 'bar',
95     },
96   );
97
98 Problem:
99   * The prefetch->join change needs to happen ONLY IF there are conditions
100     that depend on bar being joined.
101   * How will this work when the $rs is further searched on? Those clauses
102     need to be added to the subquery, not the outer one. This is particularly
103     true if rows is added in the attribute later per the Pager.