Skip to content

Commit

Permalink
refactor: make approximate ops subclasses of their non-approximate va…
Browse files Browse the repository at this point in the history
…riants
  • Loading branch information
jcrist committed Aug 23, 2024
1 parent 57f5ff6 commit 9d218d1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 26 deletions.
2 changes: 1 addition & 1 deletion ibis/backends/clickhouse/tests/test_aggregations.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def test_reduction_invalid_where(alltypes, reduction):
),
(
lambda t, cond: t.int_col.approx_median(),
lambda df, cond: np.int32(df.int_col.median()),
lambda df, cond: df.int_col.median(),
),
(
lambda t, cond: t.double_col.min(),
Expand Down
29 changes: 10 additions & 19 deletions ibis/expr/operations/reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,11 @@ class Median(QuantileBase):
"""Compute the median of a column."""


@public
class ApproxMedian(Median):
"""Compute the approximate median of a column."""


@public
class Quantile(QuantileBase):
"""Compute the quantile of a column."""
Expand Down Expand Up @@ -325,25 +330,6 @@ class ArgMin(Filterable, Reduction):
dtype = rlz.dtype_like("arg")


@public
class ApproxCountDistinct(Filterable, Reduction):
"""Approximate number of unique values."""

arg: Column

# Impala 2.0 and higher returns a DOUBLE
dtype = dt.int64


@public
class ApproxMedian(Filterable, Reduction):
"""Compute the approximate median of a set of comparable values."""

arg: Column

dtype = rlz.dtype_like("arg")


@public
class GroupConcat(Filterable, Reduction):
"""Concatenate strings in a group with a given separator character."""
Expand All @@ -364,6 +350,11 @@ class CountDistinct(Filterable, Reduction):
dtype = dt.int64


@public
class ApproxCountDistinct(CountDistinct):
"""Approximate number of unique values."""


@public
class ArrayCollect(Filterable, Reduction):
"""Collect values into an array."""
Expand Down
12 changes: 6 additions & 6 deletions ibis/expr/types/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1644,13 +1644,13 @@ def approx_median(self, where: ir.BooleanValue | None = None) -> Scalar:
>>> ibis.options.interactive = True
>>> t = ibis.examples.penguins.fetch()
>>> t.body_mass_g.approx_median()
┌──────┐
│ 4030 │
└──────┘
┌────────
│ 4030.0
└────────
>>> t.body_mass_g.approx_median(where=t.species == "Chinstrap")
┌──────┐
│ 3700 │
└──────┘
┌────────
│ 3700.0
└────────
"""
return ops.ApproxMedian(self, where=self._bind_to_parent_table(where)).to_expr()

Expand Down

0 comments on commit 9d218d1

Please sign in to comment.