- [bug] Fixed bug in over() construct whereby

passing an empty list for either partition_by
    or order_by, as opposed to None, would fail
    to generate correctly.
    Courtesy Gunnlaugur Por Briem.
    [ticket:2574]
This commit is contained in:
Mike Bayer 2012-09-30 17:20:04 -04:00
parent 0d41daa509
commit 2205d49fe7
3 changed files with 46 additions and 11 deletions

View File

@ -50,6 +50,13 @@ CHANGES
an Index associated with a Table in a remote
schema. [ticket:2571]
- [bug] Fixed bug in over() construct whereby
passing an empty list for either partition_by
or order_by, as opposed to None, would fail
to generate correctly.
Courtesy Gunnlaugur Þór Briem.
[ticket:2574]
- [bug] Fixed CTE bug whereby positional
bound parameters present in the CTEs themselves
would corrupt the overall ordering of

View File

@ -520,17 +520,17 @@ class SQLCompiler(engine.Compiled):
cast.typeclause._compiler_dispatch(self, **kwargs))
def visit_over(self, over, **kwargs):
x ="%s OVER (" % over.func._compiler_dispatch(self, **kwargs)
if over.partition_by is not None:
x += "PARTITION BY %s" % \
over.partition_by._compiler_dispatch(self, **kwargs)
if over.order_by is not None:
x += " "
if over.order_by is not None:
x += "ORDER BY %s" % \
over.order_by._compiler_dispatch(self, **kwargs)
x += ")"
return x
return "%s OVER (%s)" % (
over.func._compiler_dispatch(self, **kwargs),
' '.join(
'%s BY %s' % (word, clause._compiler_dispatch(self, **kwargs))
for word, clause in (
('PARTITION', over.partition_by),
('ORDER', over.order_by)
)
if clause is not None and len(clause)
)
)
def visit_extract(self, extract, **kwargs):
field = self.extract_map.get(extract.field, extract.field)

View File

@ -2253,6 +2253,10 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL):
dialect=sqlite.dialect())
def test_over(self):
self.assert_compile(
func.row_number().over(),
"row_number() OVER ()"
)
self.assert_compile(
func.row_number().over(
order_by=[table1.c.name, table1.c.description]
@ -2292,6 +2296,30 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL):
"ORDER BY mytable.name, mytable.description)"
)
self.assert_compile(
func.row_number().over(
partition_by=[],
order_by=[table1.c.name, table1.c.description]
),
"row_number() OVER (ORDER BY mytable.name, mytable.description)"
)
self.assert_compile(
func.row_number().over(
partition_by=[table1.c.name, table1.c.description],
order_by=[]
),
"row_number() OVER (PARTITION BY mytable.name, "
"mytable.description)"
)
self.assert_compile(
func.row_number().over(
partition_by=[],
order_by=[]
),
"row_number() OVER ()"
)
self.assert_compile(
select([func.row_number().over(
order_by=table1.c.description