Skip to content

Commit

Permalink
refactor(common): remove traverse() function's filter argument si…
Browse files Browse the repository at this point in the history
…nce it can be expressed using the visitor
  • Loading branch information
kszucs committed Dec 14, 2023
1 parent 442199a commit e4e2993
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 12 deletions.
13 changes: 2 additions & 11 deletions ibis/common/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from ibis.common.bases import Hashable
from ibis.common.collections import frozendict
from ibis.common.patterns import NoMatch, Pattern, pattern
from ibis.common.patterns import NoMatch, pattern
from ibis.util import experimental

if TYPE_CHECKING:
Expand Down Expand Up @@ -485,9 +485,7 @@ def toposort(node: Node) -> Graph:


def traverse(
fn: Callable[[Node], tuple[bool | Iterable, Any]],
node: Iterable[Node] | Node,
filter: Optional[Any] = None,
fn: Callable[[Node], tuple[bool | Iterable, Any]], node: Iterable[Node] | Node
) -> Iterator[Any]:
"""Utility for generic expression tree traversal.
Expand All @@ -498,24 +496,17 @@ def traverse(
the traversal, and the second is the result if its not `None`.
node
The Node expression or a list of expressions.
filter
Pattern-like object to filter out nodes from the traversal. The traversal will
only visit nodes that match the given pattern and stop otherwise.
"""

args = reversed(node) if isinstance(node, Sequence) else [node]
todo: deque[Node] = deque(args)
seen: set[Node] = set()
filter: Pattern = pattern(filter or ...)

while todo:
node = todo.pop()

if node in seen:
continue
if filter.match(node, {}) is NoMatch:
continue

seen.add(node)

control, result = fn(node)
Expand Down
2 changes: 1 addition & 1 deletion ibis/expr/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,4 +470,4 @@ def finder(node):
find_first_base_table(node) if isinstance(node, ops.Unnest) else None,
)

return g.traverse(finder, nodes, filter=ops.Node)
return g.traverse(finder, nodes)

0 comments on commit e4e2993

Please sign in to comment.