# HG changeset patch
# User Sylvain Thénault <sylvain.thenault@logilab.fr>
# Date 1280745126 -7200
#      Mon Aug 02 12:32:06 2010 +0200
# Branch stable
# Node ID 7b8e124f230c6bc0e43e0f93e8f312ed0a592b9f
# Parent  5a8ae1ed1c84006e5e6c628e163e3d68af1164c2
fix solutions computation crash with some query using sub-queries (closes #37423)

diff --git a/ChangeLog b/ChangeLog
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,9 @@
 ChangeLog for RQL
 =================
 
+	--
+   * fix solutions computation crash with some query using sub-queries (closes #37423)
+
 2010-07-28  --  0.26.4
     * fix re-annotation pb: some stinfo keys were not properly reinitialized
       which may cause pb later (at sql generation time for instance)
diff --git a/analyze.py b/analyze.py
--- a/analyze.py
+++ b/analyze.py
@@ -377,8 +377,8 @@
                     alltypes.add(targettypes)
         else:
             alltypes = get_target_types()
-
-        constraints.var_has_types( var, [ str(t) for t in alltypes] )
+        domain = constraints.domains[var]
+        constraints.var_has_types( var, [str(t) for t in alltypes if t in domain] )
 
     def visit(self, node, uid_func_mapping=None, kwargs=None, debug=False):
         # FIXME: not thread safe
@@ -509,18 +509,26 @@
                         samevar = True
                     else:
                         rhsvars.append(v.name)
+            lhsdomain = constraints.domains[lhsvar]
             if rhsvars:
                 s2 = '=='.join(rhsvars)
+                # filter according to domain necessary for column aliases
+                rhsdomain = constraints.domains[rhsvars[0]]
                 res = []
                 for fromtype, totypes in rschema.associations():
-                    res.append( [ ( [lhsvar], [str(fromtype)]), (rhsvars, [ str(t) for t in totypes]) ] )
+                    if not fromtype in lhsdomain:
+                        continue
+                    ptypes = [str(t) for t in totypes if t in rhsdomain]
+                    res.append( [ ( [lhsvar], [str(fromtype)]), (rhsvars, ptypes) ] )
                 constraints.or_and( res )
             else:
-                constraints.var_has_types( lhsvar, [ str(subj) for subj in rschema.subjects()] )
+                ptypes = [str(subj) for subj in rschema.subjects()
+                          if subj in lhsdomain]
+                constraints.var_has_types( lhsvar, ptypes )
             if samevar:
                 res = []
                 for fromtype, totypes in rschema.associations():
-                    if not fromtype in totypes:
+                    if not (fromtype in totypes and fromtype in lhsdomain):
                         continue
                     res.append(str(fromtype))
                 constraints.var_has_types( lhsvar, res )