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