Merge lp:~ack/storm/expr-is-in-resultset into lp:storm

Proposed by Alberto Donato
Status: Merged
Merged at revision: 483
Proposed branch: lp:~ack/storm/expr-is-in-resultset
Merge into: lp:storm
Diff against target: 52 lines (+22/-2)
2 files modified
storm/expr.py (+6/-1)
tests/store/base.py (+16/-1)
To merge this branch: bzr merge lp:~ack/storm/expr-is-in-resultset
Reviewer Review Type Date Requested Status
Free Ekanayaka (community) Approve
Review via email: mp+294599@code.launchpad.net

Description of the change

This change allows to use a subquery (insted of performing two separate queries) when the Comparable.is_in(ResultSet) expression is used.

To post a comment you must log in.
Revision history for this message
Free Ekanayaka (free.ekanayaka) wrote :

+1 with a couple of comments.

review: Approve
Revision history for this message
Alberto Donato (ack) wrote :

@Free, thanks, fixed as suggested.

lp:~ack/storm/expr-is-in-resultset updated
484. By Alberto Donato

Address review comments.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'storm/expr.py'
2--- storm/expr.py 2012-03-26 14:27:29 +0000
3+++ storm/expr.py 2016-05-13 09:48:56 +0000
4@@ -486,7 +486,12 @@
5 return Neg(self)
6
7 def is_in(self, others):
8- if not isinstance(others, Expr):
9+ from storm.store import ResultSet
10+ if isinstance(others, ResultSet):
11+ # If a ResultSet is passed, use it for a SELECT subquery, instead
12+ # of performing a separate query and passing in the resulting list.
13+ others = others._get_select()
14+ elif not isinstance(others, Expr):
15 others = list(others)
16 if not others:
17 return False
18
19=== modified file 'tests/store/base.py'
20--- tests/store/base.py 2015-04-20 08:46:20 +0000
21+++ tests/store/base.py 2016-05-13 09:48:56 +0000
22@@ -44,6 +44,7 @@
23 from storm.cache import Cache
24 from storm.store import AutoReload, EmptyResultSet, Store, ResultSet
25 from storm.tracer import debug
26+from storm.testing import CaptureTracer
27
28 from tests.info import Wrapper
29 from tests.helper import TestHelper
30@@ -5684,8 +5685,22 @@
31
32 self.assertEquals(result3.count(), 2)
33
34+ def test_is_in_result_set_subquery(self):
35+ """
36+ When is_in() is passed a ResultSet, a single query with a subquery
37+ is performed.
38+ """
39+ with CaptureTracer() as tracer:
40+ result1 = self.store.find(Foo.id, Foo.id < 25)
41+ result2 = self.store.find(Foo, Foo.id.is_in(result1))
42+ self.assertEquals(result2.count(), 2)
43+ self.assertEqual(
44+ ["SELECT COUNT(*) FROM foo WHERE foo.id IN"
45+ " (SELECT foo.id FROM foo WHERE foo.id < 25)"],
46+ tracer.queries)
47+
48 def test_is_in_empty_result_set(self):
49- result1 = self.store.find(Foo, Foo.id < 10)
50+ result1 = self.store.find(Foo.id, Foo.id < 10)
51 result2 = self.store.find(Foo, Or(Foo.id > 20, Foo.id.is_in(result1)))
52 self.assertEquals(result2.count(), 1)
53

Subscribers

People subscribed via source and target branches

to status/vote changes: