[rql2sql] Fix iter_exists_sols() excessive cleaning of _state.tables
Closes #5503548
--- a/server/sources/rql2sql.py Thu Jun 25 22:12:49 2015 +0200
+++ b/server/sources/rql2sql.py Thu Jun 18 18:26:02 2015 +0200
@@ -397,11 +397,14 @@
yield 1
return
thisexistssols, thisexistsvars = self.existssols[exists]
+ notdone_outside_vars = set()
# when iterating other solutions inner to an EXISTS subquery, we should
# reset variables which have this exists node as scope at each iteration
for var in exists.stmt.defined_vars.itervalues():
if var.scope is exists:
thisexistsvars.add(var.name)
+ elif var.name not in self.done:
+ notdone_outside_vars.add(var)
origsol = self.solution
origtables = self.tables
done = self.done
@@ -417,6 +420,10 @@
for var in thisexistsvars:
if var in done:
done.remove(var)
+ for var in list(notdone_outside_vars):
+ if var.name in done and var._q_sqltable in self.tables:
+ origtables[var._q_sqltable] = self.tables[var._q_sqltable]
+ notdone_outside_vars.remove(var)
for rel in exists.iget_nodes(Relation):
if rel in done:
done.remove(rel)
--- a/server/test/unittest_rql2sql.py Thu Jun 25 22:12:49 2015 +0200
+++ b/server/test/unittest_rql2sql.py Thu Jun 18 18:26:02 2015 +0200
@@ -563,6 +563,17 @@
'''SELECT _X.cw_eid
FROM cw_Note AS _X
WHERE _X.cw_eid IN(999998, 999999) AND NOT (EXISTS(SELECT 1 FROM cw_source_relation AS rel_cw_source0 WHERE rel_cw_source0.eid_from=_X.cw_eid))'''),
+
+ # Test for https://www.cubicweb.org/ticket/5503548
+ ('''Any X
+ WHERE X is CWSourceSchemaConfig,
+ EXISTS(X created_by U, U login L),
+ X cw_schema X_CW_SCHEMA,
+ X owned_by X_OWNED_BY?
+ ''', '''SELECT _X.cw_eid
+FROM cw_CWSourceSchemaConfig AS _X LEFT OUTER JOIN owned_by_relation AS rel_owned_by1 ON (rel_owned_by1.eid_from=_X.cw_eid)
+WHERE EXISTS(SELECT 1 FROM created_by_relation AS rel_created_by0, cw_CWUser AS _U WHERE rel_created_by0.eid_from=_X.cw_eid AND rel_created_by0.eid_to=_U.cw_eid) AND _X.cw_cw_schema IS NOT NULL
+''')
]
ADVANCED_WITH_GROUP_CONCAT = [