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