support for postgresql windowing functions

Issue #1844 resolved
Former user created an issue

Comments (10)

  1. Former user Account Deleted

    This seems like part of the way there:

    from sqlalchemy.ext.compiler import compiles
    from sqlalchemy.sql.expression import Executable, ClauseElement, ClauseList, _generative
    from sqlalchemy import util
    
    
    class Over(Executable, ClauseList):
        def __init__(self, *column_or_columns):
            ClauseList.__init__(self,*util.to_list(column_or_columns) or [       self.over_clause = ClauseList(*util.to_list(column_or_columns) or [](])
    ))
    
    @compiles(Over)
    def visit_over(grouping, compiler, asfrom=False, **kwargs):
        return "OVER (PARTITION BY " + compiler.process(grouping.over_clause, **kwargs) + ")"
    

    Gregg L.

  2. Former user Account Deleted

    From where I'm looking at things... it looks like it's very hard to add new methods onto select and friends... I'm wondering if there could be a decorator, similar to '_generative' that would note that a function can be 'chained'.... thus select([some_column_set](some_column_set)).over(some_col1) would have some parts like this:

    @chainable
    class Over(Executable, ClauseList):
    

    and in select and friends:

    def getattr(self,attr):
        if attr in global_chainables_registry:
             return ...
    

    I realize I'm being a bit vague, and this might warrant a larger discussion!

    Gregg

  3. Mike Bayer repo owner

    sorry, those are WITH queries, not the same thing as windowing functions. windowing functions are not as tough to implement.

  4. Mike Bayer repo owner

    also I just had to use a window the other day, its pretty easy in the usual case, from_self() makes the usual "subquery from self" a snap:

    for x, in Session.query(SomeClass.x, literal_column("ROW_NUMBER OVER (ORDER BY foo.bar) AS rownum")).\
                    from_self(SomeClass.x).\
                    filter("rownum %% %d=1" % modulus)
    
  5. Mike Bayer repo owner
    • changed milestone to 0.7.0

    putting this on 0.7.0 for the moment, will force me to look at it, see if I can do it with a clause element. I am using window functions right now across PG / SQL Server. Perhaps supporting just the OVER (expression) version to start, with WINDOW coming in later.

    select([table.c.b.over(order_by=table.c.c)](table.c.a,))
    select([table.c.b.over(partition_by=table.c.c)](table.c.a,))
    
    w = window(partition_by=table.c.a, order_by=table.c.b.desc())
    select([table.c.b.over(w)](table.c.a,))
    
  6. Log in to comment