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