Merge lp:~wgrant/storm/distinct-on into lp:storm

Proposed by William Grant
Status: Merged
Approved by: Thomas Herve
Approved revision: 391
Merged at revision: 392
Proposed branch: lp:~wgrant/storm/distinct-on
Merge into: lp:storm
Diff against target: 81 lines (+26/-10)
4 files modified
NEWS (+8/-6)
storm/expr.py (+5/-2)
storm/store.py (+3/-2)
tests/expr.py (+10/-0)
To merge this branch: bzr merge lp:~wgrant/storm/distinct-on
Reviewer Review Type Date Requested Status
Thomas Herve (community) Approve
Данило Шеган (community) Approve
Review via email: mp+60582@code.launchpad.net

Description of the change

This (surprisingly small) branch adds DISTINCT ON support. Select()'s distinct argument has traditionally been a boolean, so it seemed safe and sensible enough to allow it to optionally take a list of columns to imply DISTINCT ON. I opted to use raw=True, as is already used for the other Select arguments.

To post a comment you must log in.
Revision history for this message
Данило Шеган (danilo) wrote :

Does it make sense to have a test to confirm it works properly for multi-table selects and distinct on a column which is not included in the select columns?

Otherwise, looks pretty good.

review: Approve
Revision history for this message
Thomas Herve (therve) wrote :

It's relatively unfortunate to change the global compiler, as it's only supported on Postgres. OTOH it should fail pretty obviously on other databases, so I guess it's fine.

[1] It would be nice to update the docstring of Store.config to mention the new format for distinct (if you're using Postgres).

[2] The state.push calls should be moved before the compilation of the columns.

[3] Please add a note in the NEWS file mentioning the feature.

Thanks, merge once fixed!

review: Approve
lp:~wgrant/storm/distinct-on updated
392. By William Grant

Update config(distinct)'s docstring.

393. By William Grant

Push state before compiling columns.

394. By William Grant

Add NEWS entry.

395. By William Grant

Fix existing NEWS indentation.

396. By William Grant

Note that DISTINCT ON is only supported by postgres.

397. By William Grant

Merge trunk.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'NEWS'
2--- NEWS 2011-05-04 11:50:23 +0000
3+++ NEWS 2011-05-16 10:52:31 +0000
4@@ -18,12 +18,14 @@
5 "schema-uri: "postgres://schema_user@host/db"}]
6 manager = ZStormResourceManager(databases)
7
8- where the "schema-uri" key is optional and would default to "uri" if
9- not given. The old format of the 'databases' parameter is still supported
10- but deprecated.
11- - A new storm.twisted.transact module has been added with facilities to
12- integrate Storm with twisted, by running transactions in a separate
13- thread from the main one in order to not block the reactor.
14+ where the "schema-uri" key is optional and would default to "uri" if
15+ not given. The old format of the 'databases' parameter is still supported
16+ but deprecated.
17+ - A new storm.twisted.transact module has been added with facilities to
18+ integrate Storm with twisted, by running transactions in a separate
19+ thread from the main one in order to not block the reactor.
20+ - ResultSet.config's "distinct" argument now also accepts a tuple of
21+ columns, which will be turned into a DISTINCT ON clause.
22
23 Bug fixes
24 ---------
25
26=== modified file 'storm/expr.py'
27--- storm/expr.py 2011-03-15 07:54:00 +0000
28+++ storm/expr.py 2011-05-16 10:52:31 +0000
29@@ -656,10 +656,13 @@
30 @compile.when(Select)
31 def compile_select(compile, select, state):
32 tokens = ["SELECT "]
33+ state.push("auto_tables", [])
34+ state.push("context", COLUMN)
35 if select.distinct:
36 tokens.append("DISTINCT ")
37- state.push("auto_tables", [])
38- state.push("context", COLUMN)
39+ if isinstance(select.distinct, (tuple, list)):
40+ tokens.append(
41+ "ON (%s) " % compile(select.distinct, state, raw=True))
42 tokens.append(compile(select.columns, state))
43 tables_pos = len(tokens)
44 parameters_pos = len(state.parameters)
45
46=== modified file 'storm/store.py'
47--- storm/store.py 2011-01-05 10:19:49 +0000
48+++ storm/store.py 2011-05-16 10:52:31 +0000
49@@ -935,8 +935,9 @@
50 def config(self, distinct=None, offset=None, limit=None):
51 """Configure this result object in-place. All parameters are optional.
52
53- @param distinct: Boolean enabling/disabling usage of the DISTINCT
54- keyword in the query made.
55+ @param distinct: If True, enables usage of the DISTINCT keyword in
56+ the query. If a tuple or list of columns, inserts a DISTINCT ON
57+ (only supported by PostgreSQL).
58 @param offset: Offset where results will start to be retrieved
59 from the result set.
60 @param limit: Limit the number of objects retrieved from the
61
62=== modified file 'tests/expr.py'
63--- tests/expr.py 2010-12-07 15:00:30 +0000
64+++ tests/expr.py 2011-05-16 10:52:31 +0000
65@@ -657,6 +657,16 @@
66 'SELECT DISTINCT column1, column2 FROM "table 1"')
67 self.assertEquals(state.parameters, [])
68
69+ def test_select_distinct_on(self):
70+ expr = Select([column1, column2], Undef, [table1],
71+ distinct=[column2, column1])
72+ state = State()
73+ statement = compile(expr, state)
74+ self.assertEquals(statement,
75+ 'SELECT DISTINCT ON (column2, column1) '
76+ 'column1, column2 FROM "table 1"')
77+ self.assertEquals(state.parameters, [])
78+
79 def test_select_where(self):
80 expr = Select([column1, Func1()],
81 Func1(),

Subscribers

People subscribed via source and target branches

to status/vote changes: