# Buggy code
def _format_window(translator, op, window):
    components = []

    if len(window._group_by) > 0:
        partition_args = [translator.translate(x) for x in window._group_by]
        components.append("PARTITION BY {}".format(", ".join(partition_args)))

    if len(window._order_by) > 0:
        order_args = []
        for expr in window._order_by:
            key = expr.op()
            translated = translator.translate(key.expr)
            if not key.ascending:
                translated += " DESC"
            order_args.append(translated)

        components.append("ORDER BY {}".format(", ".join(order_args)))

    p, f = window.preceding, window.following

    def _prec(p: Optional[int]) -> str:
        assert p is None or p >= 0

        if p is None:
            prefix = "UNBOUNDED"
        else:
            if not p:
                return "CURRENT ROW"
            prefix = str(p)
        return "{} PRECEDING".format(prefix)

    def _foll(f: Optional[int]) -> str:
        assert f is None or f >= 0

        if f is None:
            prefix = "UNBOUNDED"
        else:
            if not f:
                return "CURRENT ROW"
            prefix = str(f)

        return "{} FOLLOWING".format(prefix)

    frame_clause_not_allowed = (
        ops.Lag,
        ops.Lead,
        ops.DenseRank,
        ops.MinRank,
        ops.PercentRank,
        ops.RowNumber,
    )

    if isinstance(op.expr.op(), frame_clause_not_allowed):
        frame = None
    elif p is not None and f is not None:
        frame = "{} BETWEEN {} AND {}".format(window.how.upper(), _prec(p), _foll(f))

    elif p is not None:
        if isinstance(p, tuple):
            start, end = p
            frame = "{} BETWEEN {} AND {}".format(
                window.how.upper(), _prec(start), _prec(end)
            )
        else:
            kind = "ROWS" if p > 0 else "RANGE"
            frame = "{} BETWEEN {} AND UNBOUNDED FOLLOWING".format(kind, _prec(p))
    elif f is not None:
        if isinstance(f, tuple):
            start, end = f
            frame = "{} BETWEEN {} AND {}".format(
                window.how.upper(), _foll(start), _foll(end)
            )
        else:
            kind = "ROWS" if f > 0 else "RANGE"
            frame = "{} BETWEEN UNBOUNDED PRECEDING AND {}".format(kind, _foll(f))
    else:
        # no-op, default is full sample
        frame = None

    if frame is not None:
        components.append(frame)

    return "OVER ({})".format(" ".join(components))


# 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 _format_window(translator, op, window):
    components = []

    if len(window._group_by) > 0:
        partition_args = [translator.translate(x) for x in window._group_by]
        components.append("PARTITION BY {}".format(", ".join(partition_args)))

    if len(window._order_by) > 0:
        order_args = []
        for expr in window._order_by:
            key = expr.op()
            translated = translator.translate(key.expr)
            if not key.ascending:
                translated += " DESC"
            order_args.append(translated)

        components.append("ORDER BY {}".format(", ".join(order_args)))

    p, f = window.preceding, window.following

    def _prec(p: Optional[int]) -> str:
        assert p is None or p >= 0

        if p is None:
            prefix = "UNBOUNDED"
        else:
            if not p:
                return "CURRENT ROW"
            prefix = str(p)
        return "{} PRECEDING".format(prefix)

    def _foll(f: Optional[int]) -> str:
        assert f is None or f >= 0

        if f is None:
            prefix = "UNBOUNDED"
        else:
            if not f:
                return "CURRENT ROW"
            prefix = str(f)

        return "{} FOLLOWING".format(prefix)

    frame_clause_not_allowed = (
        ops.Lag,
        ops.Lead,
        ops.DenseRank,
        ops.MinRank,
        ops.NTile,
        ops.PercentRank,
        ops.RowNumber,
    )

    if isinstance(op.expr.op(), frame_clause_not_allowed):
        frame = None
    elif p is not None and f is not None:
        frame = "{} BETWEEN {} AND {}".format(window.how.upper(), _prec(p), _foll(f))

    elif p is not None:
        if isinstance(p, tuple):
            start, end = p
            frame = "{} BETWEEN {} AND {}".format(
                window.how.upper(), _prec(start), _prec(end)
            )
        else:
            kind = "ROWS" if p > 0 else "RANGE"
            frame = "{} BETWEEN {} AND UNBOUNDED FOLLOWING".format(kind, _prec(p))
    elif f is not None:
        if isinstance(f, tuple):
            start, end = f
            frame = "{} BETWEEN {} AND {}".format(
                window.how.upper(), _foll(start), _foll(end)
            )
        else:
            kind = "ROWS" if f > 0 else "RANGE"
            frame = "{} BETWEEN UNBOUNDED PRECEDING AND {}".format(kind, _foll(f))
    else:
        # no-op, default is full sample
        frame = None

    if frame is not None:
        components.append(frame)

    return "OVER ({})".format(" ".join(components))