Skip to content

Commit

Permalink
refactor(ir): remove unused lineage roots and find_nodes functions
Browse files Browse the repository at this point in the history
BREAKING CHANGE: removed ibis.expr.lineage.{roots,find_nodes} functions
  • Loading branch information
kszucs authored and cpcloud committed Mar 8, 2022
1 parent 0eb9bc5 commit d630a77
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 108 deletions.
75 changes: 0 additions & 75 deletions ibis/expr/lineage.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,81 +7,6 @@
import ibis.expr.types as ir


def find_nodes(expr, node_types):
"""Depth-first search of the expression tree yielding nodes of a given
type or set of types.
Parameters
----------
expr: ibis.expr.types.Expr
node_types: type or tuple of types
Yields
------
op: type
A node of given node_types
"""

def extender(op):
return (arg for arg in op.args if isinstance(arg, ir.Expr))

return _search_for_nodes([expr], extender, node_types)


def _search_for_nodes(stack, extender, node_types):
seen = set()
while stack:
expr = stack.pop()
op = expr.op()
if op not in seen:
if isinstance(op, node_types):
yield op
seen.add(op)
stack.extend(extender(op))


def roots(expr, types=(ops.PhysicalTable,)):
"""Yield every node of a particular type on which an expression depends.
Parameters
----------
expr : Expr
The expression to analyze
types : tuple(type), optional, default
(:mod:`ibis.expr.operations.PhysicalTable`,)
The node types to traverse
Yields
------
table : Expr
Unique node types on which an expression depends
Notes
-----
If your question is: "What nodes of type T does `expr` depend on?", then
you've come to the right place. By default, we yield the physical tables
that an expression depends on.
"""
stack = [
arg.to_expr()
for arg in reversed(expr.op().root_tables())
if isinstance(arg, types)
]

def extender(op):
return reversed(
list(
itertools.chain.from_iterable(
arg.op().root_tables()
for arg in op.flat_args()
if isinstance(arg, types)
)
)
)

return _search_for_nodes(stack, extender, types)


class Container:

__slots__ = ('data',)
Expand Down
33 changes: 0 additions & 33 deletions ibis/tests/expr/test_lineage.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import ibis
import ibis.expr.lineage as lin
import ibis.expr.operations as ops
from ibis.tests.util import assert_equal


Expand Down Expand Up @@ -206,35 +205,3 @@ def test_lineage_join(companies, rounds):
assert len(results) == len(expected)
for r, e in zip(results, expected):
assert_equal(r, e)


@pytest.mark.parametrize(
'expr,node_type,expected_count',
[
(
lambda c: (c.funding_rounds + c.funding_total_usd)
+ c.funding_rounds,
ops.Add,
2,
),
(
lambda c: (c.funding_rounds + c.funding_total_usd)
- c.funding_rounds,
ops.Add,
1,
),
(lambda c: c.funding_rounds - c.funding_total_usd, ops.Add, 0),
],
)
def test_find_nodes(companies, expr, node_type, expected_count):
e = expr(companies)
nodes = list(lin.find_nodes(e, node_type))
assert expected_count == len(nodes)


def test_roots(companies):
expr = companies.funding_rounds + companies.funding_total_usd
roots = list(lin.roots(expr))
assert 1 == len(roots)

assert companies.op().equals(roots[0])

0 comments on commit d630a77

Please sign in to comment.