author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Tue, 23 Oct 2012 15:00:53 +0200 | |
changeset 8576 | c9c72ac10db3 |
parent 8342 | 7a5271182ef0 |
child 9361 | 0542a85fe667 |
permissions | -rw-r--r-- |
8342
7a5271182ef0
[rql annotation] fix has_text_query detection (if coming from sub-query and if has_text on a column aliases. Closes #2275322
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7734
diff
changeset
|
1 |
# copyright 2003-2012 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): |
|
8342
7a5271182ef0
[rql annotation] fix has_text_query detection (if coming from sub-query and if has_text on a column aliases. Closes #2275322
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7734
diff
changeset
|
31 |
has_text_query = False |
0 | 32 |
for subquery in rqlst.with_: |
8342
7a5271182ef0
[rql annotation] fix has_text_query detection (if coming from sub-query and if has_text on a column aliases. Closes #2275322
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7734
diff
changeset
|
33 |
if annotator._annotate_union(subquery.query): |
7a5271182ef0
[rql annotation] fix has_text_query detection (if coming from sub-query and if has_text on a column aliases. Closes #2275322
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7734
diff
changeset
|
34 |
has_text_query = True |
0 | 35 |
#if server.DEBUG: |
36 |
# print '-------- sql annotate', repr(rqlst) |
|
37 |
getrschema = annotator.schema.rschema |
|
38 |
need_distinct = rqlst.distinct |
|
39 |
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
|
40 |
if getrschema(rel.r_type).symmetric and not isinstance(rel.parent, Exists): |
0 | 41 |
for vref in rel.iget_nodes(VariableRef): |
42 |
stinfo = vref.variable.stinfo |
|
43 |
if not stinfo['constnode'] and stinfo['selected']: |
|
44 |
need_distinct = True |
|
45 |
# XXX could mark as not invariant |
|
46 |
break |
|
4719
aaed3f813ef8
kill dead/useless code as suggested by pylint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4467
diff
changeset
|
47 |
for var in rqlst.defined_vars.itervalues(): |
0 | 48 |
stinfo = var.stinfo |
49 |
if stinfo.get('ftirels'): |
|
50 |
has_text_query = True |
|
51 |
if stinfo['attrvar']: |
|
52 |
stinfo['invariant'] = False |
|
53 |
stinfo['principal'] = _select_main_var(stinfo['rhsrelations']) |
|
54 |
continue |
|
5004
4cc020ee70e2
le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
55 |
if not stinfo['relations'] and stinfo['typerel'] is None: |
0 | 56 |
# Any X, Any MAX(X)... |
57 |
# those particular queries should be executed using the system |
|
58 |
# entities table unless there is some type restriction |
|
59 |
stinfo['invariant'] = True |
|
60 |
stinfo['principal'] = None |
|
61 |
continue |
|
62 |
if any(rel for rel in stinfo['relations'] if rel.r_type == 'eid' and rel.operator() != '=') and \ |
|
63 |
not any(r for r in var.stinfo['relations'] - var.stinfo['rhsrelations'] |
|
64 |
if r.r_type != 'eid' and (getrschema(r.r_type).inlined or getrschema(r.r_type).final)): |
|
65 |
# Any X WHERE X eid > 2 |
|
66 |
# those particular queries should be executed using the system entities table |
|
67 |
stinfo['invariant'] = True |
|
68 |
stinfo['principal'] = None |
|
69 |
continue |
|
70 |
if stinfo['selected'] and var.valuable_references() == 1+bool(stinfo['constnode']): |
|
71 |
# "Any X", "Any X, Y WHERE X attr Y" |
|
72 |
stinfo['invariant'] = False |
|
73 |
continue |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
74 |
joins = set() |
0 | 75 |
invariant = False |
76 |
for ref in var.references(): |
|
77 |
rel = ref.relation() |
|
78 |
if rel is None or rel.is_types_restriction(): |
|
79 |
continue |
|
80 |
lhs, rhs = rel.get_parts() |
|
81 |
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
|
82 |
role = 'subject' if onlhs else 'object' |
0 | 83 |
if rel.r_type == 'eid': |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
84 |
if not (onlhs and len(stinfo['relations']) > 1): |
0 | 85 |
break |
86 |
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
|
87 |
joins.add( (rel, role) ) |
0 | 88 |
continue |
89 |
elif rel.r_type == 'identity': |
|
90 |
# identity can't be used as principal, so check other relation are used |
|
91 |
# XXX explain rhs.operator == '=' |
|
92 |
if rhs.operator != '=' or len(stinfo['relations']) <= 1: #(stinfo['constnode'] and rhs.operator == '='): |
|
93 |
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
|
94 |
joins.add( (rel, role) ) |
0 | 95 |
continue |
96 |
rschema = getrschema(rel.r_type) |
|
97 |
if rel.optional: |
|
5004
4cc020ee70e2
le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
98 |
if rel in stinfo.get('optrelations', ()): |
0 | 99 |
# optional variable can't be invariant if this is the lhs |
100 |
# variable of an inlined relation |
|
101 |
if not rel in stinfo['rhsrelations'] and rschema.inlined: |
|
102 |
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
|
103 |
# 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
|
104 |
# 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
|
105 |
# 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
|
106 |
elif not stinfo['constnode']: |
0 | 107 |
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
|
108 |
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
|
109 |
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
|
110 |
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
|
111 |
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
|
112 |
ostinfo = lhs.variable.stinfo |
7734
59fea81647e5
[rql2sql] closes #1892473: enhance handling of optional inlined relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7707
diff
changeset
|
113 |
if not (ostinfo.get('optcomparisons') or |
59fea81647e5
[rql2sql] closes #1892473: enhance handling of optional inlined relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7707
diff
changeset
|
114 |
any(orel for orel in ostinfo['relations'] |
59fea81647e5
[rql2sql] closes #1892473: enhance handling of optional inlined relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7707
diff
changeset
|
115 |
if orel.optional and orel is not rel)): |
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
|
116 |
break |
0 | 117 |
if rschema.final or (onlhs and rschema.inlined): |
118 |
if rschema.type != 'has_text': |
|
119 |
# need join anyway if the variable appears in a final or |
|
120 |
# inlined relation |
|
121 |
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
|
122 |
joins.add( (rel, role) ) |
0 | 123 |
continue |
124 |
if not stinfo['constnode']: |
|
125 |
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
|
126 |
# if relation is inlined, can't be invariant if that |
0 | 127 |
# variable is used anywhere else. |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
128 |
# see 'Any P WHERE NOT N ecrit_par P, N eid 512': |
0 | 129 |
# sql for 'NOT N ecrit_par P' is 'N.ecrit_par is NULL' so P |
130 |
# can use N.ecrit_par as principal |
|
131 |
if (stinfo['selected'] or len(stinfo['relations']) > 1): |
|
132 |
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
|
133 |
elif rschema.symmetric and stinfo['selected']: |
0 | 134 |
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
|
135 |
joins.add( (rel, role) ) |
0 | 136 |
else: |
137 |
# if there is at least one ambigous relation and no other to |
|
138 |
# restrict types, can't be invariant since we need to filter out |
|
139 |
# other types |
|
140 |
if not annotator.is_ambiguous(var): |
|
141 |
invariant = True |
|
142 |
stinfo['invariant'] = invariant |
|
143 |
if invariant and joins: |
|
144 |
# remember rqlst/solutions analyze information |
|
145 |
# we have to select a kindof "main" relation which will "extrajoins" |
|
146 |
# the other |
|
147 |
# priority should be given to relation which are not in inner queries |
|
148 |
# (eg exists) |
|
149 |
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
|
150 |
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
|
151 |
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
|
152 |
# 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
|
153 |
# 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
|
154 |
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
|
155 |
sstinfo['scope'] = common_parent(sstinfo['scope'], stinfo['scope']).scope |
0 | 156 |
except CantSelectPrincipal: |
157 |
stinfo['invariant'] = False |
|
8342
7a5271182ef0
[rql annotation] fix has_text_query detection (if coming from sub-query and if has_text on a column aliases. Closes #2275322
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7734
diff
changeset
|
158 |
# see unittest_rqlannotation. test_has_text_security_cache_bug |
7a5271182ef0
[rql annotation] fix has_text_query detection (if coming from sub-query and if has_text on a column aliases. Closes #2275322
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7734
diff
changeset
|
159 |
# XXX probably more to do, but yet that work without more... |
7a5271182ef0
[rql annotation] fix has_text_query detection (if coming from sub-query and if has_text on a column aliases. Closes #2275322
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7734
diff
changeset
|
160 |
for col_alias in rqlst.aliases.itervalues(): |
7a5271182ef0
[rql annotation] fix has_text_query detection (if coming from sub-query and if has_text on a column aliases. Closes #2275322
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7734
diff
changeset
|
161 |
if col_alias.stinfo.get('ftirels'): |
7a5271182ef0
[rql annotation] fix has_text_query detection (if coming from sub-query and if has_text on a column aliases. Closes #2275322
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7734
diff
changeset
|
162 |
has_text_query = True |
0 | 163 |
rqlst.need_distinct = need_distinct |
164 |
return has_text_query |
|
165 |
||
166 |
||
167 |
||
4721
8f63691ccb7f
pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
168 |
class CantSelectPrincipal(Exception): |
8f63691ccb7f
pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
169 |
"""raised when no 'principal' variable can be found""" |
0 | 170 |
|
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
|
171 |
def _select_principal(scope, relations, _sort=lambda x:x): |
0 | 172 |
"""given a list of rqlst relations, select one which will be used to |
173 |
represent an invariant variable (e.g. using on extremity of the relation |
|
174 |
instead of the variable's type table |
|
175 |
""" |
|
599
9ef680acd92a
fix select principal so results are predictable during tests
Sylvain <syt@logilab.fr>
parents:
438
diff
changeset
|
176 |
# _sort argument is there for test |
0 | 177 |
diffscope_rels = {} |
178 |
ored_rels = set() |
|
179 |
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
|
180 |
for rel, role in _sort(relations): |
0 | 181 |
# note: only eid and has_text among all final relations may be there |
182 |
if rel.r_type in ('eid', 'identity'): |
|
183 |
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
|
184 |
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
|
185 |
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
|
186 |
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
|
187 |
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
|
188 |
continue |
0 | 189 |
if rel.ored(traverse_scope=True): |
190 |
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
|
191 |
elif rel.scope is scope: |
0 | 192 |
return rel |
193 |
elif not rel.neged(traverse_scope=True): |
|
194 |
diffscope_rels.add(rel) |
|
195 |
if len(ored_rels) > 1: |
|
196 |
ored_rels_copy = tuple(ored_rels) |
|
197 |
for rel1 in ored_rels_copy: |
|
198 |
for rel2 in ored_rels_copy: |
|
199 |
if rel1 is rel2: |
|
200 |
continue |
|
201 |
if isinstance(common_parent(rel1, rel2), Or): |
|
202 |
ored_rels.discard(rel1) |
|
203 |
ored_rels.discard(rel2) |
|
599
9ef680acd92a
fix select principal so results are predictable during tests
Sylvain <syt@logilab.fr>
parents:
438
diff
changeset
|
204 |
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
|
205 |
if rel.scope is scope: |
0 | 206 |
return rel |
207 |
diffscope_rels.add(rel) |
|
208 |
# if DISTINCT query, can use variable from a different scope as principal |
|
209 |
# 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
|
210 |
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
|
211 |
return iter(_sort(diffscope_rels)).next() |
7651
7c0af7ef3325
[repo, ms] fix planning of some queries where variable 'non-invarianess' should be considered, leading for instance to junk in entities table on source deletion
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7523
diff
changeset
|
212 |
# XXX could use a relation from a different scope if it can't generate |
7c0af7ef3325
[repo, ms] fix planning of some queries where variable 'non-invarianess' should be considered, leading for instance to junk in entities table on source deletion
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7523
diff
changeset
|
213 |
# duplicates, so we should have to check cardinality |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
214 |
raise CantSelectPrincipal() |
0 | 215 |
|
216 |
def _select_main_var(relations): |
|
217 |
"""given a list of rqlst relations, select one which will be used as main |
|
218 |
relation for the rhs variable |
|
219 |
""" |
|
3815
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3694
diff
changeset
|
220 |
principal = None |
7707
936530f8d32c
[sql gen] handle optional on comparison node (eg HAVING expression) and on rhs of final relation. Closes #1859609
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7651
diff
changeset
|
221 |
others = [] |
3815
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3694
diff
changeset
|
222 |
# 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
|
223 |
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
|
224 |
# 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
|
225 |
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
|
226 |
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
|
227 |
continue |
7707
936530f8d32c
[sql gen] handle optional on comparison node (eg HAVING expression) and on rhs of final relation. Closes #1859609
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7651
diff
changeset
|
228 |
if rel.optional: |
936530f8d32c
[sql gen] handle optional on comparison node (eg HAVING expression) and on rhs of final relation. Closes #1859609
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7651
diff
changeset
|
229 |
others.append(rel) |
936530f8d32c
[sql gen] handle optional on comparison node (eg HAVING expression) and on rhs of final relation. Closes #1859609
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7651
diff
changeset
|
230 |
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
|
231 |
if rel.scope is rel.stmt: |
0 | 232 |
return rel |
233 |
principal = rel |
|
3815
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3694
diff
changeset
|
234 |
if principal is None: |
7707
936530f8d32c
[sql gen] handle optional on comparison node (eg HAVING expression) and on rhs of final relation. Closes #1859609
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7651
diff
changeset
|
235 |
if others: |
936530f8d32c
[sql gen] handle optional on comparison node (eg HAVING expression) and on rhs of final relation. Closes #1859609
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7651
diff
changeset
|
236 |
return others[0] |
3815
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3694
diff
changeset
|
237 |
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
|
238 |
r.as_string() for r in relations)) |
0 | 239 |
return principal |
240 |
||
241 |
||
438 | 242 |
def set_qdata(getrschema, union, noinvariant): |
0 | 243 |
"""recursive function to set querier data on variables in the syntax tree |
244 |
""" |
|
245 |
for select in union.children: |
|
246 |
for subquery in select.with_: |
|
438 | 247 |
set_qdata(getrschema, subquery.query, noinvariant) |
0 | 248 |
for var in select.defined_vars.itervalues(): |
249 |
if var.stinfo['invariant']: |
|
250 |
if var in noinvariant and not var.stinfo['principal'].r_type == 'has_text': |
|
251 |
var._q_invariant = False |
|
252 |
else: |
|
253 |
var._q_invariant = True |
|
254 |
else: |
|
255 |
var._q_invariant = False |
|
256 |
||
257 |
||
258 |
class SQLGenAnnotator(object): |
|
259 |
def __init__(self, schema): |
|
260 |
self.schema = schema |
|
261 |
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
|
262 |
if not eschema.final) |
0 | 263 |
|
264 |
def annotate(self, rqlst): |
|
265 |
"""add information to the rql syntax tree to help sources to do their |
|
266 |
job (read sql generation) |
|
267 |
||
268 |
a variable is tagged as invariant if: |
|
269 |
* it's a non final variable |
|
270 |
* it's not used as lhs in any final or inlined relation |
|
271 |
* there is no type restriction on this variable (either explicit in the |
|
272 |
syntax tree or because a solution for this variable has been removed |
|
273 |
due to security filtering) |
|
274 |
""" |
|
3694 | 275 |
#assert rqlst.TYPE == 'select', rqlst |
0 | 276 |
rqlst.has_text_query = self._annotate_union(rqlst) |
277 |
||
278 |
def _annotate_union(self, union): |
|
279 |
has_text_query = False |
|
280 |
for select in union.children: |
|
8342
7a5271182ef0
[rql annotation] fix has_text_query detection (if coming from sub-query and if has_text on a column aliases. Closes #2275322
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7734
diff
changeset
|
281 |
if _annotate_select(self, select): |
0 | 282 |
has_text_query = True |
283 |
return has_text_query |
|
284 |
||
285 |
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
|
286 |
# 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
|
287 |
# 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
|
288 |
# 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
|
289 |
# 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
|
290 |
# 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
|
291 |
# 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
|
292 |
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
|
293 |
# 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
|
294 |
# 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
|
295 |
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
|
296 |
if len(rels) == 1 and rels[0].r_type == 'has_text': |
0 | 297 |
return False |
298 |
try: |
|
299 |
data = var.stmt._deamb_data |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
300 |
except AttributeError: |
0 | 301 |
data = var.stmt._deamb_data = IsAmbData(self.schema, self.nfdomain) |
302 |
data.compute(var.stmt) |
|
303 |
return data.is_ambiguous(var) |
|
304 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
305 |
|
0 | 306 |
class IsAmbData(object): |
307 |
def __init__(self, schema, nfdomain): |
|
308 |
self.schema = schema |
|
309 |
# shortcuts |
|
310 |
self.rschema = schema.rschema |
|
311 |
self.eschema = schema.eschema |
|
312 |
# domain for non final variables |
|
313 |
self.nfdomain = nfdomain |
|
314 |
# {var: possible solutions set} |
|
315 |
self.varsols = {} |
|
316 |
# set of ambiguous variables |
|
317 |
self.ambiguousvars = set() |
|
318 |
# remember if a variable has been deambiguified by another to avoid |
|
319 |
# doing the opposite |
|
320 |
self.deambification_map = {} |
|
321 |
# not invariant variables (access to final.inlined relation) |
|
322 |
self.not_invariants = set() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
323 |
|
0 | 324 |
def is_ambiguous(self, var): |
325 |
return var in self.ambiguousvars |
|
326 |
||
327 |
def restrict(self, var, restricted_domain): |
|
328 |
self.varsols[var] &= restricted_domain |
|
329 |
if var in self.ambiguousvars and self.varsols[var] == var.stinfo['possibletypes']: |
|
330 |
self.ambiguousvars.remove(var) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
331 |
|
0 | 332 |
def compute(self, rqlst): |
333 |
# set domains for each variable |
|
334 |
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
|
335 |
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
|
336 |
self.eschema(rqlst.solutions[0][varname]).final: |
0 | 337 |
ptypes = var.stinfo['possibletypes'] |
338 |
else: |
|
339 |
ptypes = set(self.nfdomain) |
|
340 |
self.ambiguousvars.add(var) |
|
341 |
self.varsols[var] = ptypes |
|
342 |
if not self.ambiguousvars: |
|
343 |
return |
|
344 |
# apply relation restriction |
|
345 |
self.maydeambrels = maydeambrels = {} |
|
346 |
for rel in rqlst.iget_nodes(Relation): |
|
4287 | 347 |
if rel.r_type == 'eid' or rel.is_types_restriction(): |
0 | 348 |
continue |
349 |
lhs, rhs = rel.get_variable_parts() |
|
350 |
if isinstance(lhs, VariableRef) or isinstance(rhs, VariableRef): |
|
351 |
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
|
352 |
if rschema.inlined or rschema.final: |
0 | 353 |
self.not_invariants.add(lhs.variable) |
354 |
self.set_rel_constraint(lhs, rel, rschema.subjects) |
|
355 |
self.set_rel_constraint(rhs, rel, rschema.objects) |
|
356 |
# try to deambiguify more variables by considering other variables'type |
|
357 |
modified = True |
|
358 |
while modified and self.ambiguousvars: |
|
359 |
modified = False |
|
360 |
for var in self.ambiguousvars.copy(): |
|
361 |
try: |
|
362 |
for rel in (var.stinfo['relations'] & maydeambrels[var]): |
|
363 |
if self.deambiguifying_relation(var, rel): |
|
364 |
modified = True |
|
365 |
break |
|
366 |
except KeyError: |
|
367 |
# no relation to deambiguify |
|
368 |
continue |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
369 |
|
0 | 370 |
def _debug_print(self): |
371 |
print 'varsols', dict((x, sorted(str(v) for v in values)) |
|
372 |
for x, values in self.varsols.iteritems()) |
|
373 |
print 'ambiguous vars', sorted(self.ambiguousvars) |
|
374 |
||
375 |
def set_rel_constraint(self, term, rel, etypes_func): |
|
376 |
if isinstance(term, VariableRef) and self.is_ambiguous(term.variable): |
|
377 |
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
|
378 |
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
|
379 |
or rel.scope is var.scope or rel.r_type == 'identity': |
0 | 380 |
self.restrict(var, frozenset(etypes_func())) |
381 |
try: |
|
382 |
self.maydeambrels[var].add(rel) |
|
383 |
except KeyError: |
|
384 |
self.maydeambrels[var] = set((rel,)) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
385 |
|
0 | 386 |
def deambiguifying_relation(self, var, rel): |
387 |
lhs, rhs = rel.get_variable_parts() |
|
388 |
onlhs = var is getattr(lhs, 'variable', None) |
|
389 |
other = onlhs and rhs or lhs |
|
390 |
otheretypes = None |
|
391 |
# XXX isinstance(other.variable, Variable) to skip column alias |
|
392 |
if isinstance(other, VariableRef) and isinstance(other.variable, Variable): |
|
393 |
deambiguifier = other.variable |
|
394 |
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
|
395 |
if var.stinfo['typerel'] is None: |
0 | 396 |
otheretypes = deambiguifier.stinfo['possibletypes'] |
397 |
elif not self.is_ambiguous(deambiguifier): |
|
398 |
otheretypes = self.varsols[deambiguifier] |
|
399 |
elif deambiguifier in self.not_invariants: |
|
400 |
# we know variable won't be invariant, try to use |
|
401 |
# it to deambguify the current variable |
|
402 |
otheretypes = self.varsols[deambiguifier] |
|
5004
4cc020ee70e2
le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
403 |
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
|
404 |
# 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
|
405 |
# 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
|
406 |
deambiguifier = None |
0 | 407 |
elif isinstance(other, Constant) and other.uidtype: |
408 |
otheretypes = (other.uidtype,) |
|
409 |
deambiguifier = None |
|
410 |
if otheretypes is not None: |
|
967 | 411 |
# to restrict, we must check that for all type in othertypes, |
412 |
# possible types on the other end of the relation are matching |
|
413 |
# variable's possible types |
|
0 | 414 |
rschema = self.rschema(rel.r_type) |
415 |
if onlhs: |
|
416 |
rtypefunc = rschema.subjects |
|
417 |
else: |
|
418 |
rtypefunc = rschema.objects |
|
419 |
for otheretype in otheretypes: |
|
420 |
reltypes = frozenset(rtypefunc(otheretype)) |
|
421 |
if var.stinfo['possibletypes'] != reltypes: |
|
4287 | 422 |
return False |
423 |
self.restrict(var, var.stinfo['possibletypes']) |
|
424 |
self.deambification_map[var] = deambiguifier |
|
425 |
return True |
|
0 | 426 |
return False |