LIMIT/OFFSET workaround for mssql loses auto-correlation for the ORDER BY clause

Issue #2538 resolved
Sok Ann Yap created an issue

With the LIMIT/OFFSET workaround for mssql, there is no auto-correlation for the ORDER BY clause, as it is compiled separately.

Seems to work as intended if I get the whole SELECT to compile together using the over() method added in version 0.7.

Comments (10)

  1. Mike Bayer repo owner

    OK just curious, can you test that this does it also, as an option to avoid those parens for now (there's a different fix for that in 0.8):

    diff -r 30e8cd5960de3c2c72d60ee51ade31232775d76a lib/sqlalchemy/dialects/mssql/base.py
    --- a/lib/sqlalchemy/dialects/mssql/base.py Tue Jul 17 09:32:11 2012 -0400
    +++ b/lib/sqlalchemy/dialects/mssql/base.py Tue Jul 24 12:01:13 2012 -0400
    @@ -821,7 +821,7 @@
                 select._mssql_visit = True
                 select = select.column(
                     sql.literal_column("ROW_NUMBER() OVER (ORDER BY %s)" \
    -                % orderby).label("mssql_rn")
    +                % self.process(orderby, **kwargs)).label("mssql_rn")
                                        ).order_by(None).alias()
    
                 mssql_rn = sql.column('mssql_rn')
    
  2. Sok Ann Yap reporter

    With the change in comment:1, I got exception:

    AttributeError: 'str' object has no attribute '_compiler_dispatch'
    
  3. Mike Bayer repo owner

    OK, I'd gather maybe some tests pass a string for order_by, will have to play with it.

    (testing trac comment editing)

  4. Sok Ann Yap reporter

    The extra parens added by my original diff breaks ORDER BY with DESC, as it generate clauses such as ROW_NUMBER() OVER (ORDER BY (xxx.id DESC)). I have attached another diff without this problem.

  5. Log in to comment