Skip to content

Commit

Permalink
refactor(analysis): remove is_reduction()
Browse files Browse the repository at this point in the history
  • Loading branch information
kszucs authored and cpcloud committed Oct 17, 2023
1 parent 492b296 commit 2acc31f
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 36 deletions.
32 changes: 0 additions & 32 deletions ibis/expr/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,38 +431,6 @@ def predicate(node):
return list(g.traverse(predicate, node))


def is_reduction(node):
"""Check whether an expression contains a reduction or not.
Aggregations yield typed scalar expressions, since the result of an
aggregation is a single value. When creating an table expression
containing a GROUP BY equivalent, we need to be able to easily check
that we are looking at the result of an aggregation.
As an example, the expression we are looking at might be something
like: foo.sum().log10() + bar.sum().log10()
We examine the operator DAG in the expression to determine if there
are aggregations present.
A bound aggregation referencing a separate table is a "false
aggregation" in a GROUP BY-type expression and should be treated a
literal, and must be computed as a separate query and stored in a
temporary variable (or joined, for bound aggregations with keys)
"""

def predicate(node):
if isinstance(node, ops.Reduction):
return g.halt, True
elif isinstance(node, ops.TableNode):
# don't go below any table nodes
return g.halt, None
else:
return g.proceed, None

return any(g.traverse(predicate, node))


def find_predicates(node, flatten=True):
# TODO(kszucs): consider to remove flatten argument and compose with
# flatten_predicates instead
Expand Down
1 change: 0 additions & 1 deletion ibis/tests/expr/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,6 @@ def test_group_by_kwargs(table):
def test_compound_aggregate_expr(table):
# See ibis #24
compound_expr = (table["a"].sum() / table["a"].mean()).name("foo")
assert an.is_reduction(compound_expr.op())

# Validates internally
table.aggregate([compound_expr])
Expand Down
5 changes: 2 additions & 3 deletions ibis/tests/expr/test_value_exprs.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import ibis
import ibis.common.exceptions as com
import ibis.expr.analysis as an
import ibis.expr.datashape as ds
import ibis.expr.datatypes as dt
import ibis.expr.operations as ops
Expand Down Expand Up @@ -489,7 +488,7 @@ def test_arbitrary(table, column, how, condition_fn):
expr = col.arbitrary(how=how, where=where)
assert expr.type() == col.type()
assert isinstance(expr, ir.Scalar)
assert an.is_reduction(expr.op())
assert isinstance(expr.op(), ops.Arbitrary)


@pytest.mark.parametrize(
Expand All @@ -505,7 +504,7 @@ def test_arbitrary(table, column, how, condition_fn):
def test_any_all_notany(table, column, operation):
expr = operation(table[column])
assert isinstance(expr, ir.BooleanScalar)
assert an.is_reduction(expr.op())
assert expr.op().find(ops.Reduction)


@pytest.mark.parametrize(
Expand Down

0 comments on commit 2acc31f

Please sign in to comment.