@dispatch(Tail)
def _subexpr_optimize(expr):
    child = sorter = expr._child
    while not isinstance(sorter, Sort):
        try:
            sorter = sorter._child
        except __HOLE__:
            break
    else:
        # Invert the sort order, then take the head, then re-sort based on
        # the original key.
        return child._subs({
            sorter: sorter._child.sort(
                sorter._key,
                ascending=not sorter.ascending,
            ),
        }).head(expr.n).sort(sorter._key, ascending=sorter.ascending)

    # If there is no sort order, then we can swap out a head with a tail.
    # This is equivalent in this backend and considerably faster.
    warnings.warn(
        "'tail' of a sql operation with no sort is the same as 'head'",
    )
    return child.head(expr.n)
dataset/ETHPy150Open blaze/blaze/blaze/compute/sql.py/_subexpr_optimize