Skip to content

Commit

Permalink
feat: support table.sort_by(ibis.random())
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrist authored and cpcloud committed Aug 24, 2022
1 parent 222b2ba commit 693005d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion ibis/backends/base/sql/alchemy/query_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def _can_lower_sort_column(table_set, expr):
# aggregation so they appear in same query. It's generally for
# cosmetics and doesn't really affect query semantics.
bases = {op: op.to_expr() for op in expr.op().root_tables()}
if len(bases) > 1:
if len(bases) != 1:
return False

base = list(bases.values())[0]
Expand Down
5 changes: 5 additions & 0 deletions ibis/backends/pyspark/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2134,3 +2134,8 @@ def compile_where(t, expr, scope, timecontext, **kwargs):
t.translate(op.bool_expr, scope, timecontext, **kwargs),
t.translate(op.true_expr, scope, timecontext, **kwargs),
).otherwise(t.translate(op.false_null_expr, scope, timecontext, **kwargs))


@compiles(ops.RandomScalar)
def compile_random(*args, **kwargs):
return F.rand()
15 changes: 15 additions & 0 deletions ibis/backends/tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,21 @@ def test_sort_by(backend, alltypes, df, key, df_kwargs):
backend.assert_frame_equal(result, expected)


@pytest.mark.notimpl(["dask", "datafusion", "impala", "pandas"])
@pytest.mark.notyet(
["clickhouse"],
reason="clickhouse doesn't have a [0.0, 1.0) random implementation",
)
def test_sort_by_random(alltypes):
expr = alltypes.filter(_.id < 100).sort_by(ibis.random()).limit(5)
r1 = expr.execute()
r2 = expr.execute()
assert len(r1) == 5
assert len(r2) == 5
# Ensure that multiple executions returns different results
assert not r1.equals(r2)


def check_table_info(buf, schema):
info_str = buf.getvalue()

Expand Down
6 changes: 1 addition & 5 deletions ibis/expr/operations/sortkeys.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ def _to_sort_key(key, *, table=None):
else:
sort_order = True

# # HACK: support `ORDER BY 1` / `ORDER BY 42.42`
# if isinstance(key, (int, float)):
# key = ibis.literal(key)

if not isinstance(key, ir.Expr):
if table is None:
raise com.IbisTypeError("cannot resolve key with table=None")
Expand Down Expand Up @@ -60,7 +56,7 @@ def _maybe_convert_sort_keys(tables, exprs):

@public
class SortKey(Node):
expr = rlz.column(rlz.any)
expr = rlz.any
ascending = rlz.optional(
rlz.map_to(
{
Expand Down
5 changes: 5 additions & 0 deletions ibis/tests/sql/test_sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,11 @@ def test_aggregate(con, star1, sa_star1, expr_fn, expected_fn):
lambda b: sa.select([b]).order_by(b.c.c, b.c.f.desc()),
id="sort_by_mixed",
),
pytest.param(
lambda t: t.sort_by(ibis.random()),
lambda b: sa.select([b]).order_by(sa.func.random()),
id="sort_by_random",
),
],
)
def test_sort_by(con, star1, sa_star1, expr_fn, expected_fn):
Expand Down

0 comments on commit 693005d

Please sign in to comment.