author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 09 Mar 2010 11:54:27 +0100 | |
changeset 4848 | 41f84eea63c9 |
parent 4721 | 8f63691ccb7f |
child 5004 | 4cc020ee70e2 |
child 5421 | 8167de96c523 |
permissions | -rw-r--r-- |
0 | 1 |
"""Functions to add additional annotations on a rql syntax tree to ease later |
2 |
code generation. |
|
3 |
||
4 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3815
diff
changeset
|
5 |
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 6 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
7 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 8 |
""" |
9 |
__docformat__ = "restructuredtext en" |
|
10 |
||
11 |
from logilab.common.compat import any |
|
12 |
||
3815
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3694
diff
changeset
|
13 |
from rql import BadRQLQuery |
1132 | 14 |
from rql.nodes import Relation, VariableRef, Constant, Variable, Or |
0 | 15 |
from rql.utils import common_parent |
16 |
||
17 |
def _annotate_select(annotator, rqlst): |
|
18 |
for subquery in rqlst.with_: |
|
19 |
annotator._annotate_union(subquery.query) |
|
20 |
#if server.DEBUG: |
|
21 |
# print '-------- sql annotate', repr(rqlst) |
|
22 |
getrschema = annotator.schema.rschema |
|
438 | 23 |
has_text_query = False |
0 | 24 |
need_distinct = rqlst.distinct |
25 |
for rel in rqlst.iget_nodes(Relation): |
|
4467
0e73d299730a
fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4287
diff
changeset
|
26 |
if getrschema(rel.r_type).symmetric and not rel.neged(strict=True): |
0 | 27 |
for vref in rel.iget_nodes(VariableRef): |
28 |
stinfo = vref.variable.stinfo |
|
29 |
if not stinfo['constnode'] and stinfo['selected']: |
|
30 |
need_distinct = True |
|
31 |
# XXX could mark as not invariant |
|
32 |
break |
|
4719
aaed3f813ef8
kill dead/useless code as suggested by pylint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4467
diff
changeset
|
33 |
for var in rqlst.defined_vars.itervalues(): |
0 | 34 |
stinfo = var.stinfo |
35 |
if stinfo.get('ftirels'): |
|
36 |
has_text_query = True |
|
37 |
if stinfo['attrvar']: |
|
38 |
stinfo['invariant'] = False |
|
39 |
stinfo['principal'] = _select_main_var(stinfo['rhsrelations']) |
|
40 |
continue |
|
41 |
if not stinfo['relations'] and not stinfo['typerels']: |
|
42 |
# Any X, Any MAX(X)... |
|
43 |
# those particular queries should be executed using the system |
|
44 |
# entities table unless there is some type restriction |
|
45 |
stinfo['invariant'] = True |
|
46 |
stinfo['principal'] = None |
|
47 |
continue |
|
48 |
if any(rel for rel in stinfo['relations'] if rel.r_type == 'eid' and rel.operator() != '=') and \ |
|
49 |
not any(r for r in var.stinfo['relations'] - var.stinfo['rhsrelations'] |
|
50 |
if r.r_type != 'eid' and (getrschema(r.r_type).inlined or getrschema(r.r_type).final)): |
|
51 |
# Any X WHERE X eid > 2 |
|
52 |
# those particular queries should be executed using the system entities table |
|
53 |
stinfo['invariant'] = True |
|
54 |
stinfo['principal'] = None |
|
55 |
continue |
|
56 |
if stinfo['selected'] and var.valuable_references() == 1+bool(stinfo['constnode']): |
|
57 |
# "Any X", "Any X, Y WHERE X attr Y" |
|
58 |
stinfo['invariant'] = False |
|
59 |
continue |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
60 |
joins = set() |
0 | 61 |
invariant = False |
62 |
for ref in var.references(): |
|
63 |
rel = ref.relation() |
|
64 |
if rel is None or rel.is_types_restriction(): |
|
65 |
continue |
|
66 |
lhs, rhs = rel.get_parts() |
|
67 |
onlhs = ref is lhs |
|
68 |
if rel.r_type == 'eid': |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
69 |
if not (onlhs and len(stinfo['relations']) > 1): |
0 | 70 |
break |
71 |
if not stinfo['constnode']: |
|
72 |
joins.add(rel) |
|
73 |
continue |
|
74 |
elif rel.r_type == 'identity': |
|
75 |
# identity can't be used as principal, so check other relation are used |
|
76 |
# XXX explain rhs.operator == '=' |
|
77 |
if rhs.operator != '=' or len(stinfo['relations']) <= 1: #(stinfo['constnode'] and rhs.operator == '='): |
|
78 |
break |
|
79 |
joins.add(rel) |
|
80 |
continue |
|
81 |
rschema = getrschema(rel.r_type) |
|
82 |
if rel.optional: |
|
83 |
if rel in stinfo['optrelations']: |
|
84 |
# optional variable can't be invariant if this is the lhs |
|
85 |
# variable of an inlined relation |
|
86 |
if not rel in stinfo['rhsrelations'] and rschema.inlined: |
|
87 |
break |
|
88 |
else: |
|
89 |
# variable used as main variable of an optional relation |
|
90 |
# can't be invariant |
|
91 |
break |
|
92 |
if rschema.final or (onlhs and rschema.inlined): |
|
93 |
if rschema.type != 'has_text': |
|
94 |
# need join anyway if the variable appears in a final or |
|
95 |
# inlined relation |
|
96 |
break |
|
97 |
joins.add(rel) |
|
98 |
continue |
|
99 |
if not stinfo['constnode']: |
|
100 |
if rschema.inlined and rel.neged(strict=True): |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
101 |
# if relation is inlined, can't be invariant if that |
0 | 102 |
# variable is used anywhere else. |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
103 |
# see 'Any P WHERE NOT N ecrit_par P, N eid 512': |
0 | 104 |
# sql for 'NOT N ecrit_par P' is 'N.ecrit_par is NULL' so P |
105 |
# can use N.ecrit_par as principal |
|
106 |
if (stinfo['selected'] or len(stinfo['relations']) > 1): |
|
107 |
break |
|
4467
0e73d299730a
fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4287
diff
changeset
|
108 |
elif rschema.symmetric and stinfo['selected']: |
0 | 109 |
break |
110 |
joins.add(rel) |
|
111 |
else: |
|
112 |
# if there is at least one ambigous relation and no other to |
|
113 |
# restrict types, can't be invariant since we need to filter out |
|
114 |
# other types |
|
115 |
if not annotator.is_ambiguous(var): |
|
116 |
invariant = True |
|
117 |
stinfo['invariant'] = invariant |
|
118 |
if invariant and joins: |
|
119 |
# remember rqlst/solutions analyze information |
|
120 |
# we have to select a kindof "main" relation which will "extrajoins" |
|
121 |
# the other |
|
122 |
# priority should be given to relation which are not in inner queries |
|
123 |
# (eg exists) |
|
124 |
try: |
|
125 |
stinfo['principal'] = _select_principal(var.sqlscope, joins) |
|
126 |
except CantSelectPrincipal: |
|
127 |
stinfo['invariant'] = False |
|
128 |
rqlst.need_distinct = need_distinct |
|
129 |
return has_text_query |
|
130 |
||
131 |
||
132 |
||
4721
8f63691ccb7f
pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
133 |
class CantSelectPrincipal(Exception): |
8f63691ccb7f
pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
134 |
"""raised when no 'principal' variable can be found""" |
0 | 135 |
|
599
9ef680acd92a
fix select principal so results are predictable during tests
Sylvain <syt@logilab.fr>
parents:
438
diff
changeset
|
136 |
def _select_principal(sqlscope, relations, _sort=lambda x:x): |
0 | 137 |
"""given a list of rqlst relations, select one which will be used to |
138 |
represent an invariant variable (e.g. using on extremity of the relation |
|
139 |
instead of the variable's type table |
|
140 |
""" |
|
599
9ef680acd92a
fix select principal so results are predictable during tests
Sylvain <syt@logilab.fr>
parents:
438
diff
changeset
|
141 |
# _sort argument is there for test |
0 | 142 |
diffscope_rels = {} |
143 |
ored_rels = set() |
|
144 |
diffscope_rels = set() |
|
599
9ef680acd92a
fix select principal so results are predictable during tests
Sylvain <syt@logilab.fr>
parents:
438
diff
changeset
|
145 |
for rel in _sort(relations): |
0 | 146 |
# note: only eid and has_text among all final relations may be there |
147 |
if rel.r_type in ('eid', 'identity'): |
|
148 |
continue |
|
149 |
if rel.ored(traverse_scope=True): |
|
150 |
ored_rels.add(rel) |
|
151 |
elif rel.sqlscope is sqlscope: |
|
152 |
return rel |
|
153 |
elif not rel.neged(traverse_scope=True): |
|
154 |
diffscope_rels.add(rel) |
|
155 |
if len(ored_rels) > 1: |
|
156 |
ored_rels_copy = tuple(ored_rels) |
|
157 |
for rel1 in ored_rels_copy: |
|
158 |
for rel2 in ored_rels_copy: |
|
159 |
if rel1 is rel2: |
|
160 |
continue |
|
161 |
if isinstance(common_parent(rel1, rel2), Or): |
|
162 |
ored_rels.discard(rel1) |
|
163 |
ored_rels.discard(rel2) |
|
599
9ef680acd92a
fix select principal so results are predictable during tests
Sylvain <syt@logilab.fr>
parents:
438
diff
changeset
|
164 |
for rel in _sort(ored_rels): |
0 | 165 |
if rel.sqlscope is sqlscope: |
166 |
return rel |
|
167 |
diffscope_rels.add(rel) |
|
168 |
# if DISTINCT query, can use variable from a different scope as principal |
|
169 |
# since introduced duplicates will be removed |
|
170 |
if sqlscope.stmt.distinct and diffscope_rels: |
|
599
9ef680acd92a
fix select principal so results are predictable during tests
Sylvain <syt@logilab.fr>
parents:
438
diff
changeset
|
171 |
return iter(_sort(diffscope_rels)).next() |
0 | 172 |
# XXX could use a relation for a different scope if it can't generate |
173 |
# duplicates, so we would have to check cardinality |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
174 |
raise CantSelectPrincipal() |
0 | 175 |
|
176 |
def _select_main_var(relations): |
|
177 |
"""given a list of rqlst relations, select one which will be used as main |
|
178 |
relation for the rhs variable |
|
179 |
""" |
|
3815
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3694
diff
changeset
|
180 |
principal = None |
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3694
diff
changeset
|
181 |
# sort for test predictability |
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3694
diff
changeset
|
182 |
for rel in sorted(relations, key=lambda x: (x.children[0].name, x.r_type)): |
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3694
diff
changeset
|
183 |
# only equality relation with a variable as rhs may be principal |
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3694
diff
changeset
|
184 |
if rel.operator() not in ('=', 'IS') \ |
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3694
diff
changeset
|
185 |
or not isinstance(rel.children[1].children[0], VariableRef): |
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3694
diff
changeset
|
186 |
continue |
0 | 187 |
if rel.sqlscope is rel.stmt: |
188 |
return rel |
|
189 |
principal = rel |
|
3815
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3694
diff
changeset
|
190 |
if principal is None: |
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3694
diff
changeset
|
191 |
raise BadRQLQuery('unable to find principal in %s' % ', '.join( |
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3694
diff
changeset
|
192 |
r.as_string() for r in relations)) |
0 | 193 |
return principal |
194 |
||
195 |
||
438 | 196 |
def set_qdata(getrschema, union, noinvariant): |
0 | 197 |
"""recursive function to set querier data on variables in the syntax tree |
198 |
""" |
|
199 |
for select in union.children: |
|
200 |
for subquery in select.with_: |
|
438 | 201 |
set_qdata(getrschema, subquery.query, noinvariant) |
0 | 202 |
for var in select.defined_vars.itervalues(): |
203 |
if var.stinfo['invariant']: |
|
204 |
if var in noinvariant and not var.stinfo['principal'].r_type == 'has_text': |
|
205 |
var._q_invariant = False |
|
206 |
else: |
|
207 |
var._q_invariant = True |
|
208 |
else: |
|
209 |
var._q_invariant = False |
|
438 | 210 |
for rel in select.iget_nodes(Relation): |
211 |
if rel.neged(strict=True) and not rel.is_types_restriction(): |
|
212 |
rschema = getrschema(rel.r_type) |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3249
diff
changeset
|
213 |
if not rschema.final: |
438 | 214 |
# if one of the relation's variable is ambiguous but not |
215 |
# invariant, an intersection will be necessary |
|
216 |
for vref in rel.get_nodes(VariableRef): |
|
217 |
var = vref.variable |
|
218 |
if (not var._q_invariant and var.valuable_references() == 1 |
|
219 |
and len(var.stinfo['possibletypes']) > 1): |
|
220 |
select.need_intersect = True |
|
221 |
break |
|
222 |
else: |
|
223 |
continue |
|
224 |
break |
|
225 |
else: |
|
226 |
select.need_intersect = False |
|
0 | 227 |
|
228 |
||
229 |
class SQLGenAnnotator(object): |
|
230 |
def __init__(self, schema): |
|
231 |
self.schema = schema |
|
232 |
self.nfdomain = frozenset(eschema.type for eschema in schema.entities() |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3249
diff
changeset
|
233 |
if not eschema.final) |
0 | 234 |
|
235 |
def annotate(self, rqlst): |
|
236 |
"""add information to the rql syntax tree to help sources to do their |
|
237 |
job (read sql generation) |
|
238 |
||
239 |
a variable is tagged as invariant if: |
|
240 |
* it's a non final variable |
|
241 |
* it's not used as lhs in any final or inlined relation |
|
242 |
* there is no type restriction on this variable (either explicit in the |
|
243 |
syntax tree or because a solution for this variable has been removed |
|
244 |
due to security filtering) |
|
245 |
""" |
|
3694 | 246 |
#assert rqlst.TYPE == 'select', rqlst |
0 | 247 |
rqlst.has_text_query = self._annotate_union(rqlst) |
248 |
||
249 |
def _annotate_union(self, union): |
|
250 |
has_text_query = False |
|
251 |
for select in union.children: |
|
252 |
htq = _annotate_select(self, select) |
|
253 |
if htq: |
|
254 |
has_text_query = True |
|
255 |
return has_text_query |
|
256 |
||
257 |
def is_ambiguous(self, var): |
|
258 |
# ignore has_text relation |
|
259 |
if len([rel for rel in var.stinfo['relations'] |
|
260 |
if rel.sqlscope is var.sqlscope and rel.r_type == 'has_text']) == 1: |
|
261 |
return False |
|
262 |
try: |
|
263 |
data = var.stmt._deamb_data |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
264 |
except AttributeError: |
0 | 265 |
data = var.stmt._deamb_data = IsAmbData(self.schema, self.nfdomain) |
266 |
data.compute(var.stmt) |
|
267 |
return data.is_ambiguous(var) |
|
268 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
269 |
|
0 | 270 |
class IsAmbData(object): |
271 |
def __init__(self, schema, nfdomain): |
|
272 |
self.schema = schema |
|
273 |
# shortcuts |
|
274 |
self.rschema = schema.rschema |
|
275 |
self.eschema = schema.eschema |
|
276 |
# domain for non final variables |
|
277 |
self.nfdomain = nfdomain |
|
278 |
# {var: possible solutions set} |
|
279 |
self.varsols = {} |
|
280 |
# set of ambiguous variables |
|
281 |
self.ambiguousvars = set() |
|
282 |
# remember if a variable has been deambiguified by another to avoid |
|
283 |
# doing the opposite |
|
284 |
self.deambification_map = {} |
|
285 |
# not invariant variables (access to final.inlined relation) |
|
286 |
self.not_invariants = set() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
287 |
|
0 | 288 |
def is_ambiguous(self, var): |
289 |
return var in self.ambiguousvars |
|
290 |
||
291 |
def restrict(self, var, restricted_domain): |
|
292 |
self.varsols[var] &= restricted_domain |
|
293 |
if var in self.ambiguousvars and self.varsols[var] == var.stinfo['possibletypes']: |
|
294 |
self.ambiguousvars.remove(var) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
295 |
|
0 | 296 |
def compute(self, rqlst): |
297 |
# set domains for each variable |
|
298 |
for varname, var in rqlst.defined_vars.iteritems(): |
|
299 |
if var.stinfo['uidrels'] or \ |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3249
diff
changeset
|
300 |
self.eschema(rqlst.solutions[0][varname]).final: |
0 | 301 |
ptypes = var.stinfo['possibletypes'] |
302 |
else: |
|
303 |
ptypes = set(self.nfdomain) |
|
304 |
self.ambiguousvars.add(var) |
|
305 |
self.varsols[var] = ptypes |
|
306 |
if not self.ambiguousvars: |
|
307 |
return |
|
308 |
# apply relation restriction |
|
309 |
self.maydeambrels = maydeambrels = {} |
|
310 |
for rel in rqlst.iget_nodes(Relation): |
|
4287 | 311 |
if rel.r_type == 'eid' or rel.is_types_restriction(): |
0 | 312 |
continue |
313 |
lhs, rhs = rel.get_variable_parts() |
|
314 |
if isinstance(lhs, VariableRef) or isinstance(rhs, VariableRef): |
|
315 |
rschema = self.rschema(rel.r_type) |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3249
diff
changeset
|
316 |
if rschema.inlined or rschema.final: |
0 | 317 |
self.not_invariants.add(lhs.variable) |
318 |
self.set_rel_constraint(lhs, rel, rschema.subjects) |
|
319 |
self.set_rel_constraint(rhs, rel, rschema.objects) |
|
320 |
# try to deambiguify more variables by considering other variables'type |
|
321 |
modified = True |
|
322 |
while modified and self.ambiguousvars: |
|
323 |
modified = False |
|
324 |
for var in self.ambiguousvars.copy(): |
|
325 |
try: |
|
326 |
for rel in (var.stinfo['relations'] & maydeambrels[var]): |
|
327 |
if self.deambiguifying_relation(var, rel): |
|
328 |
modified = True |
|
329 |
break |
|
330 |
except KeyError: |
|
331 |
# no relation to deambiguify |
|
332 |
continue |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
333 |
|
0 | 334 |
def _debug_print(self): |
335 |
print 'varsols', dict((x, sorted(str(v) for v in values)) |
|
336 |
for x, values in self.varsols.iteritems()) |
|
337 |
print 'ambiguous vars', sorted(self.ambiguousvars) |
|
338 |
||
339 |
def set_rel_constraint(self, term, rel, etypes_func): |
|
340 |
if isinstance(term, VariableRef) and self.is_ambiguous(term.variable): |
|
341 |
var = term.variable |
|
342 |
if len(var.stinfo['relations'] - var.stinfo['typerels']) == 1 \ |
|
3249
280080eadb22
fix rqlannotation bug w/ identity relation in subscope
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2353
diff
changeset
|
343 |
or rel.sqlscope is var.sqlscope or rel.r_type == 'identity': |
0 | 344 |
self.restrict(var, frozenset(etypes_func())) |
345 |
try: |
|
346 |
self.maydeambrels[var].add(rel) |
|
347 |
except KeyError: |
|
348 |
self.maydeambrels[var] = set((rel,)) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
349 |
|
0 | 350 |
def deambiguifying_relation(self, var, rel): |
351 |
lhs, rhs = rel.get_variable_parts() |
|
352 |
onlhs = var is getattr(lhs, 'variable', None) |
|
353 |
other = onlhs and rhs or lhs |
|
354 |
otheretypes = None |
|
355 |
# XXX isinstance(other.variable, Variable) to skip column alias |
|
356 |
if isinstance(other, VariableRef) and isinstance(other.variable, Variable): |
|
357 |
deambiguifier = other.variable |
|
358 |
if not var is self.deambification_map.get(deambiguifier): |
|
359 |
if not var.stinfo['typerels']: |
|
360 |
otheretypes = deambiguifier.stinfo['possibletypes'] |
|
361 |
elif not self.is_ambiguous(deambiguifier): |
|
362 |
otheretypes = self.varsols[deambiguifier] |
|
363 |
elif deambiguifier in self.not_invariants: |
|
364 |
# we know variable won't be invariant, try to use |
|
365 |
# it to deambguify the current variable |
|
366 |
otheretypes = self.varsols[deambiguifier] |
|
4285
ea590101691c
don't record deambiguifier when it has no type restriction using 'is', so we don't miss later some available constraints
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
367 |
if not deambiguifier.stinfo['typerels']: |
ea590101691c
don't record deambiguifier when it has no type restriction using 'is', so we don't miss later some available constraints
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
368 |
# if deambiguifier has no type restriction using 'is', |
ea590101691c
don't record deambiguifier when it has no type restriction using 'is', so we don't miss later some available constraints
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
369 |
# don't record it |
ea590101691c
don't record deambiguifier when it has no type restriction using 'is', so we don't miss later some available constraints
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
370 |
deambiguifier = None |
0 | 371 |
elif isinstance(other, Constant) and other.uidtype: |
372 |
otheretypes = (other.uidtype,) |
|
373 |
deambiguifier = None |
|
374 |
if otheretypes is not None: |
|
967 | 375 |
# to restrict, we must check that for all type in othertypes, |
376 |
# possible types on the other end of the relation are matching |
|
377 |
# variable's possible types |
|
0 | 378 |
rschema = self.rschema(rel.r_type) |
379 |
if onlhs: |
|
380 |
rtypefunc = rschema.subjects |
|
381 |
else: |
|
382 |
rtypefunc = rschema.objects |
|
383 |
for otheretype in otheretypes: |
|
384 |
reltypes = frozenset(rtypefunc(otheretype)) |
|
385 |
if var.stinfo['possibletypes'] != reltypes: |
|
4287 | 386 |
return False |
387 |
self.restrict(var, var.stinfo['possibletypes']) |
|
388 |
self.deambification_map[var] = deambiguifier |
|
389 |
return True |
|
0 | 390 |
return False |