Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6824763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:56:27+00:00 2026-05-26T21:56:27+00:00

Based on example found here but I guess I’m not understanding it. This works

  • 0

Based on example found here but I guess I’m not understanding it. This works for single column primary keys but fails on multiple ones.
This is my code

@classmethod
    def column_windows(cls, q, columns, windowsize, where = None):
        """Return a series of WHERE clauses against 
        a given column that break it into windows.

        Result is an iterable of tuples, consisting of
        ((start, end), whereclause), where (start, end) are the ids.

        Requires a database that supports window functions, 
        i.e. Postgresql, SQL Server, Oracle.

        Enhance this yourself !  Add a "where" argument
        so that windows of just a subset of rows can
        be computed.
        """
             #Here is the thing... how to compare...
        def int_for_range(start_id, end_id):
            if end_id:
                return and_(
                    columns>=start_id,
                    columns<end_id
                )
            else:
                return columns>=start_id

        if isinstance(columns, Column):
            columns_k=(columns,)
        else:
            columns_k=tuple(columns)
        q2=None
        cols=()
        for c in columns:
            cols = cols + (c,)
            if not q2:
                q2=q.session.query(c)
            else:
                q2=q2.add_column(c)
        q2 = q2.add_column(func.row_number().over(order_by=columns_k).label('rownum'))
        q2=q2.filter(q._criterion).from_self(cols)
        if windowsize > 1:
            q2 = q2.filter("rownum %% %d=1" % windowsize)
        for res in q2:
            print res
        intervals = [id for id, in q2]
        while intervals:
            start = intervals.pop(0)
            if intervals:
                end = intervals[0]
            else:
                end = None
            yield int_for_range(start, end)

    @classmethod
    def windowed_query(cls, q, columns, windowsize):
        """"Break a Query into windows on a given column."""

        for whereclause in cls.column_windows(q,columns, windowsize):
            for row in q.filter(whereclause).order_by(columns):
                yield row

Now I have the problem when comparing the set of columns of the primary key. Well I guess kind of recursive clause generating function should do it… Let’s try it…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:56:28+00:00Added an answer on May 26, 2026 at 9:56 pm

    Well, result is not what expected but got it to work: Now it really windows any query keeping all in place, multi column unique ordering, and so on:
    Here is my code, hope it may be usefull for someone else:

    @classmethod
        def window_query(cls, q, windowsize, windows=None):
            """
                q=Query object we want to window results
                windowsize=The number of elements each window has
                windows=The window, or window list, numbers: 1-based to query
            """
            windowselect=False
            if windows:
                if not isinstance(windows,list):
                    windows=list(windows)
                windowselect=True
            #Appending u_columns to ordered counting subquery will ensure unique ordering
            u_columns=list([col for col in cls.getBestUniqueColumns()])
            #o_columns is the list of order by columns for the query
            o_columns=list([col for col in q._order_by])
            #we append columns from u_columns not in o_columns to ensure unique ordering but keeping the desired one
            sq_o_columns=list(o_columns)
            for col in u_columns:
                if not col in sq_o_columns:
                    sq_o_columns.append(col)
    
            sub=None
            #we select unique columns in subquery that we'll need to join in parent query
            for col in u_columns:
                if not sub:
                    sub=q.session.query(col)
                else:
                    sub=sub.add_column(col)
    
            #Generate a tuple from sq_o_columns list (I don't know why over() won't accept list itself TODO: more elegant
            sq_o_col_tuple=()
            for col in sq_o_columns:
                sq_o_col_tuple=sq_o_col_tuple + (col,)
            #we add row counting column, counting on generated combined ordering+unique columns tuple
            sub = sub.add_column(func.row_number().over(order_by=sq_o_col_tuple).label('rownum')).filter(q._criterion)
    
            #Prepare sub query to use as subquery (LOL)
            sub=sub.subquery('lacrn')
    
            #Prepare join ON clauses epxression comparing unique columns defined by u_columns 
            joinclause=expression.BooleanClauseList()
            for col in u_columns:
                joinclause=joinclause.__and__(col == sub.c[col.key])
            #Make the joining
            q=q.join(sub,joinclause
                     )
            i=-1
            while True:
                #We try to query windows defined by windows list
                if windowselect:
                    #We want selected-windows-results to returned
                    if windows:
                        i=windows.pop(0)-1
                    else:
                        break
                else:
                    #We want all-windows-results to be returned
                    i=i+1
                res=q.filter(and_(sub.c.rownum > (i*windowsize), sub.c.rownum <= ((i+1)*windowsize))).all()
                if not (res or windowselect):
                    #We end an all-windows-results because of no more results, we must check if is selected-window-query
                    #because of selected-window-results may not exist and the are unordered
                    #EX: [1,2,9999999999999,3] : Assuming the third page required has no results it will return pages 1, 2, and 3 
                    break
                for row in res:
                    yield row
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got the following code which is based off an example i found here
I'm working on sanitizing my Html using Jeff Atwood's code found here But the
GWT Activities/Places/MVP concepts were discussed quite a lot here, but I haven't found a
I've been working on a single page app based on Knockoutjs' excellent mail example
This code is working, but I think it's anchoring on the wrong tag. Here
I have very little to go on here. I can't reproduce this locally, but
I'm writing a PyGtk paint program based on the basic tutorial found here .
For example: <input name=abutton type=button value=This is not a button /> I know this
I feel like I am missing something very simple here, but this is the
I,m trying to work through and test a Voice Recognition example based on the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.