Skip to content

Commit

Permalink
feat(postgres): add ApproxMedian aggregation
Browse files Browse the repository at this point in the history
  • Loading branch information
mesejo authored and cpcloud committed Apr 25, 2023
1 parent 43b8152 commit 887f572
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
9 changes: 9 additions & 0 deletions ibis/backends/postgres/registry.py
Expand Up @@ -444,6 +444,14 @@ def _quantile(t, op):
)


def _approx_median(t, op):
arg = op.arg
if (where := op.where) is not None:
arg = ops.Where(where, arg, None)

return sa.func.percentile_cont(0.5).within_group(t.translate(arg))


def _binary_variance_reduction(func):
def variance_compiler(t, op):
x = op.left
Expand Down Expand Up @@ -659,6 +667,7 @@ def _unnest(t, op):
ops.Correlation: _corr,
ops.BitwiseXor: _bitwise_op("#"),
ops.Mode: _mode,
ops.ApproxMedian: _approx_median,
ops.Quantile: _quantile,
ops.MultiQuantile: _quantile,
ops.TimestampNow: lambda t, op: sa.literal_column(
Expand Down
2 changes: 1 addition & 1 deletion ibis/backends/postgres/tests/test_functions.py
Expand Up @@ -717,7 +717,7 @@ def test_interactive_repr_shows_error(alltypes):
# #591. Doing this in PostgreSQL because so many built-in functions are
# not available

expr = alltypes.double_col.approx_median()
expr = alltypes.int_col.convert_base(10, 2)

with config.option_context('interactive', True):
result = repr(expr)
Expand Down
2 changes: 1 addition & 1 deletion ibis/backends/tests/test_aggregation.py
Expand Up @@ -1010,7 +1010,7 @@ def test_corr_cov(


@pytest.mark.notimpl(
["datafusion", "mysql", "postgres", "sqlite", "snowflake", "mssql", "druid"],
["datafusion", "mysql", "sqlite", "snowflake", "mssql", "druid"],
raises=com.OperationNotDefinedError,
)
@pytest.mark.broken(
Expand Down

0 comments on commit 887f572

Please sign in to comment.