# Buggy code
def get_result(self):
    # Got to unravel the join stack; the nesting order could be
    # arbitrary, so we do a depth first search and push the join tokens
    # and predicates onto a flat list, then format them
    op = self.expr.op()

    if isinstance(op, ops.Join):
        self._walk_join_tree(op)
    else:
        self.join_tables.append(self._format_table(self.expr))

    result = self.join_tables[0]
    for jtype, table, preds in zip(
        self.join_types, self.join_tables[1:], self.join_predicates
    ):
        if len(preds):
            sqla_preds = [self._translate(pred) for pred in preds]
            onclause = _and_all(sqla_preds)
        else:
            onclause = None

        if jtype in (ops.InnerJoin, ops.CrossJoin):
            result = result.join(table, onclause)
        elif jtype is ops.LeftJoin:
            result = result.join(table, onclause, isouter=True)
        elif jtype is ops.RightJoin:
            result = table.join(result, onclause, isouter=True)
        elif jtype is ops.OuterJoin:
            result = result.outerjoin(table, onclause, full=True)
        elif jtype is ops.LeftSemiJoin:
            result = sa.select([result]).where(
                sa.exists(sa.select([1]).where(onclause))
            )
        elif jtype is ops.LeftAntiJoin:
            result = sa.select([result]).where(
                ~sa.exists(sa.select([1]).where(onclause))
            )
        else:
            raise NotImplementedError(jtype)

    return result


# Traceback (ProgrammingError)
-----------------------------------------------------------------------
ProgrammingError                      Traceback (most recent call last)
~/anaconda3/lib/python3.6/site-packages/sqlalchemy/engine/base.py in _execute_context(self, dialect, constructor, statement, parameters, *args)
1181                         parameters,
-> 1182                         context)
1183         except BaseException as e:

~/anaconda3/lib/python3.6/site-packages/sqlalchemy/engine/default.py in do_execute(self, cursor, statement, parameters, context)
469     def do_execute(self, cursor, statement, parameters, context=None):
--> 470         cursor.execute(statement, parameters)
471

ProgrammingError: Frame clause should not be specified for ranking window functions

# Fixed code
def get_result(self):
    # Got to unravel the join stack; the nesting order could be
    # arbitrary, so we do a depth first search and push the join tokens
    # and predicates onto a flat list, then format them
    op = self.expr.op()

    if isinstance(op, ops.Join):
        self._walk_join_tree(op)
    else:
        self.join_tables.append(self._format_table(self.expr))

    result = self.join_tables[0]
    for jtype, table, preds in zip(
        self.join_types, self.join_tables[1:], self.join_predicates
    ):
        if len(preds):
            sqla_preds = [self._translate(pred) for pred in preds]
            onclause = functools.reduce(sql.and_, sqla_preds)
        else:
            onclause = None

        if jtype in (ops.InnerJoin, ops.CrossJoin):
            result = result.join(table, onclause)
        elif jtype is ops.LeftJoin:
            result = result.join(table, onclause, isouter=True)
        elif jtype is ops.RightJoin:
            result = table.join(result, onclause, isouter=True)
        elif jtype is ops.OuterJoin:
            result = result.outerjoin(table, onclause, full=True)
        elif jtype is ops.LeftSemiJoin:
            result = sa.select([result]).where(
                sa.exists(sa.select([1]).where(onclause))
            )
        elif jtype is ops.LeftAntiJoin:
            result = sa.select([result]).where(
                ~sa.exists(sa.select([1]).where(onclause))
            )
        else:
            raise NotImplementedError(jtype)

    return result