Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Cleaned up join promotion in query.combine() with OR

Refs #19849
  • Loading branch information...
commit 10f9ba046f6122414fe5501934b6e080c3554a85 1 parent edf9312
Anssi Kääriäinen authored February 19, 2013

Showing 1 changed file with 7 additions and 23 deletions. Show diff stats Hide diff stats

  1. 30  django/db/models/sql/query.py
30  django/db/models/sql/query.py
@@ -524,31 +524,15 @@ def combine(self, rhs, connector):
524 524
                 # the join type for the unused alias.
525 525
                 self.unref_alias(new_alias)
526 526
 
527  
-        # So that we don't exclude valid results in an "or" query combination,
  527
+        # So that we don't exclude valid results in an OR query combination,
528 528
         # all joins exclusive to either the lhs or the rhs must be converted
529  
-        # to an outer join.
  529
+        # to an outer join. RHS joins were already set to outer joins above,
  530
+        # so check which joins were used only in the lhs query.
530 531
         if not conjunction:
531  
-            l_tables = set(self.tables)
532  
-            r_tables = set(rhs.tables)
533  
-            # Update r_tables aliases.
534  
-            for alias in change_map:
535  
-                if alias in r_tables:
536  
-                    # r_tables may contain entries that have a refcount of 0
537  
-                    # if the query has references to a table that can be
538  
-                    # trimmed because only the foreign key is used.
539  
-                    # We only need to fix the aliases for the tables that
540  
-                    # actually have aliases.
541  
-                    if rhs.alias_refcount[alias]:
542  
-                        r_tables.remove(alias)
543  
-                        r_tables.add(change_map[alias])
544  
-            # Find aliases that are exclusive to rhs or lhs.
545  
-            # These are promoted to outer joins.
546  
-            outer_tables = (l_tables | r_tables) - (l_tables & r_tables)
547  
-            for alias in outer_tables:
548  
-                # Again, some of the tables won't have aliases due to
549  
-                # the trimming of unnecessary tables.
550  
-                if self.alias_refcount.get(alias) or rhs.alias_refcount.get(alias):
551  
-                    self.promote_joins([alias], True)
  532
+            rhs_used_joins = set(change_map.values())
  533
+            to_promote = [alias for alias in self.tables
  534
+                          if alias not in rhs_used_joins]
  535
+            self.promote_joins(to_promote, True)
552 536
 
553 537
         # Now relabel a copy of the rhs where-clause and add it to the current
554 538
         # one.

0 notes on commit 10f9ba0

Please sign in to comment.
Something went wrong with that request. Please try again.