Only add a group_by to a complex prefetch if a non-selecting multi-join is detected
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBIHacks.pm
index db9bc27..41ca371 100644 (file)
@@ -115,12 +115,19 @@ sub _adjust_select_args_for_complex_prefetch {
       group_by => ['dummy'], %$inner_attrs,
     });
 
-    # if a multi-type join was needed in the subquery - add a group_by to simulate the
-    # collapse in the subq
+    my $inner_aliastypes =
+      $self->_resolve_aliastypes_from_select_args( $inner_from, $inner_select, $where, $inner_attrs );
+
+    # if a multi-type non-selecting (only restricting) join was needed in the subquery
+    # add a group_by to simulate the collapse in the subq
     if (
       ! $inner_attrs->{group_by}
         and
-      first { ! $_->[0]{-is_single} } (@{$inner_from}[1 .. $#$inner_from])
+      first {
+        $inner_aliastypes->{restricting}{$_}
+          and
+        ! $inner_aliastypes->{selecting}{$_}
+      } ( keys %{$inner_aliastypes->{multiplying}||{}} )
     ) {
       $inner_attrs->{group_by} = $self->_group_over_selection (
         $inner_from, $inner_select, $inner_attrs->{order_by}
@@ -247,7 +254,9 @@ sub _resolve_aliastypes_from_select_args {
   # set up a botched SQLA
   my $sql_maker = $self->sql_maker;
 
-  local $sql_maker->{having_bind};  # these are throw away results
+  # these are throw away results, do not pollute the bind stack
+  local $sql_maker->{having_bind};
+  local $sql_maker->{select_bind};
 
   # we can't scan properly without any quoting (\b doesn't cut it
   # everywhere), so unless there is proper quoting set - use our