author | Nicolas Chauvat <nicolas.chauvat@logilab.fr> |
Sat, 18 Jun 2011 23:27:23 +0200 | |
branch | stable |
changeset 7537 | 1af162bd78b8 |
parent 7523 | f6856231cc51 |
child 7651 | 7c0af7ef3325 |
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 |
|
7357
5ad3154a8810
[rql2sql] fix bug avoiding outer join relation to be used as a variable principal. Closes #1659395
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7193
diff
changeset
|
81 |
role = 'subject' if onlhs else 'object' |
0 | 82 |
if rel.r_type == 'eid': |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
83 |
if not (onlhs and len(stinfo['relations']) > 1): |
0 | 84 |
break |
85 |
if not stinfo['constnode']: |
|
7357
5ad3154a8810
[rql2sql] fix bug avoiding outer join relation to be used as a variable principal. Closes #1659395
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7193
diff
changeset
|
86 |
joins.add( (rel, role) ) |
0 | 87 |
continue |
88 |
elif rel.r_type == 'identity': |
|
89 |
# identity can't be used as principal, so check other relation are used |
|
90 |
# XXX explain rhs.operator == '=' |
|
91 |
if rhs.operator != '=' or len(stinfo['relations']) <= 1: #(stinfo['constnode'] and rhs.operator == '='): |
|
92 |
break |
|
7357
5ad3154a8810
[rql2sql] fix bug avoiding outer join relation to be used as a variable principal. Closes #1659395
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7193
diff
changeset
|
93 |
joins.add( (rel, role) ) |
0 | 94 |
continue |
95 |
rschema = getrschema(rel.r_type) |
|
96 |
if rel.optional: |
|
5004
4cc020ee70e2
le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
97 |
if rel in stinfo.get('optrelations', ()): |
0 | 98 |
# optional variable can't be invariant if this is the lhs |
99 |
# variable of an inlined relation |
|
100 |
if not rel in stinfo['rhsrelations'] and rschema.inlined: |
|
101 |
break |
|
7193
7eaef037ea9d
[sql generation] fix missing join with outer joins w/ rhs invariant variable...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7041
diff
changeset
|
102 |
# variable used as main variable of an optional relation can't |
7eaef037ea9d
[sql generation] fix missing join with outer joins w/ rhs invariant variable...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7041
diff
changeset
|
103 |
# be invariant, unless we can use some other relation as |
7eaef037ea9d
[sql generation] fix missing join with outer joins w/ rhs invariant variable...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7041
diff
changeset
|
104 |
# reference for the outer join |
7eaef037ea9d
[sql generation] fix missing join with outer joins w/ rhs invariant variable...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7041
diff
changeset
|
105 |
elif not stinfo['constnode']: |
0 | 106 |
break |
7193
7eaef037ea9d
[sql generation] fix missing join with outer joins w/ rhs invariant variable...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7041
diff
changeset
|
107 |
elif len(stinfo['relations']) == 2: |
7eaef037ea9d
[sql generation] fix missing join with outer joins w/ rhs invariant variable...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7041
diff
changeset
|
108 |
if onlhs: |
7eaef037ea9d
[sql generation] fix missing join with outer joins w/ rhs invariant variable...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7041
diff
changeset
|
109 |
ostinfo = rhs.children[0].variable.stinfo |
7eaef037ea9d
[sql generation] fix missing join with outer joins w/ rhs invariant variable...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7041
diff
changeset
|
110 |
else: |
7eaef037ea9d
[sql generation] fix missing join with outer joins w/ rhs invariant variable...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7041
diff
changeset
|
111 |
ostinfo = lhs.variable.stinfo |
7eaef037ea9d
[sql generation] fix missing join with outer joins w/ rhs invariant variable...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7041
diff
changeset
|
112 |
if not any(orel for orel in ostinfo['relations'] |
7eaef037ea9d
[sql generation] fix missing join with outer joins w/ rhs invariant variable...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7041
diff
changeset
|
113 |
if orel.optional and orel is not rel): |
7eaef037ea9d
[sql generation] fix missing join with outer joins w/ rhs invariant variable...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7041
diff
changeset
|
114 |
break |
0 | 115 |
if rschema.final or (onlhs and rschema.inlined): |
116 |
if rschema.type != 'has_text': |
|
117 |
# need join anyway if the variable appears in a final or |
|
118 |
# inlined relation |
|
119 |
break |
|
7357
5ad3154a8810
[rql2sql] fix bug avoiding outer join relation to be used as a variable principal. Closes #1659395
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7193
diff
changeset
|
120 |
joins.add( (rel, role) ) |
0 | 121 |
continue |
122 |
if not stinfo['constnode']: |
|
123 |
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
|
124 |
# if relation is inlined, can't be invariant if that |
0 | 125 |
# variable is used anywhere else. |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
126 |
# see 'Any P WHERE NOT N ecrit_par P, N eid 512': |
0 | 127 |
# sql for 'NOT N ecrit_par P' is 'N.ecrit_par is NULL' so P |
128 |
# can use N.ecrit_par as principal |
|
129 |
if (stinfo['selected'] or len(stinfo['relations']) > 1): |
|
130 |
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
|
131 |
elif rschema.symmetric and stinfo['selected']: |
0 | 132 |
break |
7357
5ad3154a8810
[rql2sql] fix bug avoiding outer join relation to be used as a variable principal. Closes #1659395
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7193
diff
changeset
|
133 |
joins.add( (rel, role) ) |
0 | 134 |
else: |
135 |
# if there is at least one ambigous relation and no other to |
|
136 |
# restrict types, can't be invariant since we need to filter out |
|
137 |
# other types |
|
138 |
if not annotator.is_ambiguous(var): |
|
139 |
invariant = True |
|
140 |
stinfo['invariant'] = invariant |
|
141 |
if invariant and joins: |
|
142 |
# remember rqlst/solutions analyze information |
|
143 |
# we have to select a kindof "main" relation which will "extrajoins" |
|
144 |
# the other |
|
145 |
# priority should be given to relation which are not in inner queries |
|
146 |
# (eg exists) |
|
147 |
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
|
148 |
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
|
149 |
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
|
150 |
# 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
|
151 |
# 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
|
152 |
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
|
153 |
sstinfo['scope'] = common_parent(sstinfo['scope'], stinfo['scope']).scope |
0 | 154 |
except CantSelectPrincipal: |
155 |
stinfo['invariant'] = False |
|
156 |
rqlst.need_distinct = need_distinct |
|
157 |
return has_text_query |
|
158 |
||
159 |
||
160 |
||
4721
8f63691ccb7f
pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
161 |
class CantSelectPrincipal(Exception): |
8f63691ccb7f
pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
162 |
"""raised when no 'principal' variable can be found""" |
0 | 163 |
|
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
|
164 |
def _select_principal(scope, relations, _sort=lambda x:x): |
0 | 165 |
"""given a list of rqlst relations, select one which will be used to |
166 |
represent an invariant variable (e.g. using on extremity of the relation |
|
167 |
instead of the variable's type table |
|
168 |
""" |
|
599
9ef680acd92a
fix select principal so results are predictable during tests
Sylvain <syt@logilab.fr>
parents:
438
diff
changeset
|
169 |
# _sort argument is there for test |
0 | 170 |
diffscope_rels = {} |
171 |
ored_rels = set() |
|
172 |
diffscope_rels = set() |
|
7357
5ad3154a8810
[rql2sql] fix bug avoiding outer join relation to be used as a variable principal. Closes #1659395
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7193
diff
changeset
|
173 |
for rel, role in _sort(relations): |
0 | 174 |
# note: only eid and has_text among all final relations may be there |
175 |
if rel.r_type in ('eid', 'identity'): |
|
176 |
continue |
|
7357
5ad3154a8810
[rql2sql] fix bug avoiding outer join relation to be used as a variable principal. Closes #1659395
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7193
diff
changeset
|
177 |
if rel.optional is not None and len(relations) > 1: |
5ad3154a8810
[rql2sql] fix bug avoiding outer join relation to be used as a variable principal. Closes #1659395
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7193
diff
changeset
|
178 |
if role == 'subject' and rel.optional == 'right': |
5ad3154a8810
[rql2sql] fix bug avoiding outer join relation to be used as a variable principal. Closes #1659395
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7193
diff
changeset
|
179 |
continue |
5ad3154a8810
[rql2sql] fix bug avoiding outer join relation to be used as a variable principal. Closes #1659395
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7193
diff
changeset
|
180 |
if role == 'object' and rel.optional == 'left': |
5ad3154a8810
[rql2sql] fix bug avoiding outer join relation to be used as a variable principal. Closes #1659395
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7193
diff
changeset
|
181 |
continue |
0 | 182 |
if rel.ored(traverse_scope=True): |
183 |
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
|
184 |
elif rel.scope is scope: |
0 | 185 |
return rel |
186 |
elif not rel.neged(traverse_scope=True): |
|
187 |
diffscope_rels.add(rel) |
|
188 |
if len(ored_rels) > 1: |
|
189 |
ored_rels_copy = tuple(ored_rels) |
|
190 |
for rel1 in ored_rels_copy: |
|
191 |
for rel2 in ored_rels_copy: |
|
192 |
if rel1 is rel2: |
|
193 |
continue |
|
194 |
if isinstance(common_parent(rel1, rel2), Or): |
|
195 |
ored_rels.discard(rel1) |
|
196 |
ored_rels.discard(rel2) |
|
599
9ef680acd92a
fix select principal so results are predictable during tests
Sylvain <syt@logilab.fr>
parents:
438
diff
changeset
|
197 |
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
|
198 |
if rel.scope is scope: |
0 | 199 |
return rel |
200 |
diffscope_rels.add(rel) |
|
201 |
# if DISTINCT query, can use variable from a different scope as principal |
|
202 |
# 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
|
203 |
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
|
204 |
return iter(_sort(diffscope_rels)).next() |
0 | 205 |
# XXX could use a relation for a different scope if it can't generate |
206 |
# 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
|
207 |
raise CantSelectPrincipal() |
0 | 208 |
|
209 |
def _select_main_var(relations): |
|
210 |
"""given a list of rqlst relations, select one which will be used as main |
|
211 |
relation for the rhs variable |
|
212 |
""" |
|
3815
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3694
diff
changeset
|
213 |
principal = None |
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3694
diff
changeset
|
214 |
# 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
|
215 |
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
|
216 |
# 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
|
217 |
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
|
218 |
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
|
219 |
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
|
220 |
if rel.scope is rel.stmt: |
0 | 221 |
return rel |
222 |
principal = rel |
|
3815
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3694
diff
changeset
|
223 |
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
|
224 |
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
|
225 |
r.as_string() for r in relations)) |
0 | 226 |
return principal |
227 |
||
228 |
||
438 | 229 |
def set_qdata(getrschema, union, noinvariant): |
0 | 230 |
"""recursive function to set querier data on variables in the syntax tree |
231 |
""" |
|
232 |
for select in union.children: |
|
233 |
for subquery in select.with_: |
|
438 | 234 |
set_qdata(getrschema, subquery.query, noinvariant) |
0 | 235 |
for var in select.defined_vars.itervalues(): |
236 |
if var.stinfo['invariant']: |
|
237 |
if var in noinvariant and not var.stinfo['principal'].r_type == 'has_text': |
|
238 |
var._q_invariant = False |
|
239 |
else: |
|
240 |
var._q_invariant = True |
|
241 |
else: |
|
242 |
var._q_invariant = False |
|
243 |
||
244 |
||
245 |
class SQLGenAnnotator(object): |
|
246 |
def __init__(self, schema): |
|
247 |
self.schema = schema |
|
248 |
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
|
249 |
if not eschema.final) |
0 | 250 |
|
251 |
def annotate(self, rqlst): |
|
252 |
"""add information to the rql syntax tree to help sources to do their |
|
253 |
job (read sql generation) |
|
254 |
||
255 |
a variable is tagged as invariant if: |
|
256 |
* it's a non final variable |
|
257 |
* it's not used as lhs in any final or inlined relation |
|
258 |
* there is no type restriction on this variable (either explicit in the |
|
259 |
syntax tree or because a solution for this variable has been removed |
|
260 |
due to security filtering) |
|
261 |
""" |
|
3694 | 262 |
#assert rqlst.TYPE == 'select', rqlst |
0 | 263 |
rqlst.has_text_query = self._annotate_union(rqlst) |
264 |
||
265 |
def _annotate_union(self, union): |
|
266 |
has_text_query = False |
|
267 |
for select in union.children: |
|
268 |
htq = _annotate_select(self, select) |
|
269 |
if htq: |
|
270 |
has_text_query = True |
|
271 |
return has_text_query |
|
272 |
||
273 |
def is_ambiguous(self, var): |
|
7523
f6856231cc51
[rql annotation] fix bad invariant variable w/ has_text relation: this is only true when has_text will be used as principal (though we don't know that yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7357
diff
changeset
|
274 |
# ignore has_text relation when we know it will be used as principal. |
f6856231cc51
[rql annotation] fix bad invariant variable w/ has_text relation: this is only true when has_text will be used as principal (though we don't know that yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7357
diff
changeset
|
275 |
# This is expected by the rql2sql generator which will use the `entities` |
f6856231cc51
[rql annotation] fix bad invariant variable w/ has_text relation: this is only true when has_text will be used as principal (though we don't know that yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7357
diff
changeset
|
276 |
# table to filter out by type if necessary, This optimisation is very |
f6856231cc51
[rql annotation] fix bad invariant variable w/ has_text relation: this is only true when has_text will be used as principal (though we don't know that yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7357
diff
changeset
|
277 |
# interesting in multi-sources cases, as it may avoid a costly query |
f6856231cc51
[rql annotation] fix bad invariant variable w/ has_text relation: this is only true when has_text will be used as principal (though we don't know that yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7357
diff
changeset
|
278 |
# on sources to get all entities of a given type to achieve this, while |
f6856231cc51
[rql annotation] fix bad invariant variable w/ has_text relation: this is only true when has_text will be used as principal (though we don't know that yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7357
diff
changeset
|
279 |
# we have all the necessary information. |
f6856231cc51
[rql annotation] fix bad invariant variable w/ has_text relation: this is only true when has_text will be used as principal (though we don't know that yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7357
diff
changeset
|
280 |
root = var.stmt.root # Union node |
f6856231cc51
[rql annotation] fix bad invariant variable w/ has_text relation: this is only true when has_text will be used as principal (though we don't know that yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7357
diff
changeset
|
281 |
# rel.scope -> Select or Exists node, so add .parent to get Union from |
f6856231cc51
[rql annotation] fix bad invariant variable w/ has_text relation: this is only true when has_text will be used as principal (though we don't know that yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7357
diff
changeset
|
282 |
# Select node |
f6856231cc51
[rql annotation] fix bad invariant variable w/ has_text relation: this is only true when has_text will be used as principal (though we don't know that yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7357
diff
changeset
|
283 |
rels = [rel for rel in var.stinfo['relations'] if rel.scope.parent is root] |
f6856231cc51
[rql annotation] fix bad invariant variable w/ has_text relation: this is only true when has_text will be used as principal (though we don't know that yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7357
diff
changeset
|
284 |
if len(rels) == 1 and rels[0].r_type == 'has_text': |
0 | 285 |
return False |
286 |
try: |
|
287 |
data = var.stmt._deamb_data |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
288 |
except AttributeError: |
0 | 289 |
data = var.stmt._deamb_data = IsAmbData(self.schema, self.nfdomain) |
290 |
data.compute(var.stmt) |
|
291 |
return data.is_ambiguous(var) |
|
292 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
293 |
|
0 | 294 |
class IsAmbData(object): |
295 |
def __init__(self, schema, nfdomain): |
|
296 |
self.schema = schema |
|
297 |
# shortcuts |
|
298 |
self.rschema = schema.rschema |
|
299 |
self.eschema = schema.eschema |
|
300 |
# domain for non final variables |
|
301 |
self.nfdomain = nfdomain |
|
302 |
# {var: possible solutions set} |
|
303 |
self.varsols = {} |
|
304 |
# set of ambiguous variables |
|
305 |
self.ambiguousvars = set() |
|
306 |
# remember if a variable has been deambiguified by another to avoid |
|
307 |
# doing the opposite |
|
308 |
self.deambification_map = {} |
|
309 |
# not invariant variables (access to final.inlined relation) |
|
310 |
self.not_invariants = set() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
311 |
|
0 | 312 |
def is_ambiguous(self, var): |
313 |
return var in self.ambiguousvars |
|
314 |
||
315 |
def restrict(self, var, restricted_domain): |
|
316 |
self.varsols[var] &= restricted_domain |
|
317 |
if var in self.ambiguousvars and self.varsols[var] == var.stinfo['possibletypes']: |
|
318 |
self.ambiguousvars.remove(var) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
319 |
|
0 | 320 |
def compute(self, rqlst): |
321 |
# set domains for each variable |
|
322 |
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
|
323 |
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
|
324 |
self.eschema(rqlst.solutions[0][varname]).final: |
0 | 325 |
ptypes = var.stinfo['possibletypes'] |
326 |
else: |
|
327 |
ptypes = set(self.nfdomain) |
|
328 |
self.ambiguousvars.add(var) |
|
329 |
self.varsols[var] = ptypes |
|
330 |
if not self.ambiguousvars: |
|
331 |
return |
|
332 |
# apply relation restriction |
|
333 |
self.maydeambrels = maydeambrels = {} |
|
334 |
for rel in rqlst.iget_nodes(Relation): |
|
4287 | 335 |
if rel.r_type == 'eid' or rel.is_types_restriction(): |
0 | 336 |
continue |
337 |
lhs, rhs = rel.get_variable_parts() |
|
338 |
if isinstance(lhs, VariableRef) or isinstance(rhs, VariableRef): |
|
339 |
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
|
340 |
if rschema.inlined or rschema.final: |
0 | 341 |
self.not_invariants.add(lhs.variable) |
342 |
self.set_rel_constraint(lhs, rel, rschema.subjects) |
|
343 |
self.set_rel_constraint(rhs, rel, rschema.objects) |
|
344 |
# try to deambiguify more variables by considering other variables'type |
|
345 |
modified = True |
|
346 |
while modified and self.ambiguousvars: |
|
347 |
modified = False |
|
348 |
for var in self.ambiguousvars.copy(): |
|
349 |
try: |
|
350 |
for rel in (var.stinfo['relations'] & maydeambrels[var]): |
|
351 |
if self.deambiguifying_relation(var, rel): |
|
352 |
modified = True |
|
353 |
break |
|
354 |
except KeyError: |
|
355 |
# no relation to deambiguify |
|
356 |
continue |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
357 |
|
0 | 358 |
def _debug_print(self): |
359 |
print 'varsols', dict((x, sorted(str(v) for v in values)) |
|
360 |
for x, values in self.varsols.iteritems()) |
|
361 |
print 'ambiguous vars', sorted(self.ambiguousvars) |
|
362 |
||
363 |
def set_rel_constraint(self, term, rel, etypes_func): |
|
364 |
if isinstance(term, VariableRef) and self.is_ambiguous(term.variable): |
|
365 |
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
|
366 |
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
|
367 |
or rel.scope is var.scope or rel.r_type == 'identity': |
0 | 368 |
self.restrict(var, frozenset(etypes_func())) |
369 |
try: |
|
370 |
self.maydeambrels[var].add(rel) |
|
371 |
except KeyError: |
|
372 |
self.maydeambrels[var] = set((rel,)) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
373 |
|
0 | 374 |
def deambiguifying_relation(self, var, rel): |
375 |
lhs, rhs = rel.get_variable_parts() |
|
376 |
onlhs = var is getattr(lhs, 'variable', None) |
|
377 |
other = onlhs and rhs or lhs |
|
378 |
otheretypes = None |
|
379 |
# XXX isinstance(other.variable, Variable) to skip column alias |
|
380 |
if isinstance(other, VariableRef) and isinstance(other.variable, Variable): |
|
381 |
deambiguifier = other.variable |
|
382 |
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
|
383 |
if var.stinfo['typerel'] is None: |
0 | 384 |
otheretypes = deambiguifier.stinfo['possibletypes'] |
385 |
elif not self.is_ambiguous(deambiguifier): |
|
386 |
otheretypes = self.varsols[deambiguifier] |
|
387 |
elif deambiguifier in self.not_invariants: |
|
388 |
# we know variable won't be invariant, try to use |
|
389 |
# it to deambguify the current variable |
|
390 |
otheretypes = self.varsols[deambiguifier] |
|
5004
4cc020ee70e2
le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
391 |
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
|
392 |
# 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
|
393 |
# 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
|
394 |
deambiguifier = None |
0 | 395 |
elif isinstance(other, Constant) and other.uidtype: |
396 |
otheretypes = (other.uidtype,) |
|
397 |
deambiguifier = None |
|
398 |
if otheretypes is not None: |
|
967 | 399 |
# to restrict, we must check that for all type in othertypes, |
400 |
# possible types on the other end of the relation are matching |
|
401 |
# variable's possible types |
|
0 | 402 |
rschema = self.rschema(rel.r_type) |
403 |
if onlhs: |
|
404 |
rtypefunc = rschema.subjects |
|
405 |
else: |
|
406 |
rtypefunc = rschema.objects |
|
407 |
for otheretype in otheretypes: |
|
408 |
reltypes = frozenset(rtypefunc(otheretype)) |
|
409 |
if var.stinfo['possibletypes'] != reltypes: |
|
4287 | 410 |
return False |
411 |
self.restrict(var, var.stinfo['possibletypes']) |
|
412 |
self.deambification_map[var] = deambiguifier |
|
413 |
return True |
|
0 | 414 |
return False |